Skip to content

Commit

Permalink
Merge 3e5e06d into a449cc4
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukkie committed Jul 1, 2019
2 parents a449cc4 + 3e5e06d commit 9a41b3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 10 additions & 2 deletions core/internal/helpers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ func ValidateIP(ipaddr string) bool {
// * Valid characters in a segment are letters, numbers, and dashes
// * Segments may not start or end with a dash
// * The exception is IPv6 addresses, which are also permitted.
// * An underscore is allowed to support Docker Swarm service names.
func ValidateHostname(hostname string) bool {
matches, _ := regexp.MatchString(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`, hostname)

if !matches {
// Try as an IP address
return ValidateIP(hostname)
// Try Docker Swarm service name
matchesDocker, _ := regexp.MatchString(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\_([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])$`, hostname)
if !matchesDocker {
// Try as an IP address
return ValidateIP(hostname)
}
return true
}

return matches
}

Expand Down
9 changes: 8 additions & 1 deletion core/internal/helpers/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
package helpers

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

type TestSet struct {
Expand Down Expand Up @@ -53,6 +54,12 @@ var testHostnames = []TestSet{
{"800.hostnames.starting.with.numbers.are.valid.because.people.suck.org", true},
{"hostnames-.may.not.end.with.a.dash.com", false},
{"no spaces.com", false},
{"docker_service.name.should.not.contain.dots", false},
{"docker-swarmservice_name-with-one-underscore-is-valid", true},
{"invalid-docker-_service-name", false},
{"invalid-docker_-service-name", false},
{"docker-service-may-not-end-with-underscore_", false},
{"_docker-service-may-not-start-with-underscore", false},
}

func TestValidateHostname(t *testing.T) {
Expand Down

0 comments on commit 9a41b3b

Please sign in to comment.