-
Notifications
You must be signed in to change notification settings - Fork 10
/
e2e_egress_policy_test.go
303 lines (273 loc) · 8.27 KB
/
e2e_egress_policy_test.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 e2e
import (
"context"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
smiSpecs "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/specs/v1alpha4"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
policyV1alpha1 "github.com/flomesh-io/fsm/pkg/apis/policy/v1alpha1"
. "github.com/flomesh-io/fsm/tests/framework"
)
type testScenario int
const (
httpEgressNoRouteMatches testScenario = iota
httpEgressWithRouteMatches
httpsEgress
tcpEgress
)
var _ = FSMDescribe("Tests external traffic using egress policy",
FSMDescribeInfo{
Tier: 1,
Bucket: 2,
},
func() {
Context("HTTP egress policy without route matches", func() {
// Tests HTTP egress traffic without SMI route matches
testEgressPolicy(httpEgressNoRouteMatches)
})
Context("HTTP egress policy with route match", func() {
// Tests HTTP egress traffic with SMI route match
testEgressPolicy(httpEgressWithRouteMatches)
})
Context("HTTPS egress policy", func() {
// Tests HTTPS egress traffic
testEgressPolicy(httpsEgress)
})
Context("TCP egress policy", func() {
// Tests TCP egress traffic
testEgressPolicy(tcpEgress)
})
})
func testEgressPolicy(scenario testScenario) {
It("Tests external traffic using egress policy", func() {
const sourceNs = "client"
const sourceName = "client"
// Install FSM
installOpts := Td.GetFSMInstallOpts()
installOpts.EgressEnabled = false // Disable global egress
Expect(Td.InstallFSM(installOpts)).To(Succeed())
// Create source namespace and add it to the mesh
Expect(Td.CreateNs(sourceNs, nil)).To(Succeed())
Expect(Td.AddNsToMesh(true, sourceNs)).To(Succeed())
// Create simple pod definitions for the source
srcSvcAcc, srcPodDef, _, err := Td.SimplePodApp(SimplePodAppDef{
PodName: sourceName,
Namespace: sourceNs,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"while true; do sleep 30; done;"},
Image: "flomesh/alpine-debug",
Ports: []int{80},
OS: Td.ClusterOS,
})
Expect(err).NotTo(HaveOccurred())
_, err = Td.CreateServiceAccount(sourceNs, &srcSvcAcc)
Expect(err).NotTo(HaveOccurred())
srcPod, err := Td.CreatePod(sourceNs, srcPodDef)
Expect(err).NotTo(HaveOccurred())
// Expect it to be up and running in it's namespace
Expect(Td.WaitForPodsRunningReady(sourceNs, 1, nil)).To(Succeed())
url, policy, smiHTTPRoute := getTestAttributes(srcSvcAcc, scenario)
httpRequest := HTTPRequestDef{
SourceNs: srcPod.Namespace,
SourcePod: srcPod.Name,
SourceContainer: sourceName,
Destination: url,
}
//
// Verify traffic is allowed when policies are applied
//
if smiHTTPRoute != nil {
// Create an SMI HTTP route
_, err := Td.SmiClients.SpecClient.SpecsV1alpha4().HTTPRouteGroups(smiHTTPRoute.Namespace).Create(context.TODO(), smiHTTPRoute, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
}
By("Creating an Egress policy")
policy, err = Td.PolicyClient.PolicyV1alpha1().Egresses(srcPod.Namespace).Create(context.TODO(), policy, metav1.CreateOptions{})
Expect(err).NotTo(HaveOccurred())
cond := Td.WaitForRepeatedSuccess(func() bool {
result := Td.HTTPRequest(httpRequest)
if result.Err != nil {
Td.T.Logf("> Request %s failed, err: %s", url, result.Err)
return false
}
if result.StatusCode != 200 {
Td.T.Logf("> Request %s failed, status code: %d", url, result.StatusCode)
}
Td.T.Logf("> Request %s succeeded, status code: %d", url, result.StatusCode)
return true
}, 5, 180*time.Second)
Expect(cond).To(BeTrue())
//
// Verify traffic is blocked when policies are removed
//
if smiHTTPRoute != nil {
// Create an SMI HTTP route
err := Td.SmiClients.SpecClient.SpecsV1alpha4().HTTPRouteGroups(smiHTTPRoute.Namespace).Delete(context.TODO(), smiHTTPRoute.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
}
By("Deleting an Egress policy")
err = Td.PolicyClient.PolicyV1alpha1().Egresses(policy.Namespace).Delete(context.TODO(), policy.Name, metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
// Expect client not to reach server
cond = Td.WaitForRepeatedSuccess(func() bool {
result := Td.HTTPRequest(httpRequest)
// Curl exit code 7 == Conn refused
if result.Err == nil || !strings.Contains(result.Err.Error(), "command terminated with exit code 7 ") {
Td.T.Logf("> Request %s did not fail as expected, status code: %d, err: %s", url, result.StatusCode, result.Err)
return false
}
Td.T.Logf("> Request %s failed correctly (%s)", url, result.Err)
return true
}, 5, 180*time.Second)
Expect(cond).To(BeTrue())
})
}
// getTestAttributes returns a URL, Egress policy, SMI HTTPRouteGroup for the given test scenario and client
func getTestAttributes(srcSvcAcc corev1.ServiceAccount, scenario testScenario) (string, *policyV1alpha1.Egress, *smiSpecs.HTTPRouteGroup) {
switch scenario {
case httpEgressNoRouteMatches:
return "http://httpbin.org:80/status/200", &policyV1alpha1.Egress{
TypeMeta: metav1.TypeMeta{
Kind: "Egress",
APIVersion: "policy.flomesh.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin-80",
Namespace: srcSvcAcc.Namespace,
},
Spec: policyV1alpha1.EgressSpec{
Sources: []policyV1alpha1.EgressSourceSpec{
{
Kind: "ServiceAccount",
Name: srcSvcAcc.Name,
Namespace: srcSvcAcc.Namespace,
},
},
Hosts: []string{
"httpbin.org",
},
Ports: []policyV1alpha1.PortSpec{
{
Number: 80,
Protocol: "http",
},
},
},
}, nil
case httpEgressWithRouteMatches:
return "http://httpbin.org:80/status/200", &policyV1alpha1.Egress{
TypeMeta: metav1.TypeMeta{
Kind: "Egress",
APIVersion: "policy.flomesh.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin-80",
Namespace: srcSvcAcc.Namespace,
},
Spec: policyV1alpha1.EgressSpec{
Sources: []policyV1alpha1.EgressSourceSpec{
{
Kind: "ServiceAccount",
Name: srcSvcAcc.Name,
Namespace: srcSvcAcc.Namespace,
},
},
Hosts: []string{
"httpbin.org",
},
Ports: []policyV1alpha1.PortSpec{
{
Number: 80,
Protocol: "http",
},
},
Matches: []corev1.TypedLocalObjectReference{
{
APIGroup: pointer.StringPtr("specs.smi-spec.io/v1alpha4"),
Kind: "HTTPRouteGroup",
Name: "httpbin-status",
},
},
},
}, &smiSpecs.HTTPRouteGroup{
TypeMeta: metav1.TypeMeta{
Kind: "Egress",
APIVersion: "policy.flomesh.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin-status",
Namespace: srcSvcAcc.Namespace,
},
Spec: smiSpecs.HTTPRouteGroupSpec{
Matches: []smiSpecs.HTTPMatch{
{
Name: "status",
PathRegex: "/status.*",
},
},
},
}
case httpsEgress:
return "https://httpbin.org:443/status/200", &policyV1alpha1.Egress{
TypeMeta: metav1.TypeMeta{
Kind: "Egress",
APIVersion: "policy.flomesh.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin-443",
Namespace: srcSvcAcc.Namespace,
},
Spec: policyV1alpha1.EgressSpec{
Sources: []policyV1alpha1.EgressSourceSpec{
{
Kind: "ServiceAccount",
Name: srcSvcAcc.Name,
Namespace: srcSvcAcc.Namespace,
},
},
Hosts: []string{
"httpbin.org",
},
Ports: []policyV1alpha1.PortSpec{
{
Number: 443,
Protocol: "https",
},
},
},
}, nil
case tcpEgress:
return "https://httpbin.org:443/status/200", &policyV1alpha1.Egress{
TypeMeta: metav1.TypeMeta{
Kind: "Egress",
APIVersion: "policy.flomesh.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin-443",
Namespace: srcSvcAcc.Namespace,
},
Spec: policyV1alpha1.EgressSpec{
Sources: []policyV1alpha1.EgressSourceSpec{
{
Kind: "ServiceAccount",
Name: srcSvcAcc.Name,
Namespace: srcSvcAcc.Namespace,
},
},
Ports: []policyV1alpha1.PortSpec{
{
Number: 443,
Protocol: "tcp",
},
},
},
}, nil
default:
Td.T.Fatal("Unsupportes test scenario: %v", scenario)
return "", nil, nil
}
}