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

Add support for late binding to IP addresses using go-sockaddr/template #2399

Merged
merged 5 commits into from
May 12, 2017
Merged

Add support for late binding to IP addresses using go-sockaddr/template #2399

merged 5 commits into from
May 12, 2017

Conversation

multani
Copy link
Contributor

@multani multani commented Mar 5, 2017

This PR adds support for late IP addresses binding as was merged in Consul in 0.7.2 and is based loosely on the same code.

As far as I'm aware, all the *addr flags or configuration options should be able to use this new format. I converted the code in normalizeAdvertise(), but I'm not sure about the conversion. It seems to me that this code could be all replaced by the functionality provided by go-sockaddr/template in a more flexible way, but I may have miss some behaviors there.

Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

It seems to me that this code could be all replaced by the functionality provided by go-sockaddr/template in a more flexible way, but I may have miss some behaviors there.

You're absolutely right about that! Definitely something I had been wanting to get to, so thanks for the PR!

Left some comments where your new logic needs to match the old logic. We'll also need to update CHANGELOG and docs (in website/).

Thanks again for taking a stab at this. I think sockaddr/template will make people very happy.

if ip.IsLoopback() && dev {
// loopback is fine for dev mode
return net.JoinHostPort(ip.String(), strconv.Itoa(defport)), nil
ipStr, err := parseSingleIPTemplate(host)
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to parse the template before attempting to SplitHostPort as SplitHostPort may fail on valid templates.

}
return "", fmt.Errorf("No valid advertise addresses, please set `advertise` manually")
// Fallback to bind address, as it has been resolved before.
return net.JoinHostPort(bind, strconv.Itoa(defport)), nil
Copy link
Member

Choose a reason for hiding this comment

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

There are 2 subtle behaviors I think your code is missing:

  1. Advertising localhost is disallowed unless:
  • Explicitly configured per advertise addr (basically use any explicitly configured addresses without asking questions)
  • DevMode == true
  1. Default to advertising the IP the hostname resolves as.
  • Sadly outside of Google Compute Engine few systems are properly configured to be able to resolve their hostname.

Since 2. doesn't work well in practice I'd be open to changing it to GetPrivateIP, but we'll need to update docs and mark this as a backward incompatible change in CHANGELOG.md

This is out of scope for what you're trying to do, but I also think we'll move bind to default to GetPrivateIP+127.0.0.1 in the future. Sadly we don't support multiple bind addresses at the moment, so it's going to be a larger effort.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, Consul will be likely making a change to its defaults in time for its 0.8 release to use GetPrivateIP as its default bind address. And yes, listening on multiple interfaces is something we all want to do (though I'd argue it should be a UNIX socket in /tmp vs 127.0.0.1 that way filesystem permissions could be applied if necessary, and you can figure out the UID of who is connecting to you via a unix socket, but not via loopback - but more on that when we get closer to multiple listeners).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@schmichael OK, so I changed the code slightly and re-added bits from the previous version, except:

  • I removed the os.Hostname() and related net.LookupIP(host) as per the problems you exposed in 2. and I'm looking up the private IP address using GetPrivateIP instead.
  • before that, I'm trying to use the bind address. I still need to do the ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast() dance since the bind address could be something like localhost which needs to be parsed in order to use IsLoopback(). I thought I could remove this part and profit from what go-sockaddr/template offers for this instead, but I'm not sure how I could prevent 127.0.0.1 to be advertised without resolving the bind address value, in which case I need to call the functions above to do the right thing...

t.Fatalf("expected HTTP advertise address 127.0.0.1:4646, got %s", c.AdvertiseAddrs.HTTP)
}

if c.AdvertiseAddrs.RPC != "127.0.0.1:4647" {
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we don't want this to happen unless AdvertiseAddrs.RPC is explicitly set to localhost.

The reason is advertising localhost any time other than for a local test node can do Very Bad Things. The worst case scenario is having a server agent advertise localhost to the cluster - even briefly! Nodes will spam their own localhost trying to contact that other node that advertised localhost. CPU and network usage will be extremely high, but things might still be able to limp along depending on other factors. So basically you end up with a crippled cluster in a difficult to diagnose way.

We could add better heuristics for advertising localhost... like only disallow it if boostrap_expect>1. But then things just get even more magical and complicated. I'd prefer to just force everyone not in dev mode to advertise a real address! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, but the configuration explicitly enabled dev mode in this case, so ... I guess it's OK to advertise 127.0.0.1 here?
I'm going to add another test to specifically check that 127.0.0.1 is NOT advertised except if dev mode is enabled or if it has been explicitly configured to do so, but could I make this particular test above clearer?

Copy link
Contributor

Choose a reason for hiding this comment

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

re: dev mode, yes, that sounds good and is a sane approach.

@multani
Copy link
Contributor Author

multani commented Mar 22, 2017

@schmichael I updated the PR with some bits in the documentation. AFAIK Consul hasn't really advertise there support in their documentation, except for the entry in the Changelog, so I'm not really sure how to document it here. I can add a ~> note block to explain succinctly the templating support if needed.

Is there anything else that should be done to move forward with this PR?

@schmichael
Copy link
Member

Terribly sorry for letting this languish for so long! Thanks!

@schmichael schmichael merged commit b9bd1b0 into hashicorp:master May 12, 2017
schmichael added a commit that referenced this pull request May 12, 2017
A slight improvement to #2399 - if bind is localhost, return an error
instead of advertising a private ip. The advertised ip isn't valid and
will just cause errors on use. It's better to fail with an error message
instructing users how to fix the problem.
schmichael added a commit that referenced this pull request May 30, 2017
A slight improvement to #2399 - if bind is localhost, return an error
instead of advertising a private ip. The advertised ip isn't valid and
will just cause errors on use. It's better to fail with an error message
instructing users how to fix the problem.
schmichael added a commit that referenced this pull request May 30, 2017
Don't advertise sockaddr support just yet; focus on the saner advertise default
@github-actions
Copy link

github-actions bot commented Apr 1, 2023

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 1, 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.

None yet

3 participants