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

server: resolve hostnames before use #16177

Merged
merged 1 commit into from
Jun 19, 2017
Merged

server: resolve hostnames before use #16177

merged 1 commit into from
Jun 19, 2017

Conversation

tamird
Copy link
Contributor

@tamird tamird commented May 26, 2017

It seems that configurations where the OS-provided hostname doesn't
actually resolve from the local machine are quite common and can cause
hard to diagnose problems for users. Avoid using the OS-provided host
if we're not even able to resolve it locally.

Updates #16173.

@tamird tamird requested a review from petermattis May 26, 2017 20:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@petermattis
Copy link
Collaborator

Channeling my inner Tamir: doesn't this deserve a test?


Review status: 0 of 2 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


pkg/server/server.go, line 1030 at r1 (raw file):

		if unresolvedHost != "" {
			// A host was provided, use it.
			return unresolvedHost

Shouldn't we try to resolve the hostname even when one was explicitly provided? This will probably require updating TestOfficializeAddr.


Comments from Reviewable

@tamird
Copy link
Contributor Author

tamird commented May 27, 2017

Why it certainly does! I don't know how to make os.Hostname return something predictably non-resolvable, though. Any ideas?


Review status: 0 of 2 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


pkg/server/server.go, line 1030 at r1 (raw file):

Previously, petermattis (Peter Mattis) wrote…

Shouldn't we try to resolve the hostname even when one was explicitly provided? This will probably require updating TestOfficializeAddr.

Well, if one was explicitly provided, it's conceivable that the user simply knows better than we do. This function is use to "officialize" 3 addresses:

  • s.cfg.Addr
  • s.cfg.AdvertiseAddr
  • s.cfg.HTTPAddr

The user may very well know better than us in the case of the latter two (i.e. the user is giving us an address that is externally resolvable, despite not being locally resolvable).

s.cfg.Addr is used to make a loopback grpc connection for the grpc-gateway handlers, though. Still, if a user gives us a nonsense --addr value, I think we should do what they say rather than try to be clever.


Comments from Reviewable

@petermattis
Copy link
Collaborator

I don't know how to make os.Hostname return something predictably non-resolvable, though. Any ideas?

Pass os.Hostname to the method that uses it. In a test, pass a closure.


Review status: 0 of 2 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed.


pkg/server/server.go, line 1030 at r1 (raw file):

Previously, tamird (Tamir Duberstein) wrote…

Well, if one was explicitly provided, it's conceivable that the user simply knows better than we do. This function is use to "officialize" 3 addresses:

  • s.cfg.Addr
  • s.cfg.AdvertiseAddr
  • s.cfg.HTTPAddr

The user may very well know better than us in the case of the latter two (i.e. the user is giving us an address that is externally resolvable, despite not being locally resolvable).

s.cfg.Addr is used to make a loopback grpc connection for the grpc-gateway handlers, though. Still, if a user gives us a nonsense --addr value, I think we should do what they say rather than try to be clever.

This is exactly the sort of misconfiguration we should be helping the user discover. Are there common deployment scenarios where a hostname is externally resolvable despite not being locally resolvable?

At the very least, we should be resolving the hostname and shouting a warning (but still using the hostname) if it is not resolvable.


Comments from Reviewable

@bdarnell
Copy link
Contributor

That's half the problem. Finding a reliably non-resolvable hostname is tricky too. I've seen resolvers give results (the bogus ad-insertion kind) even for names containing spaces and other garbage. Fortunately, Go's resolver guarantees that an empty string will never resolve.


Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions, some commit checks failed.


pkg/server/server.go, line 1030 at r1 (raw file):

Previously, petermattis (Peter Mattis) wrote…

This is exactly the sort of misconfiguration we should be helping the user discover. Are there common deployment scenarios where a hostname is externally resolvable despite not being locally resolvable?

At the very least, we should be resolving the hostname and shouting a warning (but still using the hostname) if it is not resolvable.

There are common scenarios where a hostname resolves differently locally than it does remotely (i.e. it may resolve to a loopback IP instead of a routable one). But I think it's correct to require that it always resolve to something locally.


pkg/server/server.go, line 1036 at r1 (raw file):

		// resolved address.
		if name, err := os.Hostname(); err != nil {
			log.ErrEventf(ctx, "unable to get hostname: %s", err)

Why are we logging this error (and below) instead of returning it? These errors should cause the server to fail to start instead of just logging quietly.


Comments from Reviewable

@tamird tamird changed the title server: resolve OS-provided hostname before using server: resolve hostnames before use Jun 19, 2017
@tamird
Copy link
Contributor Author

tamird commented Jun 19, 2017

Updated, PTAL.


Review status: 0 of 2 files reviewed at latest revision, 2 unresolved discussions, some commit checks pending.


pkg/server/server.go, line 1030 at r1 (raw file):

Previously, bdarnell (Ben Darnell) wrote…

There are common scenarios where a hostname resolves differently locally than it does remotely (i.e. it may resolve to a loopback IP instead of a routable one). But I think it's correct to require that it always resolve to something locally.

Done.


pkg/server/server.go, line 1036 at r1 (raw file):

Previously, bdarnell (Ben Darnell) wrote…

Why are we logging this error (and below) instead of returning it? These errors should cause the server to fail to start instead of just logging quietly.

OK, changed to return errors.


Comments from Reviewable

It seems that configurations where the OS-provided hostname doesn't
resolve from the local machine are quite common and can cause hard to
diagnose problems for users. Avoid using any hostname if we're not able
to resolve it locally.

Closes #12107.
Closes #16173.
@bdarnell
Copy link
Contributor

:lgtm:


Reviewed 2 of 2 files at r2.
Review status: all files reviewed at latest revision, 1 unresolved discussion, all commit checks successful.


Comments from Reviewable

@tamird tamird merged commit 06bf07f into cockroachdb:master Jun 19, 2017
@tamird tamird deleted the check-hostname branch June 19, 2017 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants