terrafying-components 1.12.3 → 1.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ddbde82c2d58b321194378e086691805dfe735cd7fa50e971ea8ff4ac936857a
4
- data.tar.gz: b9f54244f14ba08017775d12040f8aca706d1f07218c694cf5950d3223a7189a
3
+ metadata.gz: b6da8b7007cb656c796f3c191b3e5987eb1370674c947c49a108ab0d4ac39cf3
4
+ data.tar.gz: 1ff85bce88c2cc69d00f05591a316e54b4d5db38d92b780f74c15c88c90de72b
5
5
  SHA512:
6
- metadata.gz: a03b6859dbd50737c5adefddca1c7a06d6574b6be02d900b5a3d827827c4c8dc0f91541a614d7f9bf03a05b765409202a5126148abe5b5c2b870a9cd356bd726
7
- data.tar.gz: 83c4448f92f85ab5568d90e455666a9136188e2fd646739c10ecda051d9a0617bc641f25d2a2c50615f60936440e02fe230514553a3320a2095a9893e99d66f2
6
+ metadata.gz: 5ac9cc17797ade6fbab7ee58869061b71ad18244d6b28dcc81ca8cff4ccc72f0fad45492e3bc0705d6fdcb57fee4225aecdf7031d88201e42ad44e81ac146dba
7
+ data.tar.gz: f65261b1d1786a2ddfebba957698d4c6293599253d5ff5bf52fe33f323e70b4ad041f131c3d380ebabe1934efecb86f30621e85ff42e3b4d553f5124b70279f0
@@ -9,4 +9,5 @@ require 'terrafying/components/service'
9
9
  require 'terrafying/components/subnet'
10
10
  require 'terrafying/components/vpc'
11
11
  require 'terrafying/components/vpn'
12
+ require 'terrafying/components/vpn_oidc'
12
13
  require 'terrafying/components/zone'
@@ -32,6 +32,7 @@ module Terrafying
32
32
  def create_in(vpc, name, options = {})
