@@ -246,4 +246,145 @@ var _ = Describe("Users", func() {
246246 Expect (generatedSecret .Data ).To (HaveKey ("password" ))
247247 })
248248 })
249+
250+ When ("providing a predefined username & passwordHash" , func () {
251+ const (
252+ username = "`got*special_ch$racter5"
253+ password = "S3cur3/P455"
254+ )
255+ hash := rabbithole .Base64EncodedSaltedPasswordHashSHA512 (password )
256+
257+ var credentialSecret corev1.Secret
258+ BeforeEach (func () {
259+ credentialSecret = corev1.Secret {
260+ ObjectMeta : metav1.ObjectMeta {
261+ Name : "credential-list-secret" ,
262+ Namespace : namespace ,
263+ },
264+ Type : corev1 .SecretTypeOpaque ,
265+ Data : map [string ][]byte {
266+ "username" : []byte (username ),
267+ "passwordHash" : []byte (hash ),
268+ "password" : []byte ("should$be_ignored" ),
269+ "some.irrelevant.key" : []byte ("some-useless-value" ),
270+ },
271+ }
272+ Expect (k8sClient .Create (ctx , & credentialSecret , & client.CreateOptions {})).To (Succeed ())
273+ user = & topology.User {
274+ ObjectMeta : metav1.ObjectMeta {
275+ Name : "user-4" ,
276+ Namespace : namespace ,
277+ },
278+ Spec : topology.UserSpec {
279+ RabbitmqClusterReference : topology.RabbitmqClusterReference {
280+ Name : rmq .Name ,
281+ },
282+ ImportCredentialsSecret : & corev1.LocalObjectReference {
283+ Name : credentialSecret .Name ,
284+ },
285+ },
286+ }
287+ })
288+ AfterEach (func () {
289+ Expect (k8sClient .Delete (context .Background (), & credentialSecret )).ToNot (HaveOccurred ())
290+ Expect (k8sClient .Delete (context .Background (), user )).To (Succeed ())
291+ })
292+
293+ It ("declares a user successfully" , func () {
294+ By ("declaring user" )
295+ Expect (k8sClient .Create (ctx , user , & client.CreateOptions {})).To (Succeed ())
296+
297+ By ("creating a new Secret with the provided credentials secret" )
298+ generatedSecretKey := types.NamespacedName {
299+ Name : "user-4-user-credentials" ,
300+ Namespace : namespace ,
301+ }
302+ var generatedSecret = & corev1.Secret {}
303+ Eventually (func () error {
304+ return k8sClient .Get (ctx , generatedSecretKey , generatedSecret )
305+ }, 30 , 2 ).Should (Succeed ())
306+ Expect (generatedSecret .Data ).To (HaveKeyWithValue ("username" , []uint8 (username )))
307+ Expect (generatedSecret .Data ).To (HaveKeyWithValue ("passwordHash" , []uint8 (hash )))
308+
309+ By ("ignoring the redundant password" )
310+ Expect (generatedSecret .Data ).ToNot (HaveKey ("password" ))
311+
312+ By ("creating a user that can be authenticated with the original password" )
313+ var err error
314+ managementEndpoint , err := managementEndpoint (ctx , clientSet , user .Namespace , user .Spec .RabbitmqClusterReference .Name )
315+ Expect (err ).NotTo (HaveOccurred ())
316+ _ , err = rabbithole .NewClient (managementEndpoint , username , password )
317+ Expect (err ).NotTo (HaveOccurred ())
318+ })
319+ })
320+
321+ When ("providing a predefined username & empty passwordHash" , func () {
322+ const (
323+ username = "`got*special_ch$racter5"
324+ hash = ""
325+ ignoredPassword = "should$be_ignored"
326+ )
327+
328+ var credentialSecret corev1.Secret
329+ BeforeEach (func () {
330+ credentialSecret = corev1.Secret {
331+ ObjectMeta : metav1.ObjectMeta {
332+ Name : "credential-list-secret" ,
333+ Namespace : namespace ,
334+ },
335+ Type : corev1 .SecretTypeOpaque ,
336+ Data : map [string ][]byte {
337+ "username" : []byte (username ),
338+ "passwordHash" : []byte (hash ),
339+ "password" : []byte (ignoredPassword ),
340+ "some.irrelevant.key" : []byte ("some-useless-value" ),
341+ },
342+ }
343+ Expect (k8sClient .Create (ctx , & credentialSecret , & client.CreateOptions {})).To (Succeed ())
344+ user = & topology.User {
345+ ObjectMeta : metav1.ObjectMeta {
346+ Name : "user-4" ,
347+ Namespace : namespace ,
348+ },
349+ Spec : topology.UserSpec {
350+ RabbitmqClusterReference : topology.RabbitmqClusterReference {
351+ Name : rmq .Name ,
352+ },
353+ ImportCredentialsSecret : & corev1.LocalObjectReference {
354+ Name : credentialSecret .Name ,
355+ },
356+ },
357+ }
358+ })
359+ AfterEach (func () {
360+ Expect (k8sClient .Delete (context .Background (), & credentialSecret )).ToNot (HaveOccurred ())
361+ Expect (k8sClient .Delete (context .Background (), user )).To (Succeed ())
362+ })
363+
364+ It ("declares a passwordless user successfully" , func () {
365+ By ("declaring user" )
366+ Expect (k8sClient .Create (ctx , user , & client.CreateOptions {})).To (Succeed ())
367+
368+ By ("creating a new Secret with the provided credentials secret" )
369+ generatedSecretKey := types.NamespacedName {
370+ Name : "user-4-user-credentials" ,
371+ Namespace : namespace ,
372+ }
373+ var generatedSecret = & corev1.Secret {}
374+ Eventually (func () error {
375+ return k8sClient .Get (ctx , generatedSecretKey , generatedSecret )
376+ }, 30 , 2 ).Should (Succeed ())
377+ Expect (generatedSecret .Data ).To (HaveKeyWithValue ("username" , []uint8 (username )))
378+ Expect (generatedSecret .Data ).To (HaveKeyWithValue ("passwordHash" , []uint8 (hash )))
379+
380+ By ("ignoring the redundant password" )
381+ Expect (generatedSecret .Data ).ToNot (HaveKey ("password" ))
382+
383+ By ("creating a user with empty password hash" )
384+ var err error
385+ user , err := rabbitClient .GetUser (username )
386+ Expect (err ).NotTo (HaveOccurred ())
387+ Expect (user .PasswordHash ).To (Equal ("" ))
388+ })
389+ })
249390})
0 commit comments