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",
}
Instance Method Summary
collapse
Instance Method Details
#add_names(ports) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/terrafying/components/ports.rb', line 52
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
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/terrafying/components/ports.rb', line 29
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
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/terrafying/components/ports.rb', line 15
def add_upstream_downstream(ports)
ports.map do |port|
if port.is_a?(Numeric)
port = { upstream_port: port, downstream_port: port }
end
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 { |i|
(maskaddr >> (24 - 8 * i)) & 0xff
}.join('.')
return "#{cidr.first} #{maskip}"
end
|
#enrich_ports(ports) ⇒ Object
9
10
11
12
13
|
# File 'lib/terrafying/components/ports.rb', line 9
def enrich_ports(ports)
ports = add_upstream_downstream(ports)
ports = add_redirects(ports)
add_names(ports)
end
|
#from_port(port) ⇒ Object
61
62
63
64
|
# File 'lib/terrafying/components/ports.rb', line 61
def from_port(port)
return port unless port_range?(port)
port.split('-').first.to_i
end
|
#is_l4_port(port) ⇒ Object
75
76
77
|
# File 'lib/terrafying/components/ports.rb', line 75
def is_l4_port(port)
port[:type] == "tcp" || port[:type] == "udp"
end
|
#is_l7_port(port) ⇒ Object
79
80
81
|
# File 'lib/terrafying/components/ports.rb', line 79
def is_l7_port(port)
port[:type] == "http" || port[:type] == "https"
end
|
#port_range?(port) ⇒ Boolean
71
72
73
|
# File 'lib/terrafying/components/ports.rb', line 71
def port_range?(port)
port.is_a?(String) && port.match(/[0-9]+-[0-9]+/)
end
|
#redirect_http(from_port, to_port) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/terrafying/components/ports.rb', line 40
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
|
# File 'lib/terrafying/components/ports.rb', line 66
def to_port(port)
return port unless port_range?(port)
port.split('-').last.to_i
end
|