Skip to content

Commit d9f929a

Browse files
committed
fix CreateString function to create valid utf8 strings
Signed-off-by: kstiehl <[email protected]>
1 parent 944e318 commit d9f929a

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-framework-validators v0.19.0
1313
github.com/hashicorp/terraform-plugin-go v0.29.0
1414
github.com/hashicorp/terraform-plugin-testing v1.14.0
15+
github.com/stretchr/testify v1.10.0
1516
golang.org/x/crypto v0.45.0
1617
)
1718

@@ -20,6 +21,7 @@ require (
2021
github.com/agext/levenshtein v1.2.2 // indirect
2122
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
2223
github.com/cloudflare/circl v1.6.1 // indirect
24+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2325
github.com/fatih/color v1.16.0 // indirect
2426
github.com/golang/protobuf v1.5.4 // indirect
2527
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -49,6 +51,7 @@ require (
4951
github.com/mitchellh/mapstructure v1.5.0 // indirect
5052
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5153
github.com/oklog/run v1.1.0 // indirect
54+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5255
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
5356
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
5457
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
@@ -63,4 +66,5 @@ require (
6366
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect
6467
google.golang.org/grpc v1.75.1 // indirect
6568
google.golang.org/protobuf v1.36.9 // indirect
69+
gopkg.in/yaml.v3 v3.0.1 // indirect
6670
)

internal/provider/resource_password.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ func upgradePasswordStateV2toV3(ctx context.Context, req resource.UpgradeStateRe
523523

524524
// Regenerate the BcryptHash value.
525525
newBcryptHash, err := bcrypt.GenerateFromPassword([]byte(passwordDataV2.Result.ValueString()), bcrypt.DefaultCost)
526-
527526
if err != nil {
528527
resp.Diagnostics.AddError(
529528
"Version 3 State Upgrade Error",
@@ -613,7 +612,8 @@ func passwordSchemaV3() schema.Schema {
613612
Default: booldefault.StaticBool(true),
614613
PlanModifiers: []planmodifier.Bool{
615614
boolplanmodifier.RequiresReplace(),
616-
}},
615+
},
616+
},
617617

618618
"lower": schema.BoolAttribute{
619619
Description: "Include lowercase alphabet characters in the result. Default value is `true`.",

internal/random/string.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ func CreateString(input StringParams) ([]byte, error) {
2727
const numChars = "0123456789"
2828
const lowerChars = "abcdefghijklmnopqrstuvwxyz"
2929
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30-
var specialChars = "!@#$%&*()-_=+[]{}<>:?"
31-
var result []byte
30+
specialChars := "!@#$%&*()-_=+[]{}<>:?"
31+
var result []rune
3232

3333
if input.OverrideSpecial != "" {
3434
specialChars = input.OverrideSpecial
3535
}
3636

37-
var chars = ""
37+
chars := ""
3838
if input.Upper {
3939
chars += upperChars
4040
}
@@ -59,7 +59,7 @@ func CreateString(input StringParams) ([]byte, error) {
5959
specialChars: input.MinSpecial,
6060
}
6161

62-
result = make([]byte, 0, input.Length)
62+
result = make([]rune, 0, input.Length)
6363

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

88-
return result, nil
88+
return []byte(string(result)), nil
8989
}
9090

91-
func generateRandomBytes(charSet *string, length int64) ([]byte, error) {
91+
func generateRandomBytes(charSet *string, length int64) ([]rune, error) {
9292
if charSet == nil {
9393
return nil, errors.New("charSet is nil")
9494
}
@@ -97,14 +97,17 @@ func generateRandomBytes(charSet *string, length int64) ([]byte, error) {
9797
return nil, errors.New("charSet is empty")
9898
}
9999

100-
bytes := make([]byte, length)
101-
setLen := big.NewInt(int64(len(*charSet)))
100+
runeSet := []rune(*charSet)
101+
102+
bytes := make([]rune, length)
103+
setLen := big.NewInt(int64(len(runeSet)))
102104
for i := range bytes {
103105
idx, err := rand.Int(rand.Reader, setLen)
104106
if err != nil {
105107
return nil, err
106108
}
107-
bytes[i] = (*charSet)[idx.Int64()]
109+
character := runeSet[idx.Int64()]
110+
bytes[i] = character
108111
}
109112
return bytes, nil
110113
}

internal/random/string_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package random_test
2+
3+
import (
4+
"testing"
5+
"unicode/utf8"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
"github.com/terraform-providers/terraform-provider-random/internal/random"
10+
)
11+
12+
func TestCreateString(t *testing.T) {
13+
input := random.StringParams{
14+
MinSpecial: 5,
15+
Special: true,
16+
Length: 10,
17+
OverrideSpecial: "°",
18+
}
19+
result, err := random.CreateString(input)
20+
stringResult := string(result)
21+
22+
assert.True(t, utf8.ValidString(stringResult), "a valid string is expected here")
23+
assert.Equal(t, 10, utf8.RuneCountInString(stringResult))
24+
require.NoError(t, err)
25+
}

0 commit comments

Comments
 (0)