Skip to content

Commit 44e3a18

Browse files
authored
Use a byte slice when generating passwords
Go 1.15 complained about the conversion from int64 to string.
1 parent 83437f8 commit 44e3a18

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

internal/util/secrets.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ func CreateSecret(clientset kubernetes.Interface, db, secretName, username, pass
7676
// GeneratePassword generates a password of a given length out of the acceptable
7777
// ASCII characters suitable for a password
7878
func GeneratePassword(length int) (string, error) {
79-
// for "length" times, we are going to get a random ASCII character, and
80-
// append it to the "password" string
81-
password := ""
79+
password := make([]byte, length)
8280

8381
for i := 0; i < length; i++ {
8482
char, err := rand.Int(rand.Reader, passwordCharSelector)
@@ -88,10 +86,10 @@ func GeneratePassword(length int) (string, error) {
8886
return "", err
8987
}
9088

91-
password += string(passwordCharLower + char.Int64())
89+
password[i] = byte(passwordCharLower + char.Int64())
9290
}
9391

94-
return password, nil
92+
return string(password), nil
9593
}
9694

9795
// GeneratedPasswordLength returns the value for what the length of a

internal/util/secrets_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package util
2+
3+
/*
4+
Copyright 2020 Crunchy Data Solutions, Inc.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import (
19+
"strings"
20+
"testing"
21+
"unicode"
22+
)
23+
24+
func TestGeneratePassword(t *testing.T) {
25+
// different lengths
26+
for _, length := range []int{1, 2, 3, 5, 20} {
27+
password, err := GeneratePassword(length)
28+
if err != nil {
29+
t.Fatalf("expected no error, got %v", err)
30+
}
31+
if expected, actual := length, len(password); expected != actual {
32+
t.Fatalf("expected length %v, got %v", expected, actual)
33+
}
34+
if i := strings.IndexFunc(password, unicode.IsPrint); i > 0 {
35+
t.Fatalf("expected only printable characters, got %q in %q", password[i], password)
36+
}
37+
}
38+
39+
// random contents
40+
previous := []string{}
41+
42+
for i := 0; i < 10; i++ {
43+
password, err := GeneratePassword(5)
44+
if err != nil {
45+
t.Fatalf("expected no error, got %v", err)
46+
}
47+
if i := strings.IndexFunc(password, unicode.IsPrint); i > 0 {
48+
t.Fatalf("expected only printable characters, got %q in %q", password[i], password)
49+
}
50+
51+
for i := range previous {
52+
if password == previous[i] {
53+
t.Fatalf("expected passwords to not repeat, got %q after %q", password, previous)
54+
}
55+
}
56+
previous = append(previous, password)
57+
}
58+
}

0 commit comments

Comments
 (0)