Skip to content

Commit

Permalink
DXCDT-506: Improved Resource Name Sanitation (#837)
Browse files Browse the repository at this point in the history
Tweaking sanitize regex, modifying unit tests

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
willvedd and willvedd authored Sep 7, 2023
1 parent 8983521 commit da61765
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 100 deletions.
24 changes: 24 additions & 0 deletions internal/cli/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"

"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -397,3 +399,25 @@ func cleanOutputDirectory(outputDIR string) error {

return joinedErrors
}

// sanitizeResourceName will return a valid terraform resource name.
//
// A name must start with a letter or underscore and may
// contain only letters, digits, underscores, and dashes.
func sanitizeResourceName(name string) string {
// Regular expression pattern to remove invalid characters.
namePattern := "[^a-zA-Z0-9_]+"
re := regexp.MustCompile(namePattern)

sanitizedName := re.ReplaceAllString(name, "_")

// Regular expression pattern to remove leading digits or dashes.
namePattern = "^[0-9-]+"
re = regexp.MustCompile(namePattern)

sanitizedName = re.ReplaceAllString(sanitizedName, "")
sanitizedName = strings.Trim(sanitizedName, "_")
sanitizedName = strings.ToLower(sanitizedName)

return sanitizedName
}
28 changes: 3 additions & 25 deletions internal/cli/terraform_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cli

import (
"context"
"fmt"
"regexp"

"github.com/auth0/go-auth0/management"
"github.com/google/uuid"
Expand Down Expand Up @@ -147,7 +145,7 @@ func (f *clientGrantResourceFetcher) FetchData(ctx context.Context) (importDataL

for _, grant := range grants.ClientGrants {
data = append(data, importDataItem{
ResourceName: "auth0_client_grant." + grant.GetClientID() + "_" + sanitizeResourceName(grant.GetAudience()),
ResourceName: "auth0_client_grant." + sanitizeResourceName(grant.GetClientID()+"_"+grant.GetAudience()),
ImportID: grant.GetID(),
})
}
Expand Down Expand Up @@ -326,7 +324,7 @@ func (f *promptCustomTextResourceFetcherResourceFetcher) FetchData(ctx context.C
for _, language := range tenant.GetEnabledLocales() {
for _, promptType := range promptTypes {
data = append(data, importDataItem{
ResourceName: fmt.Sprintf("auth0_prompt_custom_text.%s-%s", language, promptType),
ResourceName: "auth0_prompt_custom_text." + sanitizeResourceName(language+"_"+promptType),
ImportID: promptType + "::" + language,
})
}
Expand Down Expand Up @@ -436,7 +434,7 @@ func (f *triggerActionsResourceFetcher) FetchData(ctx context.Context) (importDa
}
if len(res.Bindings) > 0 {
data = append(data, importDataItem{
ResourceName: "auth0_trigger_actions." + trigger,
ResourceName: "auth0_trigger_actions." + sanitizeResourceName(trigger),
ImportID: trigger,
})
}
Expand Down Expand Up @@ -474,23 +472,3 @@ func (f *actionResourceFetcher) FetchData(ctx context.Context) (importDataList,

return data, nil
}

// sanitizeResourceName will return a valid terraform resource name.
//
// A name must start with a letter or underscore and may
// contain only letters, digits, underscores, and dashes.
func sanitizeResourceName(name string) string {
// Regular expression pattern to remove invalid characters.
namePattern := "[^a-zA-Z0-9_-]+"
re := regexp.MustCompile(namePattern)

sanitizedName := re.ReplaceAllString(name, "")

// Regular expression pattern to remove leading digits or dashes.
namePattern = "^[0-9-]+"
re = regexp.MustCompile(namePattern)

sanitizedName = re.ReplaceAllString(sanitizedName, "")

return sanitizedName
}
Loading

0 comments on commit da61765

Please sign in to comment.