Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit 4b79fbb

Browse files
author
Rodrigo Valin
authored
Fix Secret creation with username with rfc-1123 non-allowed-chars. (#880)
1 parent b2a31ab commit 4b79fbb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

pkg/authentication/scram/scram.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package scram
33
import (
44
"encoding/base64"
55
"fmt"
6+
"regexp"
7+
"strings"
68

79
"github.com/pkg/errors"
810

@@ -337,7 +339,24 @@ func convertMongoDBUserToAutomationConfigUser(secretGetUpdateCreateDeleter secre
337339
return acUser, nil
338340
}
339341

340-
// GetConnectionStringSecretName returns the name of the secret where the operator stores the connection string for current user
342+
// GetConnectionStringSecretName returns the name of the secret where the
343+
// operator stores the connection string for current user.
341344
func (u User) GetConnectionStringSecretName(mdb Configurable) string {
342-
return fmt.Sprintf("%s-%s-%s", mdb.NamespacedName().Name, u.Database, u.Username)
345+
return fmt.Sprintf("%s-%s-%s", mdb.NamespacedName().Name, u.Database, normalizeUsername(u.Username))
346+
}
347+
348+
// normalizeUsername returns a string that conforms to RFC-1123, by replacing
349+
// non-allowed characters with `-`.
350+
//
351+
// The MongoDB username can contain the chars in `acceptedChars` variable, as
352+
// documented here: https://docs.mongodb.com/manual/reference/connection-string/.
353+
func normalizeUsername(username string) string {
354+
acceptedChars := `[:\/\?#\[\]@_]`
355+
re := regexp.MustCompile(acceptedChars)
356+
username = re.ReplaceAllString(username, "-")
357+
358+
// Remove duplicate `-` resulting from contiguous non-allowed chars.
359+
re = regexp.MustCompile(`\-+`)
360+
username = re.ReplaceAllString(username, "-")
361+
return strings.Trim(username, "-")
343362
}

pkg/authentication/scram/scram_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,21 @@ func newMockedSecretGetUpdateCreateDeleter(secrets ...corev1.Secret) secret.GetU
4848
}
4949
return mockSecretGetUpdateCreateDeleter
5050
}
51+
5152
func notFoundError() error {
5253
return &errors.StatusError{ErrStatus: metav1.Status{Reason: metav1.StatusReasonNotFound}}
5354
}
5455

56+
func TestUsernameIsTransformedAndValid(t *testing.T) {
57+
user := buildMongoDBUser("name_with@weird?chars")
58+
mdb := buildConfigurable("mdb")
59+
assert.Equal(t, "mdb-admin-name-with-weird-chars-user", user.GetConnectionStringSecretName(mdb))
60+
}
61+
62+
func TestUsernameCanHaveAn(t *testing.T) {
63+
assert.Equal(t, "normalize-username-with-no-allowed-chars-only", normalizeUsername("?_normalize/_-username/?@with/[]?no]?/:allowed:chars[only?"))
64+
}
65+
5566
func TestReadExistingCredentials(t *testing.T) {
5667
mdbObjectKey := types.NamespacedName{Name: "mdb-0", Namespace: "default"}
5768
user := buildMongoDBUser("mdbuser-0")
@@ -74,7 +85,6 @@ func TestReadExistingCredentials(t *testing.T) {
7485
_, _, err := readExistingCredentials(newMockedSecretGetUpdateCreateDeleter(scramCredsSecret), mdbObjectKey, "different-username")
7586
assert.Error(t, err)
7687
})
77-
7888
}
7989

8090
func TestComputeScramCredentials_ComputesSameStoredAndServerKey_WithSameSalt(t *testing.T) {
@@ -136,7 +146,6 @@ func TestEnsureScramCredentials(t *testing.T) {
136146
assert.NotEmpty(t, scram256Creds.ServerKey)
137147
assert.Equal(t, 15000, scram256Creds.IterationCount)
138148
})
139-
140149
}
141150

142151
func TestConvertMongoDBUserToAutomationConfigUser(t *testing.T) {

0 commit comments

Comments
 (0)