-
Notifications
You must be signed in to change notification settings - Fork 20
/
firewall.go
145 lines (115 loc) · 3.12 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cmd
import (
"context"
"fmt"
"net"
"os"
"time"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var (
defaultCIDR = egoscale.MustParseCIDR("0.0.0.0/0")
defaultCIDR6 = egoscale.MustParseCIDR("::/0")
)
var firewallCmd = &cobra.Command{
Use: "firewall",
Short: "Security Groups management",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
fmt.Fprintln(os.Stderr,
`**********************************************************************
The "exo firewall" commands are deprecated and will be removed in a future
version, please use "exo compute security-group" replacement commands.
**********************************************************************`)
time.Sleep(3 * time.Second)
},
Hidden: true,
}
func init() {
RootCmd.AddCommand(firewallCmd)
}
func formatRuleSource(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)
}
return source
}
func formatRulePort(rule egoscale.IngressRule) string {
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()
}
if rule.IcmpCode == -1 || rule.IcmpType == -1 {
desc = "Any"
}
ports = fmt.Sprintf("%d,%d (%s)", rule.IcmpType, rule.IcmpCode, desc)
} else if rule.StartPort == rule.EndPort {
ports = fmt.Sprint(rule.StartPort)
} else {
ports = fmt.Sprintf("%d-%d", rule.StartPort, rule.EndPort)
}
return ports
}
func getSecurityGroupByNameOrID(v string) (*egoscale.SecurityGroup, error) {
sg := &egoscale.SecurityGroup{}
id, err := egoscale.ParseUUID(v)
if err != nil {
sg.Name = v
} else {
sg.ID = id
}
resp, err := globalstate.EgoscaleClient.GetWithContext(gContext, sg)
switch err {
case nil:
return resp.(*egoscale.SecurityGroup), nil
case egoscale.ErrNotFound:
return nil, fmt.Errorf("unknown Security Group %q", v)
case egoscale.ErrTooManyFound:
return nil, fmt.Errorf("multiple Security Groups match %q", v)
default:
return nil, err
}
}
func getSecurityGroupIDs(params []string) ([]egoscale.UUID, error) {
ids := make([]egoscale.UUID, len(params))
for i, sg := range params {
s, err := getSecurityGroupByNameOrID(sg)
if err != nil {
return nil, err
}
ids[i] = *s.ID
}
return ids, 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))
}