Skip to content

Commit

Permalink
Make the SDK provider's ValidateCredentials validator reject empty …
Browse files Browse the repository at this point in the history
…strings
  • Loading branch information
SarahFrench committed Sep 22, 2023
1 parent d6b3efd commit 8c4d345
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ func TestProvider_ValidateCredentials(t *testing.T) {
return string(contents)
},
},
"configuring credentials as an empty string is not valid": {
ConfigValue: func(t *testing.T) interface{} {
return ""
},
ExpectedErrors: []error{
errors.New("please provide a value that isn't an empty string to this field"),
},
},
"leaving credentials unconfigured is valid": {
ValueNotProvided: true,
},
// configuring credentials as an empty string is handled by another validator
}

for tn, tc := range cases {
Expand Down
10 changes: 9 additions & 1 deletion mmv1/third_party/terraform/provider/provider_validators.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ import (
)

func ValidateCredentials(v interface{}, k string) (warnings []string, errors []error) {
if v == nil || v.(string) == "" {
if v == nil {
return
}
creds := v.(string)

// reject empty strings
if v.(string) == "" {
errors = append(errors,
fmt.Errorf("please provide a value that isn't an empty string to this field"))
return
}

// if this is a path and we can stat it, assume it's ok
if _, err := os.Stat(creds); err == nil {
return
Expand Down

0 comments on commit 8c4d345

Please sign in to comment.