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



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

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: "      \#{@fqdn}:443\n      tls \#{tls}\n      proxy / localhost:\#{port} {\n        transparent\n      }\n    CADDYFILE\n  }\nend\n"

#caddy_service(ca) ⇒ Object



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

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
# 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::SHA512.hexdigest(vpc.name + name + oauth2_provider[:client_secret] + oauth2_provider[:client_id])
    oauth2_provider[:cookie_hash_key]  ||= vpn_hash.byteslice(0, 64)
    oauth2_provider[:cookie_block_key] ||= vpn_hash.byteslice(64, 32)

    units.push(oauth2_proxy_service(oauth2_provider))
  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])
                  )
  self
end

#egress_security_groupObject



302
303
304
# File 'lib/terrafying/components/vpn.rb', line 302

def egress_security_group
  @service.egress_security_group
end

#ingress_security_groupObject



298
299
300
# File 'lib/terrafying/components/vpn.rb', line 298

def ingress_security_group
  @service.ingress_security_group
end

#oauth2_proxy_service(oauth2_provider) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/terrafying/components/vpn.rb', line 170

def oauth2_proxy_service(oauth2_provider)
  Ignition.container_unit(
    'authnz', 'registry.usw.co/cloud/authnz-http-proxy:0.1',
    {
      host_networking: true,
      arguments: [
        '--addr=0.0.0.0:4180',
        '--backend-url=http://localhost:8080',
        "--oauth-client-id='#{oauth2_provider[:client_id]}'",
        "--oauth-client-secret='#{oauth2_provider[:client_secret]}'",
        "--cookie-hash-key='#{oauth2_provider[:cookie_hash_key]}'",
        "--cookie-block-key='#{oauth2_provider[:cookie_block_key]}'"
      ],
      volumes: [
        '/usr/share/ca-certificates:/etc/ssl/certs:ro'
      ]
    }
  )
end

#openvpn_authz_service(route_all_traffic) ⇒ Object



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

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/terrafying/components/vpn.rb', line 229

def openvpn_conf
  {
    path: "/etc/openvpn/openvpn.conf",
    mode: "0644",
    contents: "server \#{cidr_to_split_address(@cidr)}\nverb 3\n\niproute /etc/openvpn/ovpn_ip.sh\n\nkey /etc/ssl/openvpn/server/key\nca /etc/ssl/openvpn/ca/cert\ncert /etc/ssl/openvpn/server/cert\ndh /etc/ssl/openvpn/dh.pem\ntls-auth /etc/ssl/openvpn/ta.key\n\ncipher AES-256-CBC\nauth SHA512\ntls-version-min 1.2\n\nkey-direction 0\nkeepalive 10 60\npersist-key\npersist-tun\n\nproto udp\n# Rely on Docker to do port mapping, internally always 1194\nport 1194\ndev tun0\nstatus /tmp/openvpn-status.log\n\nuser nobody\ngroup nogroup\n"
  }
end

#openvpn_envObject



266
267
268
269
270
271
272
273
274
# File 'lib/terrafying/components/vpn.rb', line 266

def openvpn_env
  {
    path: "/etc/openvpn/ovpn_env.sh",
    mode: "0644",
    contents: "declare -x OVPN_SERVER=\#{@cidr}\n"
  }
end

#openvpn_ip_delayObject

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



278
279
280
281
282
283
284
285
286
287
288
# File 'lib/terrafying/components/vpn.rb', line 278

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

#openvpn_serviceObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/terrafying/components/vpn.rb', line 128

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



306
307
308
# File 'lib/terrafying/components/vpn.rb', line 306

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

#pingable_by_cidr(*cidrs) ⇒ Object



314
315
316
# File 'lib/terrafying/components/vpn.rb', line 314

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

#security_groupObject



294
295
296
# File 'lib/terrafying/components/vpn.rb', line 294

def security_group
  @service.security_group
end

#used_by(*services) ⇒ Object



310
311
312
# File 'lib/terrafying/components/vpn.rb', line 310

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

#used_by_cidr(*cidrs) ⇒ Object



318
319
320
# File 'lib/terrafying/components/vpn.rb', line 318

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

#with_endpoint_service(*args) ⇒ Object



290
291
292
# File 'lib/terrafying/components/vpn.rb', line 290

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