Skip to content

Commit

Permalink
Merge pull request #1721 from hxyannay/main
Browse files Browse the repository at this point in the history
Add connection_string secret to default_user secret
  • Loading branch information
Zerpet authored Sep 13, 2024
2 parents 86e948e + 58996bf commit b17bec0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
10 changes: 10 additions & 0 deletions internal/resource/default_user_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (builder *DefaultUserSecretBuilder) Build() (client.Object, error) {
},
}
builder.updatePorts(secret)
builder.updateConnectionString(secret)

return secret, nil
}
Expand All @@ -87,6 +88,7 @@ func (builder *DefaultUserSecretBuilder) Update(object client.Object) error {
secret.Labels = metadata.GetLabels(builder.Instance.Name, builder.Instance.Labels)
secret.Annotations = metadata.ReconcileAndFilterAnnotations(secret.GetAnnotations(), builder.Instance.Annotations)
builder.updatePorts(secret)
builder.updateConnectionString(secret)

if err := controllerutil.SetControllerReference(builder.Instance, secret, builder.Scheme); err != nil {
return fmt.Errorf("failed setting controller reference: %w", err)
Expand Down Expand Up @@ -145,6 +147,14 @@ func (builder *DefaultUserSecretBuilder) updatePorts(secret *corev1.Secret) {
}
}

func (builder *DefaultUserSecretBuilder) updateConnectionString(secret *corev1.Secret) {
if builder.Instance.Spec.TLS.SecretName != "" {
secret.Data["connection_string"] = []byte(fmt.Sprintf("amqps://%s:%s@%s:%s/", secret.Data["username"], secret.Data["password"], secret.Data["host"], secret.Data["port"]))
} else {
secret.Data["connection_string"] = []byte(fmt.Sprintf("amqp://%s:%s@%s:%s/", secret.Data["username"], secret.Data["password"], secret.Data["host"], secret.Data["port"]))
}
}

// generateUsername returns a base64 string that has "default_user_" as prefix
// returned string has length 'l' when base64 decoded
func generateUsername(l int) (string, error) {
Expand Down
26 changes: 20 additions & 6 deletions internal/resource/default_user_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package resource_test

import (
b64 "encoding/base64"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
Expand Down Expand Up @@ -100,6 +102,16 @@ var _ = Describe("DefaultUserSecret", func() {
Expect(port).To(BeEquivalentTo("5672"))
})

By("Setting a connection string", func() {
Expect(secret.Data).To(HaveKey("username"), "Failed to find a key \"username\" in the generated Secret")
Expect(secret.Data).To(HaveKey("password"), "Failed to find a key \"password\" in the generated Secret")
Expect(secret.Data).To(HaveKey("host"), "Failed to find a key \"host\" in the generated Secret")
Expect(secret.Data).To(HaveKey("port"), "Failed to find a key \"port\" in the generated Secret")

expectedConnectionString := []byte(fmt.Sprintf("amqp://%s:%s@%s:%s/", secret.Data["username"], secret.Data["password"], secret.Data["host"], secret.Data["port"]))
Expect(secret.Data).To(HaveKeyWithValue("connection_string", expectedConnectionString))
})

By("creating a default_user.conf file that contains the correct sysctl config format to be parsed by RabbitMQ", func() {
defaultUserConf, ok := secret.Data["default_user.conf"]
Expect(ok).To(BeTrue(), "Failed to find a key \"default_user.conf\" in the generated Secret")
Expand Down Expand Up @@ -167,18 +179,18 @@ var _ = Describe("DefaultUserSecret", func() {
})

Context("when TLS is enabled", func() {
It("Uses the AMQPS port in the user secret", func() {
var port []byte

It("Uses the AMQPS protocol in the user secret", func() {
instance.Spec.TLS.SecretName = "tls-secret"

obj, err := defaultUserSecretBuilder.Build()
Expect(err).NotTo(HaveOccurred())
secret = obj.(*corev1.Secret)

port, ok := secret.Data["port"]
Expect(ok).To(BeTrue(), "Failed to find key \"port\" in the generated Secret")
Expect(port).To(BeEquivalentTo("5671"))
By("Setting the AMQPS port in the user secret")
Expect(secret.Data).To(HaveKeyWithValue("port", []byte("5671")))

By("setting the connection string to use the AMQPS protocol")
Expect(secret.Data).To(HaveKeyWithValue("connection_string", MatchRegexp("amqps:.*:5671/")))
})

Context("when MQTT, STOMP, streams, WebMQTT, and WebSTOMP are enabled", func() {
Expand Down Expand Up @@ -338,6 +350,8 @@ var _ = Describe("DefaultUserSecret", func() {
Expect(ok).To(BeTrue())
Expect(port).To(BeEquivalentTo("5671"))

Expect(secret.Data).To(HaveKeyWithValue("connection_string", MatchRegexp("amqps:.*:5671/")))

port, ok = secret.Data["mqtt-port"]
Expect(ok).To(BeTrue())
Expect(port).To(BeEquivalentTo("8883"))
Expand Down

0 comments on commit b17bec0

Please sign in to comment.