Skip to content

Commit d19baec

Browse files
standardize sanitization of platform export resource name (#80)
1 parent d9588cf commit d19baec

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

.github/workflows/codeql.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ on:
2424
jobs:
2525
analyze:
2626
name: Analyze
27-
runs-on: ubuntu-20.04
27+
runs-on: ubuntu-24.04
2828
strategy:
2929
fail-fast: false
3030
matrix:

internal/connector/common/common_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func WriteFiles(exportableResources []connector.ExportableResource, format, outp
6565
}
6666

6767
for _, importBlock := range *importBlocks {
68-
// Sanitize import block "to". Make lowercase, remove special chars, convert space to underscore
68+
// Sanitize import block "to". Add pingcli-- prefix, hexidecimal encode special chars and spaces
6969
importBlock.Sanitize()
7070

7171
switch format {

internal/connector/exportable_resource.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7-
"strings"
87

98
pingoneGoClient "github.com/patrickcping/pingone-go-sdk-v2/pingone"
109
pingfederateGoClient "github.com/pingidentity/pingfederate-go-client/v1210/configurationapi"
@@ -36,16 +35,12 @@ type ExportableResource interface {
3635
}
3736

3837
func (b *ImportBlock) Sanitize() {
39-
// Replace spaces with underscores
40-
b.ResourceName = strings.ReplaceAll(b.ResourceName, " ", "_")
41-
// Replace dashes with underscores
42-
b.ResourceName = strings.ReplaceAll(b.ResourceName, "-", "_")
43-
// Replace period char with underscores
44-
b.ResourceName = strings.ReplaceAll(b.ResourceName, ".", "_")
45-
// Remove all non-Alphanumeric characters/non-underscores
46-
b.ResourceName = regexp.MustCompile(`[^a-zA-Z0-9_]+`).ReplaceAllString(b.ResourceName, "")
47-
// Make everything lowercase
48-
b.ResourceName = strings.ToLower(b.ResourceName)
38+
// Hexidecimal encode special characters
39+
b.ResourceName = regexp.MustCompile(`[^0-9A-Za-z_\-]`).ReplaceAllStringFunc(b.ResourceName, func(s string) string {
40+
return fmt.Sprintf("-%04X-", s)
41+
})
42+
// Prefix resource names with pingcli--
43+
b.ResourceName = "pingcli--" + b.ResourceName
4944
}
5045

5146
func (b *ImportBlock) Equals(a ImportBlock) bool {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package connector_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pingidentity/pingcli/internal/connector"
7+
)
8+
9+
// Test sanitization of resource name
10+
func TestSanitize(t *testing.T) {
11+
sanitizedResourceName := "pingcli--Customer-0020-HTML-0020-Form-0020--0028-PF-0029-"
12+
13+
importBlock := connector.ImportBlock{
14+
ResourceName: "Customer HTML Form (PF)",
15+
}
16+
17+
importBlock.Sanitize()
18+
19+
if importBlock.ResourceName != sanitizedResourceName {
20+
t.Errorf("Sanitize function test failed")
21+
}
22+
}

0 commit comments

Comments
 (0)