Skip to content

Commit 1a7eb36

Browse files
committed
Merge pull request #372 from philips/bind-addr-ports
fix(server): override port of bind
2 parents d971d22 + 9db521c commit 1a7eb36

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

server/config.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ func (c *Config) PeerTLSConfig() (TLSConfig, error) {
445445
return c.PeerTLSInfo().Config()
446446
}
447447

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

479-
ahost, aport, err := net.SplitHostPort(aurl.Host)
480-
if err != nil {
481-
return "", err
479+
// If it is a valid host:port simply return with no further checks.
480+
bhost, bport, err := net.SplitHostPort(bindAddr)
481+
if err == nil && bhost != "" {
482+
return bindAddr, nil
483+
}
484+
485+
// SplitHostPort makes the host optional, but we don't want that.
486+
if bhost == "" && bport != "" {
487+
return "", fmt.Errorf("IP required can't use a port only")
482488
}
483489

484-
// If the listen host isn't set use the advertised host
485-
if bindAddr == "" {
486-
bindAddr = ahost
490+
// bindAddr doesn't have a port if we reach here so take the port from the
491+
// advertised URL.
492+
_, aport, err := net.SplitHostPort(aurl.Host)
493+
if err != nil {
494+
return "", err
487495
}
488496

489497
return net.JoinHostPort(bindAddr, aport), nil

server/config_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,29 @@ func TestConfigBindAddrFlag(t *testing.T) {
223223
assert.Equal(t, c.BindAddr, "127.0.0.1:4003", "")
224224
}
225225

226+
// Ensures that a the Listen Host port overrides the advertised port
227+
func TestConfigBindAddrOverride(t *testing.T) {
228+
c := NewConfig()
229+
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1:4010"}), "")
230+
assert.Nil(t, c.Sanitize())
231+
assert.Equal(t, c.BindAddr, "127.0.0.1:4010", "")
232+
}
233+
234+
// Ensures that a the Listen Host inherits its port from the advertised addr
235+
func TestConfigBindAddrInheritPort(t *testing.T) {
236+
c := NewConfig()
237+
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", "127.0.0.1"}), "")
238+
assert.Nil(t, c.Sanitize())
239+
assert.Equal(t, c.BindAddr, "127.0.0.1:4009", "")
240+
}
241+
242+
// Ensures that a port only argument errors out
243+
func TestConfigBindAddrErrorOnNoHost(t *testing.T) {
244+
c := NewConfig()
245+
assert.Nil(t, c.LoadFlags([]string{"-addr", "127.0.0.1:4009", "-bind-addr", ":4010"}), "")
246+
assert.Error(t, c.Sanitize())
247+
}
248+
226249
// Ensures that the peers can be parsed from the environment.
227250
func TestConfigPeersEnv(t *testing.T) {
228251
withEnv("ETCD_PEERS", "coreos.com:4001,coreos.com:4002", func(c *Config) {

server/usage.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ Cluster Configuration Options:
3131
should match the peer's '-peer-addr' flag.
3232
3333
Client Communication Options:
34-
-addr=<host:port> The public host:port used for client communication.
35-
-bind-addr=<host> The listening hostname used for client communication.
36-
-ca-file=<path> Path to the client CA file.
37-
-cert-file=<path> Path to the client cert file.
38-
-key-file=<path> Path to the client key file.
34+
-addr=<host:port> The public host:port used for client communication.
35+
-bind-addr=<host[:port]> The listening host:port used for client communication.
36+
-ca-file=<path> Path to the client CA file.
37+
-cert-file=<path> Path to the client cert file.
38+
-key-file=<path> Path to the client key file.
3939
4040
Peer Communication Options:
41-
-peer-addr=<host:port> The public host:port used for peer communication.
42-
-peer-bind-addr=<host> The listening hostname used for peer communication.
43-
-peer-ca-file=<path> Path to the peer CA file.
44-
-peer-cert-file=<path> Path to the peer cert file.
45-
-peer-key-file=<path> Path to the peer key file.
41+
-peer-addr=<host:port> The public host:port used for peer communication.
42+
-peer-bind-addr=<host[:port]> The listening host:port used for peer communication.
43+
-peer-ca-file=<path> Path to the peer CA file.
44+
-peer-cert-file=<path> Path to the peer cert file.
45+
-peer-key-file=<path> Path to the peer key file.
4646
4747
Other Options:
4848
-max-result-buffer Max size of the result buffer.

0 commit comments

Comments
 (0)