|
| 1 | +{ package }: |
| 2 | +import ../make-test-python.nix ( |
| 3 | + { pkgs, lib, ... }: |
| 4 | + let |
| 5 | + hosts = { |
| 6 | + a = { |
| 7 | + ip = "192.168.1.1"; |
| 8 | + subnet = "192.168.11.0/24"; |
| 9 | + tunnelIp = "192.168.11.1"; |
| 10 | + }; |
| 11 | + b = { |
| 12 | + ip = "192.168.1.2"; |
| 13 | + subnet = "192.168.12.0/24"; |
| 14 | + tunnelIp = "192.168.12.1"; |
| 15 | + }; |
| 16 | + }; |
| 17 | + |
| 18 | + ipUpDownScript = host: rec{ |
| 19 | + storePath = pkgs.writeShellScript "strongswan-updown.sh" '' |
| 20 | + set +e |
| 21 | + |
| 22 | + case "$PLUTO_VERB" in |
| 23 | + up-client) |
| 24 | + ip link add test type dummy |
| 25 | + ip addr add ${host.tunnelIp}/32 dev test |
| 26 | + ip link set up dev test |
| 27 | + ;; |
| 28 | + esac |
| 29 | + ''; |
| 30 | + shortEtcPath = "strongswan/strongswan-updown.sh"; |
| 31 | + fullEtcPath = "/etc/${shortEtcPath}"; |
| 32 | + }; |
| 33 | + |
| 34 | + conn = left: right: { |
| 35 | + auto = "start"; |
| 36 | + leftupdown = (ipUpDownScript left).fullEtcPath; |
| 37 | + left = left.ip; |
| 38 | + leftsubnet = left.subnet; |
| 39 | + right = right.ip; |
| 40 | + rightsubnet = right.subnet; |
| 41 | + authby = "secret"; |
| 42 | + type = "tunnel"; |
| 43 | + keyexchange = "ikev2"; |
| 44 | + dpdaction = "restart"; |
| 45 | + }; |
| 46 | + |
| 47 | + mkNode = name: { pkgs, ... }: |
| 48 | + let |
| 49 | + me = hosts.${name}; |
| 50 | + other = if name == "a" then hosts.b else hosts.a; |
| 51 | + myIfUpDownScript = ipUpDownScript me; |
| 52 | + allowESP = "iptables --insert INPUT --protocol ESP --jump ACCEPT"; |
| 53 | + in |
| 54 | + { |
| 55 | + environment.systemPackages = with pkgs; [ |
| 56 | + iproute2 |
| 57 | + ]; |
| 58 | + |
| 59 | + networking.firewall = { |
| 60 | + allowedUDPPorts = [ |
| 61 | + 4500 |
| 62 | + 500 |
| 63 | + ]; |
| 64 | + extraCommands = allowESP; |
| 65 | + }; |
| 66 | + |
| 67 | + networking.useDHCP = false; |
| 68 | + networking.interfaces.eth0.useDHCP = true; |
| 69 | + networking.interfaces.eth1.ipv4.addresses = lib.mkForce [ |
| 70 | + { address = me.ip; prefixLength = 24; } |
| 71 | + ]; |
| 72 | + |
| 73 | + environment.etc.${myIfUpDownScript.shortEtcPath} = { |
| 74 | + source = myIfUpDownScript.storePath; |
| 75 | + mode = "0755"; |
| 76 | + }; |
| 77 | + |
| 78 | + services.strongswan = { |
| 79 | + enable = true; |
| 80 | + inherit package; |
| 81 | + connections.test = conn me other; |
| 82 | + secrets = [ |
| 83 | + (toString (pkgs.writeText "test.secrets" "${other.ip} : PSK NixpZAZqEN6Ti9sqt4ZP5EWcqx")) |
| 84 | + ]; |
| 85 | + }; |
| 86 | + }; |
| 87 | + in |
| 88 | + { |
| 89 | + name = "strongswan"; |
| 90 | + meta.maintainers = [ lib.maintainers.johanot ]; |
| 91 | + |
| 92 | + nodes = { |
| 93 | + nodeA = mkNode "a"; |
| 94 | + nodeB = mkNode "b"; |
| 95 | + }; |
| 96 | + |
| 97 | + testScript = '' |
| 98 | + start_all() |
| 99 | +
|
| 100 | + nodeA.wait_until_succeeds("ping -c 1 ${hosts.b.tunnelIp}") |
| 101 | + nodeB.wait_until_succeeds("ping -c 1 ${hosts.a.tunnelIp}") |
| 102 | + ''; |
| 103 | + } |
| 104 | +) |
0 commit comments