-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathreconcile_tls_test.go
More file actions
303 lines (255 loc) · 10.1 KB
/
reconcile_tls_test.go
File metadata and controls
303 lines (255 loc) · 10.1 KB
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 controllers_test
import (
"context"
"fmt"
"github.com/rabbitmq/cluster-operator/internal/status"
"k8s.io/utils/pointer"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
var _ = Describe("Reconcile TLS", func() {
var (
cluster *rabbitmqv1beta1.RabbitmqCluster
defaultNamespace = "default"
ctx = context.Background()
)
Context("Mutual TLS", func() {
AfterEach(func() {
Expect(client.Delete(ctx, cluster)).To(Succeed())
Eventually(func() bool {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
return apierrors.IsNotFound(err)
}, 5).Should(BeTrue())
})
Context("Mutual TLS with single secret", func() {
It("Deploys successfully", func() {
tlsSecretWithCACert(ctx, "tls-secret", defaultNamespace)
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "tls-secret",
CaSecretName: "tls-secret",
}
cluster = rabbitmqClusterWithTLS(ctx, "mutual-tls-success", defaultNamespace, tlsSpec)
waitForClusterCreation(ctx, cluster, client)
sts, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(sts.Spec.Template.Spec.Volumes).To(ContainElement(corev1.Volume{
Name: "rabbitmq-tls",
VolumeSource: corev1.VolumeSource{
Projected: &corev1.ProjectedVolumeSource{
Sources: []corev1.VolumeProjection{
{
Secret: &corev1.SecretProjection{
LocalObjectReference: corev1.LocalObjectReference{
Name: "tls-secret",
},
Optional: pointer.BoolPtr(true),
},
},
},
DefaultMode: pointer.Int32Ptr(400),
},
},
}))
Expect(extractContainer(sts.Spec.Template.Spec.Containers, "rabbitmq").VolumeMounts).To(ContainElement(corev1.VolumeMount{
Name: "rabbitmq-tls",
MountPath: "/etc/rabbitmq-tls/",
ReadOnly: true,
}))
})
It("Does not deploy if the cert name does not match the contents of the secret", func() {
tlsData := map[string]string{
"tls.crt": "this is a tls cert",
"tls.key": "this is a tls key",
"wrong-key": "certificate",
}
_, err := createSecret(ctx, "tls-secret-missing", defaultNamespace, tlsData)
if !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred())
}
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "tls-secret-missing",
CaSecretName: "tls-secret-missing",
}
cluster = rabbitmqClusterWithTLS(ctx, "tls-secret-missing", defaultNamespace, tlsSpec)
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret tls-secret-missing in namespace %s does not have the field ca.crt", defaultNamespace))
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
})
})
Context("Mutual TLS with a separate CA certificate secret", func() {
It("Does not deploy the RabbitmqCluster, and retries every 10 seconds", func() {
tlsSecretWithoutCACert(ctx, "rabbitmq-tls-secret-does-not-exist", defaultNamespace)
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "rabbitmq-tls-secret-does-not-exist",
CaSecretName: "ca-cert-secret",
}
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-tls-secret-does-not-exist", defaultNamespace, tlsSpec)
verifyTLSErrorEvents(ctx, cluster, "Failed to get CA certificate secret")
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
Expect(err).To(HaveOccurred())
// create missing secret
caData := map[string]string{
"ca.crt": "this is a ca cert",
}
_, err = createSecret(ctx, "ca-cert-secret", defaultNamespace, caData)
Expect(err).NotTo(HaveOccurred())
waitForClusterCreation(ctx, cluster, client)
statefulSet(ctx, cluster)
})
It("Does not deploy if the cert name does not match the contents of the secret", func() {
tlsSecretWithoutCACert(ctx, "tls-secret", defaultNamespace)
caData := map[string]string{
"cacrt": "this is a ca cert",
}
_, err := createSecret(ctx, "ca-cert-secret-invalid", defaultNamespace, caData)
if !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred())
}
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "tls-secret",
CaSecretName: "ca-cert-secret-invalid",
}
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-mutual-tls-missing", defaultNamespace, tlsSpec)
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret ca-cert-secret-invalid in namespace %s does not have the field ca.crt", defaultNamespace))
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
})
})
})
Context("TLS set on the instance", func() {
AfterEach(func() {
Expect(client.Delete(ctx, cluster)).To(Succeed())
Eventually(func() bool {
rmq := &rabbitmqv1beta1.RabbitmqCluster{}
err := client.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, rmq)
return apierrors.IsNotFound(err)
}, 5).Should(BeTrue())
})
BeforeEach(func() {
tlsSecretWithoutCACert(ctx, "tls-secret", defaultNamespace)
})
It("Deploys successfully", func() {
cluster = &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "rabbitmq-tls",
Namespace: defaultNamespace,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
TLS: rabbitmqv1beta1.TLSSpec{
SecretName: "tls-secret",
},
},
}
Expect(client.Create(ctx, cluster)).To(Succeed())
waitForClusterCreation(ctx, cluster, client)
})
When("the TLS secret does not have the expected keys - tls.crt, or tls.key", func() {
BeforeEach(func() {
secretData := map[string]string{
"somekey": "someval",
"tls.key": "this is a tls key",
}
_, err := createSecret(ctx, "rabbitmq-tls-malformed", defaultNamespace, secretData)
if !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred())
}
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "rabbitmq-tls-malformed",
}
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-tls-malformed", defaultNamespace, tlsSpec)
})
It("fails to deploy the RabbitmqCluster", func() {
verifyTLSErrorEvents(ctx, cluster, fmt.Sprintf("TLS secret rabbitmq-tls-malformed in namespace %s does not have the fields tls.crt and tls.key", defaultNamespace))
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
})
})
When("the TLS secret does not exist", func() {
It("fails to deploy the RabbitmqCluster until the secret is detected", func() {
tlsSpec := rabbitmqv1beta1.TLSSpec{
SecretName: "tls-secret-does-not-exist",
}
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-tls-secret-does-not-exist", defaultNamespace, tlsSpec)
verifyTLSErrorEvents(ctx, cluster, "Failed to get TLS secret")
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
Expect(err).To(HaveOccurred())
// create missing secret
secretData := map[string]string{
"tls.crt": "this is a tls cert",
"tls.key": "this is a tls key",
}
_, err = createSecret(ctx, "tls-secret-does-not-exist", defaultNamespace, secretData)
Expect(err).NotTo(HaveOccurred())
waitForClusterCreation(ctx, cluster, client)
statefulSet(ctx, cluster)
})
})
})
When("DiableNonTLSListeners set to true", func() {
It("logs TLSError and set ReconcileSuccess to false when TLS is not enabled", func() {
tlsSpec := rabbitmqv1beta1.TLSSpec{
DisableNonTLSListeners: true,
}
cluster = rabbitmqClusterWithTLS(ctx, "rabbitmq-disablenontlslisteners", defaultNamespace, tlsSpec)
verifyTLSErrorEvents(ctx, cluster, "TLS must be enabled if disableNonTLSListeners is set to true")
_, err := clientSet.AppsV1().StatefulSets(cluster.Namespace).Get(ctx, cluster.ChildResourceName("server"), metav1.GetOptions{})
Expect(err).To(HaveOccurred())
verifyReconcileSuccessFalse(cluster.Name, cluster.Namespace)
})
})
})
func verifyReconcileSuccessFalse(name, namespace string) bool {
return EventuallyWithOffset(1, func() string {
rabbit := &rabbitmqv1beta1.RabbitmqCluster{}
Expect(client.Get(ctx, runtimeClient.ObjectKey{
Name: name,
Namespace: namespace,
}, rabbit)).To(Succeed())
for i := range rabbit.Status.Conditions {
if rabbit.Status.Conditions[i].Type == status.ReconcileSuccess {
return fmt.Sprintf("ReconcileSuccess status: %s", rabbit.Status.Conditions[i].Status)
}
}
return "ReconcileSuccess status: condition not present"
}, 5).Should(Equal("ReconcileSuccess status: False"))
}
func tlsSecretWithCACert(ctx context.Context, secretName, namespace string) {
tlsData := map[string]string{
"tls.crt": "this is a tls cert",
"tls.key": "this is a tls key",
"ca.crt": "certificate",
}
_, err := createSecret(ctx, secretName, namespace, tlsData)
if !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred())
}
}
func tlsSecretWithoutCACert(ctx context.Context, secretName, namespace string) {
tlsData := map[string]string{
"tls.crt": "this is a tls cert",
"tls.key": "this is a tls key",
}
_, err := createSecret(ctx, secretName, namespace, tlsData)
if !apierrors.IsAlreadyExists(err) {
Expect(err).NotTo(HaveOccurred())
}
}
func rabbitmqClusterWithTLS(ctx context.Context, clustername string, namespace string, tlsSpec rabbitmqv1beta1.TLSSpec) *rabbitmqv1beta1.RabbitmqCluster {
rabbitmqCluster := &rabbitmqv1beta1.RabbitmqCluster{
ObjectMeta: metav1.ObjectMeta{
Name: clustername,
Namespace: namespace,
},
Spec: rabbitmqv1beta1.RabbitmqClusterSpec{
TLS: tlsSpec,
},
}
Expect(client.Create(ctx, rabbitmqCluster)).To(Succeed())
return rabbitmqCluster
}