-
Notifications
You must be signed in to change notification settings - Fork 20
/
firewall_add.go
306 lines (250 loc) · 7.18 KB
/
firewall_add.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
303
304
305
306
package cmd
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type portRange struct {
start uint16
end uint16
}
func init() {
firewallAddCmd.Flags().BoolP("ipv6", "6", false, "Set ipv6 on default rules or on --my-ip")
firewallAddCmd.Flags().BoolP("my-ip", "", false, "Set CIDR for my ip")
firewallAddCmd.Flags().BoolP("egress", "e", false, "By default rule is INGRESS (set --egress to have EGRESS rule)")
firewallAddCmd.Flags().StringP("protocol", "p", "", "Rule Protocol available [tcp, udp, icmp, icmpv6, ah, esp, gre]")
firewallAddCmd.Flags().StringP("cidr", "c", "", "Rule CIDR [CIDR 0.0.0.0/0,::/0,...]")
firewallAddCmd.Flags().StringP("security-group", "s", "", "Rule Security Group [NAME|ID ex: sg1,sg2...]")
firewallAddCmd.Flags().StringP("port", "P", "", "Rule port range [80-80,443,22-22]")
firewallAddCmd.Flags().Int("icmp-type", -1, "Rule ICMP type")
firewallAddCmd.Flags().Int("icmp-code", -1, "Rule ICMP code")
firewallAddCmd.Flags().StringP("description", "d", "", "Rule description")
firewallCmd.AddCommand(firewallAddCmd)
}
var firewallAddCmd = &cobra.Command{
Use: "add SECURITY-GROUP-NAME|ID [ping|ssh|rdp]",
Short: "Add a rule to a Security Group",
Long: `
No arguments create a IPv4 wide open rule.
For ICMP rules, specify the icmp-code and icmp-type.
firewall add <Security Group> --protocol icmp --icmp-type 8 --icmp-code 0
For the other protocols specify the port ranges.
firewall add <Security Group> --protocol tcp --port 8000-8080
A set of predefined commands exists: ping, ssh, rdp.
firewall add <Security Group> ssh
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
securityGroup, err := getSecurityGroupByNameOrID(args[0])
if err != nil {
return err
}
desc, err := cmd.Flags().GetString("description")
if err != nil {
return err
}
protocol, err := cmd.Flags().GetString("protocol")
if err != nil {
return err
}
isEgress, err := cmd.Flags().GetBool("egress")
if err != nil {
return err
}
icmptype, err := cmd.Flags().GetInt("icmp-type")
if err != nil {
return err
}
icmpcode, err := cmd.Flags().GetInt("icmp-code")
if err != nil {
return err
}
port, err := cmd.Flags().GetString("port")
if err != nil {
return err
}
cidrList, err := cmd.Flags().GetString("cidr")
if err != nil {
return err
}
sg, err := cmd.Flags().GetString("security-group")
if err != nil {
return err
}
isIpv6, err := cmd.Flags().GetBool("ipv6")
if err != nil {
return err
}
isMyIP, err := cmd.Flags().GetBool("my-ip")
if err != nil {
return err
}
if port != "" && protocol == "" {
return errors.New(`"--port" can only be specified with "--protocol"`)
}
var ip *egoscale.CIDR
if isMyIP {
cidr, cirdErr := getMyCIDR(isIpv6)
if cirdErr != nil {
return cirdErr
}
ip = cidr
}
tasks := []task{}
for i := 1; true; i++ {
if i >= len(args) && len(args) != 1 {
break
}
rule := &egoscale.AuthorizeSecurityGroupIngress{}
if len(args) > 1 {
rule, err = getDefaultRule(args[i])
if err != nil {
return err
}
}
rule.Description = desc
rule.SecurityGroupID = securityGroup.ID
if protocol != "" {
rule.Protocol = strings.ToLower(protocol)
}
if ip != nil {
rule.CIDRList = append(rule.CIDRList, *ip)
}
if cidrList != "" {
cidrs := getCommaflag(cidrList)
for _, cidr := range cidrs {
c, errCidr := egoscale.ParseCIDR(cidr)
if errCidr != nil {
return errCidr
}
rule.CIDRList = append(rule.CIDRList, *c)
}
}
if sg != "" {
sgs := getCommaflag(sg)
userSecurityGroups, sgErr := getUserSecurityGroups(sgs)
if sgErr != nil {
return sgErr
}
rule.UserSecurityGroupList = userSecurityGroups
} else if len(rule.CIDRList) == 0 {
if (isIpv6 || rule.Protocol == "ICMPv6") && rule.Protocol != "ICMP" {
rule.CIDRList = append(rule.CIDRList, *defaultCIDR6)
} else {
rule.CIDRList = append(rule.CIDRList, *defaultCIDR)
}
}
if strings.HasPrefix(rule.Protocol, "icmp") {
rule.IcmpType = icmptype
rule.IcmpCode = icmpcode
}
// Not best practice but waiting to find better solution
if port != "" && (rule.Protocol == "tcp" || rule.Protocol == "udp") {
ports := getCommaflag(port)
portsRange, err := getPortsRange(ports)
if err != nil {
return err
}
for _, portRange := range portsRange {
rule.StartPort = portRange.start
rule.EndPort = portRange.end
msg := fmt.Sprintf("Add rule for %q with port %d", securityGroup.Name, rule.StartPort)
tasks = append(tasks, newFirewallRuleTask(*rule, msg, isEgress))
}
}
// Not best practice but waiting to find better solution
if port == "" || !(rule.Protocol == "tcp" || rule.Protocol == "udp") {
msg := fmt.Sprintf("Add rule for %q", securityGroup.Name)
if len(args) > 1 {
msg = fmt.Sprintf("Add %q rule for %q", args[i], securityGroup.Name)
}
tasks = append(tasks, newFirewallRuleTask(*rule, msg, isEgress))
}
if len(args) == 1 {
break
}
}
resps := asyncTasks(tasks)
errs := filterErrors(resps)
if len(errs) > 0 {
return errs[0]
}
return nil
},
}
func getPortsRange(ports []string) ([]portRange, error) {
portsRange := make([]portRange, len(ports))
for i, p := range ports {
pRange := strings.Split(p, "-")
if len(pRange) > 2 || len(pRange) == 0 {
return nil, fmt.Errorf("failed to find port ranges into: %q", p)
}
p1, err := strconv.ParseUint(pRange[0], 10, 16)
if err != nil {
return nil, err
}
portsRange[i].start = uint16(p1)
portsRange[i].end = uint16(p1)
if len(pRange) == 2 {
p2, err := strconv.ParseUint(pRange[1], 10, 16)
if err != nil {
return nil, err
}
portsRange[i].end = uint16(p2)
}
}
return portsRange, nil
}
func getUserSecurityGroups(names []string) ([]egoscale.UserSecurityGroup, error) {
us := make([]egoscale.UserSecurityGroup, 0, len(names))
for _, sg := range names {
s, err := getSecurityGroupByNameOrID(sg)
if err != nil {
return nil, err
}
us = append(us, s.UserSecurityGroup())
}
return us, nil
}
func getDefaultRule(ruleName string) (*egoscale.AuthorizeSecurityGroupIngress, error) {
ruleName = strings.ToLower(ruleName)
if ruleName == "ping" {
return &egoscale.AuthorizeSecurityGroupIngress{
Protocol: "ICMP",
CIDRList: []egoscale.CIDR{},
IcmpType: int(echo),
IcmpCode: 0,
}, nil
}
if ruleName == "ping6" {
return &egoscale.AuthorizeSecurityGroupIngress{
Protocol: "ICMPv6",
CIDRList: []egoscale.CIDR{},
IcmpType: int(echoRequest),
IcmpCode: 0,
}, nil
}
for d := Daytime; d <= Minecraft; d++ {
if strings.ToLower(port.String(d)) == ruleName {
return &egoscale.AuthorizeSecurityGroupIngress{
Protocol: "TCP",
CIDRList: []egoscale.CIDR{},
StartPort: uint16(d),
EndPort: uint16(d),
}, nil
}
}
return nil, fmt.Errorf("default rule %q not found", ruleName)
}
func newFirewallRuleTask(rule egoscale.AuthorizeSecurityGroupIngress, msg string, isEgress bool) task {
if isEgress {
req := (egoscale.AuthorizeSecurityGroupEgress)(rule)
return task{req, msg}
}
return task{rule, msg}
}