Class: Terrafying::Components::OIDCVPN

Inherits:
Terrafying::Context
  • Object
show all
Defined in:
lib/terrafying/components/vpn_oidc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vpc:, name:, client_id:, issuer_url:, ca: nil, groups: [], cidr: '10.8.0.0/24', public: true, subnets: vpc.subnets.fetch(:public, []), static: false, route_all_traffic: false, route_dns_entries: [], units: [], tags: {}, service_options: {}, openvpn_image: 'quay.io/uswitch/openvpn:2.4.8') ⇒ OIDCVPN

Returns a new instance of OIDCVPN.



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
# File 'lib/terrafying/components/vpn_oidc.rb', line 35

def initialize(
  vpc:,
  name:,
  client_id:,
  issuer_url:,
  ca: nil,
  groups: [],
  cidr: '10.8.0.0/24',
  public: true,
  subnets: vpc.subnets.fetch(:public, []),
  static: false,
  route_all_traffic: false,
  route_dns_entries: [],
  units: [],
  tags: {},
  service_options: {},
  openvpn_image: 'quay.io/uswitch/openvpn:2.4.8'
)
  super()
  @vpc = vpc
  @name = name
  @client_id = client_id
  @issuer_url = issuer_url
  @ca = ca
  @groups = groups
  @cidr = cidr
  @zone = vpc.zone
  @fqdn = @zone.qualify(name)
  @public = public
  @subnets = subnets
  @static = static
  @route_all_traffic = route_all_traffic
  @route_dns_entries = route_dns_entries
  @units = units
  @tags = tags
  @service_options = service_options
  @openvpn_image = openvpn_image
end

Instance Attribute Details

#cidrObject (readonly)

Returns the value of attribute cidr.



29
30
31
# File 'lib/terrafying/components/vpn_oidc.rb', line 29

def cidr
  @cidr
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



29
30
31
# File 'lib/terrafying/components/vpn_oidc.rb', line 29

def ip_address
  @ip_address
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/terrafying/components/vpn_oidc.rb', line 29

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service.



29
30
31
# File 'lib/terrafying/components/vpn_oidc.rb', line 29

def service
  @service
end

Class Method Details

.create_in(options) ⇒ Object



31
32
33
# File 'lib/terrafying/components/vpn_oidc.rb', line 31

def self.create_in(options)
  new(**options).tap(&:create_in)
end

Instance Method Details

#allow_security_group_in(vpc, name: '') ⇒ Object



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
# File 'lib/terrafying/components/vpn_oidc.rb', line 129

def allow_security_group_in(vpc, name: '')
  name = "allow-#{@vpc.name}-vpn".downcase if name.empty?

  ingress_rules = [
    {
      from_port: 0,
      to_port: 0,
      protocol: -1,
      security_groups: [@service.egress_security_group]
    }
  ]

  if @public
    ingress_rules << {
      from_port: 0,
      to_port: 0,
      protocol: -1,
      cidr_blocks: ["#{@ip_address}/32"]
    }
  end

  resource :aws_security_group, tf_safe("#{name}-#{vpc.name}"),
           name: name,
           vpc_id: vpc.id,
           ingress: ingress_rules
end

#cert_checking_confObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/terrafying/components/vpn_oidc.rb', line 156

def cert_checking_conf
  {
    path: '/opt/cert_checking.yml',
    mode: '0644',
    contents: <<~CERT_CHECKING_CONF
      casource: #{@ca.source}
      caname: #{@ca.name}
      fqdn: #{@fqdn}
    CERT_CHECKING_CONF
  }
end

#cert_checking_pathObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/terrafying/components/vpn_oidc.rb', line 204

def cert_checking_path
  {

    name: 'cert_checking.path',
    contents: <<~CERT_CHECKING_PATH
      [Unit]
      Description=Monitor the file for changes
      [Path]
      PathChanged=/etc/ssl/#{@ca.name}/#{@fqdn}/cert
      Unit=restart-openvpn-authz.service
      [Install]
      WantedBy=multi-user.target
    CERT_CHECKING_PATH
  }
end

#cert_checking_serviceObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/terrafying/components/vpn_oidc.rb', line 184

