forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecgroups.go
565 lines (503 loc) · 17.8 KB
/
secgroups.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package cfclient
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
)
type SecGroupResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []SecGroupResource `json:"resources"`
}
type SecGroupCreateResponse struct {
Code int `json:"code"`
ErrorCode string `json:"error_code"`
Description string `json:"description"`
}
type SecGroupResource struct {
Meta Meta `json:"metadata"`
Entity SecGroup `json:"entity"`
}
type SecGroup struct {
Guid string `json:"guid"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Rules []SecGroupRule `json:"rules"`
Running bool `json:"running_default"`
Staging bool `json:"staging_default"`
SpacesURL string `json:"spaces_url"`
StagingSpacesURL string `json:"staging_spaces_url"`
SpacesData []SpaceResource `json:"spaces"`
StagingSpacesData []SpaceResource `json:"staging_spaces"`
c *Client
}
type SecGroupRule struct {
Protocol string `json:"protocol"`
Ports string `json:"ports,omitempty"` //e.g. "4000-5000,9142"
Destination string `json:"destination"` //CIDR Format
Description string `json:"description,omitempty"` //Optional description
Code int `json:"code"` // ICMP code
Type int `json:"type"` //ICMP type. Only valid if Protocol=="icmp"
Log bool `json:"log,omitempty"` //If true, log this rule
}
var MinStagingSpacesVersion *semver.Version = getMinStagingSpacesVersion()
func (c *Client) ListSecGroups() (secGroups []SecGroup, err error) {
requestURL := "/v2/security_groups?inline-relations-depth=1"
for requestURL != "" {
var secGroupResp SecGroupResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting sec groups")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading sec group response body")
}
err = json.Unmarshal(resBody, &secGroupResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling sec group")
}
for _, secGroup := range secGroupResp.Resources {
secGroup.Entity.Guid = secGroup.Meta.Guid
secGroup.Entity.CreatedAt = secGroup.Meta.CreatedAt
secGroup.Entity.UpdatedAt = secGroup.Meta.UpdatedAt
secGroup.Entity.c = c
for i, space := range secGroup.Entity.SpacesData {
space.Entity.Guid = space.Meta.Guid
secGroup.Entity.SpacesData[i] = space
}
if len(secGroup.Entity.SpacesData) == 0 {
spaces, err := secGroup.Entity.ListSpaceResources()
if err != nil {
return nil, err
}
for _, space := range spaces {
secGroup.Entity.SpacesData = append(secGroup.Entity.SpacesData, space)
}
}
if len(secGroup.Entity.StagingSpacesData) == 0 {
spaces, err := secGroup.Entity.ListStagingSpaceResources()
if err != nil {
return nil, err
}
for _, space := range spaces {
secGroup.Entity.StagingSpacesData = append(secGroup.Entity.SpacesData, space)
}
}
secGroups = append(secGroups, secGroup.Entity)
}
requestURL = secGroupResp.NextUrl
resp.Body.Close()
}
return secGroups, nil
}
func (c *Client) ListRunningSecGroups() ([]SecGroup, error) {
secGroups := make([]SecGroup, 0)
requestURL := "/v2/config/running_security_groups"
for requestURL != "" {
var secGroupResp SecGroupResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting sec groups")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading sec group response body")
}
err = json.Unmarshal(resBody, &secGroupResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling sec group")
}
for _, secGroup := range secGroupResp.Resources {
secGroup.Entity.Guid = secGroup.Meta.Guid
secGroup.Entity.CreatedAt = secGroup.Meta.CreatedAt
secGroup.Entity.UpdatedAt = secGroup.Meta.UpdatedAt
secGroup.Entity.c = c
secGroups = append(secGroups, secGroup.Entity)
}
requestURL = secGroupResp.NextUrl
resp.Body.Close()
}
return secGroups, nil
}
func (c *Client) ListStagingSecGroups() ([]SecGroup, error) {
secGroups := make([]SecGroup, 0)
requestURL := "/v2/config/staging_security_groups"
for requestURL != "" {
var secGroupResp SecGroupResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting sec groups")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading sec group response body")
}
err = json.Unmarshal(resBody, &secGroupResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling sec group")
}
for _, secGroup := range secGroupResp.Resources {
secGroup.Entity.Guid = secGroup.Meta.Guid
secGroup.Entity.CreatedAt = secGroup.Meta.CreatedAt
secGroup.Entity.UpdatedAt = secGroup.Meta.UpdatedAt
secGroup.Entity.c = c
secGroups = append(secGroups, secGroup.Entity)
}
requestURL = secGroupResp.NextUrl
resp.Body.Close()
}
return secGroups, nil
}
func (c *Client) GetSecGroupByName(name string) (secGroup SecGroup, err error) {
requestURL := "/v2/security_groups?q=name:" + name
var secGroupResp SecGroupResponse
r := c.NewRequest("GET", requestURL)
resp, err := c.DoRequest(r)
if err != nil {
return secGroup, errors.Wrap(err, "Error requesting sec groups")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return secGroup, errors.Wrap(err, "Error reading sec group response body")
}
err = json.Unmarshal(resBody, &secGroupResp)
if err != nil {
return secGroup, errors.Wrap(err, "Error unmarshaling sec group")
}
if len(secGroupResp.Resources) == 0 {
return secGroup, fmt.Errorf("No security group with name %v found", name)
}
secGroup = secGroupResp.Resources[0].Entity
secGroup.Guid = secGroupResp.Resources[0].Meta.Guid
secGroup.CreatedAt = secGroupResp.Resources[0].Meta.CreatedAt
secGroup.UpdatedAt = secGroupResp.Resources[0].Meta.UpdatedAt
secGroup.c = c
resp.Body.Close()
return secGroup, nil
}
func (secGroup *SecGroup) ListSpaceResources() ([]SpaceResource, error) {
var spaceResources []SpaceResource
requestURL := secGroup.SpacesURL
for requestURL != "" {
spaceResp, err := secGroup.c.getSpaceResponse(requestURL)
if err != nil {
return []SpaceResource{}, err
}
for i, spaceRes := range spaceResp.Resources {
spaceRes.Entity.Guid = spaceRes.Meta.Guid
spaceRes.Entity.CreatedAt = spaceRes.Meta.CreatedAt
spaceRes.Entity.UpdatedAt = spaceRes.Meta.UpdatedAt
spaceResp.Resources[i] = spaceRes
}
spaceResources = append(spaceResources, spaceResp.Resources...)
requestURL = spaceResp.NextUrl
}
return spaceResources, nil
}
func (secGroup *SecGroup) ListStagingSpaceResources() ([]SpaceResource, error) {
var spaceResources []SpaceResource
requestURL := secGroup.StagingSpacesURL
for requestURL != "" {
spaceResp, err := secGroup.c.getSpaceResponse(requestURL)
if err != nil {
// if this is a 404, let's make sure that it's not because we're on a legacy system
if cause := errors.Cause(err); cause != nil {
if httpErr, ok := cause.(CloudFoundryHTTPError); ok {
if httpErr.StatusCode == 404 {
info, infoErr := secGroup.c.GetInfo()
if infoErr != nil {
return nil, infoErr
}
apiVersion, versionErr := semver.NewVersion(info.APIVersion)
if versionErr != nil {
return nil, versionErr
}
if MinStagingSpacesVersion.GreaterThan(apiVersion) {
// this is probably not really an error, we're just trying to use a non-existent api
return nil, nil
}
}
}
}
return []SpaceResource{}, err
}
for i, spaceRes := range spaceResp.Resources {
spaceRes.Entity.Guid = spaceRes.Meta.Guid
spaceRes.Entity.CreatedAt = spaceRes.Meta.CreatedAt
spaceRes.Entity.UpdatedAt = spaceRes.Meta.UpdatedAt
spaceResp.Resources[i] = spaceRes
}
spaceResources = append(spaceResources, spaceResp.Resources...)
requestURL = spaceResp.NextUrl
}
return spaceResources, nil
}
/*
CreateSecGroup contacts the CF endpoint for creating a new security group.
name: the name to give to the created security group
rules: A slice of rule objects that describe the rules that this security group enforces.
This can technically be nil or an empty slice - we won't judge you
spaceGuids: The security group will be associated with the spaces specified by the contents of this slice.
If nil, the security group will not be associated with any spaces initially.
*/
func (c *Client) CreateSecGroup(name string, rules []SecGroupRule, spaceGuids []string) (*SecGroup, error) {
return c.secGroupCreateHelper("/v2/security_groups", "POST", name, rules, spaceGuids)
}
/*
UpdateSecGroup contacts the CF endpoint to update an existing security group.
guid: identifies the security group that you would like to update.
name: the new name to give to the security group
rules: A slice of rule objects that describe the rules that this security group enforces.
If this is left nil, the rules will not be changed.
spaceGuids: The security group will be associated with the spaces specified by the contents of this slice.
If nil, the space associations will not be changed.
*/
func (c *Client) UpdateSecGroup(guid, name string, rules []SecGroupRule, spaceGuids []string) (*SecGroup, error) {
return c.secGroupCreateHelper("/v2/security_groups/"+guid, "PUT", name, rules, spaceGuids)
}
/*
DeleteSecGroup contacts the CF endpoint to delete an existing security group.
guid: Indentifies the security group to be deleted.
*/
func (c *Client) DeleteSecGroup(guid string) error {
//Perform the DELETE and check for errors
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/security_groups/%s", guid)))
if err != nil {
return err
}
if resp.StatusCode != 204 { //204 No Content
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
GetSecGroup contacts the CF endpoint for fetching the info for a particular security group.
guid: Identifies the security group to fetch information from
*/
func (c *Client) GetSecGroup(guid string) (*SecGroup, error) {
//Perform the GET and check for errors
resp, err := c.DoRequest(c.NewRequest("GET", "/v2/security_groups/"+guid))
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
//get the json out of the response body
return respBodyToSecGroup(resp.Body, c)
}
/*
BindSecGroup contacts the CF endpoint to associate a space with a security group
secGUID: identifies the security group to add a space to
spaceGUID: identifies the space to associate
*/
func (c *Client) BindSecGroup(secGUID, spaceGUID string) error {
//Perform the PUT and check for errors
resp, err := c.DoRequest(c.NewRequest("PUT", fmt.Sprintf("/v2/security_groups/%s/spaces/%s", secGUID, spaceGUID)))
if err != nil {
return err
}
if resp.StatusCode != 201 { //201 Created
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
BindSpaceStagingSecGroup contacts the CF endpoint to associate a space with a security group for staging functions only
secGUID: identifies the security group to add a space to
spaceGUID: identifies the space to associate
*/
func (c *Client) BindStagingSecGroupToSpace(secGUID, spaceGUID string) error {
//Perform the PUT and check for errors
resp, err := c.DoRequest(c.NewRequest("PUT", fmt.Sprintf("/v2/security_groups/%s/staging_spaces/%s", secGUID, spaceGUID)))
if err != nil {
return err
}
if resp.StatusCode != 201 { //201 Created
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
BindRunningSecGroup contacts the CF endpoint to associate a security group
secGUID: identifies the security group to add a space to
*/
func (c *Client) BindRunningSecGroup(secGUID string) error {
//Perform the PUT and check for errors
resp, err := c.DoRequest(c.NewRequest("PUT", fmt.Sprintf("/v2/config/running_security_groups/%s", secGUID)))
if err != nil {
return err
}
if resp.StatusCode != 200 { //200
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
UnbindRunningSecGroup contacts the CF endpoint to dis-associate a security group
secGUID: identifies the security group to add a space to
*/
func (c *Client) UnbindRunningSecGroup(secGUID string) error {
//Perform the DELETE and check for errors
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/config/running_security_groups/%s", secGUID)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent { //204
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
BindStagingSecGroup contacts the CF endpoint to associate a space with a security group
secGUID: identifies the security group to add a space to
*/
func (c *Client) BindStagingSecGroup(secGUID string) error {
//Perform the PUT and check for errors
resp, err := c.DoRequest(c.NewRequest("PUT", fmt.Sprintf("/v2/config/staging_security_groups/%s", secGUID)))
if err != nil {
return err
}
if resp.StatusCode != 200 { //200
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
UnbindStagingSecGroup contacts the CF endpoint to dis-associate a space with a security group
secGUID: identifies the security group to add a space to
*/
func (c *Client) UnbindStagingSecGroup(secGUID string) error {
//Perform the DELETE and check for errors
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/config/staging_security_groups/%s", secGUID)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent { //204
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
/*
UnbindSecGroup contacts the CF endpoint to dissociate a space from a security group
secGUID: identifies the security group to remove a space from
spaceGUID: identifies the space to dissociate from the security group
*/
func (c *Client) UnbindSecGroup(secGUID, spaceGUID string) error {
//Perform the DELETE and check for errors
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/security_groups/%s/spaces/%s", secGUID, spaceGUID)))
if err != nil {
return err
}
if resp.StatusCode != 204 { //204 No Content
return fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return nil
}
//Reads most security group response bodies into a SecGroup object
func respBodyToSecGroup(body io.ReadCloser, c *Client) (*SecGroup, error) {
//get the json from the response body
bodyRaw, err := ioutil.ReadAll(body)
if err != nil {
return nil, errors.Wrap(err, "Could not read response body")
}
jStruct := SecGroupResource{}
//make it a SecGroup
err = json.Unmarshal(bodyRaw, &jStruct)
if err != nil {
return nil, errors.Wrap(err, "Could not unmarshal response body as json")
}
//pull a few extra fields from other places
ret := jStruct.Entity
ret.Guid = jStruct.Meta.Guid
ret.CreatedAt = jStruct.Meta.CreatedAt
ret.UpdatedAt = jStruct.Meta.UpdatedAt
ret.c = c
return &ret, nil
}
func convertStructToMap(st interface{}) map[string]interface{} {
reqRules := make(map[string]interface{})
v := reflect.ValueOf(st)
t := reflect.TypeOf(st)
for i := 0; i < v.NumField(); i++ {
key := strings.ToLower(t.Field(i).Name)
typ := v.FieldByName(t.Field(i).Name).Kind().String()
structTag := t.Field(i).Tag.Get("json")
jsonName := strings.TrimSpace(strings.Split(structTag, ",")[0])
value := v.FieldByName(t.Field(i).Name)
// if jsonName is not empty use it for the key
if jsonName != "" {
key = jsonName
}
if typ == "string" {
if !(value.String() == "" && strings.Contains(structTag, "omitempty")) {
reqRules[key] = value.String()
}
} else if typ == "int" {
reqRules[key] = value.Int()
} else {
reqRules[key] = value.Interface()
}
}
return reqRules
}
//Create and Update secGroup pretty much do the same thing, so this function abstracts those out.
func (c *Client) secGroupCreateHelper(url, method, name string, rules []SecGroupRule, spaceGuids []string) (*SecGroup, error) {
reqRules := make([]map[string]interface{}, len(rules))
for i, rule := range rules {
reqRules[i] = convertStructToMap(rule)
protocol := strings.ToLower(reqRules[i]["protocol"].(string))
// if not icmp protocol need to remove the Code/Type fields
if protocol != "icmp" {
delete(reqRules[i], "code")
delete(reqRules[i], "type")
}
}
req := c.NewRequest(method, url)
//set up request body
inputs := map[string]interface{}{
"name": name,
"rules": reqRules,
}
if spaceGuids != nil {
inputs["space_guids"] = spaceGuids
}
req.obj = inputs
//fire off the request and check for problems
resp, err := c.DoRequest(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 { // Both create and update should give 201 CREATED
var response SecGroupCreateResponse
bodyRaw, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(bodyRaw, &response)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling response")
}
return nil, fmt.Errorf(`Request failed CF API returned with status code %d
-------------------------------
Error Code %s
Code %d
Description %s`,
resp.StatusCode, response.ErrorCode, response.Code, response.Description)
}
//get the json from the response body
return respBodyToSecGroup(resp.Body, c)
}
func getMinStagingSpacesVersion() *semver.Version {
v, _ := semver.NewVersion("2.68.0")
return v
}