From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id KCmYJUoJsWV2DAAAqHPOHw:P1 (envelope-from ) for ; Wed, 24 Jan 2024 13:57:46 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0.migadu.com with LMTPS id KCmYJUoJsWV2DAAAqHPOHw (envelope-from ) for ; Wed, 24 Jan 2024 13:57:46 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=zancanaro.id.au header.s=k1 header.b=cpiyLfnh; spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zancanaro.id.au (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1706101066; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=nJ6MpeTgJ71O49cyD9p/bXXHCjm8KHW0Enj0/oi+IY4=; b=MZDLetU2E0GU++nxncvrdS/10kPUTOm5cV2Ix/oNbsibs3NA6CpLlvqF/I4c25dysNPKCk eRBnXTUocbsXbOMW61WZoTJ4xFCPCT3MJJD6zkZ1mR5T8tMAjY2E7FqTmOz4xILd6K8qZd qv3JeoTTSkEBpyKIxVPhA4c8OlOn5r8CMfNW/9y8ZYwxKWVfbKDMK+AdJw2q8Hc1l724vM Xse4YZJOQjVEt25HblAwFwsOxCPXHMaalN6vWBm6ybhkw05wHM74fS9sZpEZj338qjyYiC qyGPHTgKDtlNLwjQY+Htyyc+kbHlkmNvevNuxV7Zfs2+3QjTR6ELO7M1OZ9D6Q== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=zancanaro.id.au header.s=k1 header.b=cpiyLfnh; spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org"; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zancanaro.id.au (policy=none) ARC-Seal: i=1; s=key1; d=yhetil.org; t=1706101066; a=rsa-sha256; cv=none; b=qorQTmFAvdfPUKdII3I3bWPbONWBGwvQQOKOH6me+rUTEn4IZilD74sdj4yDmwPPR7KDFy KRbNMFMoOGL99Pq0PLU3zCFnl9bWEQGNuFx41u9qOuTwu+kZYmBn760LGMWNBAC1c8moHN H93sXcs3eJv56LQ40jf7hbXEnHUjBC6SakKfoAPnc20L9vjYgFbVuLYAytIrZUM2amfMH4 CR9tvBT9XLYtFd2CGr2hlqAvqOYZXRdllgd4wphWmbE6e2Pt7aJMpdov49XugSjiB5PFna kWS4EUX0hqSUmP9JYjXnwyMnM2aVIRnOuzCz2gBs4NnYE6XdUTONnoZEGqaj/w== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 7B03552B2A for ; Wed, 24 Jan 2024 13:57:46 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rScoV-0003ZK-4D; Wed, 24 Jan 2024 07:57:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rScoM-0003Rw-74 for bug-guix@gnu.org; Wed, 24 Jan 2024 07:57:00 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rScoL-0001IY-GB for bug-guix@gnu.org; Wed, 24 Jan 2024 07:56:57 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rScoQ-0003ui-N5 for bug-guix@gnu.org; Wed, 24 Jan 2024 07:57:02 -0500 X-Loop: help-debbugs@gnu.org Subject: bug#46961: [PATCH 2/2] services: certbot: Create self-signed certificates before certbot runs Resent-From: Carlo Zancanaro Original-Sender: "Debbugs-submit" Resent-CC: bug-guix@gnu.org Resent-Date: Wed, 24 Jan 2024 12:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 46961 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: 46961@debbugs.gnu.org Received: via spool by 46961-submit@debbugs.gnu.org id=B46961.170610101515010 (code B ref 46961); Wed, 24 Jan 2024 12:57:02 +0000 Received: (at 46961) by debbugs.gnu.org; 24 Jan 2024 12:56:55 +0000 Received: from localhost ([127.0.0.1]:44721 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rScoI-0003tw-H6 for submit@debbugs.gnu.org; Wed, 24 Jan 2024 07:56:55 -0500 Received: from voltorb.zancanaro.id.au ([45.77.50.64]:32908) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rScoE-0003t2-GK for 46961@debbugs.gnu.org; Wed, 24 Jan 2024 07:56:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=k1; bh=nJ6MpeTgJ71O49c yD9p/bXXHCjm8KHW0Enj0/oi+IY4=; h=references:in-reply-to:date:subject: to:from; d=zancanaro.id.au; b=cpiyLfnhVdLL15PoCkq9qinUMjsScJsssPAr/pgK 9R29IBm+9KNk9d3bJ3G5lh7AhyKdZaGzdUfzQWvYaZ5Y0AQVcDr/+YLpKT1nSc3h8Ha3MK 8sfOZ4byBXY0yvMa1vUARQbZTiqoHc79YoBJgRw2nfISJDblnUI8qkXrO/5h8= Received: by voltorb.zancanaro.id.au (OpenSMTPD) with ESMTPSA id 4f05989a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for <46961@debbugs.gnu.org>; Wed, 24 Jan 2024 12:56:26 +0000 (UTC) From: Carlo Zancanaro Date: Wed, 24 Jan 2024 23:18:38 +1100 Message-ID: <788b23efd206b4ef627de06236f868f97e3cbe05.1706098718.git.carlo@zancanaro.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: bug-guix-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Spam-Score: 3.84 X-Migadu-Scanner: mx13.migadu.com X-Spam-Score: 3.84 X-Migadu-Queue-Id: 7B03552B2A X-TUID: oDTVB7Ij3as8 * gnu/services/certbot.scm (): Add start-self-signed? field. (generate-certificate-gexp): New procedure. (certbot-activation): Generate self-signed certificates when start-self-signed? is #t. * doc/guix.texi (Certificate services): Document start-self-signed?. --- doc/guix.texi | 6 +++++ gnu/services/certbot.scm | 56 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 2d43ab9a65..15b256d0a3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -32664,6 +32664,12 @@ Certificate Services contain a space-delimited list of renewed certificate domains (for example, @samp{"example.com www.example.com"}. +@item @code{start-self-signed?} (default: @code{#t}) +Whether to generate an initial self-signed certificate during system +activation. This option is particularly useful to allow @code{nginx} to +start before @code{certbot} has run, because @code{certbot} relies on +@code{nginx} running to perform HTTP challenges. + @end table @end deftp diff --git a/gnu/services/certbot.scm b/gnu/services/certbot.scm index 58e709f8a4..bb321a1b50 100644 --- a/gnu/services/certbot.scm +++ b/gnu/services/certbot.scm @@ -64,7 +64,9 @@ (define-record-type* (cleanup-hook certificate-cleanup-hook (default #f)) (deploy-hook certificate-configuration-deploy-hook - (default #f))) + (default #f)) + (start-self-signed? certificate-configuration-start-self-signed? + (default #t))) (define-record-type* certbot-configuration make-certbot-configuration @@ -91,7 +93,10 @@ (define-record-type* (define (certbot-deploy-hook name deploy-hook-script) "Returns a gexp which creates symlinks for privkey.pem and fullchain.pem from /etc/certs/NAME to /etc/letsenctypt/live/NAME. If DEPLOY-HOOK-SCRIPT is -not #f then it is run after the symlinks have been created." +not #f then it is run after the symlinks have been created. This wrapping is +necessary for certificates with start-self-signed? set to #t, as it will +overwrite the initial self-signed certificates upon the first successful +deploy." (program-file (string-append name "-deploy-hook") (with-imported-modules '((guix build utils)) @@ -108,7 +113,8 @@ (define (certbot-deploy-hook name deploy-hook-script) "/etc/letsencrypt/live/" name "/fullchain.pem") #$(string-append "/etc/certs/" name "/fullchain.pem.new")) - ;; Rename over the top of the old ones, if there are any. + ;; Rename over the top of the old ones, just in case they were the + ;; original self-signed certificates. (rename-file #$(string-append "/etc/certs/" name "/privkey.pem.new") #$(string-append "/etc/certs/" name "/privkey.pem")) (rename-file #$(string-append "/etc/certs/" name "/fullchain.pem.new") @@ -182,6 +188,44 @@ (define (certbot-renewal-jobs config) #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60))) #$(certbot-command config)))) +(define (generate-certificate-gexp certbot-cert-directory rsa-key-size) + (match-lambda + (($ name (primary-domain other-domains ...) challenge + csr authentication-hook + cleanup-hook deploy-hook) + (let (;; Arbitrary default subject, with just the + ;; right domain filled in. These values don't + ;; have any real significance. + (subject (string-append "/C=US/ST=Oregon/L=Portland/O=Company Name/OU=Org/CN=" + primary-domain)) + (alt-names (if (null? other-domains) + #f + (format #f "subjectAltName=~{DNS:~a~^,~}" other-domains))) + (directory (string-append "/etc/certs/" (or name primary-domain)))) + #~(begin + (use-modules (ice-9 format)) + (when (not (file-exists? #$directory)) + ;; Due to the way certbot runs, we need to + ;; create the self-signed certificates in the + ;; archive folder and symlink them into the live + ;; folder. This mimics what certbot does well + ;; enough to make acquiring new certificates + ;; work. + (mkdir-p #$directory) + (chmod #$directory #o755) + (invoke #$(file-append openssl "/bin/openssl") + "req" "-x509" + "-newkey" #$(string-append "rsa:" (or rsa-key-size "4096")) + "-keyout" #$(string-append directory "/privkey.pem") + "-out" #$(string-append directory "/fullchain.pem") + "-sha256" + "-days" "1" ; Only one day, because we expect certbot to run + "-nodes" + "-subj" #$subject + #$@(if alt-names + (list "-addext" alt-names) + (list))))))))) + (define (certbot-activation config) (let* ((certbot-directory "/var/lib/certbot") (certbot-cert-directory "/etc/letsencrypt/live") @@ -196,6 +240,12 @@ (define (certbot-activation config) (mkdir-p #$webroot) (mkdir-p #$certbot-directory) (mkdir-p #$certbot-cert-directory) + + #$@(map (generate-certificate-gexp certbot-cert-directory + (and rsa-key-size (number->string rsa-key-size))) + (filter certificate-configuration-start-self-signed? + certificates)) + (copy-file #$(certbot-command config) #$script) (display #$message))))))) -- 2.41.0