33
33
  options = {
34
34
  public: false,
35
+ eip: false,
35
36
  ami: aws.ami('base-image-24b8d5fb', owners = ['136393635417']),
36
37
  instance_type: 't3a.micro',
37
38
  instances: { min: 1, max: 1, desired: 1, tags: {} },
@@ -14,6 +14,7 @@ module Terrafying
14
14
  volumes: [],
15
15
  environment_variables: [],
16
16
  arguments: [],
17
+ ports: [],
17
18
  require_units: [],
18
19
  host_networking: false,
19
20
  privileged: false
@@ -29,17 +30,9 @@ module Terrafying
29
30
 
30
31
  docker_options = []
31
32
 
32
- if options[:environment_variables].count > 0
33
- docker_options += options[:environment_variables].map do |var|
34
- "-e #{var}"
35
- end
36
- end
37
-
38
- if options[:volumes].count > 0
39
- docker_options += options[:volumes].map do |volume|
40
- "-v #{volume}"
41
- end
42
- end
33
+ docker_options += options[:environment_variables].map { |var| "-e #{var}" }
34
+ docker_options += options[:volumes].map { |volume| "-v #{volume}" }
35
+ docker_options += options[:ports].map { |port| "-p #{port}:#{port}" }
43
36
 
44
37
  docker_options << '--net=host' if options[:host_networking]
45
38
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'xxhash'
4
4
 
5
+ require 'terrafying/components/ports'
5
6
  require 'terrafying/components/usable'
6
7
 
7
8
  module Terrafying
@@ -30,6 +31,7 @@ module Terrafying
30
31
  def create_in(vpc, name, options = {})
31
32
  options = {
32
33
  public: false,
34
+ eip: false,
33
35
  instance_type: 't3a.micro',
34
36
  instance_profile: nil,
35
37
  ports: [],
@@ -78,12 +80,14 @@ module Terrafying
78
80
  @subnet = subnets[subnet_index]
79
81
  end
80
82
 
83
+ associate_public_ip_address = options[:eip] || options[:public]
84
+
81
85
  @id = resource :aws_instance, ident, {
82
86
  ami: options[:ami],
83
87
  instance_type: options[:instance_type],
84
88
  iam_instance_profile: profile_from(options[:instance_profile]),
85
89
  subnet_id: @subnet.id,
86
- associate_public_ip_address: options[:public],
90
+ associate_public_ip_address: associate_public_ip_address,
87
91
  root_block_device: {
88
92
  volume_type: 'gp2',
89
93
  volume_size: 32
@@ -101,7 +105,15 @@ module Terrafying
101
105
  depends_on: options[:depends_on]
102
106
  }.merge(options[:ip_address] ? { private_ip: options[:ip_address] } : {}).merge(lifecycle)
103
107
 
104
- @ip_address = output_of(:aws_instance, ident, options[:public] ? :public_ip : :private_ip)
108
+ @ip_address = @id[options[:public] ? :public_ip : :private_ip]
109
+
110
+ if options[:eip]
111
+ @eip = resource :aws_eip, ident, {
112
+ instance: @id,
113
+ vpc: true
114
+ }
115
+ @ip_address = @eip[:public_ip]
116
+ end
105
117
 
106
118
  self
107
119
  end
@@ -22,7 +22,7 @@ module Terrafying
22
22
  thanos_name: 'thanos',
23
23
  thanos_version: 'v0.4.0',
24
24
  prom_name: 'prometheus',
25
- prom_version: 'v2.9.2',
25
+ prom_version: 'v2.10.0',
26
26
  instances: 2,
27
27
  instance_type: 't3a.small'
28
28
  )
@@ -37,6 +37,7 @@ module Terrafying
37
37
  def create_in(vpc, name, options = {})
38
38
  options = {
39
39
  public: false,
40
+ eip: false,
40
41
  ami: aws.ami('base-image-24b8d5fb', owners = ['136393635417']),
41
42
  instance_type: 't3a.micro',
42
43
  subnets: vpc.subnets.fetch(:private, []),
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Terrafying
4
4
  module Components
5
- VERSION = '1.12.3'
5
+ VERSION = '1.12.4'
6
6
  end
7
7
  end
@@ -0,0 +1,285 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ require 'digest'
5
+ require 'netaddr'
6
+
7
+ require 'terrafying/components/ignition'
8
+ require 'terrafying/components/service'
9
+ require 'terrafying/generator'
10
+
11
+ IN4MASK = 0xffffffff
12
+
13
+ def cidr_to_split_address(raw_cidr)
14
+ cidr = NetAddr::CIDR.create(raw_cidr)
15
+
16
+ masklen = 32 - cidr.bits
17
+ maskaddr = ((IN4MASK >> masklen) << masklen)
18
+
19
+ maskip = (0..3).map do |i|
20
+ (maskaddr >> (24 - 8 * i)) & 0xff
21
+ end.join('.')
22
+
23
+ "#{cidr.first} #{maskip}"
24
+ end
25
+
26
+ module Terrafying
27
+ module Components
28
+ class OIDCVPN < Terrafying::Context
29
+ attr_reader :name, :cidr, :service, :ip_address
30
+
31
+ def self.create_in(options)
32
+ new(**options).tap(&:create_in)
33
+ end
34
+
35
+ def initialize(
36
+ vpc:,
37
+ name:,
38
+ client_id:,
39
+ issuer_url:,
40
+ ca: nil,
41
+ groups: [],
42
+ cidr: '10.8.0.0/24',
43
+ public: true,
44
+ subnets: vpc.subnets.fetch(:public, []),
45
+ static: false,
46
+ route_all_traffic: false,
47
+ route_dns_entries: [],
48
+ units: [],
49
+ tags: {},
50
+ service_options: {}
51
+ )
52
+ super()
53
+ @vpc = vpc
54
+ @name = name
55
+ @client_id = client_id
56
+ @issuer_url = issuer_url
57
+ @ca = ca
58
+ @groups = groups
59
+ @cidr = cidr
60
+ @fqdn = vpc.zone.qualify(name)
61
+ @public = public
62
+ @subnets = subnets
63
+ @static = static
64
+ @route_all_traffic = route_all_traffic
65
+ @route_dns_entries = route_dns_entries
66
+ @units = units
67
+ @tags = tags
68
+ @service_options = service_options
69
+ end
70
+
71
+ def create_in
72
+ units = [
73
+ openvpn_service,
74
+ openvpn_authz_service(@ca, @fqdn, @route_all_traffic, @route_dns_entries, @groups, @client_id, @issuer_url),
75
+ ]
76
+ files = [
77
+ openvpn_conf,
78
+ openvpn_env,
79
+ openvpn_ip_delay,
80
+ ]
81
+ keypairs = []
82
+ keypairs.push(@ca.create_keypair_in(self, @fqdn)) if @ca
83
+
84
+ instances = [{}]
85
+ if @static
86
+ subnet = @subnets.first
87
+ instances = [{ subnet: subnet, ip_address: subnet.ip_addresses.first }]
88
+ end
89
+
90
+ @service = add! Service.create_in(
91
+ @vpc, @name,
92
+ {
93
+ eip: @public,
94
+ public: @public,
95
+ ports: [22, 443, { number: 1194, type: 'udp' }],
96
+ tags:@tags,
97
+ units: units + @units,
98
+ files: files,
99
+ keypairs: keypairs,
100
+ subnets: @subnets,
101
+ instances: instances,
102
+ iam_policy_statements: [
103
+ {
104
+ Effect: 'Allow',
105
+ Action: [
106
+ 'ec2:DescribeRouteTables'
107
+ ],
108
+ Resource: [
109
+ '*'
110
+ ]
111
+ }
112
+ ]
113
+ }.merge(@service_options)
114
+ )
115
+
116
+ @ip_address = @service.instance_set.instances.first.ip_address
117
+ end
118
+
119
+ def allow_security_group_in(vpc, name: '')
120
+ name = "allow-#{@vpc.name}-vpn".downcase if name.empty?
121
+
122
+ ingress_rules = [
123
+ {
124
+ from_port: 0,
125
+ to_port: 0,
126
+ protocol: -1,
127
+ security_groups: [@service.egress_security_group]
128
+ }
129
+ ]
130
+
131
+ if @public
132
+ ingress_rules << {
133
+ from_port: 0,
134
+ to_port: 0,
135
+ protocol: -1,
136
+ cidr_blocks: ["#{@ip_address}/32"]
137
+ }
138
+ end
139
+
140
+ resource :aws_security_group, tf_safe("#{name}-#{vpc.name}"),
141
+ name: name,
142
+ vpc_id: vpc.id,
143
+ ingress: ingress_rules
144
+ end
145
+
146
+ def openvpn_service
147
+ Ignition.container_unit(
148
+ 'openvpn', 'kylemanna/openvpn',
149
+ host_networking: true,
150
+ privileged: true,
151
+ volumes: [
152
+ '/etc/ssl/openvpn:/etc/ssl/openvpn:ro',
153
+ '/etc/openvpn:/etc/openvpn'
154
+ ],
155
+ required_units: ['docker.service', 'network-online.target', 'openvpn-authz.service']
156
+ )
157
+ end
158
+
159
+ def openvpn_authz_service(ca, fqdn, route_all_traffic, route_dns_entry, groups, client_id, issuer_url)
160
+ optional_arguments = []
161
+ optional_volumes = []
162
+
163
+ optional_arguments << '--route-all' if route_all_traffic
164
+ optional_arguments += groups.map { |group| "--oidc-allowed-groups \"#{group}\"" }
165
+ optional_arguments += route_dns_entry.map { |entry| "--route-dns-entries #{entry}" }
166
+ optional_arguments << "--tls-cert-file /etc/ssl/#{ca.name}/#{fqdn}/cert" if ca
167
+ optional_arguments << "--tls-key-file /etc/ssl/#{ca.name}/#{fqdn}/key" if ca
168
+ optional_volumes << "/etc/ssl/#{ca.name}:/etc/ssl/#{ca.name}" if ca
169
+
170
+ Ignition.container_unit(
171
+ 'openvpn-authz', 'quay.io/uswitch/openvpn-authz:2.1',
172
+ volumes: optional_volumes + [
173
+ '/etc/ssl/openvpn:/etc/ssl/openvpn',
174
+ '/var/openvpn-authz:/var/openvpn-authz'
175
+ ],
176
+ environment_variables: [
177
+ "AWS_REGION=#{aws.region}"
178
+ ],
179
+ ports: ['443'],
180
+ arguments: optional_arguments + [
181
+ "--http-address https://0.0.0.0:443",
182
+ "--fqdn #{fqdn}",
183
+ '--cache /var/openvpn-authz',
184
+ "--oidc-client-id \"#{client_id}\"",
185
+ "--oidc-issuer-url \"#{issuer_url}\"",
186
+ '/etc/ssl/openvpn'
187
+ ]
188
+ )
189
+ end
190
+
191
+ def openvpn_conf
192
+ {
193
+ path: '/etc/openvpn/openvpn.conf',
194
+ mode: '0644',
195
+ contents: <<~EOF
196
+ server #{cidr_to_split_address(@cidr)}
197
+ verb 3
198
+
199
+ iproute /etc/openvpn/ovpn_ip.sh
200
+
201
+ key /etc/ssl/openvpn/server/key
202
+ ca /etc/ssl/openvpn/ca/cert
203
+ cert /etc/ssl/openvpn/server/cert
204
+ dh /etc/ssl/openvpn/dh.pem
205
+ tls-auth /etc/ssl/openvpn/ta.key
206
+
207
+ cipher AES-256-CBC
208
+ auth SHA512
209
+ tls-version-min 1.2
210
+
211
+ key-direction 0
212
+ keepalive 10 60
213
+ persist-key
214
+ persist-tun
215
+
216
+ proto udp
217
+ # Rely on Docker to do port mapping, internally always 1194
218
+ port 1194
219
+ dev tun0
220
+ status /tmp/openvpn-status.log
221
+
222
+ user nobody
223
+ group nogroup
224
+ EOF
225
+ }
226
+ end
227
+
228
+ def openvpn_env
229
+ {
230
+ path: '/etc/openvpn/ovpn_env.sh',
231
+ mode: '0644',
232
+ contents: <<~EOF
233
+ declare -x OVPN_SERVER=#{@cidr}
234
+ EOF
235
+ }
236
+ end
237
+
238
+ # OpenVPN doesn't wait long enough for the tun0 device to init
239
+ # https://github.com/kylemanna/docker-openvpn/issues/370
240
+ def openvpn_ip_delay
241
+ {
242
+ path: '/etc/openvpn/ovpn_ip.sh',
243
+ mode: '0755',
244
+ contents: <<~IP_SCRIPT
245
+ #!/usr/bin/env bash
246
+ sleep 0.1
247
+ /sbin/ip $*
248
+ IP_SCRIPT
249
+ }
250
+ end
251
+
252
+ def with_endpoint_service(*args)
253
+ @service.with_endpoint_service(*args)
254
+ end
255
+
256
+ def security_group
257
+ @service.security_group
258
+ end
259
+
260
+ def ingress_security_group
261
+ @service.ingress_security_group
262
+ end
263
+
264
+ def egress_security_group
265
+ @service.egress_security_group
266
+ end
267
+
268
+ def pingable_by(*services)
269
+ @service.pingable_by(*services)
270
+ end
271
+
272
+ def used_by(*services)
273
+ @service.used_by(*services)
274
+ end
275
+
276
+ def pingable_by_cidr(*cidrs)
277
+ @service.pingable_by_cidr(*cidrs)
278
+ end
279
+
280
+ def used_by_cidr(*cidrs)
281
+ @service.used_by_cidr(*cidrs)
282
+ end
283
+ end
284
+ end
285
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terrafying-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.3
4
+ version: 1.12.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - uSwitch Limited
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-24 00:00:00.000000000 Z
11
+ date: 2019-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,7 @@ files:
132
132
  - lib/terrafying/components/version.rb
133
133
  - lib/terrafying/components/vpc.rb
134
134
  - lib/terrafying/components/vpn.rb
135
+ - lib/terrafying/components/vpn_oidc.rb
135
136
  - lib/terrafying/components/zone.rb
136
137
  homepage: https://github.com/uswitch/terrafying-components
137
138
  licenses: