Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/provider/resource_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ func upgradePasswordStateV2toV3(ctx context.Context, req resource.UpgradeStateRe

// Regenerate the BcryptHash value.
newBcryptHash, err := bcrypt.GenerateFromPassword([]byte(passwordDataV2.Result.ValueString()), bcrypt.DefaultCost)

if err != nil {
resp.Diagnostics.AddError(
"Version 3 State Upgrade Error",
Expand Down Expand Up @@ -613,7 +612,8 @@ func passwordSchemaV3() schema.Schema {
Default: booldefault.StaticBool(true),
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.RequiresReplace(),
}},
},
},

"lower": schema.BoolAttribute{
Description: "Include lowercase alphabet characters in the result. Default value is `true`.",
Expand Down
21 changes: 12 additions & 9 deletions internal/random/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func CreateString(input StringParams) ([]byte, error) {
const numChars = "0123456789"
const lowerChars = "abcdefghijklmnopqrstuvwxyz"
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var specialChars = "!@#$%&*()-_=+[]{}<>:?"
var result []byte
specialChars := "!@#$%&*()-_=+[]{}<>:?"
var result []rune

if input.OverrideSpecial != "" {
specialChars = input.OverrideSpecial
}

var chars = ""
chars := ""
if input.Upper {
chars += upperChars
}
Expand All @@ -59,7 +59,7 @@ func CreateString(input StringParams) ([]byte, error) {
specialChars: input.MinSpecial,
}

result = make([]byte, 0, input.Length)
result = make([]rune, 0, input.Length)

for k, v := range minMapping {
s, err := generateRandomBytes(&k, v)
Expand All @@ -85,10 +85,10 @@ func CreateString(input StringParams) ([]byte, error) {
return order[i] < order[j]
})

return result, nil
return []byte(string(result)), nil
}

func generateRandomBytes(charSet *string, length int64) ([]byte, error) {
func generateRandomBytes(charSet *string, length int64) ([]rune, error) {
if charSet == nil {
return nil, errors.New("charSet is nil")
}
Expand All @@ -97,14 +97,17 @@ func generateRandomBytes(charSet *string, length int64) ([]byte, error) {
return nil, errors.New("charSet is empty")
}

bytes := make([]byte, length)
setLen := big.NewInt(int64(len(*charSet)))
runeSet := []rune(*charSet)

bytes := make([]rune, length)
setLen := big.NewInt(int64(len(runeSet)))
for i := range bytes {
idx, err := rand.Int(rand.Reader, setLen)
if err != nil {
return nil, err
}
bytes[i] = (*charSet)[idx.Int64()]
character := runeSet[idx.Int64()]
bytes[i] = character
}
return bytes, nil
}
25 changes: 25 additions & 0 deletions internal/random/string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package random_test

import (
"testing"
"unicode/utf8"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/terraform-providers/terraform-provider-random/internal/random"
)

func TestCreateString(t *testing.T) {
input := random.StringParams{
MinSpecial: 5,
Special: true,
Length: 10,
OverrideSpecial: "°",
}
result, err := random.CreateString(input)
stringResult := string(result)

assert.True(t, utf8.ValidString(stringResult), "a valid string is expected here")
assert.Equal(t, 10, utf8.RuneCountInString(stringResult))
require.NoError(t, err)
}