def cert_checking_service
  {
  name: 'cert_checking.service',
  enabled: false,
  contents: <<~CERT_CHECKING_SERVICE
      [Install]
      WantedBy=multi-user.target
      [Unit]
      Description=cert_checking
      [Service]
      Type=oneshot
      ExecStartPre=-/usr/bin/docker rm -f cert_checking
      ExecStart=/usr/bin/docker run --name cert_checking  \
      -e AWS_REGION=#{aws.region} \
      -v /etc/ssl/#{@ca.name}:/etc/ssl/#{@ca.name} \
      -v /opt/cert_checking.yml:/cert_checking.yml quay.io/uswitch/cert-downloader:v1.0
  CERT_CHECKING_SERVICE
  }
end

#cert_checking_timerObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/terrafying/components/vpn_oidc.rb', line 168

def cert_checking_timer
  {

    name: 'cert_checking.timer',
    contents: <<~CERT_CHECKING_TIMER
      [Unit]
      Description=Certificate Checking Service Timer
      [Timer]
      OnCalendar=*-*-* 00:00:00
      Unit=cert_checking.service
      [Install]
      WantedBy=multi-user.target
    CERT_CHECKING_TIMER
  }
end

#create_inObject



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
# File 'lib/terrafying/components/vpn_oidc.rb', line 74

def create_in
  units = [
    openvpn_service,
    openvpn_authz_service(@ca, @fqdn, @route_all_traffic, @route_dns_entries, @groups, @client_id, @issuer_url),
  ]

  files = [
    openvpn_conf,
    openvpn_env,
    openvpn_ip_delay,
  ]

  if @ca
    units += [cert_checking_service, cert_checking_path, cert_checking_timer,restart_openvpn_authz_service]
    files << cert_checking_conf
  end

  keypairs = []
  keypairs.push(@ca.create_keypair_in(self, @fqdn, zone: @zone)) if @ca

  instances = [{}]
  if @static
    subnet = @subnets.first
    instances = [{ subnet: subnet, ip_address: subnet.ip_addresses.first }]
  end

  @service = add! Service.create_in(
    @vpc, @name,
    {
      eip: @public,
      public: @public,
      ports: [22, 443, { number: 1194, type: 'udp' }],
      tags:@tags,
      units: units + @units,
      files: files,
      keypairs: keypairs,
      subnets: @subnets,
      instances: instances,
      iam_policy_statements: [
        {
          Effect: 'Allow',
          Action: [
            'ec2:DescribeRouteTables'
          ],
          Resource: [
            '*'
          ]
        }
      ]
    }.merge(@service_options)
  )

  @ip_address = @service.instance_set.instances.first.ip_address
end

#egress_security_groupObject



354
355
356
# File 'lib/terrafying/components/vpn_oidc.rb', line 354

def egress_security_group
  @service.egress_security_group
end

#ingress_security_groupObject



350
351
352
# File 'lib/terrafying/components/vpn_oidc.rb', line 350

def ingress_security_group
  @service.ingress_security_group
end

#openvpn_authz_service(ca, fqdn, route_all_traffic, route_dns_entry, groups, client_id, issuer_url) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/terrafying/components/vpn_oidc.rb', line 249

def openvpn_authz_service(ca, fqdn, route_all_traffic, route_dns_entry, groups, client_id, issuer_url)
  optional_arguments = []
  optional_volumes = []

  optional_arguments << '--route-all' if route_all_traffic
  optional_arguments += groups.map { |group| "--oidc-allowed-groups \"#{group}\"" }
  optional_arguments += route_dns_entry.map { |entry| "--route-dns-entries #{entry}" }
  optional_arguments << "--tls-cert-file /etc/ssl/#{ca.name}/#{fqdn}/cert" if ca
  optional_arguments << "--tls-key-file /etc/ssl/#{ca.name}/#{fqdn}/key" if ca
  optional_volumes << "/etc/ssl/#{ca.name}:/etc/ssl/#{ca.name}" if ca

  Ignition.container_unit(
    'openvpn-authz', 'quay.io/uswitch/openvpn-authz:2.1',
    volumes: optional_volumes + [
      '/etc/ssl/openvpn:/etc/ssl/openvpn',
      '/var/openvpn-authz:/var/openvpn-authz'
    ],
    environment_variables: [
      "AWS_REGION=#{aws.region}"
    ],
    ports: ['443'],
    arguments: optional_arguments + [
      "--http-address https://0.0.0.0:443",
      "--fqdn #{fqdn}",
      '--cache /var/openvpn-authz',
      "--oidc-client-id \"#{client_id}\"",
      "--oidc-issuer-url \"#{issuer_url}\"",
      '/etc/ssl/openvpn'
    ]
  )
