Class: Terrafying::Components::VPN

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVPN

Returns a new instance of VPN.



37
38
39
# File 'lib/terrafying/components/vpn.rb', line 37

def initialize()
  super
end

Instance Attribute Details

#cidrObject (readonly)

Returns the value of attribute cidr.



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

def cidr
  @cidr
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.create_in(vpc, name, provider, options = {}) ⇒ Object



33
34
35
# File 'lib/terrafying/components/vpn.rb', line 33

def self.create_in(vpc, name, provider, options={})
  VPN.new.create_in vpc, name, provider, options
end

Instance Method Details

#caddy_conf(ca, has_provider) ⇒ Object



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

def caddy_conf(ca, has_provider)
  port = has_provider ? "4180" : "8080"
  tls = ca ? "/etc/ssl/#{ca.name}/#{@fqdn}/cert /etc/ssl/#{ca.name}/#{@fqdn}/key" : "[email protected]"
  {
    path: "/etc/caddy/Caddyfile",
    mode: "0644",
    contents: <<EOF
#{@fqdn}:443
tls #{tls}
proxy / localhost:#{port}
EOF
}
end

#caddy_service(ca) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/terrafying/components/vpn.rb', line 224

def caddy_service(ca)
  optional_volumes = []

  if ca
    optional_volumes << "/etc/ssl/#{ca.name}:/etc/ssl/#{ca.name}:ro"
  end

  Ignition.container_unit(
    "caddy", "abiosoft/caddy:0.10.10",
    {
      host_networking: true,
      volumes: [
        "/etc/ssl/certs:/etc/ssl/cert:ro",
        "/etc/caddy/Caddyfile:/etc/Caddyfile",
        "/etc/caddy/certs:/etc/caddy/certs",
      ] + optional_volumes,
      environment_variables: [
        "CADDYPATH=/etc/caddy/certs",
      ],
    }
  )
end

#create_in(vpc, name, oauth2_provider, options = {}) ⇒ Object



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

def create_in(vpc, name, oauth2_provider, options={})
  options = {
    group: "uSwitch Developers",
    cidr: "10.8.0.0/24",
    public: true,
    subnets: vpc.subnets.fetch(:public, []),
    static: false,
    route_all_traffic: false,
    units: [],
    tags: {},
    service: {},
  }.merge(options)

  @name = name
  @vpc = vpc
  @cidr = options[:cidr]
  @fqdn = vpc.zone.qualify(name)

  if ! oauth2_provider.is_a?(Hash)
    raise "You need to give a provider hash containing a type, client_id and client_secret"
  end

  has_provider = oauth2_provider[:type] != "none"

  if has_provider and ! [:type, :client_id, :client_secret].all? {|k| oauth2_provider.has_key?(k) }
    raise "You need to set type, client_id and client_secret"
  end

  units = [
    openvpn_service,
    openvpn_authz_service(options[:route_all_traffic]),
    caddy_service(options[:ca])
  ]
  files = [
    openvpn_conf,
    openvpn_env,
    openvpn_ip_delay,
    caddy_conf(options[:ca], has_provider)
  ]
  keypairs = []

  if has_provider
    vpn_hash = Digest::SHA2.digest(vpc.name + name + oauth2_provider[:client_secret] + oauth2_provider[:client_id])
    cookie_secret = Base64.strict_encode64(vpn_hash.byteslice(0,16))

    units.push(oauth2_proxy_service(oauth2_provider, cookie_secret))
  end

  if options.has_key?(:ca)
    keypairs.push(options[:ca].create_keypair_in(self, @fqdn))
  end

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

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

  if oauth2_provider[:type] == "azure" and oauth2_provider[:register]

    provider :null, {}

    resource :null_resource, "ad-app-configure", {
               triggers: {
                 service_resources: @service.resources.join(","),
               },
               provisioner: [
                 {
                   "local-exec" => {
                     when: "create",
                     command: "#{File.expand_path(File.dirname(__FILE__))}/support/register-vpn '#{oauth2_provider[:client_id]}' '#{oauth2_provider[:tenant_id]}' '#{@fqdn}'"
                   },
                 },
                 {
                   "local-exec" => {
                     when: "destroy",
                     command: "#{File.expand_path(File.dirname(__FILE__))}/support/deregister-vpn '#{oauth2_provider[:client_id]}' '#{oauth2_provider[:tenant_id]}' '#{@fqdn}'"
                   }
                 },
               ],
             }
  end

  self
end

#egress_security_groupObject



334
335
336
# File 'lib/terrafying/components/vpn.rb', line 334

def egress_security_group
  @service.egress_security_group
end

#ingress_security_groupObject



330
331
332
# File 'lib/terrafying/components/vpn.rb', line 330

def ingress_security_group
  @service.ingress_security_group
end

#oauth2_proxy_service(oauth2_provider, cookie_secret) ⇒ Object



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

def oauth2_proxy_service(oauth2_provider, cookie_secret)
  optional_arguments = []

  if oauth2_provider.has_key?(:permit_groups)
    optional_arguments << "-permit-groups '#{oauth2_provider[:permit_groups].join(",")}'"
  end

  Ignition.container_unit(
    "oauth2_proxy", "quay.io/uswitch/oauth2_proxy:stable",
    {
      host_networking: true,
      arguments: [
        "-client-id='#{oauth2_provider[:client_id]}'",
        "-client-secret='#{oauth2_provider[:client_secret]}'",
        "-email-domain='*'",
        "-cookie-secret='#{cookie_secret}'",
        "-provider=#{oauth2_provider[:type]}",
        "-http-address='0.0.0.0:4180'",
        "-redirect-url='https://#{@fqdn}/oauth2/callback'",
        "-upstream='http://localhost:8080'",
        "-approval-prompt=''",
        "-cookie-secure",
        "-pass-access-token=true",
        "-pass-groups",
      ] + optional_arguments
    }
  )
end

#openvpn_authz_service(route_all_traffic) ⇒ Object



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

def openvpn_authz_service(route_all_traffic)
  optional_arguments = []

  if route_all_traffic
    optional_arguments << "--route-all"
  end

  Ignition.container_unit(
    "openvpn-authz", "quay.io/uswitch/openvpn-authz:stable",
    {
      host_networking: true,
      volumes: [
        "/etc/ssl/openvpn:/etc/ssl/openvpn",
        "/var/openvpn-authz:/var/openvpn-authz",
      ],
      environment_variables: [
        "AWS_REGION=#{aws.region}",
      ],
      arguments: optional_arguments + [
        "--fqdn #{@fqdn}",
        "--cache /var/openvpn-authz",
        "/etc/ssl/openvpn",
      ],
    }
  )
end

#openvpn_confObject



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
290
291
292
293
294
295
296
# File 'lib/terrafying/components/vpn.rb', line 261

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



298
299
300
301
302
303
304
305
306
# File 'lib/terrafying/components/vpn.rb', line 298

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



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/terrafying/components/vpn.rb', line 310

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



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

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



338
339
340
# File 'lib/terrafying/components/vpn.rb', line 338

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

#pingable_by_cidr(*cidrs) ⇒ Object



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

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

#security_groupObject



326
327
328
# File 'lib/terrafying/components/vpn.rb', line 326

def security_group
  @service.security_group
end

#used_by(*services) ⇒ Object



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

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

#used_by_cidr(*cidrs) ⇒ Object



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

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

#with_endpoint_service(*args) ⇒ Object



322
323
324
# File 'lib/terrafying/components/vpn.rb', line 322

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