diff --git a/cli/cmd/init.go b/cli/cmd/init.go index c18a3ebacd..6fd4192cc0 100644 --- a/cli/cmd/init.go +++ b/cli/cmd/init.go @@ -25,6 +25,7 @@ import ( "github.com/cortexlabs/cortex/pkg/lib/errors" "github.com/cortexlabs/cortex/pkg/lib/files" + "github.com/cortexlabs/cortex/pkg/lib/urls" ) var initCmd = &cobra.Command{ @@ -35,6 +36,10 @@ var initCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { appName := args[0] + if err := urls.CheckDNS1123(appName); err != nil { + errors.Exit(err) + } + cwd, err := os.Getwd() if err != nil { errors.Exit(err) diff --git a/pkg/lib/configreader/string.go b/pkg/lib/configreader/string.go index e0ecec4189..4b13ead1a0 100644 --- a/pkg/lib/configreader/string.go +++ b/pkg/lib/configreader/string.go @@ -37,6 +37,7 @@ type StringValidation struct { AlphaNumericDashDotUnderscore bool AlphaNumericDashUnderscore bool DNS1035 bool + DNS1123 bool Validator func(string) (string, error) } @@ -203,6 +204,12 @@ func ValidateStringVal(val string, v *StringValidation) error { } } + if v.DNS1123 { + if err := urls.CheckDNS1123(val); err != nil { + return err + } + } + return nil } diff --git a/pkg/lib/configreader/string_ptr.go b/pkg/lib/configreader/string_ptr.go index e067ef0ab1..2c0cac4288 100644 --- a/pkg/lib/configreader/string_ptr.go +++ b/pkg/lib/configreader/string_ptr.go @@ -32,6 +32,7 @@ type StringPtrValidation struct { AlphaNumericDashDotUnderscore bool AlphaNumericDashUnderscore bool DNS1035 bool + DNS1123 bool Validator func(*string) (*string, error) } @@ -43,6 +44,7 @@ func makeStringValValidation(v *StringPtrValidation) *StringValidation { AlphaNumericDashDotUnderscore: v.AlphaNumericDashDotUnderscore, AlphaNumericDashUnderscore: v.AlphaNumericDashUnderscore, DNS1035: v.DNS1035, + DNS1123: v.DNS1123, } } diff --git a/pkg/lib/urls/errors.go b/pkg/lib/urls/errors.go index 7dc9fc8191..ade48f4bc7 100644 --- a/pkg/lib/urls/errors.go +++ b/pkg/lib/urls/errors.go @@ -28,15 +28,17 @@ const ( ErrUnknown ErrorKind = iota ErrInvalidURL ErrDNS1035 + ErrDNS1123 ) var errorKinds = []string{ "err_unknown", "err_invalid_url", "err_dns1035", + "err_dns1123", } -var _ = [1]int{}[int(ErrDNS1035)-(len(errorKinds)-1)] // Ensure list length matches +var _ = [1]int{}[int(ErrDNS1123)-(len(errorKinds)-1)] // Ensure list length matches func (t ErrorKind) String() string { return errorKinds[t] @@ -94,3 +96,10 @@ func ErrorDNS1035(provided string) error { message: fmt.Sprintf("%s must contain only lower case letters, numbers, and dashes, start with a letter, and cannot end with a dash", s.UserStr(provided)), } } + +func ErrorDNS1123(provided string) error { + return Error{ + Kind: ErrDNS1123, + message: fmt.Sprintf("%s must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character", s.UserStr(provided)), + } +} diff --git a/pkg/lib/urls/urls.go b/pkg/lib/urls/urls.go index 6e9333bfc6..00dc5377b4 100644 --- a/pkg/lib/urls/urls.go +++ b/pkg/lib/urls/urls.go @@ -24,7 +24,10 @@ import ( s "github.com/cortexlabs/cortex/pkg/lib/strings" ) -var dns1035Regex = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`) +var ( + dns1035Regex = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`) + dns1123Regex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`) +) func Parse(rawurl string) (*url.URL, error) { u, err := url.Parse(rawurl) @@ -49,7 +52,14 @@ func Join(strs ...string) string { func CheckDNS1035(s string) error { if !dns1035Regex.MatchString(s) { - ErrorDNS1035(s) + return ErrorDNS1035(s) + } + return nil +} + +func CheckDNS1123(s string) error { + if !dns1123Regex.MatchString(s) { + return ErrorDNS1123(s) } return nil } diff --git a/pkg/operator/api/userconfig/app.go b/pkg/operator/api/userconfig/app.go index 33fe995ed2..1e05fa8bef 100644 --- a/pkg/operator/api/userconfig/app.go +++ b/pkg/operator/api/userconfig/app.go @@ -32,6 +32,7 @@ var appValidation = &cr.StructValidation{ StringValidation: &cr.StringValidation{ Required: true, AlphaNumericDashUnderscore: true, + DNS1123: true, }, }, typeFieldValidation,