end

#openvpn_confObject



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/terrafying/components/vpn_oidc.rb', line 281

def openvpn_conf
  {
    path: '/etc/openvpn/openvpn.conf',
    mode: '0644',
    contents: <<~EOF
      server #{cidr_to_split_address(@cidr)}
      verb 3

      iproute /etc/openvpn/ovpn_ip.sh

      key /etc/ssl/openvpn/server/key
      ca /etc/ssl/openvpn/ca/cert
      cert /etc/ssl/openvpn/server/cert
      dh /etc/ssl/openvpn/dh.pem
      tls-auth /etc/ssl/openvpn/ta.key

      cipher AES-256-CBC
      auth SHA512
      tls-version-min 1.2

      key-direction 0
      keepalive 10 60
      persist-key
      persist-tun

      proto udp
      # Rely on Docker to do port mapping, internally always 1194
      port 1194
      dev tun0
      status /tmp/openvpn-status.log

      user nobody
      group nogroup
    EOF
  }
end

#openvpn_envObject



318
319
320
321
322
323
324
325
326
# File 'lib/terrafying/components/vpn_oidc.rb', line 318

def openvpn_env
  {
    path: '/etc/openvpn/ovpn_env.sh',
    mode: '0644',
    contents: <<~EOF
      declare -x OVPN_SERVER=#{@cidr}
    EOF
  }
end

#openvpn_ip_delayObject

OpenVPN doesn’t wait long enough for the tun0 device to init github.com/kylemanna/docker-openvpn/issues/370



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/terrafying/components/vpn_oidc.rb', line 330

def openvpn_ip_delay
  {
    path: '/etc/openvpn/ovpn_ip.sh',
    mode: '0755',
    contents: <<~IP_SCRIPT
      #!/usr/bin/env bash
      sleep 0.1
      /sbin/ip $*
    IP_SCRIPT
  }
end

#openvpn_serviceObject



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/terrafying/components/vpn_oidc.rb', line 236

def openvpn_service
  Ignition.container_unit(
    'openvpn', @openvpn_image,
    host_networking: true,
    privileged: true,
    volumes: [
      '/etc/ssl/openvpn:/etc/ssl/openvpn:ro',
      '/etc/openvpn:/etc/openvpn'
    ],
    required_units: ['docker.service', 'network-online.target', 'openvpn-authz.service']
  )
end

#pingable_by(*services) ⇒ Object



358
359
360
# File 'lib/terrafying/components/vpn_oidc.rb', line 358

def pingable_by(*services)
  @service.pingable_by(*services)
end

#pingable_by_cidr(*cidrs) ⇒ Object



366
367
368
# File 'lib/terrafying/components/vpn_oidc.rb', line 366

def pingable_by_cidr(*cidrs)
  @service.pingable_by_cidr(*cidrs)
end

#restart_openvpn_authz_serviceObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/terrafying/components/vpn_oidc.rb', line 220

def restart_openvpn_authz_service
  {
    name: 'restart-openvpn-authz.service',
    enabled: false,
    contents: <<~RESTART_OPENVPN_AUTHZ
        [Install]
        WantedBy=multi-user.target
        [Unit]
        Description=restart openvpn-authz service
        [Service]
        Type=oneshot
        ExecStart=/usr/bin/systemctl restart openvpn-authz.service
    RESTART_OPENVPN_AUTHZ
    }
end

#security_groupObject



346
347
348
# File 'lib/terrafying/components/vpn_oidc.rb', line 346

def security_group
  @service.security_group
end

#used_by(*services) ⇒ Object



362
363
364
# File 'lib/terrafying/components/vpn_oidc.rb', line 362

def used_by(*services)
  @service.used_by(*services)
end

#used_by_cidr(*cidrs) ⇒ Object



370
371
372
# File 'lib/terrafying/components/vpn_oidc.rb', line 370

def used_by_cidr(*cidrs)
  @service.used_by_cidr(*cidrs)
end

#with_endpoint_service(*args) ⇒ Object



342
343
344
# File 'lib/terrafying/components/vpn_oidc.rb', line 342

def with_endpoint_service(*args)
  @service.with_endpoint_service(*args)
end