Skip to content

Validate that app name is valid DNS1123 #112

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 5 commits into from
May 29, 2019
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
5 changes: 5 additions & 0 deletions cli/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/lib/configreader/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type StringValidation struct {
AlphaNumericDashDotUnderscore bool
AlphaNumericDashUnderscore bool
DNS1035 bool
DNS1123 bool
Validator func(string) (string, error)
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/lib/configreader/string_ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type StringPtrValidation struct {
AlphaNumericDashDotUnderscore bool
AlphaNumericDashUnderscore bool
DNS1035 bool
DNS1123 bool
Validator func(*string) (*string, error)
}

Expand All @@ -43,6 +44,7 @@ func makeStringValValidation(v *StringPtrValidation) *StringValidation {
AlphaNumericDashDotUnderscore: v.AlphaNumericDashDotUnderscore,
AlphaNumericDashUnderscore: v.AlphaNumericDashUnderscore,
DNS1035: v.DNS1035,
DNS1123: v.DNS1123,
}
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/lib/urls/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)),
}
}
14 changes: 12 additions & 2 deletions pkg/lib/urls/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
1 change: 1 addition & 0 deletions pkg/operator/api/userconfig/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var appValidation = &cr.StructValidation{
StringValidation: &cr.StringValidation{
Required: true,
AlphaNumericDashUnderscore: true,
DNS1123: true,
},
},
typeFieldValidation,
Expand Down