-
Notifications
You must be signed in to change notification settings - Fork 228
/
policy_linux.go
263 lines (230 loc) · 7.13 KB
/
policy_linux.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
package policies
import (
"fmt"
"strconv"
"strings"
"github.com/Azure/azure-container-networking/npm/metrics"
"github.com/Azure/azure-container-networking/npm/pkg/dataplane/ipsets"
"github.com/Azure/azure-container-networking/npm/util"
)
type UniqueDirection bool
const (
forIngress UniqueDirection = true
forEgress UniqueDirection = false
// 5-6 elements depending on Included boolean
maxLengthForMatchSetSpecs = 6
)
// the NPMNetworkPolicy ACLPolicyID field is unnused in Linux
func aclPolicyID(_, _ string) string {
return ""
}
// returns two booleans indicating whether the network policy has ingress and egress respectively
func (networkPolicy *NPMNetworkPolicy) hasIngressAndEgress() (hasIngress, hasEgress bool) {
hasIngress = false
hasEgress = false
for _, aclPolicy := range networkPolicy.ACLs {
hasIngress = hasIngress || aclPolicy.hasIngress()
hasEgress = hasEgress || aclPolicy.hasEgress()
}
return
}
func (networkPolicy *NPMNetworkPolicy) egressChainName() string {
return networkPolicy.chainName(util.IptablesAzureEgressPolicyChainPrefix)
}
func (networkPolicy *NPMNetworkPolicy) ingressChainName() string {
return networkPolicy.chainName(util.IptablesAzureIngressPolicyChainPrefix)
}
func (networkPolicy *NPMNetworkPolicy) chainName(prefix string) string {
policyHash := util.Hash(networkPolicy.PolicyKey)
return joinWithDash(prefix, policyHash)
}
func (networkPolicy *NPMNetworkPolicy) commentForJumpToIngress() string {
return networkPolicy.commentForJump(forIngress)
}
func (networkPolicy *NPMNetworkPolicy) commentForJumpToEgress() string {
return networkPolicy.commentForJump(forEgress)
}
func (networkPolicy *NPMNetworkPolicy) commentForJump(direction UniqueDirection) string {
prefix := "EGRESS"
if direction == forIngress {
prefix = "INGRESS"
}
toFrom := "FROM"
if direction == forIngress {
toFrom = "TO"
}
podSelectorComment := "all"
if len(networkPolicy.PodSelectorList) > 0 {
podSelectorComment = commentForInfos(networkPolicy.PodSelectorList)
}
return fmt.Sprintf("%s-POLICY-%s-%s-%s-IN-ns-%s", prefix, networkPolicy.PolicyKey, toFrom, podSelectorComment, networkPolicy.Namespace)
}
func commentForInfos(infos []SetInfo) string {
infoComments := make([]string, 0, len(infos))
for _, info := range infos {
infoComments = append(infoComments, info.comment())
}
return strings.Join(infoComments, "-AND-")
}
func (info SetInfo) comment() string {
name := info.IPSet.GetPrefixName()
if info.Included {
return name
}
return "!" + name
}
func (info SetInfo) matchSetSpecs(matchString string) []string {
specs := make([]string, 0, maxLengthForMatchSetSpecs)
specs = append(specs, util.IptablesModuleFlag, util.IptablesSetModuleFlag)
if !info.Included {
specs = append(specs, util.IptablesNotFlag)
}
hashedSetName := info.IPSet.GetHashedName()
specs = append(specs, util.IptablesMatchSetFlag, hashedSetName, matchString)
return specs
}
func (aclPolicy *ACLPolicy) comment() string {
// cleanPeerList contains peers that aren't namedports
var cleanPeerList []SetInfo
// TODO remove the if check and initialize the list like in egress once we replace SrcList and DstList with PeerList
if aclPolicy.Direction == Ingress {
cleanPeerList = aclPolicy.SrcList
} else {
cleanPeerList = make([]SetInfo, 0, len(aclPolicy.DstList))
}
var namedPortPeer SetInfo
foundNamedPortPeer := false
for _, info := range aclPolicy.DstList {
if info.IPSet.Type == ipsets.NamedPorts {
if foundNamedPortPeer {
metrics.SendErrorLogAndMetric(util.IptmID, "while creating ACL comment, unexpectedly found more than one namedPort peer for ACL:\n%s", aclPolicy.PrettyString())
}
namedPortPeer = info
foundNamedPortPeer = true
} else if aclPolicy.Direction == Egress {
// TODO remove the if check once we replace SrcList and DstList with PeerList
cleanPeerList = append(cleanPeerList, info)
}
}
builder := strings.Builder{}
if aclPolicy.Target == Allowed {
builder.WriteString("ALLOW")
} else {
builder.WriteString("DROP")
}
if len(cleanPeerList) == 0 {
builder.WriteString("-ALL")
} else {
if aclPolicy.Direction == Ingress {
builder.WriteString("-FROM-")
} else {
builder.WriteString("-TO-")
}
builder.WriteString(commentForInfos(cleanPeerList))
}
builder.WriteString(aclPolicy.Protocol.comment())
builder.WriteString(aclPolicy.DstPorts.comment())
if foundNamedPortPeer {
builder.WriteString("-TO-" + namedPortPeer.comment())
}
return builder.String()
}
func (proto Protocol) comment() string {
if proto == UnspecifiedProtocol {
return ""
}
return fmt.Sprintf("-ON-%s", string(proto))
}
func (portRange *Ports) comment() string {
if portRange.Port == 0 {
return ""
}
if portRange.Port >= portRange.EndPort {
return fmt.Sprintf("-TO-PORT-%d", portRange.Port)
}
return fmt.Sprintf("-TO-PORT-%d:%d", portRange.Port, portRange.EndPort)
}
func (portRange *Ports) toIPTablesString() string {
start := strconv.Itoa(int(portRange.Port))
if portRange.Port == portRange.EndPort {
return start
}
end := strconv.Itoa(int(portRange.EndPort))
return start + ":" + end
}
/*
Notes on commenting
v1 overall:
"[value]" means include if needed e.g. if a port is specified
- prefix:
- no to/from rules and not allowing external:
- allowed: "ALLOW-ALL"
- denied: "DROP-ALL"
- otherwise: drop the "ALL"
- suffix:
- no to/from rules or port rules and not allowing external:
- ingress: "-FROM-all-namespaces"
- egress: "-TO-all-namespaces"
- otherwise: "" (no suffix)
- append these to each other to form the whole comment:
prefix
[-cidrIPSetName]
[-AND-nsSelectorComment]
[-AND]
[-podSelectorComment]
[-protocolComment]
[-portComment]
-TO (or "-FROM" if egress)
-targetSelectorComment
suffix
v2 overall for ACLs:
- prefix:
- allowed:
- no IPSets in SrcList/DstList (i.e. allow external): "ALLOW-ALL"
- otherwise:
- ingress: "ALLOW-FROM"
- egress: "ALLOW-TO"
- denied: replace "ALLOW" with "DROP"
- similar idea (think there are at most two non-namedPort ipsets e.g. ns selector and pod selector):
prefix
[-ipset1Name]
[-AND]
[-ipset2Name]
[-ON-protocolComment]
[-TO-namedPortIPSetName]
[-TO-portComment]
NOTE: can have none or only one of namedPort and port (range)
v2 for jumps to ingress/egress chains:
- prefix:
- ingress: "INGRESS"
- egress: "EGRESS"
- form:
INGRESS (or "EGRESS-" if egress)
-POLICY
-policyKey
-TO (or "-FROM" if egress)
[-podSelectorComment] (or "all" if there are no pod selectors)
-IN-ns
-namespaceName
strings for protocol, ports, selectors:
protocol: just "name"
port (range):
- v1:
- for single port: PORT-x
- for namedport: PORT-name
- v2:
- for single port: PORT-x
- with endport: PORT-x:y
- in v2, namedports are specified as IPSets
selector:
"[!]" means optionally include "!" if the label is not included
- namespace selectors (only for v1):
- if there are no match expressions or labels:
all-namespaces
- otherwise:
ns-[!]label1-AND-ns-[!]label2...
- other w/out ns:
[!]label1-AND-[!]label2...
- other w/ ns:
[!]label1-AND-[!]label2...-AND-ns-[!]labelM-IN-ns-name
*/