Skip to content

Commit

Permalink
Merge pull request #372 from philips/bind-addr-ports
Browse files Browse the repository at this point in the history
fix(server): override port of bind
  • Loading branch information
philips committed Dec 5, 2013
2 parents d971d22 + 9db521c commit 1a7eb36
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
22 changes: 15 additions & 7 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func (c *Config) PeerTLSConfig() (TLSConfig, error) {
return c.PeerTLSInfo().Config()
}

// sanitizeURL will cleanup a host string in the format hostname:port and
// sanitizeURL will cleanup a host string in the format hostname[:port] and
// attach a schema.
func sanitizeURL(host string, defaultScheme string) (string, error) {
// Blank URLs are fine input, just return it
Expand Down Expand Up @@ -476,14 +476,22 @@ func sanitizeBindAddr(bindAddr string, addr string) (string, error) {
return "", err
}

ahost, aport, err := net.SplitHostPort(aurl.Host)
if err != nil {
return "", err
// If it is a valid host:port simply return with no further checks.
bhost, bport, err := net.SplitHostPort(bindAddr)
if err == nil && bhost != "" {
return bindAddr, nil
}

// SplitHostPort makes the host optional, but we don't want that.
if bhost == "" && bport != "" {
return "", fmt.Errorf("IP required can't use a port only")
}

// If the listen host isn't set use the advertised host
if bindAddr == "" {
bindAddr = ahost
// bindAddr doesn't have a port if we reach here so take the port from the
// advertised URL.
_, aport, err := net.SplitHostPort(aurl.Host)
if err != nil {
return "", err
}

return net.JoinHostPort(bindAddr, aport), nil
Expand Down
23 changes: 23 additions & 0 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ func TestConfigBindAddrFlag(t *testing.T) {
assert.Equal(t, c.BindAddr, "127.0.0.1:4003", "")
}

// Ensures that a the Listen Host port overrides the advertised port
func TestConfigBindAddrOverride(t *testing.T) {
c := NewConfig()
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1:4010"}), "")
assert.Nil(t, c.Sanitize())
assert.Equal(t, c.BindAddr, "127.0.0.1:4010", "")
}

// Ensures that a the Listen Host inherits its port from the advertised addr
func TestConfigBindAddrInheritPort(t *testing.T) {
c := NewConfig()
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1"}), "")
assert.Nil(t, c.Sanitize())
assert.Equal(t, c.BindAddr, "127.0.0.1:4009", "")
}

// Ensures that a port only argument errors out
func TestConfigBindAddrErrorOnNoHost(t *testing.T) {
c := NewConfig()
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", ":4010"}), "")
assert.Error(t, c.Sanitize())
}

// Ensures that the peers can be parsed from the environment.
func TestConfigPeersEnv(t *testing.T) {
withEnv("ETCD_PEERS", "coreos.com:4001,coreos.com:4002", func(c *Config) {
Expand Down
20 changes: 10 additions & 10 deletions server/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ Cluster Configuration Options:
should match the peer's '-peer-addr' flag.
Client Communication Options:
-addr=<host:port> The public host:port used for client communication.
-bind-addr=<host> The listening hostname used for client communication.
-ca-file=<path> Path to the client CA file.
-cert-file=<path> Path to the client cert file.
-key-file=<path> Path to the client key file.
-addr=<host:port> The public host:port used for client communication.
-bind-addr=<host[:port]> The listening host:port used for client communication.
-ca-file=<path> Path to the client CA file.
-cert-file=<path> Path to the client cert file.
-key-file=<path> Path to the client key file.
Peer Communication Options:
-peer-addr=<host:port> The public host:port used for peer communication.
-peer-bind-addr=<host> The listening hostname used for peer communication.
-peer-ca-file=<path> Path to the peer CA file.
-peer-cert-file=<path> Path to the peer cert file.
-peer-key-file=<path> Path to the peer key file.
-peer-addr=<host:port> The public host:port used for peer communication.
-peer-bind-addr=<host[:port]> The listening host:port used for peer communication.
-peer-ca-file=<path> Path to the peer CA file.
-peer-cert-file=<path> Path to the peer cert file.
-peer-key-file=<path> Path to the peer key file.
Other Options:
-max-result-buffer Max size of the result buffer.
Expand Down

0 comments on commit 1a7eb36

Please sign in to comment.