Gitea SSL mit LetEncrypt

Gitea Dienst unter git.xxx.org soll mit SSL Verschlüsselung ausgestattet werden. Es werden verschiedene Domains auf dem Server bedient. Als Reverse Proxy ist bereits NginX vorgeschaltet. Daher muss nicht einmal die Gitea Konfiguration geändert werden. Es kann über NginX die Verschlüsselung mit SSL geschehen.

Die LetsEncrypt Initiative macht es sehr einfach Webseiten mit einem gültigen Zertifikat auszustellen. Dazu gibt es verschiedene Clients die die Verwaltung vornehmen. Ich verwende hier den Certbot.

Installation Certbot unter Arch Linux

yaourt -S --noconfirm certbot-nginx

Erstellen eines Zertifikates

Das Plugin für Certbot parst das ConfigFile von NginX und zeigt alle Domains an, die auf dem Server einen eigenen Serverblock besitzen. Da wir die Konfiguration nicht verändern wollen, rufen wir den certbot mit dem Parameter certonly auf. Damit wird nur ein Zertifikat für die gewählten Domains erstellt. Hierfür muss man natürlich im Besitz der Domain sein.

certbot --nginx certonly

[root@server conf]# certbot --nginx certonly
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): xxx@web.de

-------------------------------------------------------------------------------
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

-------------------------------------------------------------------------------
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

Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: ----
2: git.xxx.org
3: ----
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 2
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for mrpeacockgit.duckdns.org
2018/06/02 09:50:20 [notice] 9946#9946: signal process started
Waiting for verification...
Cleaning up challenges
2018/06/02 09:50:25 [notice] 9948#9948: signal process started

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/git.xxx.org/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/git.xxx.org/privkey.pem
Your cert will expire on 2018-08-31. 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"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- 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

Wie man in der Ausgabe erkennen kann, legt der certbot unterhalb /etc/letsencrypt/live/ für jede Domain ein Verzeichnis an. In diesem wird das Zertifikat und der private Schlüssel abgelegt.

Einbinden des Zertifikates in NginX

Die Konfiguration für den Gitea Service ist in eine eigene Datei ausgelagert. Diese wird per Include Anweisung eingebunden. Hierfür ist ein separates conf-Verzeichnis unterhalb /etc/nginx/ ratsam.

#
# Gitea Service
#
include conf/git.conf;

Damit alte Links bzw. der direkte Aufruf mit dem unverschlüsselten HTTP Protokoll weiterhin funtkioniert muss eine 301 Weiterleitung eingerichtet werden.

# Redirect http -> https
server {
    listen 80;
    server_name git.xxx.org;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;

    server_name git.xxx.org;

    # logging
    error_log /var/log/nginx/gitea_error.log debug;
    access_log /var/log/nginx/gitea_access.log;

    client_max_body_size 100M; # Push large Objects to gitea

    # Configuration for letsencrypt
    location ^~ /.well-known/acme-challenge/ {
        allow all;
        root /var/lib/letsencrypt/;
        default_type "text/plain";
        try_files $uri =404;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
    }

    # SSL
    ssl on;
    ssl_certificate /etc/letsencrypt/live/git.xxx.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/git.xxx.org/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

Konfiguration von Gitea

Da in diesem Beispiel NGinX als Reverse Proxy vorgeschaltet ist, muss hier nur die Root URL angepasst werden. Es ist HTTPS als Protokoll und 443 als Port anzugeben. Gitea selbst arbeitet weiterhin mit dem HTTP Protokoll.

[server]
; Listen protocol. One of 'http', 'https', 'unix' or 'fcgi'.
PROTOCOL                   = http
DOMAIN                     = git.xxx.org
ROOT_URL                   = https://git.xxx.org:443/

Wildcard Zertifikate

In einem anderen Artikel beschreibe ich wie man lokale Dienste mit einem Wildcard Zertifikat absichern kann. Hierfür setzte ich den freien DNS Dienst DuckDNS ein.