From 540d3d065ebcd6b93c8eb4639414ab39562db922 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Thu, 23 May 2024 15:45:24 -0700 Subject: [PATCH] Add RepoKey validator Check resty log for header existence before redacting it Clean up test utility --- client/client.go | 7 +++-- go.mod | 2 +- testutil/test.go | 11 -------- validator/fw/string/repo_key.go | 50 +++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 validator/fw/string/repo_key.go diff --git a/client/client.go b/client/client.go index 0e033fa..b165871 100644 --- a/client/client.go +++ b/client/client.go @@ -22,6 +22,7 @@ func Build(URL, productId string) (*resty.Client, error) { restyBase := resty.New(). SetBaseURL(baseUrl). + SetDebug(strings.ToLower(os.Getenv("TF_LOG")) == "debug"). OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { tfLogLevel := strings.ToLower(os.Getenv("TF_LOG")) if slices.Contains([]string{"debug", "trace"}, tfLogLevel) { @@ -29,9 +30,11 @@ func Build(URL, productId string) (*resty.Client, error) { } return nil }). - OnRequestLog(func(r *resty.RequestLog) error { + OnRequestLog(func(log *resty.RequestLog) error { // Never log auth token - r.Header.Set("Authorization", "") + if log.Header.Get("Authorization") != "" { + log.Header.Set("Authorization", "") + } return nil }). SetHeader("content-type", "application/json"). diff --git a/go.mod b/go.mod index ff17a07..ebfeaff 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require ( github.com/hashicorp/hcl/v2 v2.18.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect github.com/hashicorp/terraform-exec v0.19.0 // indirect - github.com/hashicorp/terraform-json v0.17.1 // indirect + github.com/hashicorp/terraform-json v0.17.1 github.com/hashicorp/terraform-plugin-framework-validators v0.10.0 github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect github.com/hashicorp/terraform-registry-address v0.2.2 // indirect diff --git a/testutil/test.go b/testutil/test.go index 6a41f6b..b6fa682 100644 --- a/testutil/test.go +++ b/testutil/test.go @@ -1,7 +1,6 @@ package testutil import ( - "bytes" "context" "encoding/json" "fmt" @@ -9,7 +8,6 @@ import ( "reflect" "strings" "testing" - "text/template" "github.com/go-resty/resty/v2" tfjson "github.com/hashicorp/terraform-json" @@ -32,15 +30,6 @@ func RandSelect(items ...interface{}) interface{} { return items[RandomInt()%len(items)] } -func ExecuteTemplate(name, temp string, fields interface{}) string { - var tpl bytes.Buffer - if err := template.Must(template.New(name).Parse(temp)).Execute(&tpl, fields); err != nil { - panic(err) - } - - return tpl.String() -} - func GetEnvVarWithFallback(t *testing.T, envVars ...string) string { envVarValue, err := schema.MultiEnvDefaultFunc(envVars, nil)() if envVarValue == "" || envVarValue == nil || err != nil { diff --git a/validator/fw/string/repo_key.go b/validator/fw/string/repo_key.go new file mode 100644 index 0000000..890b013 --- /dev/null +++ b/validator/fw/string/repo_key.go @@ -0,0 +1,50 @@ +package string + +import ( + "context" + "strings" + + "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" +) + +// Ensure our implementation satisfies the validator.String interface. +var _ validator.String = &repoKeyValidator{} + +type repoKeyValidator struct{} + +func (v repoKeyValidator) Description(_ context.Context) string { + return "value must be a valid email address" +} + +func (v repoKeyValidator) MarkdownDescription(ctx context.Context) string { + return v.Description(ctx) +} + +func (v repoKeyValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) { + if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() { + return + } + + value := request.ConfigValue.ValueString() + + if len(value) == 0 || len(value) > 64 { + response.Diagnostics.Append(validatordiag.InvalidAttributeValueLengthDiagnostic( + request.Path, + "must be 1 - 64 alphanumeric and hyphen characters", + value, + )) + } + + if strings.ContainsAny(value, " !@#$%^&*()+={}[]:;<>,/?~`|\\") { + response.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic( + request.Path, + "cannot contain spaces or special characters: !@#$%^&*()+={}[]:;<>,/?~`|\\", + value, + )) + } +} + +func RepoKey() validator.String { + return repoKeyValidator{} +}