@@ -25,6 +25,35 @@ import (
25
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
26
)
27
27
28
+ var (
29
+ // TLS test variables.
30
+ crKind = "Pod"
31
+ crName = "example-pod"
32
+ certName = "app-cert"
33
+
34
+ caConfigMapAndSecretName = tlsutil .ToCASecretAndConfigMapName (crKind , crName )
35
+ caConfigMap = & v1.ConfigMap {
36
+ ObjectMeta : metav1.ObjectMeta {
37
+ Name : caConfigMapAndSecretName ,
38
+ },
39
+ }
40
+ caSecret = & v1.Secret {
41
+ ObjectMeta : metav1.ObjectMeta {
42
+ Name : caConfigMapAndSecretName ,
43
+ },
44
+ }
45
+
46
+ appSecret = & v1.Secret {
47
+ ObjectMeta : metav1.ObjectMeta {
48
+ Name : tlsutil .ToAppSecretName (crKind , crName , certName ),
49
+ },
50
+ }
51
+
52
+ ccfg = & tlsutil.CertConfig {
53
+ CertName : certName ,
54
+ }
55
+ )
56
+
28
57
// TestBothAppAndCATLSAssetsExist ensures that when both application
29
58
// and CA TLS assets exist in the k8s cluster for a given cr,
30
59
// the GenerateCert() simply returns those to the caller.
@@ -37,9 +66,23 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
37
66
t .Fatal (err )
38
67
}
39
68
69
+ appSecret , err := f .KubeClient .CoreV1 ().Secrets (namespace ).Create (appSecret )
70
+ if err != nil {
71
+ t .Fatal (err )
72
+ }
73
+
74
+ caConfigMap , err := f .KubeClient .CoreV1 ().ConfigMaps (namespace ).Create (caConfigMap )
75
+ if err != nil {
76
+ t .Fatal (err )
77
+ }
78
+
79
+ caSecret , err := f .KubeClient .CoreV1 ().Secrets (namespace ).Create (caSecret )
80
+ if err != nil {
81
+ t .Fatal (err )
82
+ }
83
+
84
+ cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
40
85
// Use Pod as a dummy runtime object for the CR input of GenerateCert().
41
- crKind := "Pod"
42
- crName := "example-pod"
43
86
mCR := & v1.Pod {
44
87
TypeMeta : metav1.TypeMeta {
45
88
Kind : crKind ,
@@ -49,55 +92,53 @@ func TestBothAppAndCATLSAssetsExist(t *testing.T) {
49
92
Namespace : namespace ,
50
93
},
51
94
}
52
-
53
- certName := "app-cert"
54
- appSecret := & v1.Secret {
55
- ObjectMeta : metav1.ObjectMeta {
56
- Name : tlsutil .ToAppSecretName (crKind , crName , certName ),
57
- },
58
- }
59
- appSecret , err = f .KubeClient .CoreV1 ().Secrets (namespace ).Create (appSecret )
95
+ actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (mCR , nil , ccfg )
60
96
if err != nil {
61
97
t .Fatal (err )
62
98
}
63
99
64
- caConfigMapAndSecretName := tlsutil .ToCASecretAndConfigMapName (crKind , crName )
65
- caConfigMap := & v1.ConfigMap {
66
- ObjectMeta : metav1.ObjectMeta {
67
- Name : caConfigMapAndSecretName ,
68
- },
100
+ if ! reflect .DeepEqual (appSecret , actualAppSecret ) {
101
+ t .Fatalf ("expect %+v, but got %+v" , appSecret , actualAppSecret )
69
102
}
70
- caConfigMap , err = f .KubeClient .CoreV1 ().ConfigMaps (namespace ).Create (caConfigMap )
71
- if err != nil {
72
- t .Fatal (err )
103
+ if ! reflect .DeepEqual (caConfigMap , actualCaConfigMap ) {
104
+ t .Fatalf ("expect %+v, but got %+v" , caConfigMap , actualCaConfigMap )
73
105
}
74
-
75
- caSecret := & v1.Secret {
76
- ObjectMeta : metav1.ObjectMeta {
77
- Name : caConfigMapAndSecretName ,
78
- },
106
+ if ! reflect .DeepEqual (caSecret , actualCaSecret ) {
107
+ t .Fatalf ("expect %+v, but got %+v" , caSecret , actualCaSecret )
79
108
}
80
- caSecret , err = f .KubeClient .CoreV1 ().Secrets (namespace ).Create (caSecret )
109
+ }
110
+
111
+ // 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.
112
+ func TestOnlyAppSecretExist (t * testing.T ) {
113
+ f := framework .Global
114
+ ctx := f .NewTestCtx (t )
115
+ defer ctx .Cleanup (t )
116
+ namespace , err := ctx .GetNamespace ()
81
117
if err != nil {
82
118
t .Fatal (err )
83
119
}
84
120
85
- cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
86
- ccfg := & tlsutil.CertConfig {
87
- CertName : certName ,
88
- }
89
- actualAppSecret , actualCaConfigMap , actualCaSecret , err := cg .GenerateCert (mCR , nil , ccfg )
121
+ _ , err = f .KubeClient .CoreV1 ().Secrets (namespace ).Create (appSecret )
90
122
if err != nil {
91
123
t .Fatal (err )
92
124
}
93
125
94
- if ! reflect .DeepEqual (appSecret , actualAppSecret ) {
95
- t .Fatalf ("expect %+v, got %+v" , appSecret , actualAppSecret )
126
+ cg := tlsutil .NewSDKCertGenerator (f .KubeClient )
127
+ // Use Pod as a dummy runtime object for the CR input of GenerateCert().
128
+ mCR := & v1.Pod {
129
+ TypeMeta : metav1.TypeMeta {
130
+ Kind : crKind ,
131
+ },
132
+ ObjectMeta : metav1.ObjectMeta {
133
+ Name : crName ,
134
+ Namespace : namespace ,
135
+ },
96
136
}
97
- if ! reflect .DeepEqual (caConfigMap , actualCaConfigMap ) {
98
- t .Fatalf ("expect %+v, got %+v" , caConfigMap , actualCaConfigMap )
137
+ _ , _ , _ , err = cg .GenerateCert (mCR , nil , ccfg )
138
+ if err == nil {
139
+ t .Fatal ("expect error, but got none" )
99
140
}
100
- if ! reflect . DeepEqual ( caSecret , actualCaSecret ) {
101
- t .Fatalf ("expect %+ v, got %+ v" , caSecret , actualCaSecret )
141
+ if err != tlsutil . ErrCANotFound {
142
+ t .Fatalf ("expect %v, but got %v" , tlsutil . ErrCANotFound . Error (), err . Error () )
102
143
}
103
144
}
0 commit comments