Skip to content

Commit 991d823

Browse files
authored
Add support for preemptive Basic auth for proxy (#6585)
Similar to #6333. Note implementations differ because Apache 5.x actually adds support simply configuring the BasicScheme with preemptive auth that wasn't present in 4.x.
1 parent 760de82 commit 991d823

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Apache5 HTTP Client (Preview)",
4+
"contributor": "",
5+
"description": "Fix bug where preemptive Basic authentication was not honored for proxies. Similar to fix for Apache 4.x in [#6333](https://github.com/aws/aws-sdk-java-v2/issues/6333)."
6+
}

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/utils/Apache5Utils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ private static void addPreemptiveAuthenticationProxy(HttpClientContext clientCon
123123
AuthCache authCache = new BasicAuthCache();
124124
// Generate BASIC scheme object and add it to the local auth cache
125125
BasicScheme basicAuth = new BasicScheme();
126+
basicAuth.initPreemptive(credsProvider.getCredentials(newAuthScope(proxyConfiguration), clientContext));
126127
authCache.put(targetHost, basicAuth);
127128

128129
clientContext.setCredentialsProvider(credsProvider);

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientProxyAuthTest.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.any;
2020
import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
2121
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
22+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
2223
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
2324
import static org.assertj.core.api.Assertions.assertThat;
2425

2526
import com.github.tomakehurst.wiremock.WireMockServer;
27+
import com.github.tomakehurst.wiremock.client.WireMock;
2628
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
2729
import java.net.URI;
2830
import java.util.Base64;
@@ -66,6 +68,43 @@ public void teardown() {
6668
}
6769
}
6870

71+
@Test
72+
public void proxyAuthentication_whenPreemptiveAuthEnabled_shouldSendProxyAuthorizationHeader() throws Exception {
73+
mockProxy.stubFor(any(anyUrl())
74+
.withHeader("Proxy-Authorization", equalTo(BASIC_PROXY_AUTH_HEADER))
75+
.willReturn(aResponse()
76+
.withStatus(200)
77+
.withBody("Success")));
78+
79+
// Create HTTP client with preemptive proxy authentication enabled
80+
httpClient = Apache5HttpClient.builder()
81+
.proxyConfiguration(ProxyConfiguration.builder()
82+
.endpoint(URI.create("http://localhost:" + mockProxy.port()))
83+
.username("testuser")
84+
.password("testpass")
85+
.preemptiveBasicAuthenticationEnabled(true)
86+
.build())
87+
.build();
88+
89+
// Create a request
90+
SdkHttpRequest request = SdkHttpRequest.builder()
91+
.method(SdkHttpMethod.GET)
92+
.uri(URI.create("http://example.com/test"))
93+
.build();
94+
95+
HttpExecuteRequest executeRequest = HttpExecuteRequest.builder()
96+
.request(request)
97+
.build();
98+
99+
// Execute the request - should succeed with preemptive auth header
100+
HttpExecuteResponse response = httpClient.prepareRequest(executeRequest).call();
101+
assertThat(response.httpResponse().statusCode()).isEqualTo(200);
102+
103+
mockProxy.verify(1, anyRequestedFor(anyUrl()));
104+
mockProxy.verify(WireMock.getRequestedFor(anyUrl())
105+
.withHeader("Proxy-Authorization", equalTo(BASIC_PROXY_AUTH_HEADER)));
106+
}
107+
69108
@Test
70109
public void proxyAuthentication_whenPreemptiveAuthDisabled_shouldUseChallengeResponseAuth() throws Exception {
71110
// First request without auth header should get 407
@@ -83,13 +122,13 @@ public void proxyAuthentication_whenPreemptiveAuthDisabled_shouldUseChallengeRes
83122

84123
// Create HTTP client with preemptive proxy authentication disabled
85124
httpClient = Apache5HttpClient.builder()
86-
.proxyConfiguration(ProxyConfiguration.builder()
87-
.endpoint(URI.create("http://localhost:" + mockProxy.port()))
88-
.username("testuser")
89-
.password("testpass")
90-
.preemptiveBasicAuthenticationEnabled(false)
91-
.build())
92-
.build();
125+
.proxyConfiguration(ProxyConfiguration.builder()
126+
.endpoint(URI.create("http://localhost:" + mockProxy.port()))
127+
.username("testuser")
128+
.password("testpass")
129+
.preemptiveBasicAuthenticationEnabled(false)
130+
.build())
131+
.build();
93132

94133
// Create a request
95134
SdkHttpRequest request = SdkHttpRequest.builder()

0 commit comments

Comments
 (0)