Class: Terrafying::Components::VPN
- Inherits:
-
Terrafying::Context
- Object
- Terrafying::Context
- Terrafying::Components::VPN
- Defined in:
- lib/terrafying/components/vpn.rb
Instance Attribute Summary collapse
-
#cidr ⇒ Object
readonly
Returns the value of attribute cidr.
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
- #allow_security_group_in(vpc, name: '') ⇒ Object
- #caddy_conf(ca, has_provider) ⇒ Object
- #caddy_service(ca) ⇒ Object
- #create_in(vpc, name, oauth2_provider, options = {}) ⇒ Object
- #egress_security_group ⇒ Object
- #ingress_security_group ⇒ Object
-
#initialize ⇒ VPN
constructor
A new instance of VPN.
- #oauth2_proxy_service(oauth2_provider) ⇒ Object
- #openvpn_authz_service(route_all_traffic, route_dns_entry) ⇒ Object
- #openvpn_conf ⇒ Object
- #openvpn_env ⇒ Object
-
#openvpn_ip_delay ⇒ Object
OpenVPN doesn’t wait long enough for the tun0 device to init github.com/kylemanna/docker-openvpn/issues/370.
- #openvpn_service ⇒ Object
- #pingable_by(*services) ⇒ Object
- #pingable_by_cidr(*cidrs) ⇒ Object
- #security_group ⇒ Object
- #used_by(*services) ⇒ Object
- #used_by_cidr(*cidrs) ⇒ Object
- #with_endpoint_service(*args) ⇒ Object
Constructor Details
#initialize ⇒ VPN
Returns a new instance of VPN.
33 34 35 |
# File 'lib/terrafying/components/vpn.rb', line 33 def initialize super end |
Instance Attribute Details
#cidr ⇒ Object (readonly)
Returns the value of attribute cidr.
27 28 29 |
# File 'lib/terrafying/components/vpn.rb', line 27 def cidr @cidr end |
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
27 28 29 |
# File 'lib/terrafying/components/vpn.rb', line 27 def ip_address @ip_address end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/terrafying/components/vpn.rb', line 27 def name @name end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
27 28 29 |
# File 'lib/terrafying/components/vpn.rb', line 27 def service @service end |
Class Method Details
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.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 @is_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 |
#caddy_conf(ca, has_provider) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/terrafying/components/vpn.rb', line 244 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: <<~CADDYFILE #{@fqdn}:443 tls #{tls} proxy / localhost:#{port} { transparent } CADDYFILE } end |
#caddy_service(ca) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/terrafying/components/vpn.rb', line 225 def caddy_service(ca) optional_volumes = [] optional_volumes << "/etc/ssl/#{ca.name}:/etc/ssl/#{ca.name}:ro" if ca 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
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 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 |
# File 'lib/terrafying/components/vpn.rb', line 37 def create_in(vpc, name, oauth2_provider, = {}) = { group: 'uSwitch Developers', 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: {} }.merge() @name = name @vpc = vpc @cidr = [:cidr] @fqdn = vpc.zone.qualify(name) unless 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 && !%i[type client_id client_secret].all? { |k| oauth2_provider.key?(k) } raise 'You need to set type, client_id and client_secret' end units = [ openvpn_service, openvpn_authz_service([:route_all_traffic], [:route_dns_entries]), caddy_service([:ca]) ] files = [ openvpn_conf, openvpn_env, openvpn_ip_delay, caddy_conf([: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 .key?(:ca) keypairs.push([:ca].create_keypair_in(self, @fqdn)) end if [:static] subnet = [:subnets].first instances = [{ subnet: subnet, ip_address: subnet.ip_addresses.first }] else instances = [{}] end @is_public = [:public] @service = add! Service.create_in( vpc, name, { public: @is_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]) ) @ip_address = @service.instance_set.instances[0].ip_address self end |
#egress_security_group ⇒ Object
333 334 335 |
# File 'lib/terrafying/components/vpn.rb', line 333 def egress_security_group @service.egress_security_group end |
#ingress_security_group ⇒ Object
329 330 331 |
# File 'lib/terrafying/components/vpn.rb', line 329 def ingress_security_group @service.ingress_security_group end |
#oauth2_proxy_service(oauth2_provider) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/terrafying/components/vpn.rb', line 207 def oauth2_proxy_service(oauth2_provider) Ignition.container_unit( 'authnz', 'quay.io/uswitch/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, route_dns_entry) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/terrafying/components/vpn.rb', line 179 def openvpn_authz_service(route_all_traffic, route_dns_entry) optional_arguments = [] optional_arguments << '--route-all' if route_all_traffic if route_dns_entry.count > 0 optional_arguments += route_dns_entry.map { |entry| "--route-dns-entries #{entry}" } end Ignition.container_unit( 'openvpn-authz', 'quay.io/uswitch/openvpn-authz:1.2', 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', '--user-header "X-Forwarded-Email"', '/etc/ssl/openvpn' ] ) end |
#openvpn_conf ⇒ Object
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 290 291 292 293 294 295 |
# File 'lib/terrafying/components/vpn.rb', line 260 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_env ⇒ Object
297 298 299 300 301 302 303 304 305 |
# File 'lib/terrafying/components/vpn.rb', line 297 def openvpn_env { path: '/etc/openvpn/ovpn_env.sh', mode: '0644', contents: <<~EOF declare -x OVPN_SERVER=#{@cidr} EOF } end |
#openvpn_ip_delay ⇒ Object
OpenVPN doesn’t wait long enough for the tun0 device to init github.com/kylemanna/docker-openvpn/issues/370
309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/terrafying/components/vpn.rb', line 309 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_service ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/terrafying/components/vpn.rb', line 166 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
337 338 339 |
# File 'lib/terrafying/components/vpn.rb', line 337 def pingable_by(*services) @service.pingable_by(*services) end |
#pingable_by_cidr(*cidrs) ⇒ Object
345 346 347 |
# File 'lib/terrafying/components/vpn.rb', line 345 def pingable_by_cidr(*cidrs) @service.pingable_by_cidr(*cidrs) end |
#security_group ⇒ Object
325 326 327 |
# File 'lib/terrafying/components/vpn.rb', line 325 def security_group @service.security_group end |
#used_by(*services) ⇒ Object
341 342 343 |
# File 'lib/terrafying/components/vpn.rb', line 341 def used_by(*services) @service.used_by(*services) end |
#used_by_cidr(*cidrs) ⇒ Object
349 350 351 |
# File 'lib/terrafying/components/vpn.rb', line 349 def used_by_cidr(*cidrs) @service.used_by_cidr(*cidrs) end |
#with_endpoint_service(*args) ⇒ Object
321 322 323 |
# File 'lib/terrafying/components/vpn.rb', line 321 def with_endpoint_service(*args) @service.with_endpoint_service(*args) end |