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

8326949: Authorization header is removed when a proxy Authenticator is set on HttpClient #21249

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Michael-Mc-Mahon
Copy link
Member

@Michael-Mc-Mahon Michael-Mc-Mahon commented Sep 29, 2024

This fix relaxes the constraints on user set authentication headers. Currently, any user set authentication headers are filtered out, if the HttpClient has an Authenticator set. The reason being that the authenticator is expected to manage authentication. With this fix, it will be possible to use pre-emptive authentication through user set headers, even if an authenticator is set. The expected use case is where the authenticator would manage either proxy or server authentication and the user set headers would manage server authentication if the authenticator is managing proxy (or vice versa).
If the pre-emptive authentication fails, then this behavior is disabled on further retries and it would be up to the authenticator to provide the right credentials then.

Thanks,
Michael


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8326949: Authorization header is removed when a proxy Authenticator is set on HttpClient (Enhancement - P3)

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/21249/head:pull/21249
$ git checkout pull/21249

Update a local copy of the PR:
$ git checkout pull/21249
$ git pull https://git.openjdk.org/jdk.git pull/21249/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21249

View PR using the GUI difftool:
$ git pr show -t 21249

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/21249.diff

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 29, 2024

👋 Welcome back michaelm! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Sep 29, 2024

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk openjdk bot added the rfr Pull request is ready for review label Sep 29, 2024
@openjdk
Copy link

openjdk bot commented Sep 29, 2024

@Michael-Mc-Mahon The following label will be automatically applied to this pull request:

  • net

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@mlbridge
Copy link

mlbridge bot commented Sep 29, 2024

Webrevs

@AlanBateman
Copy link
Contributor

AlanBateman commented Sep 29, 2024

Would there be any merit to adding something to Builder.authenticator(Authenticator) to document the behavior? I don't know if this would be an API note or impNote. I'm mostly wondering where a developer might read about how/when it is used.

Copy link
Member

@dfuch dfuch left a comment

Choose a reason for hiding this comment

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

The test seems to be missing a scenario where the provided preemptive credentials are wrong (or obsolete) and the stack then defaults to using the provided authenticator.

var plainCreds = "user:pwd";
var encoded = java.util.Base64.getEncoder().encodeToString(plainCreds.getBytes(US_ASCII));
var badCreds = "user:wrong";
var encoded1 = java.util.Base64.getEncoder().encodeToString(badCreds.getBytes(US_ASCII));
Copy link
Member

@dfuch dfuch Sep 30, 2024

Choose a reason for hiding this comment

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

Is some part of the test missing? I don't see where encoded1 is used.
Also should there be some assertion as to whether ProxyAuth() was called or not?

Comment on lines +88 to +90
try {

var client = HttpClient.newBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

Ideally should use try-with-resource to also close the client.

Comment on lines +124 to +127
try {
var client = HttpClient.newBuilder()
.version(java.net.http.HttpClient.Version.HTTP_1_1)
.build();
Copy link
Member

Choose a reason for hiding this comment

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

should use try-with-resource to close the client

Comment on lines +154 to +155
try {
var client = HttpClient.newBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

should use try-with-resource to close the client

}
@Override
public List<Proxy> select(URI uri) {
return List.of(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port)));
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to use InetAddress.getLoopbackAddress() rather than "localhost"

@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() != RequestorType.SERVER) {
// We only want to handle proxy authentication here
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// We only want to handle proxy authentication here
// We only want to handle server authentication here

Comment on lines +223 to +233
/*
return (k, v) -> client.authenticator().isEmpty()
|| (!k.equalsIgnoreCase("Authorization")
&& !k.equalsIgnoreCase("Proxy-Authorization"))

// flag may be true for first attempt, and will be false
// for subsequent attempts if the first attempt failed
// due to 401/407

|| req.tryUserSetAuthorization();
*/
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this commented code be removed?

}

public String baseURL() {
return "http://127.0.0.1:" + getPort();
Copy link
Member

Choose a reason for hiding this comment

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

Should use URIBuilder here to deal with possible IPv6-only envs

@dfuch
Copy link
Member

dfuch commented Sep 30, 2024

I agree with Alan that it would be good to document how we use the Authenticator in HttpClient.Builder.
In which case this will probably require a CSR, or at least a release note.

@Michael-Mc-Mahon
Copy link
Member Author

Would there be any merit to adding something to Builder.authenticator(Authenticator) to document the behavior? I don't know if this would be an API note or impNote. I'm mostly wondering where a developer might read about how/when it is used.

That would be useful. It definitely should be documented somewhere.

@Michael-Mc-Mahon
Copy link
Member Author

Quite a few existing authentication regression tests are failing with the change. So, I need to investigate them and will address the other PR comments after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net [email protected] rfr Pull request is ready for review
Development

Successfully merging this pull request may close these issues.

3 participants