-
Notifications
You must be signed in to change notification settings - Fork 0
/
policygen.go
177 lines (173 loc) · 4.12 KB
/
policygen.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package policygen
var policiesTestSuite = PolicyTestSuite{
l3Checks: []PolicyTestKind{
{
name: "No Policy",
kind: ingress,
tests: ConnResultAllOK,
template: map[string]string{
"fromEndpoints": `[{}]`,
},
},
{
name: "Ingress Label",
kind: ingress,
tests: ConnResultAllOK,
template: map[string]string{
"fromEndpoints": `[{"matchLabels": { "id": "{{.SrcPod}}"}}]`,
},
},
{
name: "Ingress Label Invalid",
kind: ingress,
tests: ConnResultAllTimeout,
template: map[string]string{
"fromEndpoints": `[{"matchLabels": { "id": "{{.SrcPod}}Invalid"}}]`,
},
},
},
l4Checks: []PolicyTestKind{
{
name: "No Policy",
kind: ingress,
tests: ConnResultAllOK,
template: map[string]string{},
},
{
name: "Ingress Port 80 No protocol",
kind: ingress,
tests: ConnResultOnlyHTTP,
template: map[string]string{
"ports": `[{"port": "80"}]`,
},
},
{
name: "Egress Port 80 No protocol",
kind: egress,
tests: ConnResultOnlyHTTP,
template: map[string]string{
"ports": `[{"port": "80"}]`,
},
},
{
name: "Ingress Port 80 TCP",
kind: ingress,
tests: ConnResultOnlyHTTP,
template: map[string]string{
"ports": `[{"port": "80", "protocol": "TCP"}]`,
},
},
{
name: "Ingress Port 80 UDP",
kind: ingress,
tests: ConnResultAllTimeout,
template: map[string]string{
"ports": `[{"port": "80", "protocol": "UDP"}]`,
},
},
{
name: "Egress Port 80 TCP",
kind: egress,
tests: ConnResultOnlyHTTP,
template: map[string]string{
"ports": `[{"port": "80", "protocol": "TCP"}]`,
},
},
{
name: "Egress Port 80 UDP",
kind: egress,
tests: ConnResultAllTimeout,
template: map[string]string{
"ports": `[{"port": "80", "protocol": "UDP"}]`,
},
},
},
l7Checks: []PolicyTestKind{
{
name: "No Policy",
kind: ingress,
tests: ConnResultAllOK,
template: map[string]string{},
},
{
name: "Ingress policy /private/",
kind: ingress,
tests: ConnResultOnlyHTTPPrivate,
template: map[string]string{
"rules": `{"http": [{"method": "GET", "path": "/private"}]}`,
"ports": `[{"port": "80", "protocol": "TCP"}]`,
},
exclude: []string{
"L4:Ingress Port 80 UDP",
"L4:Ingress Port 80 No protocol",
},
},
{
name: "Egress policy to /private/",
kind: egress,
tests: ConnResultOnlyHTTPPrivate,
template: map[string]string{
"rules": `{"http": [{"method": "GET", "path": "/private"}]}`,
"ports": `[{"port": "80", "protocol": "TCP"}]`,
},
exclude: []string{
"L4:Egress Port 80 UDP",
"L4:Egress Port 80 No protocol",
},
},
},
}
// GeneratedTestSpec returns a `TestSpec` array with all the policies
// possibilities based on all combinations of `policiesTestSuite`
func GeneratedTestSpec() []TestSpec {
var testSpecs = []TestSpec{}
for _, l3 := range policiesTestSuite.l3Checks {
for _, l4 := range policiesTestSuite.l4Checks {
for _, l7 := range policiesTestSuite.l7Checks {
for _, dst := range DestinationsTypes {
testSpecs = append(testSpecs, TestSpec{
l3: l3,
l4: l4,
l7: l7,
Destination: dst,
})
}
}
}
}
return testSpecs
}
// GetBasicTestSpec returns a very simple TestSpec with a L4 and L7 policy that
// allow traffic only to /private/
func GetBasicTestSpec() TestSpec {
return TestSpec{
l3: PolicyTestKind{
name: "No Policy",
kind: ingress,
tests: ConnResultAllOK,
template: map[string]string{
"FromEndpoints": `[{}]`,
},
},
l4: PolicyTestKind{
name: "Ingress Port 80 TCP",
kind: ingress,
tests: ConnResultOnlyHTTP,
template: map[string]string{
"Ports": `[{"port": "80", "protocol": "TCP"}]`,
},
},
l7: PolicyTestKind{
name: "Ingress policy /private/",
kind: ingress,
tests: ConnResultOnlyHTTPPrivate,
template: map[string]string{
"Rules": `{"http": [{"method": "GET", "path": "/private"}]}`,
"Ports": `[{"port": "80", "protocol": "TCP"}]`,
},
},
Destination: DestinationsTypes[0],
}
}