forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_util_test.go
337 lines (296 loc) · 10.5 KB
/
tls_util_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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2018 The Operator-SDK 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 e2e
import (
"io/ioutil"
"reflect"
"testing"
framework "github.com/operator-framework/operator-sdk/pkg/test"
tlsutil "github.com/operator-framework/operator-sdk/pkg/tls"
"k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var (
// TLS test variables.
crKind = "Pod"
crName = "example-pod"
certName = "app-cert"
caConfigMapAndSecretName = tlsutil.ToCASecretAndConfigMapName(crKind, crName)
appSecretName = tlsutil.ToAppSecretName(crKind, crName, certName)
caConfigMap *v1.ConfigMap
caSecret *v1.Secret
appSecret *v1.Secret
ccfg *tlsutil.CertConfig
)
// setup test variables.
func init() {
caCertBytes, err := ioutil.ReadFile("./testdata/ca.crt")
if err != nil {
panic(err)
}
caConfigMap = &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: caConfigMapAndSecretName,
},
Data: map[string]string{tlsutil.TLSCACertKey: string(caCertBytes)},
}
caKeyBytes, err := ioutil.ReadFile("./testdata/ca.key")
if err != nil {
panic(err)
}
caSecret = &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: caConfigMapAndSecretName,
},
Data: map[string][]byte{tlsutil.TLSPrivateCAKeyKey: caKeyBytes},
}
appSecret = &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: appSecretName,
},
}
ccfg = &tlsutil.CertConfig{
CertName: certName,
}
}
// TestBothAppAndCATLSAssetsExist ensures that when both application
// and CA TLS assets exist in the k8s cluster for a given cr,
// the GenerateCert() simply returns those to the caller.
func TestBothAppAndCATLSAssetsExist(t *testing.T) {
ctx := framework.NewTestCtx(t)
defer ctx.Cleanup()
namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
appSecret, err := f.KubeClient.CoreV1().Secrets(namespace).Create(appSecret)
if err != nil {
t.Fatal(err)
}
caConfigMap, err := f.KubeClient.CoreV1().ConfigMaps(namespace).Create(caConfigMap)
if err != nil {
t.Fatal(err)
}
caSecret, err := f.KubeClient.CoreV1().Secrets(namespace).Create(caSecret)
if err != nil {
t.Fatal(err)
}
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
actualAppSecret, actualCaConfigMap, actualCaSecret, err := cg.GenerateCert(newDummyCR(namespace), nil, ccfg)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(appSecret, actualAppSecret) {
t.Fatalf("Expect %+v, but got %+v", appSecret, actualAppSecret)
}
if !reflect.DeepEqual(caConfigMap, actualCaConfigMap) {
t.Fatalf("Expect %+v, but got %+v", caConfigMap, actualCaConfigMap)
}
if !reflect.DeepEqual(caSecret, actualCaSecret) {
t.Fatalf("Expect %+v, but got %+v", caSecret, actualCaSecret)
}
}
// TestOnlyAppSecretExist tests a case where the application TLS asset exists but its
// correspoding CA asset doesn't. In this case, CertGenerator can't genereate a new CA because
// it won't verify the existing application TLS cert. Therefore, CertGenerator can't proceed
// and returns an error to the caller.
func TestOnlyAppSecretExist(t *testing.T) {
ctx := framework.NewTestCtx(t)
defer ctx.Cleanup()
namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
_, err = f.KubeClient.CoreV1().Secrets(namespace).Create(appSecret)
if err != nil {
t.Fatal(err)
}
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
_, _, _, err = cg.GenerateCert(newDummyCR(namespace), nil, ccfg)
if err == nil {
t.Fatal("Expect error, but got none")
}
if err != tlsutil.ErrCANotFound {
t.Fatalf("Expect %v, but got %v", tlsutil.ErrCANotFound.Error(), err.Error())
}
}
// TestOnlyCAExist tests the case where only the CA exists in the cluster;
// GenerateCert can retrieve the CA and uses it to create a new application secret.
func TestOnlyCAExist(t *testing.T) {
ctx := framework.NewTestCtx(t)
defer ctx.Cleanup()
namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
_, err = f.KubeClient.CoreV1().ConfigMaps(namespace).Create(caConfigMap)
if err != nil {
t.Fatal(err)
}
_, err = f.KubeClient.CoreV1().Secrets(namespace).Create(caSecret)
if err != nil {
t.Fatal(err)
}
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
appSecret, _, _, err := cg.GenerateCert(newDummyCR(namespace), newAppSvc(namespace), ccfg)
if err != nil {
t.Fatal(err)
}
verifyAppSecret(t, appSecret, namespace)
}
// TestNoneOfCaAndAppSecretExist ensures that when none of the CA and Application TLS assets
// exist, GenerateCert() creates both and put them into the k8s cluster.
func TestNoneOfCaAndAppSecretExist(t *testing.T) {
ctx := framework.NewTestCtx(t)
defer ctx.Cleanup()
namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
appSecret, caConfigMap, caSecret, err := cg.GenerateCert(newDummyCR(namespace), newAppSvc(namespace), ccfg)
if err != nil {
t.Fatal(err)
}
verifyAppSecret(t, appSecret, namespace)
verifyCaConfigMap(t, caConfigMap, namespace)
verifyCASecret(t, caSecret, namespace)
}
// TestCustomCA ensures that if a user provides a custom Key and Cert and the CA and Application TLS assets
// do not exist, the GenerateCert method can use the custom CA to generate the TLS assest.
func TestCustomCA(t *testing.T) {
ctx := framework.NewTestCtx(t)
defer ctx.Cleanup()
namespace, err := ctx.GetNamespace()
if err != nil {
t.Fatal(err)
}
f := framework.Global
cg := tlsutil.NewSDKCertGenerator(f.KubeClient)
customConfig := &tlsutil.CertConfig{
CertName: certName,
CAKey: "testdata/ca.key",
CACert: "testdata/ca.crt",
}
appSecret, _, _, err := cg.GenerateCert(newDummyCR(namespace), newAppSvc(namespace), customConfig)
if err != nil {
t.Fatal(err)
}
verifyAppSecret(t, appSecret, namespace)
// ensure caConfigMap does not exist in k8s cluster.
_, err = framework.Global.KubeClient.CoreV1().Secrets(namespace).Get(caConfigMapAndSecretName, metav1.GetOptions{})
if !apiErrors.IsNotFound(err) {
t.Fatal(err)
}
// ensure caConfigMap does not exist in k8s cluster.
_, err = framework.Global.KubeClient.CoreV1().Secrets(namespace).Get(caConfigMapAndSecretName, metav1.GetOptions{})
if !apiErrors.IsNotFound(err) {
t.Fatal(err)
}
}
func verifyCASecret(t *testing.T, caSecret *v1.Secret, namespace string) {
// check if caConfigMap has the correct fields.
if caConfigMapAndSecretName != caSecret.Name {
t.Fatalf("Expect the ca config name %v, but got %v", caConfigMapAndSecretName, caConfigMap.Name)
}
if namespace != caSecret.Namespace {
t.Fatalf("Expect the ca config namespace %v, but got %v", namespace, appSecret.Namespace)
}
if _, ok := caSecret.Data[tlsutil.TLSPrivateCAKeyKey]; !ok {
t.Fatalf("Expect the ca config to have the data field %v, but got none", tlsutil.TLSPrivateCAKeyKey)
}
// check if caConfigMap exists in k8s cluster.
caSecretFromCluster, err := framework.Global.KubeClient.CoreV1().Secrets(namespace).Get(caConfigMapAndSecretName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
// check if caSecret returned from GenerateCert is the same as the one that exists in the k8s.
if !reflect.DeepEqual(caSecret, caSecretFromCluster) {
t.Fatalf("Expect %+v, but got %+v", caSecret, caSecretFromCluster)
}
}
func verifyCaConfigMap(t *testing.T, caConfigMap *v1.ConfigMap, namespace string) {
// check if caConfigMap has the correct fields.
if caConfigMapAndSecretName != caConfigMap.Name {
t.Fatalf("Expect the ca config name %v, but got %v", caConfigMapAndSecretName, caConfigMap.Name)
}
if namespace != caConfigMap.Namespace {
t.Fatalf("Expect the ca config namespace %v, but got %v", namespace, appSecret.Namespace)
}
if _, ok := caConfigMap.Data[tlsutil.TLSCACertKey]; !ok {
t.Fatalf("Expect the ca config to have the data field %v, but got none", tlsutil.TLSCACertKey)
}
// check if caConfigMap exists in k8s cluster.
caConfigMapFromCluster, err := framework.Global.KubeClient.CoreV1().ConfigMaps(namespace).Get(caConfigMapAndSecretName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
// check if caConfigMap returned from GenerateCert is the same as the one that exists in the k8s.
if !reflect.DeepEqual(caConfigMap, caConfigMapFromCluster) {
t.Fatalf("Expect %+v, but got %+v", caConfigMap, caConfigMapFromCluster)
}
}
func verifyAppSecret(t *testing.T, appSecret *v1.Secret, namespace string) {
// check if appSecret has the correct fields.
if appSecretName != appSecret.Name {
t.Fatalf("Expect the secret name %v, but got %v", appSecretName, appSecret.Name)
}
if namespace != appSecret.Namespace {
t.Fatalf("Expect the secret namespace %v, but got %v", namespace, appSecret.Namespace)
}
if v1.SecretTypeTLS != appSecret.Type {
t.Fatalf("Expect the secret type %v, but got %v", v1.SecretTypeTLS, appSecret.Type)
}
if _, ok := appSecret.Data[v1.TLSCertKey]; !ok {
t.Fatalf("Expect the secret to have the data field %v, but got none", v1.TLSCertKey)
}
if _, ok := appSecret.Data[v1.TLSPrivateKeyKey]; !ok {
t.Fatalf("Expect the secret to have the data field %v, but got none", v1.TLSPrivateKeyKey)
}
// check if appSecret exists in k8s cluster.
appSecretFromCluster, err := framework.Global.KubeClient.CoreV1().Secrets(namespace).Get(appSecretName, metav1.GetOptions{})
if err != nil {
t.Fatal(err)
}
// check if appSecret returned from GenerateCert is the same as the one that exists in the k8s.
if !reflect.DeepEqual(appSecret, appSecretFromCluster) {
t.Fatalf("Expect %+v, but got %+v", appSecret, appSecretFromCluster)
}
}
// newDummyCR returns a dummy runtime object for the CR input of GenerateCert().
func newDummyCR(namespace string) runtime.Object {
return &v1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: crKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Namespace: namespace,
},
}
}
func newAppSvc(namespace string) *v1.Service {
return &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "app-service",
Namespace: namespace,
},
}
}