metznet-channel/metznet/services/certificates.scm

47 lines
2.0 KiB
Scheme

(define-module (metznet services certificates)
#:use-module (guix gexp)
#:export (certificate-copy-deploy-hook))
;;; Commentary:
;;;
;;; Daemons that drop privileges can't read certbot's files under
;;; /etc/letsencrypt (root-only), so they serve copies they own. This makes a
;;; certbot deploy hook that refreshes those copies after each renewal:
;;;
;;; (certificate-configuration
;;; (domains '("ldap.metznet.ca"))
;;; (deploy-hook (certificate-copy-deploy-hook "ldap.metznet.ca"
;;; "/var/lib/slapd" "slapd" #:restart '(slapd))))
;;;
;;; The hook is a plain program, so running it by hand copies the current
;;; certificate too.
;;;
;;; Code:
(define* (certificate-copy-deploy-hook domain directory user
#:key
(files '("cert.pem" "chain.pem"
"privkey.pem"))
(restart '()))
"Return a certbot deploy hook copying FILES of DOMAIN's live certificate
into DIRECTORY, owned by USER (and USER's primary group), then restarting the
Shepherd services in RESTART. privkey.pem is mode 0600, the rest 0644."
(program-file
(string-append domain "-certificate-copy-hook")
(with-imported-modules '((gnu services herd))
#~(begin
(use-modules (gnu services herd))
(let ((live #$(string-append "/etc/letsencrypt/live/" domain))
(owner (getpw #$user)))
(for-each
(lambda (name)
;; copy then rename, so a reader never sees a partial file
(let ((new (string-append #$directory "/" name ".new")))
(copy-file (string-append live "/" name) new)
(chown new (passwd:uid owner) (passwd:gid owner))
(chmod new (if (string=? name "privkey.pem") #o600 #o644))
(rename-file new (string-append #$directory "/" name))))
'#$files)
;; most daemons only load their certificate at startup
(for-each restart-service '#$restart))))))