-
Notifications
You must be signed in to change notification settings - Fork 14
/
security_group.go
396 lines (352 loc) · 9.84 KB
/
security_group.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package v2
import (
"context"
"errors"
"net"
"strings"
apiv2 "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)
// SecurityGroupRule represents a Security Group rule.
type SecurityGroupRule struct {
Description *string
EndPort *uint16
FlowDirection *string `req-for:"create"`
ICMPCode *int64
ICMPType *int64
ID *string `req-for:"delete"`
Network *net.IPNet
Protocol *string `req-for:"create"`
SecurityGroupID *string
StartPort *uint16
}
func securityGroupRuleFromAPI(r *oapi.SecurityGroupRule) *SecurityGroupRule {
return &SecurityGroupRule{
Description: r.Description,
EndPort: func() (v *uint16) {
if r.EndPort != nil {
port := uint16(*r.EndPort)
v = &port
}
return
}(),
FlowDirection: (*string)(r.FlowDirection),
ICMPCode: func() (v *int64) {
if r.Icmp != nil {
v = r.Icmp.Code
}
return
}(),
ICMPType: func() (v *int64) {
if r.Icmp != nil {
v = r.Icmp.Type
}
return
}(),
ID: r.Id,
Network: func() (v *net.IPNet) {
if r.Network != nil {
_, v, _ = net.ParseCIDR(*r.Network)
}
return
}(),
Protocol: (*string)(r.Protocol),
SecurityGroupID: func() (v *string) {
if r.SecurityGroup != nil {
v = &r.SecurityGroup.Id
}
return
}(),
StartPort: func() (v *uint16) {
if r.StartPort != nil {
port := uint16(*r.StartPort)
v = &port
}
return
}(),
}
}
// SecurityGroup represents a Security Group.
type SecurityGroup struct {
Description *string
ID *string `req-for:"update,delete"`
Name *string `req-for:"create"`
ExternalSources *[]string
Rules []*SecurityGroupRule
}
func securityGroupFromAPI(s *oapi.SecurityGroup) *SecurityGroup {
return &SecurityGroup{
Description: s.Description,
ID: s.Id,
Name: s.Name,
ExternalSources: s.ExternalSources,
Rules: func() (rules []*SecurityGroupRule) {
if s.Rules != nil {
rules = make([]*SecurityGroupRule, 0)
for _, rule := range *s.Rules {
rule := rule
rules = append(rules, securityGroupRuleFromAPI(&rule))
}
}
return rules
}(),
}
}
// CreateSecurityGroup creates a Security Group.
func (c *Client) CreateSecurityGroup(
ctx context.Context,
zone string,
securityGroup *SecurityGroup,
) (*SecurityGroup, error) {
if err := validateOperationParams(securityGroup, "create"); err != nil {
return nil, err
}
resp, err := c.CreateSecurityGroupWithResponse(ctx, oapi.CreateSecurityGroupJSONRequestBody{
Description: securityGroup.Description,
Name: *securityGroup.Name,
})
if err != nil {
return nil, err
}
res, err := oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return nil, err
}
return c.GetSecurityGroup(ctx, zone, *res.(*oapi.Reference).Id)
}
// CreateSecurityGroupRule creates a Security Group rule.
func (c *Client) CreateSecurityGroupRule(
ctx context.Context,
zone string,
securityGroup *SecurityGroup,
rule *SecurityGroupRule,
) (*SecurityGroupRule, error) {
if err := validateOperationParams(securityGroup, "update"); err != nil {
return nil, err
}
if err := validateOperationParams(rule, "create"); err != nil {
return nil, err
}
var icmp *struct {
Code *int64 `json:"code,omitempty"`
Type *int64 `json:"type,omitempty"`
}
if strings.HasPrefix(*rule.Protocol, "icmp") {
icmp = &struct {
Code *int64 `json:"code,omitempty"`
Type *int64 `json:"type,omitempty"`
}{
Code: rule.ICMPCode,
Type: rule.ICMPType,
}
}
// The API doesn't return the Security Group rule created directly, so in order to
// return a *SecurityGroupRule corresponding to the new rule we have to manually
// compare the list of rules in the SG before and after the rule creation, and
// identify the rule that wasn't there before.
// Note: in case of multiple rules creation in parallel this technique is subject
// to race condition as we could return an unrelated rule. To prevent this, we
// also compare the protocol/start port/end port parameters of the new rule to the
// ones specified in the input rule parameter.
rules := make(map[string]struct{})
for _, r := range securityGroup.Rules {
rules[*r.ID] = struct{}{}
}
resp, err := c.AddRuleToSecurityGroupWithResponse(
apiv2.WithZone(ctx, zone),
*securityGroup.ID,
oapi.AddRuleToSecurityGroupJSONRequestBody{
Description: rule.Description,
EndPort: func() (v *int64) {
if rule.EndPort != nil {
port := int64(*rule.EndPort)
v = &port
}
return
}(),
FlowDirection: oapi.AddRuleToSecurityGroupJSONBodyFlowDirection(*rule.FlowDirection),
Icmp: icmp,
Network: func() (v *string) {
if rule.Network != nil {
ip := rule.Network.String()
v = &ip
}
return
}(),
Protocol: oapi.AddRuleToSecurityGroupJSONBodyProtocol(*rule.Protocol),
SecurityGroup: func() (v *oapi.SecurityGroupResource) {
if rule.SecurityGroupID != nil {
v = &oapi.SecurityGroupResource{Id: *rule.SecurityGroupID}
}
return
}(),
StartPort: func() (v *int64) {
if rule.StartPort != nil {
port := int64(*rule.StartPort)
v = &port
}
return
}(),
})
if err != nil {
return nil, err
}
res, err := oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return nil, err
}
sgUpdated, err := c.GetSecurityGroup(ctx, zone, *res.(*oapi.Reference).Id)
if err != nil {
return nil, err
}
// Look for an unknown rule: if we find one we hope it's the one we've just created.
for _, r := range sgUpdated.Rules {
if _, ok := rules[*r.ID]; !ok && (*r.FlowDirection == *rule.FlowDirection && *r.Protocol == *rule.Protocol) {
return r, nil
}
}
return nil, errors.New("unable to identify the rule created")
}
// DeleteSecurityGroup deletes a Security Group.
func (c *Client) DeleteSecurityGroup(ctx context.Context, zone string, securityGroup *SecurityGroup) error {
if err := validateOperationParams(securityGroup, "delete"); err != nil {
return err
}
resp, err := c.DeleteSecurityGroupWithResponse(apiv2.WithZone(ctx, zone), *securityGroup.ID)
if err != nil {
return err
}
_, err = oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return err
}
return nil
}
// DeleteSecurityGroupRule deletes a Security Group rule.
func (c *Client) DeleteSecurityGroupRule(
ctx context.Context,
zone string,
securityGroup *SecurityGroup,
rule *SecurityGroupRule,
) error {
if err := validateOperationParams(securityGroup, "update"); err != nil {
return err
}
if err := validateOperationParams(rule, "delete"); err != nil {
return err
}
resp, err := c.DeleteRuleFromSecurityGroupWithResponse(apiv2.WithZone(ctx, zone), *securityGroup.ID, *rule.ID)
if err != nil {
return err
}
_, err = oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return err
}
return nil
}
// FindSecurityGroup attempts to find a Security Group by name or ID.
func (c *Client) FindSecurityGroup(ctx context.Context, zone, x string) (*SecurityGroup, error) {
res, err := c.ListSecurityGroups(ctx, zone)
if err != nil {
return nil, err
}
for _, r := range res {
if *r.ID == x || *r.Name == x {
return c.GetSecurityGroup(ctx, zone, *r.ID)
}
}
return nil, apiv2.ErrNotFound
}
// GetSecurityGroup returns the Security Group corresponding to the specified ID.
func (c *Client) GetSecurityGroup(ctx context.Context, zone, id string) (*SecurityGroup, error) {
resp, err := c.GetSecurityGroupWithResponse(apiv2.WithZone(ctx, zone), id)
if err != nil {
return nil, err
}
return securityGroupFromAPI(resp.JSON200), nil
}
// ListSecurityGroups returns the list of existing Security Groups.
func (c *Client) ListSecurityGroups(ctx context.Context, zone string) ([]*SecurityGroup, error) {
list := make([]*SecurityGroup, 0)
resp, err := c.ListSecurityGroupsWithResponse(apiv2.WithZone(ctx, zone))
if err != nil {
return nil, err
}
if resp.JSON200.SecurityGroups != nil {
for i := range *resp.JSON200.SecurityGroups {
list = append(list, securityGroupFromAPI(&(*resp.JSON200.SecurityGroups)[i]))
}
}
return list, nil
}
// AddExternalSourceToSecurityGroup adds a new external source to a
// Security Group. This operation is idempotent.
func (c *Client) AddExternalSourceToSecurityGroup(
ctx context.Context,
zone string,
securityGroup *SecurityGroup,
cidr string,
) error {
if err := validateOperationParams(securityGroup, "update"); err != nil {
return err
}
resp, err := c.AddExternalSourceToSecurityGroupWithResponse(
apiv2.WithZone(ctx, zone),
*securityGroup.ID,
oapi.AddExternalSourceToSecurityGroupJSONRequestBody{
Cidr: cidr,
})
if err != nil {
return err
}
_, err = oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return err
}
return nil
}
// RemoveExternalSourceFromSecurityGroup removes an external source from
// a Security Group. This operation is idempotent.
func (c *Client) RemoveExternalSourceFromSecurityGroup(
ctx context.Context,
zone string,
securityGroup *SecurityGroup,
cidr string,
) error {
if err := validateOperationParams(securityGroup, "update"); err != nil {
return err
}
resp, err := c.RemoveExternalSourceFromSecurityGroupWithResponse(
apiv2.WithZone(ctx, zone),
*securityGroup.ID,
oapi.RemoveExternalSourceFromSecurityGroupJSONRequestBody{
Cidr: cidr,
})
if err != nil {
return err
}
_, err = oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return err
}
return nil
}