document the ipsec::network and ipsec::peer manifests, change default address to...
[mirror/dsa-puppet.git] / modules / ipsec / manifests / network.pp
1 # make this node a member of a common ipsec network
2 #
3 # the name of this resource a tag for an network where nodes in the
4 # same "network" will have ipsec set up between them.
5 #
6 # This is sufficient to setup a tunnel between a cluster of machines,
7 # but requires a reboot, see the parent ipsec class.
8 #
9 # WARNING: default ipsec configuration tunnels only the IP address
10 # given, which means that this default configuration only tunnels
11 # IPv4, not IPv6.
12 #
13 # Use $peer_networks = [ "${::ipaddress}/32", "${::ipaddress6}/128" ]
14 # to tunnel both addresses.
15 #
16 # @param peer_ipaddress     the ipsec endpoint address of this ipsec node
17 # @param peer_networks      a list of networks behind or at this ipsec node
18 define ipsec::network (
19   Stdlib::IP::Address $peer_ipaddress       = $base::public_address,
20   Array[Stdlib::IP::Address] $peer_networks = [],
21 ) {
22   include ipsec
23
24   $ipsec_conf_file = "/etc/ipsec.conf.d/10-puppet-${name}.conf"
25   $ipsec_secrets_file = "/etc/ipsec.secrets.d/10-puppet-${name}.secrets"
26   $stored_conftag = "ipsec::peer::${name}"
27
28   $real_peer_networks = Array($peer_networks, true).map |$a| {
29     if    $a =~ Stdlib::IP::Address::V4::CIDR     { $a }
30     elsif $a =~ Stdlib::IP::Address::V4::Nosubnet { "${a}/32" }
31     elsif $a =~ Stdlib::IP::Address::V6::CIDR     { $a }
32     elsif $a =~ Stdlib::IP::Address::V6::Nosubnet { "${a}/128" }
33     else { fail("Do not know address type for ${a}") }
34   }
35
36   @@ipsec::peer{ "${name}-${::hostname}":
37     network_name       => $name,
38     peer_name          => $::hostname,
39     peer_ipaddress     => $peer_ipaddress,
40     peer_networks      => $real_peer_networks,
41     ipsec_conf_file    => $ipsec_conf_file,
42     ipsec_secrets_file => $ipsec_secrets_file,
43     tag                => $stored_conftag,
44     # those will be overriden on collection, below
45     local_name         => undef,
46     local_ipaddress    => undef,
47   }
48
49   concat { $ipsec_conf_file:
50     notify  => Service['ipsec'],
51   }
52   concat { $ipsec_secrets_file:
53     notify => Service['ipsec'],
54     mode   => '0400',
55   }
56   Ipsec::Peer <<| tag == $stored_conftag and peer_name != $::hostname|>> {
57     local_name      => $::hostname,
58     local_ipaddress => $peer_ipaddress,
59     local_networks  => $real_peer_networks,
60   }
61 }