diff --git a/internal/resource/default_user_secret.go b/internal/resource/default_user_secret.go index c744e128f..5c1e1c4d4 100644 --- a/internal/resource/default_user_secret.go +++ b/internal/resource/default_user_secret.go @@ -74,6 +74,7 @@ func (builder *DefaultUserSecretBuilder) Build() (client.Object, error) { }, } builder.updatePorts(secret) + builder.updateConnectionString(secret) return secret, nil } @@ -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) @@ -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) { diff --git a/internal/resource/default_user_secret_test.go b/internal/resource/default_user_secret_test.go index bbf87c02c..2ff71f4c9 100644 --- a/internal/resource/default_user_secret_test.go +++ b/internal/resource/default_user_secret_test.go @@ -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" @@ -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") @@ -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() { @@ -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"))