Skip to content

Commit

Permalink
Adding check to ensure that length for random_string and random_passw…
Browse files Browse the repository at this point in the history
…ord >= min_upper + min_lower + min_numeric _ min_special (#233)
  • Loading branch information
bendbennett committed Apr 21, 2022
1 parent 3f0f56c commit ca35955
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/provider/resource_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func TestAccResourceString(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"length", "lower", "number", "special", "upper", "min_lower", "min_numeric", "min_special", "min_upper", "override_special"},
},
{
Config: testAccResourceStringInvalidConfig,
ExpectError: regexp.MustCompile("Error running apply: exit status 1"),
},
},
})
}
Expand Down Expand Up @@ -153,4 +157,9 @@ resource "random_string" "min" {
min_numeric = 4
}
`
testAccResourceStringInvalidConfig = `
resource "random_string" "invalid_length" {
length = 2
min_lower = 3
}`
)
8 changes: 8 additions & 0 deletions internal/provider/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"sort"

Expand Down Expand Up @@ -144,6 +145,13 @@ func createStringFunc(sensitive bool) func(_ context.Context, d *schema.Resource
minSpecial := d.Get("min_special").(int)
overrideSpecial := d.Get("override_special").(string)

if length < minUpper+minLower+minNumeric+minSpecial {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("length (%d) must be >= min_upper + min_lower + min_numeric + min_special (%d)", length, minUpper+minLower+minNumeric+minSpecial),
})
}

if overrideSpecial != "" {
specialChars = overrideSpecial
}
Expand Down

0 comments on commit ca35955

Please sign in to comment.