summaryrefslogtreecommitdiff
path: root/puppet/modules/site_openvpn/manifests/init.pp
blob: 42146741a60646ee3974a811ec0803f462688bd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#
# An openvpn gateway can support three modes:
#
#   (1) limited and unlimited
#   (2) unlimited only
#   (3) limited only
#
# The difference is that 'unlimited' gateways only allow client certs that match
# the 'unlimited_prefix', and 'limited' gateways only allow certs that match the
# 'limited_prefix'.
#
# We potentially create four openvpn config files (thus four daemons):
#
#   (1) unlimited + tcp => tcp_config.conf
#   (2) unlimited + udp => udp_config.conf
#   (3) limited + tcp => limited_tcp_config.conf
#   (4) limited + udp => limited_udp_config.conf
#

class site_openvpn {
  tag 'leap_service'

  include site_config::x509::cert
  include site_config::x509::key
  include site_config::x509::ca_bundle


  Class['site_config::default'] -> Class['site_openvpn']

  $openvpn_config   = hiera('openvpn')
  $openvpn_ports    = $openvpn_config['ports']

  if $::ec2_instance_id {
    $openvpn_gateway_address = $::ipaddress
  } else {
    $openvpn_gateway_address         = $openvpn_config['gateway_address']
    if $openvpn_config['second_gateway_address'] {
      $openvpn_second_gateway_address = $openvpn_config['second_gateway_address']
    } else {
      $openvpn_second_gateway_address = undef
    }
  }

  $openvpn_allow_unlimited              = $openvpn_config['allow_unlimited']
  $openvpn_unlimited_prefix             = $openvpn_config['unlimited_prefix']
  $openvpn_unlimited_tcp_network_prefix = '10.41.0'
  $openvpn_unlimited_tcp_netmask        = '255.255.248.0'
  $openvpn_unlimited_tcp_cidr           = '21'
  $openvpn_unlimited_udp_network_prefix = '10.42.0'
  $openvpn_unlimited_udp_netmask        = '255.255.248.0'
  $openvpn_unlimited_udp_cidr           = '21'

  if !$::ec2_instance_id {
    $openvpn_allow_limited                = $openvpn_config['allow_limited']
    $openvpn_limited_prefix               = $openvpn_config['limited_prefix']
    $openvpn_rate_limit                   = $openvpn_config['rate_limit']
    $openvpn_limited_tcp_network_prefix   = '10.43.0'
    $openvpn_limited_tcp_netmask          = '255.255.248.0'
    $openvpn_limited_tcp_cidr             = '21'
    $openvpn_limited_udp_network_prefix   = '10.44.0'
    $openvpn_limited_udp_netmask          = '255.255.248.0'
    $openvpn_limited_udp_cidr             = '21'
  }

  # find out the netmask in cidr format of the primary IF
  # thx to https://blog.kumina.nl/tag/puppet-tips-and-tricks/
  # we can do this using an inline_template:
  $factname_primary_netmask = "netmask_cidr_${::site_config::params::interface}"
  $primary_netmask = inline_template('<%= scope.lookupvar(factname_primary_netmask) %>')

  # deploy dh keys
  include site_openvpn::dh_key

  if $openvpn_allow_unlimited and $openvpn_allow_limited {
    $unlimited_gateway_address = $openvpn_gateway_address
    $limited_gateway_address = $openvpn_second_gateway_address
  } elsif $openvpn_allow_unlimited {
    $unlimited_gateway_address = $openvpn_gateway_address
    $limited_gateway_address = undef
  } elsif $openvpn_allow_limited {
    $unlimited_gateway_address = undef
    $limited_gateway_address = $openvpn_gateway_address
  }

