-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Phase 2] Include Auth Login blocks in multiplexed Provider configura…
…tion (#2097) make updates to all auth logins and fix build run go mod tidy register all auth logins to test build add all auth login blocks and match schema implementations set default for azure scope address todos - remove commented code; neither are read from env vars - add validators
- Loading branch information
1 parent
78f1c0c
commit 289a101
Showing
32 changed files
with
1,012 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Terraform Plugin Framework Validators | ||
|
||
This package contains custom Terraform Plugin Framework [validators](https://developer.hashicorp.com/terraform/plugin/framework/validation). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validators | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
googleoauth "golang.org/x/oauth2/google" | ||
) | ||
|
||
// Credentials Validator | ||
var _ validator.String = credentialsValidator{} | ||
|
||
// credentialsValidator validates that a string Attribute's is valid JSON credentials. | ||
type credentialsValidator struct{} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (v credentialsValidator) Description(_ context.Context) string { | ||
return "value must be a path to valid JSON credentials or valid, raw, JSON credentials" | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (v credentialsValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
// ValidateString performs the validation. | ||
func (v credentialsValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { | ||
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
value := request.ConfigValue.ValueString() | ||
|
||
// if this is a path and we can stat it, assume it's ok | ||
if _, err := os.Stat(value); err == nil { | ||
return | ||
} | ||
if _, err := googleoauth.CredentialsFromJSON(context.Background(), []byte(value)); err != nil { | ||
response.Diagnostics.AddError("JSON credentials are not valid", err.Error()) | ||
} | ||
} | ||
|
||
func GCPCredentialsValidator() validator.String { | ||
return credentialsValidator{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package validators | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
const testFakeCredentialsPath = "./test-fixtures/fake_account.json" | ||
|
||
func TestFrameworkProvider_CredentialsValidator(t *testing.T) { | ||
cases := map[string]struct { | ||
ConfigValue func(t *testing.T) types.String | ||
ExpectedWarningCount int | ||
ExpectedErrorCount int | ||
}{ | ||
"configuring credentials as a path to a credentials JSON file is valid": { | ||
ConfigValue: func(t *testing.T) types.String { | ||
return types.StringValue(testFakeCredentialsPath) // Path to a test fixture | ||
}, | ||
}, | ||
"configuring credentials as a path to a non-existant file is NOT valid": { | ||
ConfigValue: func(t *testing.T) types.String { | ||
return types.StringValue("./this/path/doesnt/exist.json") // Doesn't exist | ||
}, | ||
ExpectedErrorCount: 1, | ||
}, | ||
"configuring credentials as a credentials JSON string is valid": { | ||
ConfigValue: func(t *testing.T) types.String { | ||
contents, err := ioutil.ReadFile(testFakeCredentialsPath) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %s", err) | ||
} | ||
stringContents := string(contents) | ||
return types.StringValue(stringContents) | ||
}, | ||
}, | ||
"configuring credentials as an empty string is not valid": { | ||
ConfigValue: func(t *testing.T) types.String { | ||
return types.StringValue("") | ||
}, | ||
ExpectedErrorCount: 1, | ||
}, | ||
"leaving credentials unconfigured is valid": { | ||
ConfigValue: func(t *testing.T) types.String { | ||
return types.StringNull() | ||
}, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
t.Run(tn, func(t *testing.T) { | ||
// Arrange | ||
req := validator.StringRequest{ | ||
ConfigValue: tc.ConfigValue(t), | ||
} | ||
|
||
resp := validator.StringResponse{ | ||
Diagnostics: diag.Diagnostics{}, | ||
} | ||
|
||
cv := GCPCredentialsValidator() | ||
|
||
// Act | ||
cv.ValidateString(context.Background(), req, &resp) | ||
|
||
// Assert | ||
if resp.Diagnostics.WarningsCount() > tc.ExpectedWarningCount { | ||
t.Errorf("Expected %d warnings, got %d", tc.ExpectedWarningCount, resp.Diagnostics.WarningsCount()) | ||
} | ||
if resp.Diagnostics.ErrorsCount() > tc.ExpectedErrorCount { | ||
t.Errorf("Expected %d errors, got %d", tc.ExpectedErrorCount, resp.Diagnostics.ErrorsCount()) | ||
} | ||
}) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
internal/framework/validators/test-fixtures/fake_account.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"private_key_id": "foo", | ||
"private_key": "bar", | ||
"client_email": "[email protected]", | ||
"client_id": "[email protected]", | ||
"type": "service_account" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.