git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #60138 from grahamc/wireguard-generate-key

wireguard: add generatePrivateKeyFile option + test

commit28a95c4f7f1c46399e8938d7f3e08169cda61191 authorMaximilian Bosch <maximilian@mbosch.me> dateWed 24 Apr 2019, 16:00 (8 years ago) parent072c69f06c83a1
3 files changed+132−9
nixos/modules/services/networking/wireguard.nix+74−9 nixos/tests/all-tests.nix+1 nixos/tests/wireguard/generated.nix+57
nixos/modules/services/networking/wireguard.nix+74−9
@@ -26,19 +26,28 @@ let
2626 type = with types; nullOr str;
2727 default = null;
2828 description = ''
29− Base64 private key generated by wg genkey.
29+ Base64 private key generated by <command>wg genkey</command>.
3030
3131 Warning: Consider using privateKeyFile instead if you do not
3232 want to store the key in the world-readable Nix store.
3333 '';
3434 };
3535
36+ generatePrivateKeyFile = mkOption {
37+ default = false;
38+ type = types.bool;
39+ description = ''
40+ Automatically generate a private key with
41+ <command>wg genkey</command>, at the privateKeyFile location.
42+ '';
43+ };
44+
3645 privateKeyFile = mkOption {
3746 example = "/private/wireguard_key";
3847 type = with types; nullOr str;
3948 default = null;
4049 description = ''
41− Private key file as generated by wg genkey.
50+ Private key file as generated by <command>wg genkey</command>.
4251 '';
4352 };
4453
@@ -124,8 +133,8 @@ let
124133 example = "rVXs/Ni9tu3oDBLS4hOyAUAa1qTWVA3loR8eL20os3I=";
125134 type = with types; nullOr str;
126135 description = ''
127− Base64 preshared key generated by wg genpsk. Optional,
128− and may be omitted. This option adds an additional layer of
136+ Base64 preshared key generated by <command>wg genpsk</command>.
137+ Optional, and may be omitted. This option adds an additional layer of
129138 symmetric-key cryptography to be mixed into the already existing
130139 public-key cryptography, for post-quantum resistance.
131140
@@ -139,8 +148,8 @@ let
139148 example = "/private/wireguard_psk";
140149 type = with types; nullOr str;
141150 description = ''
142− File pointing to preshared key as generated by wg pensk. Optional,
143− and may be omitted. This option adds an additional layer of
151+ File pointing to preshared key as generated by <command>wg pensk</command>.
152+ Optional, and may be omitted. This option adds an additional layer of
144153 symmetric-key cryptography to be mixed into the already existing
145154 public-key cryptography, for post-quantum resistance.
146155 '';
@@ -182,9 +191,48 @@ let
182191
183192 };
184193
185− generateUnit = name: values:
194+
195+ generatePathUnit = name: values:
196+ assert (values.privateKey == null);
197+ assert (values.privateKeyFile != null);
198+ nameValuePair "wireguard-${name}"
199+ {
200+ description = "WireGuard Tunnel - ${name} - Private Key";
201+ requiredBy = [ "wireguard-${name}.service" ];
202+ before = [ "wireguard-${name}.service" ];
203+ pathConfig.PathExists = values.privateKeyFile;
204+ };
205+
206+ generateKeyServiceUnit = name: values:
207+ assert values.generatePrivateKeyFile;
208+ nameValuePair "wireguard-${name}-key"
209+ {
210+ description = "WireGuard Tunnel - ${name} - Key Generator";
211+ wantedBy = [ "wireguard-${name}.service" ];
212+ requiredBy = [ "wireguard-${name}.service" ];
213+ before = [ "wireguard-${name}.service" ];
214+ path = with pkgs; [ wireguard ];
215+
216+ serviceConfig = {
217+ Type = "oneshot";
218+ RemainAfterExit = true;
219+ };
220+
221+ script = ''
222+ mkdir --mode 0644 -p "${dirOf values.privateKeyFile}"
223+ if [ ! -f "${values.privateKeyFile}" ]; then
224+ touch "${values.privateKeyFile}"
225+ chmod 0600 "${values.privateKeyFile}"
226+ wg genkey > "${values.privateKeyFile}"
227+ chmod 0400 "${values.privateKeyFile}"
228+ fi
229+ '';
230+ };
231+
232+
233+ generateSetupServiceUnit = name: values:
186234 # exactly one way to specify the private key must be set
187− assert (values.privateKey != null) != (values.privateKeyFile != null);
235+ #assert (values.privateKey != null) != (values.privateKeyFile != null);
188236 let privKey = if values.privateKeyFile != null then values.privateKeyFile else pkgs.writeText "wg-key" values.privateKey;
189237 in
190238 nameValuePair "wireguard-${name}"
@@ -279,10 +327,27 @@ in
279327
280328 config = mkIf (cfg.interfaces != {}) {
281329
330+ assertions = (attrValues (
331+ mapAttrs (name: value: {
332+ assertion = (value.privateKey != null) != (value.privateKeyFile != null);
333+ message = "Either networking.wireguard.interfaces.${name}.privateKey or networking.wireguard.interfaces.${name}.privateKeyFile must be set.";
334+ }) cfg.interfaces))
335+ ++ (attrValues (
336+ mapAttrs (name: value: {
337+ assertion = value.generatePrivateKeyFile -> (value.privateKey == null);
338+ message = "networking.wireguard.interfaces.${name}.generatePrivateKey must not be set if networking.wireguard.interfaces.${name}.privateKey is set.";
339+ }) cfg.interfaces));
340+
341+
282342 boot.extraModulePackages = [ kernel.wireguard ];
283343 environment.systemPackages = [ pkgs.wireguard-tools ];
284344
285− systemd.services = mapAttrs' generateUnit cfg.interfaces;
345+ systemd.services = (mapAttrs' generateSetupServiceUnit cfg.interfaces)
346+ // (mapAttrs' generateKeyServiceUnit
347+ (filterAttrs (name: value: value.generatePrivateKeyFile) cfg.interfaces));
348+
349+ systemd.paths = mapAttrs' generatePathUnit
350+ (filterAttrs (name: value: value.privateKeyFile != null) cfg.interfaces);
286351
287352 };
288353
nixos/tests/all-tests.nix+1
@@ -238,6 +238,7 @@ in
238238 vault = handleTest ./vault.nix {};
239239 virtualbox = handleTestOn ["x86_64-linux"] ./virtualbox.nix {};
240240 wireguard = handleTest ./wireguard {};
241+ wireguard-generated = handleTest ./wireguard/generated.nix {};
241242 wordpress = handleTest ./wordpress.nix {};
242243 xautolock = handleTest ./xautolock.nix {};
243244 xdg-desktop-portal = handleTest ./xdg-desktop-portal.nix {};
nixos/tests/wireguard/generated.nixnew file+57
@@ -0,0 +1,57 @@
1+import ../make-test.nix ({ pkgs, ...} : {
2+ name = "wireguard-generated";
3+ meta = with pkgs.stdenv.lib.maintainers; {
4+ maintainers = [ ma27 grahamc ];
5+ };
6+
7+ nodes = {
8+ peer1 = {
9+ networking.firewall.allowedUDPPorts = [ 12345 ];
10+ networking.wireguard.interfaces.wg0 = {
11+ ips = [ "10.10.10.1/24" ];
12+ listenPort = 12345;
13+ privateKeyFile = "/etc/wireguard/private";
14+ generatePrivateKeyFile = true;
15+
16+ };
17+ };
18+
19+ peer2 = {
20+ networking.firewall.allowedUDPPorts = [ 12345 ];
21+ networking.wireguard.interfaces.wg0 = {
22+ ips = [ "10.10.10.2/24" ];
23+ listenPort = 12345;
24+ privateKeyFile = "/etc/wireguard/private";
25+ generatePrivateKeyFile = true;
26+ };
27+ };
28+ };
29+
30+ testScript = ''
31+ startAll;
32+
33+ $peer1->waitForUnit("wireguard-wg0.service");
34+ $peer2->waitForUnit("wireguard-wg0.service");
35+
36+ my ($retcode, $peer1pubkey) = $peer1->execute("wg pubkey < /etc/wireguard/private");
37+ $peer1pubkey =~ s/\s+$//;
38+ if ($retcode != 0) {
39+ die "Could not read public key from peer1";
40+ }
41+
42+ my ($retcode, $peer2pubkey) = $peer2->execute("wg pubkey < /etc/wireguard/private");
43+ $peer2pubkey =~ s/\s+$//;
44+ if ($retcode != 0) {
45+ die "Could not read public key from peer2";
46+ }
47+
48+ $peer1->succeed("wg set wg0 peer $peer2pubkey allowed-ips 10.10.10.2/32 endpoint 192.168.1.2:12345 persistent-keepalive 1");
49+ $peer1->succeed("ip route replace 10.10.10.2/32 dev wg0 table main");
50+
51+ $peer2->succeed("wg set wg0 peer $peer1pubkey allowed-ips 10.10.10.1/32 endpoint 192.168.1.1:12345 persistent-keepalive 1");
52+ $peer2->succeed("ip route replace 10.10.10.1/32 dev wg0 table main");
53+
54+ $peer1->succeed("ping -c1 10.10.10.2");
55+ $peer2->succeed("ping -c1 10.10.10.1");
56+ '';
57+})