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
155
156
157
158
159
160
161
162
163
164
# 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],
      ipv6_cidr_blocks: nil,
      prefix_list_ids: nil,
      cidr_blocks: nil,
      self: nil,
      description: nil,
    }
  ]

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

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

#cert_checking_confObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/terrafying/components/vpn_oidc.rb', line 166

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/terrafying/components/vpn_oidc.rb', line 214

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/terrafying/components/vpn_oidc.rb', line 194

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/terrafying/components/vpn_oidc.rb', line 178

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



364
365
366
# File 'lib/terrafying/components/vpn_oidc.rb', line 364

def egress_security_group
  @service.egress_security_group
end

#ingress_security_groupObject



360
361
362
# File 'lib/terrafying/components/vpn_oidc.rb', line 360

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



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/terrafying/components/vpn_oidc.rb', line 259

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.2',
    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



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
317
318
319
320
321
322
323
324
325
326
# File 'lib/terrafying/components/vpn_oidc.rb', line 291

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



328
329
330
331
332
333
334
335
336
# File 'lib/terrafying/components/vpn_oidc.rb', line 328

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



340
341
342
343
344
345
346
347
348
349
350
# File 'lib/terrafying/components/vpn_oidc.rb', line 340

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



246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/terrafying/components/vpn_oidc.rb', line 246

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



368
369
370
# File 'lib/terrafying/components/vpn_oidc.rb', line 368

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

#pingable_by_cidr(*cidrs) ⇒ Object



376
377
378
# File 'lib/terrafying/components/vpn_oidc.rb', line 376

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

#restart_openvpn_authz_serviceObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/terrafying/components/vpn_oidc.rb', line 230

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



356
357
358
# File 'lib/terrafying/components/vpn_oidc.rb', line 356

def security_group
  @service.security_group
end

#used_by(*services) ⇒ Object



372
373
374
# File 'lib/terrafying/components/vpn_oidc.rb', line 372

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

#used_by_cidr(*cidrs) ⇒ Object



380
381
382
# File 'lib/terrafying/components/vpn_oidc.rb', line 380

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

#with_endpoint_service(*args) ⇒ Object



352
353
354
# File 'lib/terrafying/components/vpn_oidc.rb', line 352

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