-
Notifications
You must be signed in to change notification settings - Fork 51
/
policy.go
303 lines (246 loc) · 7.08 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
package policy
import "sync"
// 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
//Proxied Services string format ip:port
proxiedServices *ProxiedServicesInfo
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,
proxiedServices *ProxiedServicesInfo,
) *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 proxiedServices == nil {
proxiedServices = &ProxiedServicesInfo{}
}
return &PUPolicy{
managementID: id,
triremeAction: action,
applicationACLs: appACLs,
networkACLs: netACLs,
transmitterRules: txtags,
receiverRules: rxtags,
identity: identity,
annotations: annotations,
ips: ips,
triremeNetworks: triremeNetworks,
excludedNetworks: excludedNetworks,
proxiedServices: proxiedServices,
}
}
// NewPUPolicyWithDefaults sets up a PU policy with defaults
func NewPUPolicyWithDefaults() *PUPolicy {
return NewPUPolicy("", AllowAll, nil, nil, nil, nil, nil, nil, nil, []string{}, []string{}, &ProxiedServicesInfo{})
}
// 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.proxiedServices,
)
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
}
// DefaultIPAddress returns the default IP address for the processing unit
func (p *PUPolicy) DefaultIPAddress() (string, bool) {
p.Lock()
defer p.Unlock()
if ip, ok := p.ips[DefaultNamespace]; ok {
return ip, true
}
return "0.0.0.0/0", false
}
// TriremeNetworks returns the list of networks that Trireme must be applied
func (p *PUPolicy) TriremeNetworks() []string {
p.Lock()
defer p.Unlock()
return p.triremeNetworks
}
// ProxiedServices returns the list of networks that Trireme must be applied
func (p *PUPolicy) ProxiedServices() *ProxiedServicesInfo {
p.Lock()
defer p.Unlock()
return p.proxiedServices
}
// 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)
}