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: {}) ⇒ 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
# 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: {}
)
  super()
  @vpc = vpc
  @name = name
  @client_id = client_id
  @issuer_url = issuer_url
  @ca = ca
  @groups = groups
  @cidr = cidr
  @fqdn = vpc.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
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



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

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

#create_inObject



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

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,
  ]
  keypairs = []
  keypairs.push(@ca.create_keypair_in(self, @fqdn)) 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



264
265
266
# File 'lib/terrafying/components/vpn_oidc.rb', line 264

def egress_security_group
  @service.egress_security_group
end

#ingress_security_groupObject



260
261
262
# File 'lib/terrafying/components/vpn_oidc.rb', line 260

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



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

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



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
217
218
219
220
221
222
223
224
225
226
# File 'lib/terrafying/components/vpn_oidc.rb', line 191

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



228
229
230
231
232
233
234
235
236
# File 'lib/terrafying/components/vpn_oidc.rb', line 228

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



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/terrafying/components/vpn_oidc.rb', line 240

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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/terrafying/components/vpn_oidc.rb', line 146

def openvpn_service
  Ignition.container_unit(
    'openvpn', 'kylemanna/openvpn',
    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



268
269
270
# File 'lib/terrafying/components/vpn_oidc.rb', line 268

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

#pingable_by_cidr(*cidrs) ⇒ Object



276
277
278
# File 'lib/terrafying/components/vpn_oidc.rb', line 276

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

#security_groupObject



256
257
258
# File 'lib/terrafying/components/vpn_oidc.rb', line 256

def security_group
  @service.security_group
end

#used_by(*services) ⇒ Object



272
273
274
# File 'lib/terrafying/components/vpn_oidc.rb', line 272

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

#used_by_cidr(*cidrs) ⇒ Object



280
281
282
# File 'lib/terrafying/components/vpn_oidc.rb', line 280

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

#with_endpoint_service(*args) ⇒ Object



252
253
254
# File 'lib/terrafying/components/vpn_oidc.rb', line 252

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