From d656c63b21076546d0a74fff11b0fa941ad0c094 Mon Sep 17 00:00:00 2001 From: Phil Estes Date: Wed, 4 Feb 2015 10:20:28 -0500 Subject: [PATCH] Allow IPv6 addresses in ExtraHosts option settings Since the separator for extra host settings (for /etc/hosts in a container) is a ":", the code that handles extra hosts needed to only split on the first ":" to preserve IPv6 addresses which are passed via the command line settings as well as stored in the JSON container config. Docker-DCO-1.1-Signed-off-by: Phil Estes (github: estesp) --- opts/opts.go | 3 ++- opts/opts_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/opts/opts.go b/opts/opts.go index 7f40193412..1b57b77536 100644 --- a/opts/opts.go +++ b/opts/opts.go @@ -204,7 +204,8 @@ func validateDomain(val string) (string, error) { } func ValidateExtraHost(val string) (string, error) { - arr := strings.Split(val, ":") + // allow for IPv6 addresses in extra hosts by only splitting on first ":" + arr := strings.SplitN(val, ":", 2) if len(arr) != 2 || len(arr[0]) == 0 { return "", fmt.Errorf("bad format for add-host: %s", val) } diff --git a/opts/opts_test.go b/opts/opts_test.go index e813c44326..68a715567c 100644 --- a/opts/opts_test.go +++ b/opts/opts_test.go @@ -104,3 +104,31 @@ func TestValidateDnsSearch(t *testing.T) { } } } + +func TestValidateExtraHosts(t *testing.T) { + valid := []string{ + `myhost:192.168.0.1`, + `thathost:10.0.2.1`, + `anipv6host:2003:ab34:e::1`, + `ipv6local:::1`, + } + + invalid := []string{ + `myhost:192.notanipaddress.1`, + `thathost-nosemicolon10.0.0.1`, + `anipv6host:::::1`, + `ipv6local:::0::`, + } + + for _, extrahost := range valid { + if _, err := ValidateExtraHost(extrahost); err != nil { + t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) + } + } + + for _, extrahost := range invalid { + if _, err := ValidateExtraHost(extrahost); err == nil { + t.Fatalf("ValidateExtraHost(`" + extrahost + "`) should have failed validation") + } + } +}