-
Notifications
You must be signed in to change notification settings - Fork 20
/
firewall.go
105 lines (84 loc) · 2.3 KB
/
firewall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
package cmd
import (
"context"
"fmt"
"net"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var defaultCIDR = egoscale.MustParseCIDR("0.0.0.0/0")
var defaultCIDR6 = egoscale.MustParseCIDR("::/0")
var firewallCmd = &cobra.Command{
Use: "firewall",
Short: "Security groups management",
}
func init() {
RootCmd.AddCommand(firewallCmd)
}
// Utils func for the firewall family
func formatRules(name string, rule egoscale.IngressRule) []string {
var source string
if rule.CIDR != nil {
source = fmt.Sprintf("CIDR %s", rule.CIDR)
} else {
source = fmt.Sprintf("SG %s", rule.SecurityGroupName)
}
var ports string
if rule.Protocol == "icmp" || rule.Protocol == "icmpv6" {
c := icmpCode((uint16(rule.IcmpType) << 8) | uint16(rule.IcmpCode))
t := c.icmpType()
desc := c.StringFormatted()
if desc == "" {
desc = t.StringFormatted()
}
ports = fmt.Sprintf("%d, %d (%s)", rule.IcmpType, rule.IcmpCode, desc)
} else if rule.StartPort == rule.EndPort {
p := port(rule.StartPort)
if p.StringFormatted() != "" {
ports = fmt.Sprintf("%d (%s)", rule.StartPort, p.String())
} else {
ports = fmt.Sprintf("%d", rule.StartPort)
}
} else {
ports = fmt.Sprintf("%d-%d", rule.StartPort, rule.EndPort)
}
return []string{name, source, rule.Protocol, ports, rule.Description, rule.RuleID.String()}
}
func getSecurityGroupByNameOrID(name string) (*egoscale.SecurityGroup, error) {
sg := &egoscale.SecurityGroup{}
id, err := egoscale.ParseUUID(name)
if err != nil {
sg.Name = name
} else {
sg.ID = id
}
resp, err := cs.GetWithContext(gContext, sg)
if err != nil {
return nil, err
}
return resp.(*egoscale.SecurityGroup), nil
}
func getMyCIDR(isIpv6 bool) (*egoscale.CIDR, error) {
cidrMask := 32
dnsServer := "resolver1.opendns.com"
protocol := "udp4"
if isIpv6 {
dnsServer = "resolver2.ipv6-sandbox.opendns.com"
cidrMask = 128
protocol = "udp6"
}
resolver := net.Resolver{
Dial: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial(protocol, dnsServer+":53")
},
PreferGo: true,
}
ips, err := resolver.LookupIPAddr(gContext, "myip.opendns.com")
if err != nil {
return nil, err
}
if len(ips) < 1 {
return nil, fmt.Errorf("no IP addresses were found using OpenDNS")
}
return egoscale.ParseCIDR(fmt.Sprintf("%s/%d", ips[0].IP, cidrMask))
}