Skip to content

Commit 9de7bb9

Browse files
authored
Merge pull request #234 from hashicorp/bendbennett/issues-233
random_string and random_password require checks on length
2 parents 3f0f56c + 1766b65 commit 9de7bb9

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 3.1.3 (April 22, 2022)
2+
3+
BUG FIXES:
4+
5+
* resource/random_password: Prevent crash when length is less than 0 ([#129](https://github.com/hashicorp/terraform-provider-random/issues/129), [#181](https://github.com/hashicorp/terraform-provider-random/issues/181), [#200](https://github.com/hashicorp/terraform-provider-random/pull/200), [#233](https://github.com/hashicorp/terraform-provider-random/issues/233)).
6+
* resource/random_string: Prevent crash when length is less than 0 ([#129](https://github.com/hashicorp/terraform-provider-random/issues/129), [#181](https://github.com/hashicorp/terraform-provider-random/issues/181), [#200](https://github.com/hashicorp/terraform-provider-random/pull/200), [#233](https://github.com/hashicorp/terraform-provider-random/issues/233)).
7+
* resource/random_password: Prevent confusing inconsistent result error when length is 0 ([#222](https://github.com/hashicorp/terraform-provider-random/issues/222), [#233](https://github.com/hashicorp/terraform-provider-random/issues/233)).
8+
* resource/random_string: Prevent confusing inconsistent result error when length is 0 ([#222](https://github.com/hashicorp/terraform-provider-random/issues/222), [#233](https://github.com/hashicorp/terraform-provider-random/issues/233)).
9+
110
## 3.1.2 (March 17, 2022)
211

312
BUG FIXES:

docs/resources/password.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ resource "aws_db_instance" "example" {
3636

3737
### Required
3838

39-
- `length` (Number) The length of the string desired.
39+
- `length` (Number) The length of the string desired. The minimum value for length is 1 and, length must also be >= (`min_upper` + `min_lower` + `min_numeric` + `min_special`).
4040

4141
### Optional
4242

docs/resources/string.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "random_string" "random" {
3131

3232
### Required
3333

34-
- `length` (Number) The length of the string desired.
34+
- `length` (Number) The length of the string desired. The minimum value for length is 1 and, length must also be >= (`min_upper` + `min_lower` + `min_numeric` + `min_special`).
3535

3636
### Optional
3737

internal/provider/resource_string_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ func TestAccResourceString(t *testing.T) {
6464
ImportStateVerify: true,
6565
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
6666
},
67+
{
68+
Config: testAccResourceStringInvalidConfig,
69+
ExpectError: regexp.MustCompile(`.*length \(2\) must be >= min_upper \+ min_lower \+ min_numeric \+ min_special \(3\)`),
70+
},
71+
{
72+
Config: testAccResourceStringLengthTooShortConfig,
73+
ExpectError: regexp.MustCompile(`.*expected length to be at least \(1\), got 0`),
74+
},
6775
},
6876
})
6977
}
@@ -153,4 +161,13 @@ resource "random_string" "min" {
153161
min_numeric = 4
154162
}
155163
`
164+
testAccResourceStringInvalidConfig = `
165+
resource "random_string" "invalid_length" {
166+
length = 2
167+
min_lower = 3
168+
}`
169+
testAccResourceStringLengthTooShortConfig = `
170+
resource "random_string" "invalid_length" {
171+
length = 0
172+
}`
156173
)

internal/provider/string.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package provider
66
import (
77
"context"
88
"crypto/rand"
9+
"fmt"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
911
"math/big"
1012
"sort"
1113

@@ -29,10 +31,12 @@ func stringSchemaV1(sensitive bool) map[string]*schema.Schema {
2931
},
3032

3133
"length": {
32-
Description: "The length of the string desired.",
33-
Type: schema.TypeInt,
34-
Required: true,
35-
ForceNew: true,
34+
Description: "The length of the string desired. The minimum value for length is 1 and, length " +
35+
"must also be >= (`min_upper` + `min_lower` + `min_numeric` + `min_special`).",
36+
Type: schema.TypeInt,
37+
Required: true,
38+
ForceNew: true,
39+
ValidateDiagFunc: validation.ToDiagFunc(validation.IntAtLeast(1)),
3640
},
3741

3842
"special": {
@@ -144,6 +148,13 @@ func createStringFunc(sensitive bool) func(_ context.Context, d *schema.Resource
144148
minSpecial := d.Get("min_special").(int)
145149
overrideSpecial := d.Get("override_special").(string)
146150

151+
if length < minUpper+minLower+minNumeric+minSpecial {
152+
return append(diags, diag.Diagnostic{
153+
Severity: diag.Error,
154+
Summary: fmt.Sprintf("length (%d) must be >= min_upper + min_lower + min_numeric + min_special (%d)", length, minUpper+minLower+minNumeric+minSpecial),
155+
})
156+
}
157+
147158
if overrideSpecial != "" {
148159
specialChars = overrideSpecial
149160
}

0 commit comments

Comments
 (0)