Skip to content

Commit

Permalink
Prefix generated username with 'default_user_'
Browse files Browse the repository at this point in the history
- it will make it easier to specify exclusion rules when
using schema sync plugin
  • Loading branch information
ChunyiLyu committed Sep 13, 2021
1 parent 0706227 commit 0455f60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
15 changes: 14 additions & 1 deletion internal/resource/default_user_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
DefaultUserSecretName = "default-user"
bindingProvider = "rabbitmq"
bindingType = "rabbitmq"
usernamePrefix = "default_user_"
)

type DefaultUserSecretBuilder struct {
Expand All @@ -40,7 +41,7 @@ func (builder *RabbitmqResourceBuilder) DefaultUserSecret() *DefaultUserSecretBu
}

func (builder *DefaultUserSecretBuilder) Build() (client.Object, error) {
username, err := randomEncodedString(24)
username, err := generateUsername(24)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -146,6 +147,18 @@ func (builder *DefaultUserSecretBuilder) updatePorts(secret *corev1.Secret) {
}
}

// 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) {
encodedStr, err := randomEncodedString(l)
if err != nil {
return "", err
}

encodedStrSlice := []byte(encodedStr)
return string(append([]byte(usernamePrefix), encodedStrSlice[0:len(encodedStrSlice)-len(usernamePrefix)]...)), nil
}

func (builder *DefaultUserSecretBuilder) pluginEnabled(plugin v1beta1.Plugin) bool {
for _, value := range builder.Instance.Spec.Rabbitmq.AdditionalPlugins {
if value == plugin {
Expand Down
22 changes: 11 additions & 11 deletions internal/resource/default_user_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ package resource_test

import (
b64 "encoding/base64"

"gopkg.in/ini.v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
defaultscheme "k8s.io/client-go/kubernetes/scheme"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/api/v1beta1"
"github.com/rabbitmq/cluster-operator/internal/resource"
"gopkg.in/ini.v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
)

var _ = Describe("DefaultUserSecret", func() {
Expand Down Expand Up @@ -72,15 +70,17 @@ var _ = Describe("DefaultUserSecret", func() {
Expect(secret.Type).To(Equal(corev1.SecretTypeOpaque))
})

By("creating a rabbitmq username that is base64 encoded and 24 characters in length", func() {
By("creating a rabbitmq username that has a prefix 'default_user_', base64 encoded, and 24 characters in length when decoded", func() {
username, ok = secret.Data["username"]
Expect(ok).NotTo(BeFalse(), "Failed to find a key \"username\" in the generated Secret")
decodedUsername, err := b64.URLEncoding.DecodeString(string(username))
Expect(string(username)).To(HavePrefix("default_user_"))
//strList := strings.SplitAfter(string(username), "default_user_")
decoded, err := b64.URLEncoding.DecodeString(string(username))
Expect(err).NotTo(HaveOccurred())
Expect(len(decodedUsername)).To(Equal(24))
Expect(len(decoded)).To(Equal(24))
})

By("creating a rabbitmq password that is base64 encoded and 24 characters in length", func() {
By("creating a rabbitmq password that is base64 encoded and 24 characters in length when decoded", func() {
password, ok = secret.Data["password"]
Expect(ok).NotTo(BeFalse(), "Failed to find a key \"password\" in the generated Secret")
decodedPassword, err := b64.URLEncoding.DecodeString(string(password))
Expand Down

0 comments on commit 0455f60

Please sign in to comment.