Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose safer default advertise address #1955

Merged
merged 15 commits into from
Nov 10, 2016
Merged

Conversation

schmichael
Copy link
Member

  • -dev mode defaults bind & advertise to localhost
  • Normal mode defaults bind to 0.0.0.0 & advertise to the resolved
    hostname. If the hostname resolves to localhost it will refuse to
    start and advertise must be manually set.

@@ -115,7 +110,7 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
if a.config.Server.BootstrapExpect == 1 {
conf.Bootstrap = true
} else {
atomic.StoreInt32(&conf.BootstrapExpect, int32(a.config.Server.BootstrapExpect))
conf.BootstrapExpect = int32(a.config.Server.BootstrapExpect)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is never called concurrently with any other readers/writers (it's before the server is started), so I removed the atomic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use the atomics. It is easier to just know that all users of the atomic variable are accessing it atomically

@@ -115,7 +110,7 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
if a.config.Server.BootstrapExpect == 1 {
conf.Bootstrap = true
} else {
atomic.StoreInt32(&conf.BootstrapExpect, int32(a.config.Server.BootstrapExpect))
conf.BootstrapExpect = int32(a.config.Server.BootstrapExpect)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just use the atomics. It is easier to just know that all users of the atomic variable are accessing it atomically

@@ -135,11 +130,11 @@ func (a *Agent) serverConfig() (*nomad.Config, error) {
}

// Set up the bind addresses
rpcAddr, err := a.getRPCAddr(true)
rpcAddr, err := net.ResolveTCPAddr("tcp", a.config.Addresses.RPC)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned offline that it isn't always the case that the client can resolve its advertise address

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some investigation and discussion I discovered RPC and Serf advertise addresses are resolved later on in much harder to fix places, so we're not going to worry about it in this PR.

// Use getAdvertise(&config.AdvertiseAddrs.<Service>, <Port>) to
// retrieve a default address for advertising if one isn't set.
defaultAdvertise := ""
getAdvertise := func(addr *string, port int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it its own function func(addr string, port int) (string, error) so that it can be tested

@@ -234,6 +234,7 @@ func NewServer(config *Config, consulSyncer *consul.Syncer, logger *log.Logger)
}

// Initialize the RPC layer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove?

}

// Autoconfigure using previously set default
if defaultAdvertise != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After its own function think you can remove this

// retrieve a default address for advertising if one isn't set.
defaultAdvertise := ""
getAdvertise := func(addr *string, port int) bool {
if *addr != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should still be an error if they explicitly advertise 0.0.0.0 or localhost (in any of its ipv4 or v6 forms, or via resolution)

defaultAdvertise := ""
getAdvertise := func(addr *string, port int) bool {
if *addr != "" {
// Default to using manually configured address
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to append the default port (see original behavior)

* -dev mode defaults bind & advertise to localhost
* Normal mode defaults bind to 0.0.0.0 & advertise to the resolved
  hostname. If the hostname resolves to localhost it will refuse to
  start and advertise must be manually set.
@schmichael schmichael force-pushed the b-fix-default-advertise branch from 4c2e6cc to 2c4b19f Compare November 8, 2016 21:07
if addr := a.serverSerfAddr; addr != "10.0.0.12:4004" {
t.Fatalf("expect 10.0.0.12:4004, got: %s", addr)
if port := out.SerfConfig.MemberlistConfig.BindPort; port != 4004 {
t.Fatalf("expect 4648, got: ^d", port)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%d

//
// Returns true if config is ok and false on errors. Since some normalization
// issues aren't fatal a logger must be provided to emit warnings.
func (c *Config) normalize(logger func(string), dev bool) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return an error instead?


// Set the default port for this service to the bound port to keep
// configuration consistent.
if port != *defport {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you doing here? Have to say not really a fan of taking a pointer to the value. More idiomatic to just return the new value and not modify the caller

@@ -658,137 +663,116 @@ func (c *Config) Merge(b *Config) *Config {
return &result
}

// normalize config to set defaults and prevent nil derefence panics.
// normalize Addresses and AdvertiseAddrs to always be initialized and have
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is a little wrong

if err != nil {
return fmt.Errorf("Failed to parse HTTP advertise address: %v", err)
}
c.AdvertiseAddrs.HTTP = addr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine for this PR but I would have imagined having normalizedBindAddrs and normalizedAdvertiseAddrs and never modifying the user supplied configs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made an issue and assigned to me - #1971

//
// Loopback is only considered a valid advertise address in dev mode.
func normalizeAdvertise(addr string, bind string, defport int, dev bool) (string, error) {
if addr != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if given a host name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[summarizing offline discussion here]

Uses it here but hard fails later in rpc/serf code that resolves advertise addresses.

This matches 0.4.1 behavior, and trying to fix rpc & serf so that they don't resolve advertise addresses is out of scope for 0.5.

Leaving as-is.

@schmichael schmichael merged commit fa67fb6 into master Nov 10, 2016
@schmichael schmichael deleted the b-fix-default-advertise branch November 10, 2016 00:03
schmichael added a commit that referenced this pull request Nov 10, 2016
schmichael added a commit that referenced this pull request Nov 10, 2016
Update address docs to match behavior in #1955
devendram pushed a commit to pubnub/nomad that referenced this pull request Nov 14, 2016
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants