-
Notifications
You must be signed in to change notification settings - Fork 51
/
policy.go
432 lines (358 loc) · 11.7 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
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
package policy
import (
"sync"
"go.aporeto.io/trireme-lib/controller/pkg/usertokens"
)
// PUPolicy captures all policy information related ot the container
type PUPolicy struct {
// ManagementID is provided for the policy implementations as a means of
// holding a policy identifier related to the implementation.
managementID string
// triremeAction defines what level of policy should be applied to that container.
triremeAction PUAction
// applicationACLs is the list of ACLs to be applied when the container talks
// to IP Addresses outside the data center
applicationACLs IPRuleList
// networkACLs is the list of ACLs to be applied from IP Addresses outside
// the data center
networkACLs IPRuleList
// identity is the set of key value pairs that must be send over the wire.
identity *TagStore
// annotations are key/value pairs that should be used for accounting reasons
annotations *TagStore
// transmitterRules is the set of rules that implement the label matching at the Transmitter
transmitterRules TagSelectorList
// teceiverRules is the set of rules that implement matching at the Receiver
receiverRules TagSelectorList
// ips is the set of IP addresses and namespaces that the policy must be applied to
ips ExtendedMap
// triremeNetworks is the list of networks that Authorization must be enforced
triremeNetworks []string
// excludedNetworks a list of networks that must be excluded
excludedNetworks []string
// exposedServices is the list of services that this PU is exposing.
exposedServices ApplicationServicesList
// dependentServices is the list of services that this PU depends on.
dependentServices ApplicationServicesList
// servicesCertificate is the services certificate
servicesCertificate string
// servicePrivateKey is the service private key
servicesPrivateKey string
// servicesCA is the CA to be used for the outgoing services
servicesCA string
// scopes are the processing unit granted scopes
scopes []string
sync.Mutex
}
// PUAction defines the action types that applies for a specific PU as a whole.
type PUAction int
const (
// AllowAll allows everything for the specific PU.
AllowAll = 0x1
// Police filters on the PU based on the PolicyRules.
Police = 0x2
)
// NewPUPolicy generates a new ContainerPolicyInfo
// appACLs are the ACLs for packet coming from the Application/PU to the Network.
// netACLs are the ACLs for packet coming from the Network to the Application/PU.
func NewPUPolicy(
id string,
action PUAction,
appACLs IPRuleList,
netACLs IPRuleList,
txtags TagSelectorList,
rxtags TagSelectorList,
identity *TagStore,
annotations *TagStore,
ips ExtendedMap,
triremeNetworks []string,
excludedNetworks []string,
exposedServices ApplicationServicesList,
dependentServices ApplicationServicesList,
scopes []string,
) *PUPolicy {
if appACLs == nil {
appACLs = IPRuleList{}
}
if netACLs == nil {
netACLs = IPRuleList{}
}
if txtags == nil {
txtags = TagSelectorList{}
}
if rxtags == nil {
rxtags = TagSelectorList{}
}
if identity == nil {
identity = NewTagStore()
}
if annotations == nil {
annotations = NewTagStore()
}
if ips == nil {
ips = ExtendedMap{}
}
if exposedServices == nil {
exposedServices = ApplicationServicesList{}
}
if dependentServices == nil {
dependentServices = ApplicationServicesList{}
}
return &PUPolicy{
managementID: id,
triremeAction: action,
applicationACLs: appACLs,
networkACLs: netACLs,
transmitterRules: txtags,
receiverRules: rxtags,
identity: identity,
annotations: annotations,
ips: ips,
triremeNetworks: triremeNetworks,
excludedNetworks: excludedNetworks,
exposedServices: exposedServices,
dependentServices: dependentServices,
scopes: scopes,
}
}
// NewPUPolicyWithDefaults sets up a PU policy with defaults
func NewPUPolicyWithDefaults() *PUPolicy {
return NewPUPolicy("", AllowAll, nil, nil, nil, nil, nil, nil, nil, []string{}, []string{}, nil, nil, []string{})
}
// Clone returns a copy of the policy
func (p *PUPolicy) Clone() *PUPolicy {
p.Lock()
defer p.Unlock()
np := NewPUPolicy(
p.managementID,
p.triremeAction,
p.applicationACLs.Copy(),
p.networkACLs.Copy(),
p.transmitterRules.Copy(),
p.receiverRules.Copy(),
p.identity.Copy(),
p.annotations.Copy(),
p.ips.Copy(),
p.triremeNetworks,
p.excludedNetworks,
p.exposedServices,
p.dependentServices,
p.scopes,
)
return np
}
// ManagementID returns the management ID
func (p *PUPolicy) ManagementID() string {
p.Lock()
defer p.Unlock()
return p.managementID
}
// TriremeAction returns the TriremeAction
func (p *PUPolicy) TriremeAction() PUAction {
p.Lock()
defer p.Unlock()
return p.triremeAction
}
// SetTriremeAction returns the TriremeAction
func (p *PUPolicy) SetTriremeAction(action PUAction) {
p.Lock()
defer p.Unlock()
p.triremeAction = action
}
// ApplicationACLs returns a copy of IPRuleList
func (p *PUPolicy) ApplicationACLs() IPRuleList {
p.Lock()
defer p.Unlock()
return p.applicationACLs.Copy()
}
// NetworkACLs returns a copy of IPRuleList
func (p *PUPolicy) NetworkACLs() IPRuleList {
p.Lock()
defer p.Unlock()
return p.networkACLs.Copy()
}
// ReceiverRules returns a copy of TagSelectorList
func (p *PUPolicy) ReceiverRules() TagSelectorList {
p.Lock()
defer p.Unlock()
return p.receiverRules.Copy()
}
// AddReceiverRules adds a receiver rule
func (p *PUPolicy) AddReceiverRules(t TagSelector) {
p.Lock()
defer p.Unlock()
p.receiverRules = append(p.receiverRules, t)
}
// TransmitterRules returns a copy of TagSelectorList
func (p *PUPolicy) TransmitterRules() TagSelectorList {
p.Lock()
defer p.Unlock()
return p.transmitterRules.Copy()
}
// AddTransmitterRules adds a transmitter rule
func (p *PUPolicy) AddTransmitterRules(t TagSelector) {
p.Lock()
defer p.Unlock()
p.transmitterRules = append(p.transmitterRules, t)
}
// Identity returns a copy of the Identity
func (p *PUPolicy) Identity() *TagStore {
p.Lock()
defer p.Unlock()
return p.identity.Copy()
}
// Annotations returns a copy of the annotations
func (p *PUPolicy) Annotations() *TagStore {
p.Lock()
defer p.Unlock()
return p.annotations.Copy()
}
// AddIdentityTag adds a policy tag
func (p *PUPolicy) AddIdentityTag(k, v string) {
p.Lock()
defer p.Unlock()
p.identity.AppendKeyValue(k, v)
}
// IPAddresses returns all the IP addresses for the processing unit
func (p *PUPolicy) IPAddresses() ExtendedMap {
p.Lock()
defer p.Unlock()
return p.ips.Copy()
}
// SetIPAddresses sets the IP addresses for the processing unit
func (p *PUPolicy) SetIPAddresses(l ExtendedMap) {
p.Lock()
defer p.Unlock()
p.ips = l
}
// TriremeNetworks returns the list of networks that Trireme must be applied
func (p *PUPolicy) TriremeNetworks() []string {
p.Lock()
defer p.Unlock()
return p.triremeNetworks
}
// ExposedServices returns the exposed services
func (p *PUPolicy) ExposedServices() ApplicationServicesList {
p.Lock()
defer p.Unlock()
return p.exposedServices
}
// DependentServices returns the external services.
func (p *PUPolicy) DependentServices() ApplicationServicesList {
p.Lock()
defer p.Unlock()
return p.dependentServices
}
// UpdateTriremeNetworks updates the set of networks for trireme
func (p *PUPolicy) UpdateTriremeNetworks(networks []string) {
p.Lock()
defer p.Unlock()
p.triremeNetworks = make([]string, len(networks))
copy(p.triremeNetworks, networks)
}
// ExcludedNetworks returns the list of excluded networks.
func (p *PUPolicy) ExcludedNetworks() []string {
p.Lock()
defer p.Unlock()
return p.excludedNetworks
}
// UpdateExcludedNetworks updates the list of excluded networks.
func (p *PUPolicy) UpdateExcludedNetworks(networks []string) {
p.Lock()
defer p.Unlock()
p.excludedNetworks = make([]string, len(networks))
copy(p.excludedNetworks, networks)
}
// UpdateServiceCertificates updates the certificate and private key of the policy
func (p *PUPolicy) UpdateServiceCertificates(cert, key string) {
p.Lock()
defer p.Unlock()
p.servicesCertificate = cert
p.servicesPrivateKey = key
}
// ServiceCertificates returns the service certificate.
func (p *PUPolicy) ServiceCertificates() (string, string, string) {
p.Lock()
defer p.Unlock()
return p.servicesCertificate, p.servicesPrivateKey, p.servicesCA
}
// Scopes returns the scopes of the policy.
func (p *PUPolicy) Scopes() []string {
p.Lock()
defer p.Unlock()
return p.scopes
}
// ToPublicPolicy converts the object to a marshallable object.
func (p *PUPolicy) ToPublicPolicy() *PUPolicyPublic {
p.Lock()
defer p.Unlock()
return &PUPolicyPublic{
ManagementID: p.managementID,
TriremeAction: p.triremeAction,
ApplicationACLs: p.applicationACLs.Copy(),
NetworkACLs: p.networkACLs.Copy(),
TransmitterRules: p.transmitterRules.Copy(),
ReceiverRules: p.receiverRules.Copy(),
Annotations: p.annotations.Copy(),
Identity: p.identity.Copy(),
IPs: p.ips.Copy(),
TriremeNetworks: p.triremeNetworks,
ExcludedNetworks: p.excludedNetworks,
ExposedServices: p.exposedServices,
DependentServices: p.dependentServices,
Scopes: p.scopes,
ServicesCA: p.servicesCA,
ServicesCertificate: p.servicesCertificate,
ServicesPrivateKey: p.servicesPrivateKey,
}
}
// PUPolicyPublic captures all policy information related ot the processing
// unit in an object that can be marshalled and transmitted over the RPC interface.
type PUPolicyPublic struct {
ManagementID string `json:"managementID,omitempty"`
TriremeAction PUAction `json:"triremeAction,omitempty"`
ApplicationACLs IPRuleList `json:"applicationACLs,omitempty"`
NetworkACLs IPRuleList `json:"networkACLs,omitempty"`
Identity *TagStore `json:"identity,omitempty"`
Annotations *TagStore `json:"annotations,omitempty"`
TransmitterRules TagSelectorList `json:"transmitterRules,omitempty"`
ReceiverRules TagSelectorList `json:"receiverRules,omitempty"`
IPs ExtendedMap `json:"IPs,omitempty"`
TriremeNetworks []string `json:"triremeNetworks,omitempty"`
ExcludedNetworks []string `json:"excludedNetworks,omitempty"`
ExposedServices ApplicationServicesList `json:"exposedServices,omitempty"`
DependentServices ApplicationServicesList `json:"dependentServices,omitempty"`
ServicesCertificate string `json:"servicesCertificate,omitempty"`
ServicesPrivateKey string `json:"servicesPrivateKey,omitempty"`
ServicesCA string `json:"servicesCA,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
// ToPrivatePolicy converts the object to a private object.
func (p *PUPolicyPublic) ToPrivatePolicy(convert bool) *PUPolicy {
exposedServices := ApplicationServicesList{}
for _, e := range p.ExposedServices {
if convert {
e.JWTTokenHandler = usertokens.NewVerifier(e.JWTTokenHandler)
}
exposedServices = append(exposedServices, e)
}
return &PUPolicy{
managementID: p.ManagementID,
triremeAction: p.TriremeAction,
applicationACLs: p.ApplicationACLs,
networkACLs: p.NetworkACLs.Copy(),
transmitterRules: p.TransmitterRules.Copy(),
receiverRules: p.ReceiverRules.Copy(),
annotations: p.Annotations.Copy(),
identity: p.Identity.Copy(),
ips: p.IPs.Copy(),
triremeNetworks: p.TriremeNetworks,
excludedNetworks: p.ExcludedNetworks,
exposedServices: exposedServices,
dependentServices: p.DependentServices,
scopes: p.Scopes,
servicesCA: p.ServicesCA,
servicesCertificate: p.ServicesCertificate,
servicesPrivateKey: p.ServicesPrivateKey,
}
}