Browse code

Reject unspecified advertise addr on swarm init

Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn)

(cherry picked from commit eeac8719466dbc42de7a9919d4f549863944aa19)
Signed-off-by: Victor Vieux <victorvieux@gmail.com>

Josh Hawn authored on 2016/11/29 08:16:59
Showing 2 changed files
... ...
@@ -11,8 +11,8 @@ var (
11 11
 	errNoIP                    = errors.New("could not find the system's IP address")
12 12
 	errMustSpecifyListenAddr   = errors.New("must specify a listening address because the address to advertise is not recognized as a system address, and a system's IP address to use could not be uniquely identified")
13 13
 	errBadListenAddr           = errors.New("listen address must be an IP address or network interface (with optional port number)")
14
-	errBadAdvertiseAddr        = errors.New("advertise address must be an IP address or network interface (with optional port number)")
15
-	errBadDefaultAdvertiseAddr = errors.New("default advertise address must be an IP address or network interface (without a port number)")
14
+	errBadAdvertiseAddr        = errors.New("advertise address must be a non-zero IP address or network interface (with optional port number)")
15
+	errBadDefaultAdvertiseAddr = errors.New("default advertise address must be a non-zero IP address or network interface (without a port number)")
16 16
 )
17 17
 
18 18
 func resolveListenAddr(specifiedAddr string) (string, string, error) {
... ...
@@ -69,7 +69,7 @@ func (c *Cluster) resolveAdvertiseAddr(advertiseAddr, listenAddrPort string) (st
69 69
 		}
70 70
 
71 71
 		// If it's not an interface, it must be an IP (for now)
72
-		if net.ParseIP(advertiseHost) == nil {
72
+		if ip := net.ParseIP(advertiseHost); ip == nil || ip.IsUnspecified() {
73 73
 			return "", "", errBadAdvertiseAddr
74 74
 		}
75 75
 
... ...
@@ -89,7 +89,7 @@ func (c *Cluster) resolveAdvertiseAddr(advertiseAddr, listenAddrPort string) (st
89 89
 		}
90 90
 
91 91
 		// If it's not an interface, it must be an IP (for now)
92
-		if net.ParseIP(c.config.DefaultAdvertiseAddr) == nil {
92
+		if ip := net.ParseIP(c.config.DefaultAdvertiseAddr); ip == nil || ip.IsUnspecified() {
93 93
 			return "", "", errBadDefaultAdvertiseAddr
94 94
 		}
95 95
 
... ...
@@ -86,6 +86,13 @@ func (s *DockerSwarmSuite) TestSwarmInitIPv6(c *check.C) {
86 86
 	c.Assert(out, checker.Contains, "Swarm: active")
87 87
 }
88 88
 
89
+func (s *DockerSwarmSuite) TestSwarmInitUnspecifiedAdvertiseAddr(c *check.C) {
90
+	d := s.AddDaemon(c, false, false)
91
+	out, err := d.Cmd("swarm", "init", "--advertise-addr", "0.0.0.0")
92
+	c.Assert(err, checker.NotNil)
93
+	c.Assert(out, checker.Contains, "advertise address must be a non-zero IP address")
94
+}
95
+
89 96
 func (s *DockerSwarmSuite) TestSwarmIncompatibleDaemon(c *check.C) {
90 97
 	// init swarm mode and stop a daemon
91 98
 	d := s.AddDaemon(c, true, true)