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

ホスト名の正規化 #754

Merged
merged 3 commits into from
Oct 22, 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
8 changes: 8 additions & 0 deletions pkg/domain/app_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
)

func ValidateDomain(domain string) error {
// ドメインが大文字を含むときはエラー
if domain != strings.ToLower(domain) {
return errors.Errorf("domain %v must be lower case", domain)
}
// 面倒なのでtrailing dotは無しで統一
if strings.HasSuffix(domain, ".") {
return errors.Errorf("trailing dot not allowed in domain %v", domain)
Expand Down Expand Up @@ -151,6 +155,10 @@ func (w *Website) Validate() error {
return nil
}

func (w *Website) Normalize() {
w.FQDN = strings.ToLower(w.FQDN)
}

func (w *Website) pathComponents() []string {
// NOTE: empty PathPrefix must not exist
if w.PathPrefix == "/" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/app_website_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestValidateDomain(t *testing.T) {
{"ok 4", "underscore_allowed.example.com", false},
{"invalid characters 1", "admin@example.com", true},
{"invalid characters 2", "space not allowed.example.com", true},
{"invalid characters 3", "UPPERCASE.example.com", true},
{"wildcard ng", "*.trap.show", true},
{"multi wildcard ng", "*.*.trap.show", true},
{"wildcard in middle", "trap.*.show", true},
Expand Down Expand Up @@ -157,6 +158,7 @@ func TestWebsite_Validate(t *testing.T) {
{"invalid fqdn3", Website{FQDN: "google.*.com", PathPrefix: "/", HTTPPort: 80}, true},
{"invalid fqdn4", Website{FQDN: "goo gle.com", PathPrefix: "/", HTTPPort: 80}, true},
{"invalid fqdn5", Website{FQDN: "no space", PathPrefix: "/", HTTPPort: 80}, true},
{"invalid fqdn6", Website{FQDN: "UPPERCASE.example.com", PathPrefix: "/", HTTPPort: 80}, true},
{"invalid path1", Website{FQDN: "google.com", PathPrefix: "", HTTPPort: 80}, true},
{"invalid path2", Website{FQDN: "google.com", PathPrefix: "../test", HTTPPort: 80}, true},
{"invalid path3", Website{FQDN: "google.com", PathPrefix: "/test/", HTTPPort: 80}, true},
Expand Down
6 changes: 6 additions & 0 deletions pkg/usecase/apiserver/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (s *Service) CreateApplication(ctx context.Context, app *domain.Application
}
app.OwnerIDs = repo.OwnerIDs

for _, website := range app.Websites {
website.Normalize()
}
// Validate
err = s.validateApp(ctx, app)
if err != nil {
Expand Down Expand Up @@ -213,6 +216,9 @@ func (s *Service) UpdateApplication(ctx context.Context, id string, args *domain
}
app.Apply(args)

for _, website := range app.Websites {
website.Normalize()
}
// Validate
if err = s.validateApp(ctx, app); err != nil {
return err
Expand Down