From eacd59bb1bcc18215bedfd4b6961695e25767549 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Wed, 12 Apr 2023 11:26:25 +0200 Subject: [PATCH] slug: Prohibit slugs ending in '_' for future use --- internal/config/http.go | 48 +++++++++++++++++++++++------- internal/config/validators/slug.go | 3 ++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/internal/config/http.go b/internal/config/http.go index c0bffbf..81e5b97 100644 --- a/internal/config/http.go +++ b/internal/config/http.go @@ -15,7 +15,7 @@ type HTTPConfig struct { PrimaryDomain string `yaml:"domain" default:"localhost.kwarc.info" validate:"domain"` // By default, only the 'self' domain above is caught. - // To catch additional domains, add them here (comma seperated) + // To catch additional domains, add them here (comma separated) ExtraDomains []string `yaml:"domains" validate:"domains"` // The system can support setting up certificate(s) automatically. @@ -51,6 +51,41 @@ func (hcfg HTTPConfig) HTTPSEnabled() bool { return hcfg.CertbotEmail != "" } +type SpecialDomain string + +var ( + TriplestoreDomain SpecialDomain = "ts" +) + +func (hcfg HTTPConfig) SpecialDomain(domain SpecialDomain) string { + return fmt.Sprintf("%s.%s", string(domain)+"_", hcfg.PrimaryDomain) +} + +// Domains adds the given subdomain to the primary and alias domains. +// If sub is empty, returns only the domains. +// +// sub is not otherwise validated, and should be normalized by the caller. +func (hcfg HTTPConfig) Domains(sub string) []string { + domains := append([]string{hcfg.PrimaryDomain}, hcfg.ExtraDomains...) + if sub == "" { + return domains + } + + for i, d := range domains { + domains[i] = sub + "." + d + } + return domains +} + +// HostRule returns a HostRule for the provided subdomain. +// See Domains() for usage of sub. +func (hcfg HTTPConfig) HostRule(sub string) string { + quoted := collection.MapSlice(hcfg.Domains(sub), func(name string) string { + return "`" + name + "`" + }) + return fmt.Sprintf("Host(%s)", strings.Join(quoted, ",")) +} + // HTTPSEnabledEnv returns "true" if https is enabled, and "false" otherwise. func (hcfg HTTPConfig) HTTPSEnabledEnv() string { if hcfg.HTTPSEnabled() { @@ -101,17 +136,8 @@ func TrimSuffixFold(s string, suffix string) string { return s } -// HostRule returns a traefik rule for the given names -// TODO: Move this over! -func HostRule(names ...string) string { - quoted := collection.MapSlice(names, func(name string) string { - return "`" + name + "`" - }) - return fmt.Sprintf("Host(%s)", strings.Join(quoted, ",")) -} - // DefaultHostRule returns the default traefik hostname rule for this distillery. // This consists of the [DefaultDomain] as well as [ExtraDomains]. func (cfg HTTPConfig) DefaultHostRule() string { - return HostRule(append([]string{cfg.PrimaryDomain}, cfg.ExtraDomains...)...) + return cfg.HostRule("") } diff --git a/internal/config/validators/slug.go b/internal/config/validators/slug.go index 57da756..daa34ea 100644 --- a/internal/config/validators/slug.go +++ b/internal/config/validators/slug.go @@ -20,5 +20,8 @@ func ValidateSlug(s *string, dflt string) error { if !regexpSlug.MatchString(*s) { return ErrInvalidSlug } + if strings.HasSuffix(*s, "_") { + return ErrInvalidSlug + } return nil }