Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-506: Improved Resource Name Sanitation #837

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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