Top Level Namespace
Defined Under Namespace
Modules: Terrafying
Classes: Hash
Constant Summary
collapse
- IN4MASK =
0xffffffff
- PORT_NAMES =
{
22 => 'ssh',
80 => 'http',
443 => 'https',
1194 => 'openvpn'
}.freeze
Instance Method Summary
collapse
Instance Method Details
#add_names(ports) ⇒ Object
51
52
53
54
55
56
57
58
|
# File 'lib/terrafying/components/ports.rb', line 51
def add_names(ports)
ports.map do |port|
{
type: 'tcp',
name: PORT_NAMES.fetch(port[:upstream_port], port[:upstream_port].to_s)
}.merge(port)
end
end
|
#add_redirects(ports) ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/terrafying/components/ports.rb', line 28
def add_redirects(ports)
ports.flat_map do |port|
if port.key? :redirect_http_from_port
redirect_port = redirect_http(port[:redirect_http_from_port], port[:upstream_port])
port.delete(:redirect_http_from_port)
return [port, redirect_port]
end
port
end
end
|
#add_upstream_downstream(ports) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/terrafying/components/ports.rb', line 16
def add_upstream_downstream(ports)
ports.map do |port|
port = { upstream_port: port, downstream_port: port } if port.is_a?(Numeric)
if port.key?(:number)
port[:upstream_port] = port[:number]
port[:downstream_port] = port[:number]
end
port
end
end
|
#cidr_to_split_address(raw_cidr) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/terrafying/components/vpn.rb', line 11
def cidr_to_split_address(raw_cidr)
cidr = NetAddr::CIDR.create(raw_cidr)
masklen = 32 - cidr.bits
maskaddr = ((IN4MASK >> masklen) << masklen)
maskip = (0..3).map do |i|
(maskaddr >> (24 - 8 * i)) & 0xff
end.join('.')
"#{cidr.first} #{maskip}"
end
|
#enrich_ports(ports) ⇒ Object
#from_port(port) ⇒ Object
60
61
62
63
64
|
# File 'lib/terrafying/components/ports.rb', line 60
def from_port(port)
return port unless port_range?(port)
port.split('-').first.to_i
end
|
#is_l4_port(port) ⇒ Object
76
77
78
|
# File 'lib/terrafying/components/ports.rb', line 76
def is_l4_port(port)
port[:type] == 'tcp' || port[:type] == 'udp'
end
|
#is_l7_port(port) ⇒ Object
80
81
82
|
# File 'lib/terrafying/components/ports.rb', line 80
def is_l7_port(port)
port[:type] == 'http' || port[:type] == 'https'
end
|
#port_range?(port) ⇒ Boolean
72
73
74
|
# File 'lib/terrafying/components/ports.rb', line 72
def port_range?(port)
port.is_a?(String) && port.match(/[0-9]+-[0-9]+/)
end
|
#redirect_http(from_port, to_port) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/terrafying/components/ports.rb', line 39
def redirect_http(from_port, to_port)
{
upstream_port: from_port,
downstream_port: from_port,
type: 'http',
action: {
type: 'redirect',
redirect: { port: to_port, protocol: 'HTTPS', status_code: 'HTTP_301' }
}
}
end
|
#to_port(port) ⇒ Object
66
67
68
69
70
|
# File 'lib/terrafying/components/ports.rb', line 66
def to_port(port)
return port unless port_range?(port)
port.split('-').last.to_i
end
|