Go, systemd, Let's Encryptで躓いた所

Go製プログラムをsystemdで動かす際、Let's Encrypt(Certbot)を利用したhttps対応絡みで躓いた所メモ。
筆者はサーバー素人なので注意。
尚、Go特有の何かは一切無い。

  • 443ポートが権限不足でlistenできない
    serviceファイルに「AmbientCapabilities=CAP_NET_BIND_SERVICE」指定を追加して解決。
  • Certbotが作成するpemファイルにアプリが権限不足でアクセス出来ない
    更新プロセスを考慮すると下手にパーミッションを弄る事も憚られるが、これについてはCertbotのドキュメントに解決策が書いてある。
    https://eff-certbot.readthedocs.io/en/latest/using.html#where-are-my-certificates

    For historical reasons, the containing directories are created with permissions of 0700 meaning that certificates are accessible only to servers that run as the root user. If you will never downgrade to an older version of Certbot, then you can safely fix this using chmod 0755 /etc/letsencrypt/{live,archive}.

    For servers that drop root privileges before attempting to read the private key file, you will also need to use chgrp and chmod 0640 to allow the server to read /etc/letsencrypt/live/$domain/privkey.pem.

    権限変更後sudo certbot renew --force-renewalした所、新しく作成されたprivkey.pemも変更後のグループになっていた。

  • 証明書が更新された際、サービスを再起動したい
    Certbotにpre, post, deployの三種のフックが有る。
    https://eff-certbot.readthedocs.io/en/latest/using.html#renewing-certificates
    この場合は/etc/letsencrypt/renewal-hooks/deployにsytemctl restart ○○を記述したスクリプトを配置すれば良いように思える。
    シェルスクリプトを配置してchmod 755、certbot renew --dry-run --deploy-hook した所、意図した通りにサービスが再起動された。
    自動更新プロセス上でもきちんと動くかはまだ確認出来ていない。