Ir al contenido
  1. Publicaciones/

Gitlab Pages con TLS y Letsencrypt

··1160 palabras·6 mins

Gitlab posee su propio certificado TLS para los proyectos alojados en sus servidores, pero puede darse el caso de que tengas alojado tu blog en Gitlab Pages, el cual estará bajo un dominio tipo usuario.gitlab.io, y quieras tener tu propio dominio .com con un certificado TLS.

Como he dicho el certificado que proporciona Gitlab solo es valido para dominios tipo usuario.gitlab.io por lo que nos serviremos de Letsencrypt añadir un certificado TLS para nuestro dominio. Por si aun no lo sabias, Letsencrypt es una entidad certificadora que nos proporciona de forma gratuita certificados X.509 para el cifrado TLS,

Esta guía la voy a realizar sobre un blog construido en Jekyll sobre Gitlab Pages, imagino que los pasos para otro tipo de blog o sitio deben de ser muy parecidos.

Una vez que tengas tu dominio debemos de hacer dos cosas:

  • Añadir el dominio a Gitlab Pages
  • Añadir el certificado a nuestro sitio web

Añadir nuestro dominio a Gitlab Pages #

Pulsamos en Settings -> Pages

New Domain

En Domain ponemos el nombre del nuevo dominio

A partir de este momento nuestro sitio será accesible desde dos sitios http://tu.dominio y desde https://tunombre.gitlab.io.

Añadir el certificado a nuestro sitio web #

Para poder obtener nuestro certificado de Letsencrypt vamos a necesitar de la utilidad certbot, en arch linux lo instalaremos junto con las dependencias necesarias con :

sudo pacman -S certbot

Como las páginas están en los servidores de GitLab debemos hacerlo de forma manual

sudo certbot certonly -a manual -d TU.DOMINIO

Ponemos la dirección de correo electrónico,


Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): TU@CORREO

Aceptamos los tórminos del servicio


-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: a

Debemos de elegir si queremos compartir nuestra dirección de correo electrónico

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: y

Ahora comienza el proceso de solicitud del nuevo certificado

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for TU.DOMINIO

Como estamos solicitando el certificado de forma manual, seleccionamos Y

-------------------------------------------------------------------------------
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.

Are you OK with your IP being logged?
-------------------------------------------------------------------------------
(Y)es/(N)o: y

Cuando lleguemos al paso de más abajo, paramos, y en el la raíz de nuestro sitio web debemos crear el fichero con el contenido que nos pide. Hasta que no tengamos creado el fichero y hayamos comprobado que el fichero es accesible, no debemos de pulsar la tecla Enter para continuar

-------------------------------------------------------------------------------
Create a file containing just this data:

5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4.pW4MAhlax4MNFqsdfXOVe9PMr_5LsdfLNIdhgHA_eNs

And make it available on your web server at this URL:

http://TU.DOMINIO/.well-known/acme-challenge/5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4

-------------------------------------------------------------------------------
Press Enter to Continue

Si leemos la información, nos pide que creemos un fichero llamado 5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4, y peguemos en su interior el texto 5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4.pW4MAhlax4MNFqsdfXOVe9PMr_5LsdfLNIdhgHA_eNs

Así que en la raíz de nuestro sitio web creamos el fichero llamado 5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4 y en su interior ponemos:

---
layout: null
permalink: /.well-known/acme-challenge/5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4
---

5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4.pW4MAhlax4MNFqsdfXOVe9PMr_5LsdfLNIdhgHA_eNs

Comprobamos que funciona

curl http://TU.DOMINIO/.well-known/acme-challenge/5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4

5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzo_Wa4.pW4MAhlax4MNFqsdfXOVe9PMr_5LsdfLNIdhgHA_eNs

Si todo ha ido bien, nos debe de devolver el contenido del fichero quue hemos creado 5xVJtje7BgkOpMNSqDk-OiavoEcTrlZIAYfVvzoWa4.pW4MAhlax4MNFqsdfXOVe9PMr5LsdfLNIdhgHA_eNs

Ya podemos volver a la terminal y pulsar en la tecla Enter para continuar

Press Enter to Continue
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/TU.DOMINIO/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/TU.DOMINIO/privkey.pem
   Your cert will expire on 2018-04-13. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Como hemos solicitado el certificado usando la opción manual, los certificados se guardaran en /etc/letsencrypt/live de nuestro ordenador, en un próximo articulo veremos como hacer un backup de esos certificados y la forma de restaurarlos.

Para terminar necesitamos subir los certificados a GitLab

En nuestro proyecto vamos a Settings -> Pages y copiamos el contenido de /etc/letsencrypt/live/TU.DOMINIO/fullchain.pem en el campo “Certificate (PEM)” y /etc/letsencrypt/live/TU.DOMINIO/privkey.pem en el campo “Key (PEM)”, necesitaras utilizar sudo para poder leer el contenido de los ficheros.

Cuando hayamos terminado podemos comprobar que funciona

curl -vX HEAD https://TU.DOMINIO/

Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
*   Trying 52.167.214.3...
* TCP_NODELAY set
* Connected to TU.DOMINIO (52.167.214.3) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Client hello (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=TU.DOMINIO
*  start date: Jan 13 22:43:11 2018 GMT
*  expire date: Apr 13 22:43:11 2018 GMT
*  subjectAltName: host "TU.DOMINIO" matched cert's "TU.DOMINIO"
*  issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x5612c569c160)
> HEAD / HTTP/2
> Host: TU.DOMINIO
> User-Agent: curl/7.57.0
> Accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< accept-ranges: bytes
< cache-control: max-age=600
< content-type: text/html; charset=utf-8
< expires: Mon, 22 Jan 2018 23:02:58 UTC
< last-modified: Sat, 13 Jan 2018 23:40:45 GMT
< vary: Origin
< content-length: 88393
< date: Mon, 22 Jan 2018 22:52:58 GMT
<
* transfer closed with 88393 bytes remaining to read
* Closing connection 0
* TLSv1.2 (OUT), TLS alert, Client hello (1):
curl: (18) transfer closed with 88393 bytes remaining to read

Ahora ya puedes acceder a tu dominio desde una dirección https

Recuerda que al haber solicitado el certificado con la opción manual cuando tengas que renovar el certificado no podrás usar la opción renew y deberás hacerlo otra vez con la opcion manual

Referencias: #

Espero que te haya gustado, pasa un buen día. 🐧