Skip to content

bugfix: add domain sanitation #82

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

Merged
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
8 changes: 4 additions & 4 deletions pkg/quarkus/v1alpha/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ func (s *apiScaffolder) Scaffold() error {
var createAPITemplates []machinery.Builder
createAPITemplates = append(createAPITemplates,
&model.Model{
Package: util.ReverseDomain(s.config.GetDomain()),
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not relevant to the current PR but maybe it'd make sense to compute these (and the class name) only once.

ClassName: util.ToClassname(s.resource.Kind),
},
&model.ModelSpec{
Package: util.ReverseDomain(s.config.GetDomain()),
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
ClassName: util.ToClassname(s.resource.Kind),
},
&model.ModelStatus{
Package: util.ReverseDomain(s.config.GetDomain()),
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
ClassName: util.ToClassname(s.resource.Kind),
},
&controller.Controller{
Package: util.ReverseDomain(s.config.GetDomain()),
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
ClassName: util.ToClassname(s.resource.Kind),
},
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/quarkus/v1alpha/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package scaffolds

import (
"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/util"
"os"
"path/filepath"

"github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha/util"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"

Expand Down Expand Up @@ -70,7 +71,7 @@ func (s *initScaffolder) Scaffold() error {
}
return scaffold.Execute(
&templates.PomXmlFile{
Package: util.ReverseDomain(s.config.GetDomain()),
Package: util.ReverseDomain(util.SanitizeDomain(s.config.GetDomain())),
ProjectName: s.config.GetProjectName(),
OperatorVersion: "0.0.1",
},
Expand Down
87 changes: 86 additions & 1 deletion pkg/quarkus/v1alpha/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,70 @@

package util

import "strings"
import (
"fmt"
"strings"
)

var (
wordMapping = map[string]string{
"http": "HTTP",
"url": "URL",
"ip": "IP",
}

javaKeywords = map[string]int8{
"abstract": 0,
"assert": 0,
"boolean": 0,
"break": 0,
"byte": 0,
"case": 0,
"catch": 0,
"char": 0,
"class": 0,
"const": 0,
"continue": 0,
"default": 0,
"do": 0,
"double": 0,
"else": 0,
"enum": 0,
"extends": 0,
"final": 0,
"finally": 0,
"float": 0,
"for": 0,
"goto": 0,
"if": 0,
"implements": 0,
"import": 0,
"instanceof": 0,
"int": 0,
"interface": 0,
"long": 0,
"native": 0,
"new": 0,
"package": 0,
"private": 0,
"protected": 0,
"public": 0,
"return": 0,
"short": 0,
"static": 0,
"strictfp": 0,
"super": 0,
"switch": 0,
"synchronized": 0,
"this": 0,
"throw": 0,
"throws": 0,
"transient": 0,
"try": 0,
"void": 0,
"volatile": 0,
"while": 0,
}
)

func translateWord(word string, initCase bool) string {
Expand Down Expand Up @@ -70,3 +126,32 @@ func ToCamel(s string) string {
func ToClassname(s string) string {
return translateWord(ToCamel(s), true)
}

func SanitizeDomain(domain string) string {
// Split into the domain portions via "."
domainSplit := strings.Split(domain, ".")

// Loop through each portion of the domain
for i, domainPart := range domainSplit {
// If there are hyphens, replace with underscore
if strings.Contains(domainPart, "-") {
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)
domainSplit[i] = strings.ReplaceAll(domainPart, "-", "_")
}

// If any portion includes a keyword, replace with keyword_
if _, ok := javaKeywords[domainPart]; ok {
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)
domainSplit[i] = domainPart + "_"
}

// If any portion starts with number, make it start with underscore
if domainPart != "" && domainPart[0] >= '0' && domainPart[0] <= '9' {
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)
domainSplit[i] = "_" + domainPart
}

}

return strings.Join(domainSplit, ".")
}
15 changes: 15 additions & 0 deletions pkg/quarkus/v1alpha/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,19 @@ var _ = Describe("util", func() {
Expect("MyURL", ToClassname("my_url"))
})
})

Describe("SanitizeDomain", func() {
It("Sanitizes hyphens", func() {
Expect(SanitizeDomain("some-site.foo-bar-hyphen.com")).To(Equal("some_site.foo_bar_hyphen.com"))
})

It("Sanitizes keywords", func() {
Expect(SanitizeDomain("foobar.int.static")).To(Equal("foobar.int_.static_"))
})

It("Sanitizes when begins with digit", func() {
Expect(SanitizeDomain("123name.example.123com")).To(Equal("_123name.example._123com"))
})
})

})