-
Notifications
You must be signed in to change notification settings - Fork 51
/
policy.go
647 lines (545 loc) · 18.6 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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package policy
import (
"context"
"fmt"
"strconv"
"sync"
"go.aporeto.io/enforcerd/trireme-lib/controller/pkg/usertokens"
)
// EnforcerType defines which enforcer type should be selected
type EnforcerType int
const (
// EnforcerMapping lets the default enforcer configuration deal with it
EnforcerMapping EnforcerType = iota
// EnvoyAuthorizerEnforcer specifically asks for running an envoy enforcer/authorizer
EnvoyAuthorizerEnforcer
)
// String implements the string interface
func (t EnforcerType) String() string {
switch t {
case EnforcerMapping:
return "EnforcerMapping"
case EnvoyAuthorizerEnforcer:
return "EnvoyAuthorizerEnforcer"
default:
return strconv.Itoa(int(t))
}
}
// EnforcerTypeFromString parses `str` and tries to convert it to
func EnforcerTypeFromString(str string) (EnforcerType, error) {
switch str {
case "EnforcerMapping":
return EnforcerMapping, nil
case "EnvoyAuthorizerEnforcer":
return EnvoyAuthorizerEnforcer, nil
default:
i, err := strconv.Atoi(str)
if err != nil {
return EnforcerMapping, fmt.Errorf("failed to parse enforcer type from string number (input '%s'): %s", str, err.Error())
}
if i < int(EnforcerMapping) {
return EnforcerMapping, fmt.Errorf("failed to parse enforcer type from string number (input '%s'): below possible valid value", str)
}
if i > int(EnvoyAuthorizerEnforcer) {
return EnforcerMapping, fmt.Errorf("failed to parse enforcer type from string number (input '%s'): above possible valid value", str)
}
return EnforcerType(i), nil
}
}
// 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
// managementNamespace is provided for the policy implementations as a means of
// holding a policy sub identifier related to the implementation.
managementNamespace string
// triremeAction defines what level of policy should be applied to that container.
triremeAction PUAction
// dnsACLs is the list of DNS names and the associated ports that the container is
// allowed to talk to outside the data center
DNSACLs DNSRuleList
// 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
// compressedTags is the set of of compressed key/value pairs as binary values.
compressedTags *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
// servicesListeningPort is the port that we will use for the proxy.
servicesListeningPort int
// dnsProxyPort is the proxy port that listens dns traffic
dnsProxyPort int
// 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
// enforcerType is the enforcer type that is supposed to get used for this PU
enforcerType EnforcerType
// appDefaultPolicyAction is the application default action of the namespace
appDefaultPolicyAction ActionType
// netDefaultPolicyAction is the network default action of the namespace
netDefaultPolicyAction ActionType
// logPrefixMapping maps a short nlog prefix it it's long prefix
logPrefixMapping map[string]string
// logPrefixMappingCalculated is used to no when to calculate the log mapping
logPrefixMappingCalculated bool
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,
namespace string,
action PUAction,
appACLs IPRuleList,
netACLs IPRuleList,
dnsACLs DNSRuleList,
txtags TagSelectorList,
rxtags TagSelectorList,
identity *TagStore,
annotations *TagStore,
compressedTags *TagStore,
ips ExtendedMap,
servicesListeningPort int,
dnsProxyPort int,
exposedServices ApplicationServicesList,
dependentServices ApplicationServicesList,
scopes []string,
enforcerType EnforcerType,
appDefaultPolicyAction ActionType,
netDefaultPolicyAction ActionType,
) *PUPolicy {
if appACLs == nil {
appACLs = IPRuleList{}
}
if netACLs == nil {
netACLs = IPRuleList{}
}
if dnsACLs == nil {
dnsACLs = DNSRuleList{}
}
if txtags == nil {
txtags = TagSelectorList{}
}
if rxtags == nil {
rxtags = TagSelectorList{}
}
if identity == nil {
identity = NewTagStore()
}
if annotations == nil {
annotations = NewTagStore()
}
if compressedTags == nil {
compressedTags = NewTagStore()
}
if ips == nil {
ips = ExtendedMap{}
}
if exposedServices == nil {
exposedServices = ApplicationServicesList{}
}
if dependentServices == nil {
dependentServices = ApplicationServicesList{}
}
return &PUPolicy{
managementID: id,
managementNamespace: namespace,
triremeAction: action,
applicationACLs: appACLs,
networkACLs: netACLs,
DNSACLs: dnsACLs,
transmitterRules: txtags,
receiverRules: rxtags,
identity: identity,
compressedTags: compressedTags,
annotations: annotations,
ips: ips,
servicesListeningPort: servicesListeningPort,
dnsProxyPort: dnsProxyPort,
exposedServices: exposedServices,
dependentServices: dependentServices,
scopes: scopes,
enforcerType: enforcerType,
appDefaultPolicyAction: appDefaultPolicyAction,
netDefaultPolicyAction: netDefaultPolicyAction,
}
}
// NewPUPolicyWithDefaults sets up a PU policy with defaults
func NewPUPolicyWithDefaults() *PUPolicy {
return NewPUPolicy("", "", AllowAll, nil, nil, nil, nil, nil, nil, nil, nil, nil, 0, 0, nil, nil, []string{}, EnforcerMapping, Reject|Log, Reject|Log)
}
// Clone returns a copy of the policy
func (p *PUPolicy) Clone() *PUPolicy {
p.Lock()
defer p.Unlock()
np := NewPUPolicy(
p.managementID,
p.managementNamespace,
p.triremeAction,
p.applicationACLs.Copy(),
p.networkACLs.Copy(),
p.DNSACLs.Copy(),
p.transmitterRules.Copy(),
p.receiverRules.Copy(),
p.identity.Copy(),
p.annotations.Copy(),
p.compressedTags.Copy(),
p.ips.Copy(),
p.servicesListeningPort,
p.dnsProxyPort,
p.exposedServices,
p.dependentServices,
p.scopes,
p.enforcerType,
p.appDefaultPolicyAction,
p.netDefaultPolicyAction,
)
return np
}
// ManagementID returns the management ID
func (p *PUPolicy) ManagementID() string {
p.Lock()
defer p.Unlock()
return p.managementID
}
// ManagementNamespace returns the management Namespace
func (p *PUPolicy) ManagementNamespace() string {
p.Lock()
defer p.Unlock()
return p.managementNamespace
}
// 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()
}
// DNSNameACLs returns a copy of DNSRuleList
func (p *PUPolicy) DNSNameACLs() DNSRuleList {
p.Lock()
defer p.Unlock()
return p.DNSACLs.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()
}
// CompressedTags returns the compressed tags of the policy.
func (p *PUPolicy) CompressedTags() *TagStore {
p.Lock()
defer p.Unlock()
return p.compressedTags.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
}
// ExposedServices returns the exposed services
func (p *PUPolicy) ExposedServices() ApplicationServicesList {
p.Lock()
defer p.Unlock()
return p.exposedServices
}
// DNSProxyPort gets the dns proxy port
func (p *PUPolicy) DNSProxyPort() string {
p.Lock()
defer p.Unlock()
return strconv.Itoa(p.dnsProxyPort)
}
// DependentServices returns the external services.
func (p *PUPolicy) DependentServices() ApplicationServicesList {
p.Lock()
defer p.Unlock()
return p.dependentServices
}
// ServicesListeningPort returns the port that should be used by the proxies.
func (p *PUPolicy) ServicesListeningPort() string {
p.Lock()
defer p.Unlock()
return strconv.Itoa(p.servicesListeningPort)
}
// UpdateDNSNetworks updates the set of FQDN names allowed by the policy
func (p *PUPolicy) UpdateDNSNetworks(networks DNSRuleList) {
p.Lock()
defer p.Unlock()
for k, v := range networks {
p.DNSACLs[k] = v
}
}
// 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
}
// EnforcerType returns the enforcer type of the policy.
func (p *PUPolicy) EnforcerType() EnforcerType {
p.Lock()
defer p.Unlock()
return p.enforcerType
}
// AppDefaultPolicyAction returns default application action.
func (p *PUPolicy) AppDefaultPolicyAction() ActionType {
p.Lock()
defer p.Unlock()
return p.appDefaultPolicyAction
}
// NetDefaultPolicyAction returns default network action.
func (p *PUPolicy) NetDefaultPolicyAction() ActionType {
p.Lock()
defer p.Unlock()
return p.netDefaultPolicyAction
}
// ToPublicPolicy converts the object to a marshallable object.
func (p *PUPolicy) ToPublicPolicy() *PUPolicyPublic {
p.Lock()
defer p.Unlock()
return &PUPolicyPublic{
ManagementID: p.managementID,
ManagementNamespace: p.managementNamespace,
TriremeAction: p.triremeAction,
ApplicationACLs: p.applicationACLs.Copy(),
NetworkACLs: p.networkACLs.Copy(),
DNSACLs: p.DNSACLs.Copy(),
TransmitterRules: p.transmitterRules.Copy(),
ReceiverRules: p.receiverRules.Copy(),
Annotations: p.annotations.GetSlice(),
CompressedTags: p.compressedTags.GetSlice(),
Identity: p.identity.GetSlice(),
IPs: p.ips.Copy(),
ServicesListeningPort: p.servicesListeningPort,
DNSProxyPort: p.dnsProxyPort,
ExposedServices: p.exposedServices,
DependentServices: p.dependentServices,
Scopes: p.scopes,
ServicesCA: p.servicesCA,
ServicesCertificate: p.servicesCertificate,
ServicesPrivateKey: p.servicesPrivateKey,
EnforcerType: p.enforcerType,
AppDefaultPolicyAction: p.appDefaultPolicyAction,
NetDefaultPolicyAction: p.netDefaultPolicyAction,
}
}
// LookupLogPrefix returns the long version of the nlog prefix
func (p *PUPolicy) LookupLogPrefix(key string) (string, bool) {
p.Lock()
defer p.Unlock()
// On demand calculate the mapping
p.calculateLogPrefixes()
logPrefix, ok := p.logPrefixMapping[key]
return logPrefix, ok
}
// GetLogPrefixes returns the current map of logging prefixes
func (p *PUPolicy) GetLogPrefixes() map[string]string {
p.Lock()
defer p.Unlock()
// On demand calculate the mapping
p.calculateLogPrefixes()
clone := map[string]string{}
for key, value := range p.logPrefixMapping {
clone[key] = value
}
return clone
}
// MergeLogPrefixes merges existing prefixes with the current logging prefixes
func (p *PUPolicy) MergeLogPrefixes(prefixes map[string]string) {
p.Lock()
defer p.Unlock()
// On demand calculate the mapping
p.calculateLogPrefixes()
for key, value := range prefixes {
p.logPrefixMapping[key] = value
}
}
// calculateLogPrefixes calculates the short/long logging prefixes
func (p *PUPolicy) calculateLogPrefixes() {
// On demand calculate the mapping
if !p.logPrefixMappingCalculated {
p.logPrefixMapping = map[string]string{}
compute := func(ruleList IPRuleList) {
for _, ipRule := range ruleList {
if ipRule.Policy != nil {
key, value := ipRule.Policy.GetShortAndLongLogPrefix()
p.logPrefixMapping[key] = value
}
}
}
compute(p.applicationACLs)
compute(p.networkACLs)
// mapping has been calculated
p.logPrefixMappingCalculated = true
}
}
// 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"`
ManagementNamespace string `json:"managementNamespace,omitempty"`
TriremeAction PUAction `json:"triremeAction,omitempty"`
ApplicationACLs IPRuleList `json:"applicationACLs,omitempty"`
NetworkACLs IPRuleList `json:"networkACLs,omitempty"`
DNSACLs DNSRuleList `json:"dnsACLs,omitempty"`
Identity []string `json:"identity,omitempty"`
Annotations []string `json:"annotations,omitempty"`
CompressedTags []string `json:"compressedtags,omitempty"`
TransmitterRules TagSelectorList `json:"transmitterRules,omitempty"`
ReceiverRules TagSelectorList `json:"receiverRules,omitempty"`
IPs ExtendedMap `json:"IPs,omitempty"`
ServicesListeningPort int `json:"servicesListeningPort,omitempty"`
DNSProxyPort int `json:"dnsProxyPort,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"`
EnforcerType EnforcerType `json:"enforcerTypes,omitempty"`
AppDefaultPolicyAction ActionType `json:"appDefaultPolicyAction,omitempty"`
NetDefaultPolicyAction ActionType `json:"netDefaultPolicyAction,omitempty"`
}
// ToPrivatePolicy converts the object to a private object.
func (p *PUPolicyPublic) ToPrivatePolicy(ctx context.Context, convert bool) (*PUPolicy, error) {
var err error
exposedServices := ApplicationServicesList{}
for _, e := range p.ExposedServices {
if convert {
e.UserAuthorizationHandler, err = usertokens.NewVerifier(ctx, e.UserAuthorizationHandler)
if err != nil {
return nil, fmt.Errorf("unable to initialize user authorization handler for service: %s - error %s", e.ID, err)
}
}
exposedServices = append(exposedServices, e)
}
return &PUPolicy{
managementID: p.ManagementID,
managementNamespace: p.ManagementNamespace,
triremeAction: p.TriremeAction,
applicationACLs: p.ApplicationACLs,
networkACLs: p.NetworkACLs.Copy(),
DNSACLs: p.DNSACLs.Copy(),
transmitterRules: p.TransmitterRules.Copy(),
receiverRules: p.ReceiverRules.Copy(),
annotations: NewTagStoreFromSlice(p.Annotations),
compressedTags: NewTagStoreFromSlice(p.CompressedTags),
identity: NewTagStoreFromSlice(p.Identity),
ips: p.IPs.Copy(),
servicesListeningPort: p.ServicesListeningPort,
dnsProxyPort: p.DNSProxyPort,
exposedServices: exposedServices,
dependentServices: p.DependentServices,
scopes: p.Scopes,
enforcerType: p.EnforcerType,
servicesCA: p.ServicesCA,
servicesCertificate: p.ServicesCertificate,
servicesPrivateKey: p.ServicesPrivateKey,
appDefaultPolicyAction: p.AppDefaultPolicyAction,
netDefaultPolicyAction: p.NetDefaultPolicyAction,
}, nil
}