Skip to content

Commit

Permalink
Add RepoKey validator
Browse files Browse the repository at this point in the history
Check resty log for header existence before redacting it

Clean up test utility
  • Loading branch information
alexhung committed May 23, 2024
1 parent baed78c commit 540d3d0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
7 changes: 5 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ 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) {
r.SetDebug(true)
}
return nil
}).
OnRequestLog(func(r *resty.RequestLog) error {
OnRequestLog(func(log *resty.RequestLog) error {
// Never log auth token
r.Header.Set("Authorization", "<REDACTED>")
if log.Header.Get("Authorization") != "" {
log.Header.Set("Authorization", "<REDACTED>")
}
return nil
}).
SetHeader("content-type", "application/json").
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions testutil/test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package testutil

import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/rand"
"reflect"
"strings"
"testing"
"text/template"

"github.com/go-resty/resty/v2"
tfjson "github.com/hashicorp/terraform-json"
Expand All @@ -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 {
Expand Down
50 changes: 50 additions & 0 deletions validator/fw/string/repo_key.go
Original file line number Diff line number Diff line change
@@ -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{}
}

0 comments on commit 540d3d0

Please sign in to comment.