From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id MOsnEgZkn2OHKQAAbAwnHQ (envelope-from ) for ; Sun, 18 Dec 2022 20:03:34 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id CPIgEgZkn2O0gQEA9RJhRA (envelope-from ) for ; Sun, 18 Dec 2022 20:03:34 +0100 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 12689C9DF for ; Sun, 18 Dec 2022 20:03:32 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p6ywX-0003Dk-Md; Sun, 18 Dec 2022 14:03:25 -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 1p6ynT-0002E6-Rw for guix-patches@gnu.org; Sun, 18 Dec 2022 13:54:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p6ynT-00039m-K3 for guix-patches@gnu.org; Sun, 18 Dec 2022 13:54:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p6ynT-0003KT-Fo for guix-patches@gnu.org; Sun, 18 Dec 2022 13:54:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#60182] [PATCH] services: wireguard: Allow specifying pre-shared keys. Resent-From: Timo Wilken Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 18 Dec 2022 18:54:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 60182 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch 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 X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.167138963312775 (code B ref -1); Sun, 18 Dec 2022 18:54:03 +0000 Received: (at submit) by debbugs.gnu.org; 18 Dec 2022 18:53:53 +0000 Received: from localhost ([127.0.0.1]:35380 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p6ynI-0003Jx-8t for submit@debbugs.gnu.org; Sun, 18 Dec 2022 13:53:52 -0500 Received: from lists.gnu.org ([209.51.188.17]:58214) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p6xL5-000274-TK for submit@debbugs.gnu.org; Sun, 18 Dec 2022 12:20:40 -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 1p6xL2-0004tT-Sy for guix-patches@gnu.org; Sun, 18 Dec 2022 12:20:39 -0500 Received: from mx1.mythic-beasts.com ([2a00:1098:0:86:1000:0:2:1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p6xL0-0003VJ-Rt; Sun, 18 Dec 2022 12:20:36 -0500 Received: from [5.147.160.230] (port=51744 helo=timo-pc) by mailhub-cam-d.mythic-beasts.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1p6xKu-00GFyt-56; Sun, 18 Dec 2022 17:20:28 +0000 Date: Sun, 18 Dec 2022 18:19:47 +0100 From: Timo Wilken Message-ID: <20221218171947.vkhy4cwbr2wtwfjp@timo-pc> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-BlackCat-Spam-Score: 19 Received-SPF: pass client-ip=2a00:1098:0:86:1000:0:2:1; envelope-from=guix@twilken.net; helo=mx1.mythic-beasts.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 18 Dec 2022 13:53:50 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Mailman-Approved-At: Sun, 18 Dec 2022 14:03:24 -0500 X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1671390213; a=rsa-sha256; cv=none; b=kvVnvmqOzGh1o923aLIOcUf2S7wmzGw0IXPouSgC07pDEngETdhEzyNguV5dc2Kp6Lfl/y pmxKyFYTIDMMpVjlN5lA+likSZT+UHPoG8/fJieQ4QaATHgz+mrbFaS296ubhrs8KuGLS3 KoC8WSIeZ7OMOr1u1yMNu7B5P0VKdpHg9DOke76OiUxyhIhZXN5447qXkezeNJZQmPOYWP Y37xvH7T64wKEGbn+tPKHpPYp2pT5amvpiJT7c/0TtwnffFFLixHqLOd8W34iUn8rwt6hk 1R+ZztSwOlcRSYFqXrjKvsoTVVdxp0r/IHvc1lhwURSPRTicX/jgbkqSmhozoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1671390213; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:resent-cc:resent-from:resent-sender: resent-message-id:list-id:list-help:list-unsubscribe:list-subscribe: list-post; bh=g4LkUzpmwFhBeMgAbJ1cV7iGlGurabYOtUB2DJb8gnQ=; b=NdL0BuTeUWeWqkmSwSQncnvQjzRkQaYnmA3xS30+DQ6w5Je/kcDsrUs0Je2+70/vtG88fs BN+BywBiqIDpqiggJ9H/qGMCnmyKJ7CvM7Of7Uhs2/o+yBXPcWtDW1eDs1Zp7TR+hLW1MZ hUzY1NV9fry1n6oy6XFS2hRNftiWOoYgJnivF363F5gBHLGYHtMPIh1tgW6t7yxgDGpFok wSp7D3I94mPiWb6eHaCHMaZHUTSUFUhUyHkJfouAbw1xsNdEfsaQXGT4yP1kMfH7sx5pgo azb5UF+Q0fKHpBGJXBQO2n6CisauAUz+J4upQ3N8vqKl05qRLejrPcNcPRaWNg== Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Scanner: scn1.migadu.com X-Migadu-Spam-Score: 0.12 X-Spam-Score: 0.12 X-Migadu-Queue-Id: 12689C9DF X-TUID: 3hx53LjdYO+D 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 (): 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* (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 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