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.
37 38 39 |
# File 'lib/terrafying/components/vpn.rb', line 37 def initialize() super end |
Instance Attribute Details
#cidr ⇒ Object (readonly)
Returns the value of attribute cidr.
31 32 33 |
# File 'lib/terrafying/components/vpn.rb', line 31 def cidr @cidr end |
#ip_address ⇒ Object (readonly)
Returns the value of attribute ip_address.
31 32 33 |
# File 'lib/terrafying/components/vpn.rb', line 31 def ip_address @ip_address end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
31 32 33 |
# File 'lib/terrafying/components/vpn.rb', line 31 def name @name end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
31 32 33 |
# File 'lib/terrafying/components/vpn.rb', line 31 def service @service end |
Class Method Details
Instance Method Details
#allow_security_group_in(vpc, name: "") ⇒ Object
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 |
# File 'lib/terrafying/components/vpn.rb', line 133 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 @is_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 |
#caddy_conf(ca, has_provider) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/terrafying/components/vpn.rb', line 251 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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/terrafying/components/vpn.rb', line 228 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 |
# File 'lib/terrafying/components/vpn.rb', line 41 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) 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([: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 .has_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
340 341 342 |
# File 'lib/terrafying/components/vpn.rb', line 340 def egress_security_group @service.egress_security_group end |
#ingress_security_group ⇒ Object
336 337 338 |
# File 'lib/terrafying/components/vpn.rb', line 336 def ingress_security_group @service.ingress_security_group end |
#oauth2_proxy_service(oauth2_provider) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/terrafying/components/vpn.rb', line 208 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
176 177 178 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 206 |
# File 'lib/terrafying/components/vpn.rb', line 176 def openvpn_authz_service(route_all_traffic, route_dns_entry) optional_arguments = [] if route_all_traffic optional_arguments << "--route-all" end if route_dns_entry.count > 0 optional_arguments = 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
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 297 298 299 300 301 302 |
# File 'lib/terrafying/components/vpn.rb', line 267 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
304 305 306 307 308 309 310 311 312 |
# File 'lib/terrafying/components/vpn.rb', line 304 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
316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/terrafying/components/vpn.rb', line 316 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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/terrafying/components/vpn.rb', line 161 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
344 345 346 |
# File 'lib/terrafying/components/vpn.rb', line 344 def pingable_by(*services) @service.pingable_by(*services) end |
#pingable_by_cidr(*cidrs) ⇒ Object
352 353 354 |
# File 'lib/terrafying/components/vpn.rb', line 352 def pingable_by_cidr(*cidrs) @service.pingable_by_cidr(*cidrs) end |
#security_group ⇒ Object
332 333 334 |
# File 'lib/terrafying/components/vpn.rb', line 332 def security_group @service.security_group end |
#used_by(*services) ⇒ Object
348 349 350 |
# File 'lib/terrafying/components/vpn.rb', line 348 def used_by(*services) @service.used_by(*services) end |
#used_by_cidr(*cidrs) ⇒ Object
356 357 358 |
# File 'lib/terrafying/components/vpn.rb', line 356 def used_by_cidr(*cidrs) @service.used_by_cidr(*cidrs) end |
#with_endpoint_service(*args) ⇒ Object
328 329 330 |
# File 'lib/terrafying/components/vpn.rb', line 328 def with_endpoint_service(*args) @service.with_endpoint_service(*args) end |