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

fix: Add domain name validation when configuring ingress to loadbalancer #2334

Merged
merged 8 commits into from Nov 27, 2018
7 changes: 4 additions & 3 deletions pkg/jx/cmd/init.go
Expand Up @@ -817,7 +817,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr
return domain, err
}
log.Infof("\nOn AWS we recommend using a custom DNS name to access services in your Kubernetes cluster to ensure you can use all of your Availability Zones\n")
log.Infof("If you do not have a custom DNS name you can use yet you can register a new one here: %s\n\n", util.ColorInfo("https://console.aws.amazon.com/route53/home?#DomainRegistration:"))
log.Infof("If you do not have a custom DNS name you can use yet, then you can register a new one here: %s\n\n", util.ColorInfo("https://console.aws.amazon.com/route53/home?#DomainRegistration:"))

for {
if util.Confirm("Would you like to register a wildcard DNS ALIAS to point at this ELB address? ", true,
Expand Down Expand Up @@ -902,7 +902,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr
}
log.Successf("You can now configure a wildcard DNS pointing to the new loadbalancer address %s", address)
log.Info("\nIf you do not have a custom domain setup yet, Ingress rules will be set for magic dns nip.io.")
log.Infof("\nOnce you have a customer domain ready, you can update with the command %s", util.ColorInfo("jx upgrade ingress --cluster"))
log.Infof("\nOnce you have a custom domain ready, you can update with the command %s", util.ColorInfo("jx upgrade ingress --cluster"))

log.Infof("\nIf you don't have a wildcard DNS setup then setup a new CNAME and point it at: %s then use the DNS domain in the next input...\n", defaultDomain)

Expand All @@ -912,7 +912,7 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr
Default: defaultDomain,
Help: "Enter your custom domain that is used to generate Ingress rules, defaults to the magic dns nip.io",
}
survey.AskOne(prompt, &domain, survey.Required, surveyOpts)
survey.AskOne(prompt, &domain, survey.ComposeValidators(survey.Required, util.NoWhiteSpaceValidator()), surveyOpts)
}
if domain == "" {
domain = defaultDomain
Expand All @@ -925,3 +925,4 @@ func (o *CommonOptions) GetDomain(client kubernetes.Interface, domain string, pr

return domain, nil
}

23 changes: 23 additions & 0 deletions pkg/util/validators.go
@@ -0,0 +1,23 @@
package util

import (
"fmt"
"gopkg.in/AlecAivazis/survey.v1"
"strings"
)

//NoWhiteSpaceValidator is an input validator for the survey package that disallows any whitespace in the input
func NoWhiteSpaceValidator() survey.Validator {
// return a validator that ensures the given string does not contain any whitespace
return func(val interface{}) error {
if str, ok := val.(string); ok {
if strings.ContainsAny(str, " ") {
// yell loudly
return fmt.Errorf("supplied value \"%v\" must not contain any whitespace", str)
}
}
// the input is fine
return nil
}
}

40 changes: 40 additions & 0 deletions pkg/util/validators_test.go
@@ -0,0 +1,40 @@
package util_test

import (
"github.com/jenkins-x/jx/pkg/util"
"github.com/stretchr/testify/assert"
"gopkg.in/AlecAivazis/survey.v1"
"testing"
)

func TestNoWhitespaceValidator(t *testing.T) {
t.Parallel()

tests := []struct {
testName string
domainName string
want string
}{
{"leading whitespace"," fake.com", "supplied value \" fake.com\" must not contain any whitespace"},
{"trailing whitespace","fake.com ", "supplied value \"fake.com \" must not contain any whitespace"},
{"embedded whitespace","fake .com", "supplied value \"fake .com\" must not contain any whitespace"},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
assert.Equal(t, tt.want, testInputValidation(t, tt.domainName))
})
}
}

func testInputValidation(t *testing.T, s string) interface{} {
valid := survey.ComposeValidators(
util.NoWhiteSpaceValidator(),
)
err := valid(s)
if err != nil {
return err.Error()
}
return ""
}