Skip to content

Commit b315eaf

Browse files
committed
Add unit test for hash-based GenerateUserSettings
1 parent c4c04b5 commit b315eaf

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

internal/user_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ var _ = Describe("GenerateUserSettings", func() {
2727
userTags = []topology.UserTag{"administrator", "monitoring"}
2828
})
2929

30-
It("generates the expected rabbithole.UserSettings", func() {
30+
It("uses the password to generate the expected rabbithole.UserSettings", func() {
3131
settings, err := internal.GenerateUserSettings(&credentialSecret, userTags)
3232
Expect(err).NotTo(HaveOccurred())
3333
Expect(settings.Name).To(Equal("my-rabbit-user"))
3434
Expect(settings.Tags).To(ConsistOf("administrator", "monitoring"))
3535
Expect(settings.HashingAlgorithm.String()).To(Equal(rabbithole.HashingAlgorithmSHA512.String()))
3636

37+
// Password should not be sent, even if provided
38+
Expect(settings.Password).To(BeEmpty())
39+
3740
// The first 4 bytes of the PasswordHash will be the salt used in the hashing algorithm.
3841
// See https://www.rabbitmq.com/passwords.html#computing-password-hash.
3942
// We can take this salt and calculate what the correct hashed salted value would
@@ -45,4 +48,19 @@ var _ = Describe("GenerateUserSettings", func() {
4548
saltedHash := sha512.Sum512([]byte(string(salt) + "a-secure-password"))
4649
Expect(base64.StdEncoding.EncodeToString([]byte(string(salt) + string(saltedHash[:])))).To(Equal(settings.PasswordHash))
4750
})
51+
52+
It("uses the passwordHash to generate the expected rabbithole.UserSettings", func() {
53+
hash, _ := rabbithole.SaltedPasswordHashSHA256("a-different-password")
54+
credentialSecret.Data["passwordHash"] = []byte(hash)
55+
56+
settings, err := internal.GenerateUserSettings(&credentialSecret, userTags)
57+
Expect(err).NotTo(HaveOccurred())
58+
Expect(settings.Name).To(Equal("my-rabbit-user"))
59+
Expect(settings.Tags).To(ConsistOf("administrator", "monitoring"))
60+
Expect(settings.HashingAlgorithm.String()).To(Equal(rabbithole.HashingAlgorithmSHA512.String()))
61+
Expect(settings.PasswordHash).To(Equal(hash))
62+
63+
// Password should not be sent, even if provided
64+
Expect(settings.Password).To(BeEmpty())
65+
})
4866
})

system_tests/user_system_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ var _ = Describe("Users", func() {
194194
Expect(generatedSecret.Data).To(HaveKeyWithValue("password", []uint8("-grace.hopper_9453$")))
195195
})
196196
})
197+
197198
When("providing a pre-defined username but autogenerated password", func() {
198199
var credentialSecret corev1.Secret
199200
BeforeEach(func() {
@@ -343,7 +344,7 @@ var _ = Describe("Users", func() {
343344
Expect(k8sClient.Create(ctx, &credentialSecret, &client.CreateOptions{})).To(Succeed())
344345
user = &topology.User{
345346
ObjectMeta: metav1.ObjectMeta{
346-
Name: "user-4",
347+
Name: "user-5",
347348
Namespace: namespace,
348349
},
349350
Spec: topology.UserSpec{
@@ -367,7 +368,7 @@ var _ = Describe("Users", func() {
367368

368369
By("creating a new Secret with the provided credentials secret")
369370
generatedSecretKey := types.NamespacedName{
370-
Name: "user-4-user-credentials",
371+
Name: "user-5-user-credentials",
371372
Namespace: namespace,
372373
}
373374
var generatedSecret = &corev1.Secret{}

0 commit comments

Comments
 (0)