diff --git a/pkg/helpers/helpers_strings_test.go b/pkg/helpers/helpers_strings_test.go new file mode 100644 index 00000000000..4bdf31dc9cc --- /dev/null +++ b/pkg/helpers/helpers_strings_test.go @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package helpers + +import ( + "testing" + "unicode" +) + +// TestGenerateRandomUsername tests whether the username generated will ever start with a non letter +func TestGenerateRandomUsername(t *testing.T) { + for i := 0; i < 1000; i++ { + u := GenerateRandomUsername(10) + if !unicode.IsLetter(rune(u[0])) { + t.Errorf("index 0 of username '%s' is not a letter '%s'", u, string(u[0])) + } + } +} diff --git a/pkg/helpers/labels.go b/pkg/helpers/labels.go index 7647a895242..690bf029448 100644 --- a/pkg/helpers/labels.go +++ b/pkg/helpers/labels.go @@ -15,7 +15,7 @@ func LabelsToTags(in map[string]string) map[string]*string { newK := k value := v if strings.ContainsAny(k, "<>%/?\\") { - newK = ReplaceAny(k, []string{"<", ">", "%", "/", "\\\\", "?"}) + newK = ReplaceAny(k, []string{"<", ">", "%", "/", "\\\\", "\\?"}) } out[newK] = &value } diff --git a/pkg/helpers/stringhelper.go b/pkg/helpers/stringhelper.go index f291e29689d..7f150a24993 100644 --- a/pkg/helpers/stringhelper.go +++ b/pkg/helpers/stringhelper.go @@ -18,13 +18,15 @@ import ( ) const ( - passwordLength = 16 - passwordChars = lowerAlphaChars + upperAlphaChars + numberChars + specialChars - usernameChars = lowerAlphaChars + upperAlphaChars + numberChars + passwordLength = 16 + lowerAlphaChars = "abcdefghijklmnopqrstuvwxyz" upperAlphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" numberChars = "0123456789" specialChars = "!@#$%^&*" + passwordChars = lowerAlphaChars + upperAlphaChars + numberChars + specialChars + usernameChars = lowerAlphaChars + upperAlphaChars + numberChars + allCaseAlpha = lowerAlphaChars + upperAlphaChars ) var seededRand = NewSeeded() @@ -96,19 +98,13 @@ func GenerateRandomUsername(n int) string { b := make([]byte, n) // ensure first char is alpha - b[0] = lowerAlphaChars[seededRand.Intn(len(lowerAlphaChars))] + b[0] = allCaseAlpha[seededRand.Intn(len(allCaseAlpha))] for i := 1; i < n; i++ { b[i] = usernameChars[seededRand.Intn(len(usernameChars))] } - // For good measure, shuffle the elements of the entire []byte so that - // the 0 character isn't predicatably lowercase, etc... - for i := range b { - j := seededRand.Intn(len(b)) - b[i], b[j] = b[j], b[i] - } - return string(b) + return string(b) } // GenerateRandomPassword - helper function to generate random password for sql server