-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathpolicy.go
284 lines (238 loc) · 8.26 KB
/
policy.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
package cfnetworkingaction
import (
"code.cloudfoundry.org/cfnetworking-cli-api/cfnetworking/cfnetv1"
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/resources"
)
type Policy struct {
SourceName string
DestinationName string
Protocol string
DestinationSpaceName string
DestinationOrgName string
StartPort int
EndPort int
}
func (actor Actor) AddNetworkPolicy(srcSpaceGUID, srcAppName, destSpaceGUID, destAppName, protocol string, startPort, endPort int) (Warnings, error) {
var allWarnings Warnings
srcApp, warnings, err := actor.CloudControllerClient.GetApplicationByNameAndSpace(srcAppName, srcSpaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
destApp, warnings, err := actor.CloudControllerClient.GetApplicationByNameAndSpace(destAppName, destSpaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
err = actor.NetworkingClient.CreatePolicies([]cfnetv1.Policy{
{
Source: cfnetv1.PolicySource{
ID: srcApp.GUID,
},
Destination: cfnetv1.PolicyDestination{
ID: destApp.GUID,
Protocol: cfnetv1.PolicyProtocol(protocol),
Ports: cfnetv1.Ports{
Start: startPort,
End: endPort,
},
},
},
})
return allWarnings, err
}
func (actor Actor) NetworkPoliciesBySpace(spaceGUID string) ([]Policy, Warnings, error) {
var allWarnings Warnings
applications, warnings, err := actor.CloudControllerClient.GetApplications(ccv3.Query{
Key: ccv3.SpaceGUIDFilter,
Values: []string{spaceGUID},
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
policies, warnings, err := actor.getPoliciesForApplications(applications)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
return policies, allWarnings, nil
}
func (actor Actor) NetworkPoliciesBySpaceAndAppName(spaceGUID string, srcAppName string) ([]Policy, Warnings, error) {
var allWarnings Warnings
srcApp, warnings, err := actor.CloudControllerClient.GetApplicationByNameAndSpace(srcAppName, spaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
policies, warnings, err := actor.getPoliciesForApplications([]resources.Application{srcApp})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
return policies, allWarnings, nil
}
func (actor Actor) RemoveNetworkPolicy(srcSpaceGUID, srcAppName, destSpaceGUID, destAppName, protocol string, startPort, endPort int) (Warnings, error) {
var allWarnings Warnings
srcApp, warnings, err := actor.CloudControllerClient.GetApplicationByNameAndSpace(srcAppName, srcSpaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
destApp, warnings, err := actor.CloudControllerClient.GetApplicationByNameAndSpace(destAppName, destSpaceGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
policyToRemove := cfnetv1.Policy{
Source: cfnetv1.PolicySource{
ID: srcApp.GUID,
},
Destination: cfnetv1.PolicyDestination{
ID: destApp.GUID,
Protocol: cfnetv1.PolicyProtocol(protocol),
Ports: cfnetv1.Ports{
Start: startPort,
End: endPort,
},
},
}
v1Policies, err := actor.NetworkingClient.ListPolicies(srcApp.GUID)
if err != nil {
return allWarnings, err
}
for _, v1Policy := range v1Policies {
if v1Policy == policyToRemove {
return allWarnings, actor.NetworkingClient.RemovePolicies([]cfnetv1.Policy{policyToRemove})
}
}
return allWarnings, actionerror.PolicyDoesNotExistError{}
}
func filterPoliciesWithoutMatchingSourceGUIDs(v1Policies []cfnetv1.Policy, srcAppGUIDs []string) []cfnetv1.Policy {
srcGUIDsSet := map[string]struct{}{}
for _, srcGUID := range srcAppGUIDs {
srcGUIDsSet[srcGUID] = struct{}{}
}
var toReturn []cfnetv1.Policy
for _, policy := range v1Policies {
if _, ok := srcGUIDsSet[policy.Source.ID]; ok {
toReturn = append(toReturn, policy)
}
}
return toReturn
}
func uniqueSpaceGUIDs(applications []resources.Application) []string {
var spaceGUIDs []string
occurrences := map[string]struct{}{}
for _, app := range applications {
if _, ok := occurrences[app.SpaceGUID]; !ok {
spaceGUIDs = append(spaceGUIDs, app.SpaceGUID)
occurrences[app.SpaceGUID] = struct{}{}
}
}
return spaceGUIDs
}
func uniqueOrgGUIDs(spaces []resources.Space) []string {
var orgGUIDs []string
occurrences := map[string]struct{}{}
for _, space := range spaces {
orgGUID := space.Relationships[constant.RelationshipTypeOrganization].GUID
if _, ok := occurrences[orgGUID]; !ok {
orgGUIDs = append(orgGUIDs, orgGUID)
occurrences[orgGUID] = struct{}{}
}
}
return orgGUIDs
}
func uniqueDestGUIDs(policies []cfnetv1.Policy) []string {
var destAppGUIDs []string
occurrences := map[string]struct{}{}
for _, policy := range policies {
if _, ok := occurrences[policy.Destination.ID]; !ok {
destAppGUIDs = append(destAppGUIDs, policy.Destination.ID)
occurrences[policy.Destination.ID] = struct{}{}
}
}
return destAppGUIDs
}
func (actor Actor) orgNamesBySpaceGUID(spaces []resources.Space) (map[string]string, ccv3.Warnings, error) {
orgs, warnings, err := actor.CloudControllerClient.GetOrganizations(ccv3.Query{
Key: ccv3.GUIDFilter,
Values: uniqueOrgGUIDs(spaces),
})
if err != nil {
return nil, warnings, err
}
orgNamesByGUID := make(map[string]string, len(orgs))
for _, org := range orgs {
orgNamesByGUID[org.GUID] = org.Name
}
orgNamesBySpaceGUID := make(map[string]string, len(spaces))
for _, space := range spaces {
orgGUID := space.Relationships[constant.RelationshipTypeOrganization].GUID
orgNamesBySpaceGUID[space.GUID] = orgNamesByGUID[orgGUID]
}
return orgNamesBySpaceGUID, warnings, nil
}
func (actor Actor) getPoliciesForApplications(applications []resources.Application) ([]Policy, ccv3.Warnings, error) {
var allWarnings ccv3.Warnings
var srcAppGUIDs []string
for _, app := range applications {
srcAppGUIDs = append(srcAppGUIDs, app.GUID)
}
v1Policies, err := actor.NetworkingClient.ListPolicies(srcAppGUIDs...)
if err != nil {
return []Policy{}, allWarnings, err
}
// ListPolicies will return policies with the app guids in either the source or destination.
// It needs to be further filtered to only get policies with the app guids in the source.
v1Policies = filterPoliciesWithoutMatchingSourceGUIDs(v1Policies, srcAppGUIDs)
destAppGUIDs := uniqueDestGUIDs(v1Policies)
destApplications, warnings, err := actor.CloudControllerClient.GetApplications(ccv3.Query{
Key: ccv3.GUIDFilter,
Values: destAppGUIDs,
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
applications = append(applications, destApplications...)
spaces, _, warnings, err := actor.CloudControllerClient.GetSpaces(ccv3.Query{
Key: ccv3.GUIDFilter,
Values: uniqueSpaceGUIDs(applications),
})
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
spaceNamesByGUID := make(map[string]string, len(spaces))
for _, destSpace := range spaces {
spaceNamesByGUID[destSpace.GUID] = destSpace.Name
}
orgNamesBySpaceGUID, warnings, err := actor.orgNamesBySpaceGUID(spaces)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return []Policy{}, allWarnings, err
}
appByGUID := map[string]resources.Application{}
for _, app := range applications {
appByGUID[app.GUID] = app
}
var policies []Policy
for _, v1Policy := range v1Policies {
destination := appByGUID[v1Policy.Destination.ID]
policies = append(policies, Policy{
SourceName: appByGUID[v1Policy.Source.ID].Name,
DestinationName: destination.Name,
Protocol: string(v1Policy.Destination.Protocol),
StartPort: v1Policy.Destination.Ports.Start,
EndPort: v1Policy.Destination.Ports.End,
DestinationSpaceName: spaceNamesByGUID[destination.SpaceGUID],
DestinationOrgName: orgNamesBySpaceGUID[destination.SpaceGUID],
})
}
return policies, allWarnings, nil
}