Skip to content

Commit 06357b6

Browse files
authored
bugfix: add domain sanitation (#82)
* add domain sanitization adds sanitization of the domain so that legal java packages can be created. Adds a flag to disable to the logic if necessary fixes #81 Signed-off-by: Bryce Palmer <[email protected]> * update to only sanitize package name Signed-off-by: Bryce Palmer <[email protected]>
1 parent 2b93738 commit 06357b6

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

Diff for: pkg/quarkus/v1alpha/scaffolds/api.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ func (s *apiScaffolder) Scaffold() error {
6262
var createAPITemplates []machinery.Builder
6363
createAPITemplates = append(createAPITemplates,
6464
&model.Model{
65-
Package: util.ReverseDomain(s.config.GetDomain()),
65+
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
6666
ClassName: util.ToClassname(s.resource.Kind),
6767
},
6868
&model.ModelSpec{
69-
Package: util.ReverseDomain(s.config.GetDomain()),
69+
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
7070
ClassName: util.ToClassname(s.resource.Kind),
7171
},
7272
&model.ModelStatus{
73-
Package: util.ReverseDomain(s.config.GetDomain()),
73+
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
7474
ClassName: util.ToClassname(s.resource.Kind),
7575
},
7676
&controller.Controller{
77-
Package: util.ReverseDomain(s.config.GetDomain()),
77+
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
7878
ClassName: util.ToClassname(s.resource.Kind),
7979
},
8080
)

Diff for: pkg/quarkus/v1alpha/scaffolds/init.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
package scaffolds
1616

1717
import (
18-
"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/util"
1918
"os"
2019
"path/filepath"
20+
21+
"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/util"
2122
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2223
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2324

@@ -70,7 +71,7 @@ func (s *initScaffolder) Scaffold() error {
7071
}
7172
return scaffold.Execute(
7273
&templates.PomXmlFile{
73-
Package: util.ReverseDomain(s.config.GetDomain()),
74+
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
7475
ProjectName: s.config.GetProjectName(),
7576
OperatorVersion: "0.0.1",
7677
},

Diff for: pkg/quarkus/v1alpha/util/util.go

+86-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,70 @@
1414

1515
package util
1616

17-
import "strings"
17+
import (
18+
"fmt"
19+
"strings"
20+
)
1821

1922
var (
2023
wordMapping = map[string]string{
2124
"http": "HTTP",
2225
"url": "URL",
2326
"ip": "IP",
2427
}
28+
29+
javaKeywords = map[string]int8{
30+
"abstract": 0,
31+
"assert": 0,
32+
"boolean": 0,
33+
"break": 0,
34+
"byte": 0,
35+
"case": 0,
36+
"catch": 0,
37+
"char": 0,
38+
"class": 0,
39+
"const": 0,
40+
"continue": 0,
41+
"default": 0,
42+
"do": 0,
43+
"double": 0,
44+
"else": 0,
45+
"enum": 0,
46+
"extends": 0,
47+
"final": 0,
48+
"finally": 0,
49+
"float": 0,
50+
"for": 0,
51+
"goto": 0,
52+
"if": 0,
53+
"implements": 0,
54+
"import": 0,
55+
"instanceof": 0,
56+
"int": 0,
57+
"interface": 0,
58+
"long": 0,
59+
"native": 0,
60+
"new": 0,
61+
"package": 0,
62+
"private": 0,
63+
"protected": 0,
64+
"public": 0,
65+
"return": 0,
66+
"short": 0,
67+
"static": 0,
68+
"strictfp": 0,
69+
"super": 0,
70+
"switch": 0,
71+
"synchronized": 0,
72+
"this": 0,
73+
"throw": 0,
74+
"throws": 0,
75+
"transient": 0,
76+
"try": 0,
77+
"void": 0,
78+
"volatile": 0,
79+
"while": 0,
80+
}
2581
)
2682

2783
func translateWord(word string, initCase bool) string {
@@ -70,3 +126,32 @@ func ToCamel(s string) string {
70126
func ToClassname(s string) string {
71127
return translateWord(ToCamel(s), true)
72128
}
129+
130+
func SanitizeDomain(domain string) string {
131+
// Split into the domain portions via "."
132+
domainSplit := strings.Split(domain, ".")
133+
134+
// Loop through each portion of the domain
135+
for i, domainPart := range domainSplit {
136+
// If there are hyphens, replace with underscore
137+
if strings.Contains(domainPart, "-") {
138+
fmt.Printf("\ndomain portion (%s) contains hyphens ('-') and needs to be sanitized to create a legal Java package name. Replacing all hyphens with underscores ('_')\n", domainPart)
139+
domainSplit[i] = strings.ReplaceAll(domainPart, "-", "_")
140+
}
141+
142+
// If any portion includes a keyword, replace with keyword_
143+
if _, ok := javaKeywords[domainPart]; ok {
144+
fmt.Printf("\ndomain portion (%s) is a Java keyword and needs to be sanitized to create a legal Java package name. Adding an underscore ('_') to the end of the domain portion\n", domainPart)
145+
domainSplit[i] = domainPart + "_"
146+
}
147+
148+
// If any portion starts with number, make it start with underscore
149+
if domainPart != "" && domainPart[0] >= '0' && domainPart[0] <= '9' {
150+
fmt.Printf("\ndomain portion(%s) begins with a digit and needs to be sanitized to create a legal Java package name. Adding an underscore('_') to the beginning of the domain portion\n", domainPart)
151+
domainSplit[i] = "_" + domainPart
152+
}
153+
154+
}
155+
156+
return strings.Join(domainSplit, ".")
157+
}

Diff for: pkg/quarkus/v1alpha/util/util_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,19 @@ var _ = Describe("util", func() {
6363
Expect("MyURL", ToClassname("my_url"))
6464
})
6565
})
66+
67+
Describe("SanitizeDomain", func() {
68+
It("Sanitizes hyphens", func() {
69+
Expect(SanitizeDomain("some-site.foo-bar-hyphen.com")).To(Equal("some_site.foo_bar_hyphen.com"))
70+
})
71+
72+
It("Sanitizes keywords", func() {
73+
Expect(SanitizeDomain("foobar.int.static")).To(Equal("foobar.int_.static_"))
74+
})
75+
76+
It("Sanitizes when begins with digit", func() {
77+
Expect(SanitizeDomain("123name.example.123com")).To(Equal("_123name.example._123com"))
78+
})
79+
})
80+
6681
})

0 commit comments

Comments
 (0)