  if $openvpn_allow_unlimited {
    site_openvpn::server_config { 'tcp_config':
      port        => '1194',
      proto       => 'tcp',
      local       => $unlimited_gateway_address,
      tls_remote  => "\"${openvpn_unlimited_prefix}\"",
      server      => "${openvpn_unlimited_tcp_network_prefix}.0 ${openvpn_unlimited_tcp_netmask}",
      push        => "\"dhcp-option DNS ${openvpn_unlimited_tcp_network_prefix}.1\"",
      management  => '127.0.0.1 1000'
    }
    site_openvpn::server_config { 'udp_config':
      port        => '1194',
      proto       => 'udp',
      local       => $unlimited_gateway_address,
      tls_remote  => "\"${openvpn_unlimited_prefix}\"",
      server      => "${openvpn_unlimited_udp_network_prefix}.0 ${openvpn_unlimited_udp_netmask}",
      push        => "\"dhcp-option DNS ${openvpn_unlimited_udp_network_prefix}.1\"",
      management  => '127.0.0.1 1001'
    }
  } else {
    tidy { '/etc/openvpn/tcp_config.conf': }
    tidy { '/etc/openvpn/udp_config.conf': }
  }

  if $openvpn_allow_limited {
    site_openvpn::server_config { 'limited_tcp_config':
      port        => '1194',
      proto       => 'tcp',
      local       => $limited_gateway_address,
      tls_remote  => "\"${openvpn_limited_prefix}\"",
      server      => "${openvpn_limited_tcp_network_prefix}.0 ${openvpn_limited_tcp_netmask}",
      push        => "\"dhcp-option DNS ${openvpn_limited_tcp_network_prefix}.1\"",
      management  => '127.0.0.1 1002'
    }
    site_openvpn::server_config { 'limited_udp_config':
      port        => '1194',
      proto       => 'udp',
      local       => $limited_gateway_address,
      tls_remote  => "\"${openvpn_limited_prefix}\"",
      server      => "${openvpn_limited_udp_network_prefix}.0 ${openvpn_limited_udp_netmask}",
      push        => "\"dhcp-option DNS ${openvpn_limited_udp_network_prefix}.1\"",
      management  => '127.0.0.1 1003'
    }
  } else {
    tidy { '/etc/openvpn/limited_tcp_config.conf': }
    tidy { '/etc/openvpn/limited_udp_config.conf': }
  }

  file {
    '/usr/local/bin/add_gateway_ips.sh':
      content => template('site_openvpn/add_gateway_ips.sh.erb'),
      mode    => '0755';
  }

  exec { '/usr/local/bin/add_gateway_ips.sh':
    subscribe   => File['/usr/local/bin/add_gateway_ips.sh'],
  }

  exec { 'restart_openvpn':
    command     => '/etc/init.d/openvpn restart',
    refreshonly => true,
    subscribe   => File['/etc/openvpn'],
    require     => [
      Package['openvpn'],
      File['/etc/openvpn'],
      Class['Site_config::X509::Key'],
      Class['Site_config::X509::Cert'],
      Class['Site_config::X509::Ca_bundle'] ];
  }

  cron { 'add_gateway_ips.sh':
    command => '/usr/local/bin/add_gateway_ips.sh',
    user    => 'root',
    special => 'reboot',
  }

  # setup the resolver to listen on the vpn IP
  include site_openvpn::resolver

  include site_shorewall::eip

  package {
    'openvpn':
      ensure => installed;
  }

  service {
    'openvpn':
      ensure     => running,
      hasrestart => true,
      hasstatus  => true,
      require    => [
        Package['openvpn'],
        Exec['concat_/etc/default/openvpn'] ];
  }

  file {
    '/etc/openvpn':
      ensure  => directory,
      notify  => Exec['restart_openvpn'],
      require => Package['openvpn'];
  }

  file {
    '/etc/openvpn/keys':
      ensure  => directory,
      require => Package['openvpn'];
  }

  concat {
    '/etc/default/openvpn':
      owner  => root,
      group  => root,
      mode   => 644,
      warn   => true,
      notify => Service['openvpn'];
  }

  concat::fragment {
    'openvpn.default.header':
      content => template('openvpn/etc-default-openvpn.erb'),
      target  => '/etc/default/openvpn',
      order   => 01;
  }

  concat::fragment {
    "openvpn.default.autostart.${name}":
      content => 'AUTOSTART=all',
      target  => '/etc/default/openvpn',
      order   => 10;
  }
}