document the ipsec::network and ipsec::peer manifests, change default address to...
[mirror/dsa-puppet.git] / modules / ipsec / manifests / peer.pp
1 # an ipsec peer, another node to connect to
2 #
3 # This is the stored config part of ipsec::network.  Each node that
4 # is part of a network stores an ipsec::peer entry for itself and
5 # then collects all other nodes of that network, overwriting
6 # the local_* variables for itself.
7 #
8 # @param network_name       name of this ipsec network clique
9 # @param ipsec_conf_file    the target of the ipsec config file concat
10 # @param ipsec_secrets_file the target of the ipsec secrets file concat
11 # @param local_name         the name of this node (overwritten on collecting)
12 # @param local_ipaddress    the ipsec endpoint address on this node (overwritten on collecting)
13 # @param local_networks     a list of local networks (overwritten on collecting)
14 # @param peer_name          the name of this peer
15 # @param peer_ipaddress     the ipsec endpoint address of this peer
16 # @param peer_networks      a list of networks behind or at this peer
17 define ipsec::peer(
18   $ipsec_conf_file,
19   $ipsec_secrets_file,
20
21   $local_name,
22   $local_ipaddress,
23   $peer_name,
24   $peer_ipaddress,
25
26   $local_networks = [],
27   $peer_networks = [],
28
29   $network_name = 'ipsec',
30 ) {
31   $leftsubnet = $local_networks ? {
32     [] => '',
33     default => "leftsubnet = ${$local_networks.join(', ')}"
34   }
35   $rightsubnet = $peer_networks ? {
36     [] => '',
37     default => "rightsubnet = ${$peer_networks.join(', ')}"
38   }
39   concat::fragment { "${network_name}::${ipsec_conf_file}::${name}":
40     target  => $ipsec_conf_file,
41     content => @("EOF"),
42                # peer ${name}
43                conn ${network_name}::${peer_name}
44                  # left is us (local, ${local_name})
45                  left       = ${local_ipaddress}
46                  ${leftsubnet}
47
48                  # right is our peer (remote, ${peer_name})
49                  right      = ${peer_ipaddress}
50                  ${rightsubnet}
51
52                  auto=route
53                | EOF
54   }
55
56   # create the data portion for the key derivation function
57   #
58   # It needs to be the same data on both ends of a connection, so the
59   # corresponding secrets entry at the peer gets the same PSK.  We do
60   # this by putting the peer's info and our info in some arbitrary,
61   # yet canonical order by sorting.
62   $ipsec_psk_data = ("${local_name}(${local_ipaddress})" < "${peer_name}(${peer_ipaddress})") ? {
63     true  => "ipsec-peer-psk-${network_name}-${local_name}(${local_ipaddress})-${peer_name}(${peer_ipaddress})",
64     false => "ipsec-peer-psk-${network_name}-${peer_name}(${peer_ipaddress})-${local_name}(${local_ipaddress})"
65   }
66   $ipsec_psk = hkdf('/etc/puppet/secret', $ipsec_psk_data)
67   concat::fragment { "${network_name}::${ipsec_secrets_file}::${name}":
68     target  => $ipsec_secrets_file,
69     content => @("EOF"),
70                # peer ${peer_name}
71                ${peer_ipaddress} : PSK "${ipsec_psk}"
72                | EOF
73   }
74
75   ferm::rule { "${network_name}-${name}":
76     description => "allow ipsec protocols for peer ${peer_name}",
77     domain      => '(ip ip6)',
78     chain       => 'ipsec-peers',
79     rule        => "saddr ${peer_ipaddress} ACCEPT",
80   }
81 }