all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Timo Wilken <guix@twilken.net>
To: 60182@debbugs.gnu.org
Cc: zimon.toutoune@gmail.com, othacehe@gnu.org, ludo@gnu.org,
	mail@cbaines.net, rekado@elephly.net, rg@raghavgururajan.name,
	jgart@dismail.de, paren@disroot.org
Subject: [bug#60182] [PATCH] services: wireguard: Allow specifying pre-shared keys.
Date: Sun, 18 Dec 2022 18:19:47 +0100	[thread overview]
Message-ID: <20221218171947.vkhy4cwbr2wtwfjp@timo-pc> (raw)

Hi,

I'm new around here; please forgive any mistakes in my patch
submission! I've also CC'd the mentors; I hope that's OK.

For my WireGuard setup, I use pre-shared keys in addition to the usual
public/private keys.  `wg-quick', which is invoked by
`wireguard-service-type', supports these, Guix just needs to pass them
through to its configuration file.

After kind feedback from members of help-guix, I wrote this code such
that it takes an existing file for each pre-shared key and loads it
from the "main" configuration file, as is done for the private key.

I've tested this code using `sudo -E ./pre-inst-env guix system
reconfigure system.scm', with a system.scm that sets up a WireGuard
service with peers both with and without a pre-shared key. This
generates the correct wg-quick configuration file for me.

* gnu/services/vpn.scm (<wireguard-peer>): Add preshared-key field.
* doc/guix.texi (VPN Services): Document the new preshared-key field.
---
 doc/guix.texi        |  5 +++++
 gnu/services/vpn.scm | 14 +++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index eb37d4d393..06d6df01f8 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -31763,6 +31763,11 @@ VPN Services
 @item @code{public-key}
 The peer public-key represented as a base64 string.
 
+@item @code{preshared-key} (default: @code{#f})
+An optional pre-shared key file for this peer.  Giving a non-existent
+file name here will result in an invalid WireGuard configuration; the
+given file will not be autogenerated.
+
 @item @code{allowed-ips}
 A list of IP addresses from which incoming traffic for this peer is
 allowed and to which incoming traffic for this peer is directed.
diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm
index 7b3bb8903c..44c0d83494 100644
--- a/gnu/services/vpn.scm
+++ b/gnu/services/vpn.scm
@@ -61,6 +61,7 @@ (define-module (gnu services vpn)
             wireguard-peer-endpoint
             wireguard-peer-allowed-ips
             wireguard-peer-public-key
+            wireguard-peer-preshared-key
             wireguard-peer-keep-alive
 
             wireguard-configuration
@@ -709,6 +710,8 @@ (define-record-type* <wireguard-peer>
   (endpoint          wireguard-peer-endpoint
                      (default #f))     ;string
   (public-key        wireguard-peer-public-key)   ;string
+  (preshared-key     wireguard-peer-preshared-key
+                     (default #f))     ;string
   (allowed-ips       wireguard-peer-allowed-ips) ;list of strings
   (keep-alive        wireguard-peer-keep-alive
                      (default #f)))    ;integer
@@ -762,10 +765,18 @@ (define (wireguard-configuration-file config)
                   (format #f "PersistentKeepalive = ~a\n" keep-alive)
                   "\n"))))
 
+  (define (peers->preshared-keys peer keys)
+    (let ((public-key (wireguard-peer-public-key peer))
+          (preshared-key (wireguard-peer-preshared-key peer)))
+      (if preshared-key
+          (cons* public-key preshared-key keys)
+          keys)))
+
   (match-record config <wireguard-configuration>
     (wireguard interface addresses port private-key peers dns
                pre-up post-up pre-down post-down table)
     (let* ((config-file (string-append interface ".conf"))
+           (peer-keys (fold peers->preshared-keys (list) peers))
            (peers (map peer->config peers))
            (config
             (computed-file
@@ -780,7 +791,7 @@ (define (wireguard-configuration-file config)
 Address = ~a
 ~a
 ~a
-PostUp = ~a set %i private-key ~a
+PostUp = ~a set %i private-key ~a~{ peer ~a preshared-key ~a~}
 ~a
 ~a
 ~a
@@ -800,6 +811,7 @@ (define (wireguard-configuration-file config)
                                       "\n"))
                                #$(file-append wireguard "/bin/wg")
                                #$private-key
+                               '#$peer-keys
                                #$(if (null? post-up)
                                      ""
                                      (string-join
-- 
2.38.1





             reply	other threads:[~2022-12-18 19:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-18 17:19 Timo Wilken [this message]
2022-12-25 15:59 ` bug#60182: [PATCH] services: wireguard: Allow specifying pre-shared keys Mathieu Othacehe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221218171947.vkhy4cwbr2wtwfjp@timo-pc \
    --to=guix@twilken.net \
    --cc=60182@debbugs.gnu.org \
    --cc=jgart@dismail.de \
    --cc=ludo@gnu.org \
    --cc=mail@cbaines.net \
    --cc=othacehe@gnu.org \
    --cc=paren@disroot.org \
    --cc=rekado@elephly.net \
    --cc=rg@raghavgururajan.name \
    --cc=zimon.toutoune@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.