Skip to content

Allow the resolver to override the port setting #802

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

themulle
Copy link

The custom resolver couldn't override the default port even if id had been explicitly provided.
The reason for that is unclear as it contradicts the open closed principle.
Why: Common address resolvers should alway only return the host. If a port is being provided this has been intended.

Purpose of this Patch:
Access a kafka cluster behind a firewall via portforwarding on a jump host.
jumphost: 9095 -> kafkahost1:9092, jumphost: 9096 -> kafkahost2:9092, ...

The custom resolver couldn't override the default port even if id had been explicitly provided.
The reason for that is unclear as it contradicts the open closed principle.
Why: Common address resolvers should alway only return the host. If a port is being provided this has been intended.

Purpose of this Patch:
Access a kafka cluster behind a firewall via portforwarding on a jump host.
jumphost: 9095 -> kafkahost1:9092, jumphost: 9096 -> kafkahost2:9092, ...
Bugfix for localhost-lookups which result in the setting of a default port in splitHostPortNumber
Set the default port in the resolver unless any port is set.
This is done to pass the unit tests but should in my opinion not be the responsibility of this function.
Refactoring the PR for better reflect the changes
Altered the Ports in one test to make it pass but kept the logical behaviour
@themulle
Copy link
Author

Port overrides seem to have been prevented due to the default ports being provided.
The test had to be adapted but seemed a bit akward to me.
Prior: example.com:9092 -> 127.0.0.1:80 resulted in 127.0.0.1:9092 (why?)
Now: example.com:9092 -> 127.0.0.1:80 results in 127.0.0.1:80

@achille-roussel
Copy link
Contributor

Hello @themulle, thanks for submitting this pull request.

I left a question about the implementation, let me know what you think!

dialer.go Outdated
Comment on lines 486 to 490
} else if resolvedPort != port {
_, explicitSetPort, _ := net.SplitHostPort(resolved[0])
if explicitSetPort != "" {
port = explicitSetPort
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't explicitSetPort the same as resolvedPort in this case? It seems we are calling net.SplitHostPort(resolved[0]) twice, so we should get the same results?

Copy link
Author

Choose a reason for hiding this comment

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

Hi,

that's also been the confusing part for me.
In this case "kafka.splitHostPort" is being called which returns default port 9092.
net.SplitHostPort under some circumstances (s = own address) returns an empty host string, even if "s" is set.

func splitHostPort(s string) (host string, port string) {
	host, port, _ = net.SplitHostPort(s)
	if len(host) == 0 && len(port) == 0 {
		host = s
		port = "9092"
	}
	return
}

Example:
467 host, port := splitHostPort("mykafkahost:9095") //return mykafkahost 9095
478 resolvedHost, resolvedPort := splitHostPort("myjumphost") //return myjumphost 9092 (on "myjumphost" because net.SplitHostPort in this case returns an empty host)
-> resolvedPort!=port -> would override port setting but isn't explicitly set
because of this the Port is checked a second time

Copy link
Author

Choose a reason for hiding this comment

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

Ok, i think this can be simplified:

`
if len(resolved) > 0 {
resolvedHost, resolvedPort, _ := net.SplitHostPort(resolved[0])

		// we'll always prefer the resolved host
		if len(resolvedHost) == 0 && len(resolvedPort) == 0 {
			host = resolved[0]
		} else {
			host = resolvedHost
		}

		// only override the port if it's not set or if the resolver explicitly provides a port
		if resolvedPort != "" {
			port = resolvedPort
		}
	}

`

Copy link
Contributor

@achille-roussel achille-roussel left a comment

Choose a reason for hiding this comment

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

The change looks good, thanks for the contribution!

achille-roussel and others added 5 commits February 11, 2022 09:53
added testcase "host without port" (127.0.0.1:)
restructure test to be able to run it without local kafka broker
net.SplitHostPort returns an empty host and port (plus an error) if no port is being provided
now the error is properly handled and the port is explicitly checked
@themulle
Copy link
Author

finally a good solution after digging into net/ip.go to understand the behaviour of net.SplitHostPort

Comment on lines +366 to +368
if !runCompleteTest {
return
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we remove this before merging?

Copy link
Author

Choose a reason for hiding this comment

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

In my opinion the test should be split into a "real" unit test, which only checks the resolver behaviour and and a integration test which connects to kafka.
I don't know if this makes sense because allof the tests are "integration tests" and another behaviour would break the coding style.

Your decision:

  • shall i remove my "uint-test-code"
  • shall i completley split up the test in integration/unit-test

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a separate test to validate the resolver behavior sounds fine to me, are there any downsides or blockers?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @themulle, did you get a chance to look at addressing this last suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

@themulle up? Let me know if you want us to take this over as well, no rush :)

@chris-wangkk
Copy link

hi, this commit hasn't been merged into the main branch yet(I have the same problem)

@achille-roussel
Copy link
Contributor

Hello @chris-wangkk

The original author appears to have abandoned the pull request. There were a few comments left that haven't been addressed.

Would you be available to take over and complete this work?

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