-
Notifications
You must be signed in to change notification settings - Fork 91
/
egress_gateway_origination_test.go
285 lines (253 loc) · 11.8 KB
/
egress_gateway_origination_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
// +build integ
// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdstlsorigination
import (
"fmt"
"reflect"
"strings"
"testing"
"time"
"istio.io/istio/pkg/test/echo/common/response"
"istio.io/istio/pkg/test/framework"
"istio.io/istio/pkg/test/framework/components/echo"
"istio.io/istio/pkg/test/framework/components/istio"
"istio.io/istio/pkg/test/framework/components/namespace"
"istio.io/istio/pkg/test/util/retry"
ingressutil "istio.io/istio/tests/integration/security/sds_ingress/util"
sdstlsutil "istio.io/istio/tests/integration/security/sds_tls_origination/util"
)
// TestSimpleTlsOrigination test SIMPLE TLS mode with TLS origination happening at Gateway proxy
// It uses CredentialName set in DestinationRule API to fetch secrets from k8s API server
func TestSimpleTlsOrigination(t *testing.T) {
framework.NewTest(t).
Features("security.egress.tls.sds").
Run(func(ctx framework.TestContext) {
var (
credName = "tls-credential-cacert"
fakeCredName = "fake-tls-credential-cacert"
credNameMissing = "tls-credential-not-created-cacert"
)
var credentialA = sdstlsutil.TLSCredential{
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
var CredentialB = sdstlsutil.TLSCredential{
CaCert: sdstlsutil.FakeRoot,
}
// Add kubernetes secret to provision key/cert for gateway.
sdstlsutil.CreateKubeSecret(ctx, []string{credName}, "SIMPLE", credentialA, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{credName})
// Add kubernetes secret to provision key/cert for gateway.
sdstlsutil.CreateKubeSecret(ctx, []string{fakeCredName}, "SIMPLE", CredentialB, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{fakeCredName})
internalClient, externalServer, _, serverNamespace := sdstlsutil.SetupEcho(t, ctx)
// Set up Host Namespace
host := "server." + serverNamespace.Name() + ".svc.cluster.local"
testCases := map[string]struct {
response []string
credentialToUse string
gateway bool // true if the request is expected to be routed through gateway
}{
// Use CA certificate stored as k8s secret with the same issuing CA as server's CA.
// This root certificate can validate the server cert presented by the echoboot server instance.
"Simple TLS with Correct Root Cert": {
response: []string{response.StatusCodeOK},
credentialToUse: strings.TrimSuffix(credName, "-cacert"),
gateway: true,
},
// Use CA certificate stored as k8s secret with different issuing CA as server's CA.
// This root certificate cannot validate the server cert presented by the echoboot server instance.
"Simple TLS with Fake Root Cert": {
response: []string{response.StatusCodeUnavailable},
credentialToUse: strings.TrimSuffix(fakeCredName, "-cacert"),
gateway: false,
},
// Set up an UpstreamCluster with a CredentialName when secret doesn't even exist in istio-system ns.
// Secret fetching error at Gateway, results in a 503 response.
"Simple TLS with credentialName set when the underlying secret doesn't exist": {
response: []string{response.StatusCodeUnavailable},
credentialToUse: strings.TrimSuffix(credNameMissing, "-cacert"),
gateway: false,
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
bufDestinationRule := sdstlsutil.CreateDestinationRule(t, serverNamespace, "SIMPLE", tc.credentialToUse)
// Get namespace for gateway pod.
istioCfg := istio.DefaultConfigOrFail(ctx, ctx)
systemNS := namespace.ClaimOrFail(ctx, ctx, istioCfg.SystemNamespace)
ctx.Config().ApplyYAMLOrFail(ctx, systemNS.Name(), bufDestinationRule.String())
defer ctx.Config().DeleteYAMLOrFail(ctx, systemNS.Name(), bufDestinationRule.String())
retry.UntilSuccessOrFail(t, func() error {
resp, err := internalClient.Call(echo.CallOptions{
Target: externalServer,
PortName: "http",
Headers: map[string][]string{
"Host": {host},
},
})
if err != nil {
return fmt.Errorf("request failed: %v", err)
}
codes := make([]string, 0, len(resp))
for _, r := range resp {
codes = append(codes, r.Code)
}
if !reflect.DeepEqual(codes, tc.response) {
return fmt.Errorf("got codes %q, expected %q", codes, tc.response)
}
for _, r := range resp {
if _, f := r.RawResponse["Handled-By-Egress-Gateway"]; tc.gateway && !f {
return fmt.Errorf("expected to be handled by gateway. response: %+v", r.RawResponse)
}
}
return nil
}, retry.Delay(time.Second*1), retry.Timeout(time.Minute*2))
})
}
})
}
// TestMutualTlsOrigination test MUTUAL TLS mode with TLS origination happening at Gateway proxy
// It uses CredentialName set in DestinationRule API to fetch secrets from k8s API server
func TestMutualTlsOrigination(t *testing.T) {
framework.NewTest(t).
Features("security.egress.mtls.sds").
Run(func(ctx framework.TestContext) {
var (
credNameGeneric = "mtls-credential-generic"
credNameNotGeneric = "mtls-credential-not-generic"
fakeCredNameA = "fake-mtls-credential-a"
fakeCredNameB = "fake-mtls-credential-b"
credNameMissing = "mtls-credential-not-created"
simpleCredName = "tls-credential-simple-cacert"
)
var credentialASimple = sdstlsutil.TLSCredential{
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
var credentialAGeneric = sdstlsutil.TLSCredential{
ClientCert: sdstlsutil.MustReadCert(t, "cert-chain.pem"),
PrivateKey: sdstlsutil.MustReadCert(t, "key.pem"),
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
var credentialANonGeneric = sdstlsutil.TLSCredential{
ClientCert: sdstlsutil.MustReadCert(t, "cert-chain.pem"),
PrivateKey: sdstlsutil.MustReadCert(t, "key.pem"),
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
// Configured with an invalid ClientCert
var credentialBCert = sdstlsutil.TLSCredential{
ClientCert: sdstlsutil.FakeCert,
PrivateKey: sdstlsutil.MustReadCert(t, "key.pem"),
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
// Configured with an invalid ClientCert and PrivateKey
var credentialBCertAndKey = sdstlsutil.TLSCredential{
ClientCert: sdstlsutil.FakeCert,
PrivateKey: sdstlsutil.FakeKey,
CaCert: sdstlsutil.MustReadCert(t, "root-cert.pem"),
}
// Add kubernetes secret to provision key/cert for gateway.
sdstlsutil.CreateKubeSecret(ctx, []string{credNameGeneric}, "MUTUAL", credentialAGeneric, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{credNameGeneric})
sdstlsutil.CreateKubeSecret(ctx, []string{credNameNotGeneric}, "MUTUAL", credentialANonGeneric, true)
defer ingressutil.DeleteKubeSecret(ctx, []string{credNameNotGeneric})
sdstlsutil.CreateKubeSecret(ctx, []string{fakeCredNameA}, "MUTUAL", credentialBCert, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{fakeCredNameA})
sdstlsutil.CreateKubeSecret(ctx, []string{fakeCredNameB}, "MUTUAL", credentialBCertAndKey, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{fakeCredNameB})
sdstlsutil.CreateKubeSecret(ctx, []string{simpleCredName}, "SIMPLE", credentialASimple, false)
defer ingressutil.DeleteKubeSecret(ctx, []string{simpleCredName})
internalClient, externalServer, _, serverNamespace := sdstlsutil.SetupEcho(t, ctx)
// Set up Host Namespace
host := "server." + serverNamespace.Name() + ".svc.cluster.local"
testCases := map[string]struct {
response []string
credentialToUse string
gateway bool // true if the request is expected to be routed through gateway
}{
// Use CA certificate and client certs stored as k8s secret with the same issuing CA as server's CA.
// This root certificate can validate the server cert presented by the echoboot server instance and server CA can
// validate the client cert. Secret is of type generic.
"MUTUAL TLS with correct root cert and client certs and generic secret type": {
response: []string{response.StatusCodeOK},
credentialToUse: strings.TrimSuffix(credNameGeneric, "-cacert"),
gateway: true,
},
// Use CA certificate and client certs stored as k8s secret with the same issuing CA as server's CA.
// This root certificate can validate the server cert presented by the echoboot server instance and server CA can
// validate the client cert. Secret is not of type generic.
"MUTUAL TLS with correct root cert and client certs and non generic secret type": {
response: []string{response.StatusCodeOK},
credentialToUse: strings.TrimSuffix(credNameNotGeneric, "-cacert"),
gateway: true,
},
// Use CA certificate and client certs stored as k8s secret with the same issuing CA as server's CA.
// This root certificate can validate the server cert presented by the echoboot server instance and server CA
// cannot validate the client cert. Returns 503 response as TLS handshake fails.
"MUTUAL TLS with correct root cert but invalid client cert": {
response: []string{response.StatusCodeUnavailable},
credentialToUse: strings.TrimSuffix(fakeCredNameA, "-cacert"),
gateway: false,
},
// Set up an UpstreamCluster with a CredentialName when secret doesn't even exist in istio-system ns.
// Secret fetching error at Gateway, results in a 503 response.
"MUTUAL TLS with credentialName set when the underlying secret doesn't exist": {
response: []string{response.StatusCodeUnavailable},
credentialToUse: strings.TrimSuffix(credNameMissing, "-cacert"),
gateway: false,
},
"MUTUAL TLS with correct root cert but no client certs": {
response: []string{response.StatusCodeUnavailable},
credentialToUse: strings.TrimSuffix(simpleCredName, "-cacert"),
gateway: false,
},
}
for name, tc := range testCases {
ctx.NewSubTest(name).
Run(func(ctx framework.TestContext) {
bufDestinationRule := sdstlsutil.CreateDestinationRule(t, serverNamespace, "MUTUAL", tc.credentialToUse)
// Get namespace for gateway pod.
istioCfg := istio.DefaultConfigOrFail(t, ctx)
systemNS := namespace.ClaimOrFail(ctx, ctx, istioCfg.SystemNamespace)
ctx.Config().ApplyYAMLOrFail(ctx, systemNS.Name(), bufDestinationRule.String())
defer ctx.Config().DeleteYAMLOrFail(ctx, systemNS.Name(), bufDestinationRule.String())
retry.UntilSuccessOrFail(t, func() error {
resp, err := internalClient.Call(echo.CallOptions{
Target: externalServer,
PortName: "http",
Headers: map[string][]string{
"Host": {host},
},
})
if err != nil {
return fmt.Errorf("request failed: %v", err)
}
codes := make([]string, 0, len(resp))
for _, r := range resp {
codes = append(codes, r.Code)
}
if !reflect.DeepEqual(codes, tc.response) {
return fmt.Errorf("got codes %q, expected %q", codes, tc.response)
}
for _, r := range resp {
if _, f := r.RawResponse["Handled-By-Egress-Gateway"]; tc.gateway && !f {
return fmt.Errorf("expected to be handled by gateway. response: %+v", r.RawResponse)
}
}
return nil
}, retry.Delay(time.Second*1), retry.Timeout(time.Minute*2))
})
}
})
}