diff --git a/.github/workflows/codeql.yaml b/.github/workflows/codeql.yaml index 18b96bb3..cc00ae04 100644 --- a/.github/workflows/codeql.yaml +++ b/.github/workflows/codeql.yaml @@ -24,7 +24,7 @@ on: jobs: analyze: name: Analyze - runs-on: ubuntu-20.04 + runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: diff --git a/internal/connector/common/common_utils.go b/internal/connector/common/common_utils.go index b54012a7..02432464 100644 --- a/internal/connector/common/common_utils.go +++ b/internal/connector/common/common_utils.go @@ -65,7 +65,7 @@ func WriteFiles(exportableResources []connector.ExportableResource, format, outp } for _, importBlock := range *importBlocks { - // Sanitize import block "to". Make lowercase, remove special chars, convert space to underscore + // Sanitize import block "to". Add pingcli-- prefix, hexidecimal encode special chars and spaces importBlock.Sanitize() switch format { diff --git a/internal/connector/exportable_resource.go b/internal/connector/exportable_resource.go index 184f1c57..e0bc6a91 100644 --- a/internal/connector/exportable_resource.go +++ b/internal/connector/exportable_resource.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "regexp" - "strings" pingoneGoClient "github.com/patrickcping/pingone-go-sdk-v2/pingone" pingfederateGoClient "github.com/pingidentity/pingfederate-go-client/v1210/configurationapi" @@ -36,16 +35,12 @@ type ExportableResource interface { } func (b *ImportBlock) Sanitize() { - // Replace spaces with underscores - b.ResourceName = strings.ReplaceAll(b.ResourceName, " ", "_") - // Replace dashes with underscores - b.ResourceName = strings.ReplaceAll(b.ResourceName, "-", "_") - // Replace period char with underscores - b.ResourceName = strings.ReplaceAll(b.ResourceName, ".", "_") - // Remove all non-Alphanumeric characters/non-underscores - b.ResourceName = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(b.ResourceName, "") - // Make everything lowercase - b.ResourceName = strings.ToLower(b.ResourceName) + // Hexidecimal encode special characters + b.ResourceName = regexp.MustCompile(`[^0-9A-Za-z_\-]`).ReplaceAllStringFunc(b.ResourceName, func(s string) string { + return fmt.Sprintf("-%04X-", s) + }) + // Prefix resource names with pingcli-- + b.ResourceName = "pingcli--" + b.ResourceName } func (b *ImportBlock) Equals(a ImportBlock) bool { diff --git a/internal/connector/exportable_resource_test.go b/internal/connector/exportable_resource_test.go new file mode 100644 index 00000000..a6aec148 --- /dev/null +++ b/internal/connector/exportable_resource_test.go @@ -0,0 +1,22 @@ +package connector_test + +import ( + "testing" + + "github.com/pingidentity/pingcli/internal/connector" +) + +// Test sanitization of resource name +func TestSanitize(t *testing.T) { + sanitizedResourceName := "pingcli--Customer-0020-HTML-0020-Form-0020--0028-PF-0029-" + + importBlock := connector.ImportBlock{ + ResourceName: "Customer HTML Form (PF)", + } + + importBlock.Sanitize() + + if importBlock.ResourceName != sanitizedResourceName { + t.Errorf("Sanitize function test failed") + } +}