Skip to content

Commit 0c98c22

Browse files
authored
Validate that app name is valid DNS1123 (#112)
1 parent 92d7689 commit 0c98c22

File tree

6 files changed

+37
-3
lines changed

6 files changed

+37
-3
lines changed

cli/cmd/init.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/cortexlabs/cortex/pkg/lib/errors"
2727
"github.com/cortexlabs/cortex/pkg/lib/files"
28+
"github.com/cortexlabs/cortex/pkg/lib/urls"
2829
)
2930

3031
var initCmd = &cobra.Command{
@@ -35,6 +36,10 @@ var initCmd = &cobra.Command{
3536
Run: func(cmd *cobra.Command, args []string) {
3637
appName := args[0]
3738

39+
if err := urls.CheckDNS1123(appName); err != nil {
40+
errors.Exit(err)
41+
}
42+
3843
cwd, err := os.Getwd()
3944
if err != nil {
4045
errors.Exit(err)

pkg/lib/configreader/string.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type StringValidation struct {
3737
AlphaNumericDashDotUnderscore bool
3838
AlphaNumericDashUnderscore bool
3939
DNS1035 bool
40+
DNS1123 bool
4041
Validator func(string) (string, error)
4142
}
4243

@@ -203,6 +204,12 @@ func ValidateStringVal(val string, v *StringValidation) error {
203204
}
204205
}
205206

207+
if v.DNS1123 {
208+
if err := urls.CheckDNS1123(val); err != nil {
209+
return err
210+
}
211+
}
212+
206213
return nil
207214
}
208215

pkg/lib/configreader/string_ptr.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type StringPtrValidation struct {
3232
AlphaNumericDashDotUnderscore bool
3333
AlphaNumericDashUnderscore bool
3434
DNS1035 bool
35+
DNS1123 bool
3536
Validator func(*string) (*string, error)
3637
}
3738

@@ -43,6 +44,7 @@ func makeStringValValidation(v *StringPtrValidation) *StringValidation {
4344
AlphaNumericDashDotUnderscore: v.AlphaNumericDashDotUnderscore,
4445
AlphaNumericDashUnderscore: v.AlphaNumericDashUnderscore,
4546
DNS1035: v.DNS1035,
47+
DNS1123: v.DNS1123,
4648
}
4749
}
4850

pkg/lib/urls/errors.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ const (
2828
ErrUnknown ErrorKind = iota
2929
ErrInvalidURL
3030
ErrDNS1035
31+
ErrDNS1123
3132
)
3233

3334
var errorKinds = []string{
3435
"err_unknown",
3536
"err_invalid_url",
3637
"err_dns1035",
38+
"err_dns1123",
3739
}
3840

39-
var _ = [1]int{}[int(ErrDNS1035)-(len(errorKinds)-1)] // Ensure list length matches
41+
var _ = [1]int{}[int(ErrDNS1123)-(len(errorKinds)-1)] // Ensure list length matches
4042

4143
func (t ErrorKind) String() string {
4244
return errorKinds[t]
@@ -94,3 +96,10 @@ func ErrorDNS1035(provided string) error {
9496
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)),
9597
}
9698
}
99+
100+
func ErrorDNS1123(provided string) error {
101+
return Error{
102+
Kind: ErrDNS1123,
103+
message: fmt.Sprintf("%s must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character", s.UserStr(provided)),
104+
}
105+
}

pkg/lib/urls/urls.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import (
2424
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2525
)
2626

27-
var dns1035Regex = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
27+
var (
28+
dns1035Regex = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
29+
dns1123Regex = regexp.MustCompile(`^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`)
30+
)
2831

2932
func Parse(rawurl string) (*url.URL, error) {
3033
u, err := url.Parse(rawurl)
@@ -49,7 +52,14 @@ func Join(strs ...string) string {
4952

5053
func CheckDNS1035(s string) error {
5154
if !dns1035Regex.MatchString(s) {
52-
ErrorDNS1035(s)
55+
return ErrorDNS1035(s)
56+
}
57+
return nil
58+
}
59+
60+
func CheckDNS1123(s string) error {
61+
if !dns1123Regex.MatchString(s) {
62+
return ErrorDNS1123(s)
5363
}
5464
return nil
5565
}

pkg/operator/api/userconfig/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var appValidation = &cr.StructValidation{
3232
StringValidation: &cr.StringValidation{
3333
Required: true,
3434
AlphaNumericDashUnderscore: true,
35+
DNS1123: true,
3536
},
3637
},
3738
typeFieldValidation,

0 commit comments

Comments
 (0)