Skip to content

Commit a528c91

Browse files
authoredApr 18, 2024··
Ensure that challenge response contains body (opensearch-project#4233)
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent 9a85f23 commit a528c91

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed
 

‎src/integrationTest/java/org/opensearch/security/ResourceFocusedTests.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import org.opensearch.test.framework.cluster.LocalCluster;
4242
import org.opensearch.test.framework.cluster.TestRestClient;
4343

44+
import static org.hamcrest.MatcherAssert.assertThat;
45+
import static org.hamcrest.Matchers.equalTo;
4446
import static org.opensearch.action.support.WriteRequest.RefreshPolicy.IMMEDIATE;
4547
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL;
4648
import static org.opensearch.test.framework.TestSecurityConfig.Role.ALL_ACCESS;
@@ -127,11 +129,13 @@ private void runResourceTest(
127129
final var requests = AsyncActions.generate(() -> {
128130
final HttpPost post = new HttpPost(client.getHttpServerUri() + requestPath);
129131
post.setEntity(new ByteArrayEntity(compressedRequestBody, ContentType.APPLICATION_JSON));
130-
return client.executeRequest(post);
132+
TestRestClient.HttpResponse response = client.executeRequest(post);
133+
return response.getStatusCode();
131134
}, parrallelism, totalNumberOfRequests);
132135

133-
AsyncActions.getAll(requests, 2, TimeUnit.MINUTES)
134-
.forEach((response) -> { response.assertStatusCode(HttpStatus.SC_UNAUTHORIZED); });
136+
AsyncActions.getAll(requests, 2, TimeUnit.MINUTES).forEach((responseCode) -> {
137+
assertThat(responseCode, equalTo(HttpStatus.SC_UNAUTHORIZED));
138+
});
135139
}
136140
}
137141

‎src/integrationTest/java/org/opensearch/security/http/BasicAuthTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public void testBrowserShouldRequestForCredentials() {
106106
}
107107
}
108108

109+
@Test
110+
public void shouldRespondWithChallengeWhenNoCredentialsArePresent() {
111+
try (TestRestClient client = cluster.getRestClient()) {
112+
HttpResponse response = client.getAuthInfo();
113+
114+
assertThat(response, is(notNullValue()));
115+
response.assertStatusCode(SC_UNAUTHORIZED);
116+
assertThat(response.getHeader("WWW-Authenticate"), is(notNullValue()));
117+
assertThat(response.getHeader("WWW-Authenticate").getValue(), equalTo("Basic realm=\"OpenSearch Security\""));
118+
assertThat(response.getBody(), equalTo("Unauthorized"));
119+
}
120+
}
121+
109122
@Test
110123
public void testUserShouldNotHaveAssignedCustomAttributes() {
111124
try (TestRestClient client = cluster.getRestClient(TEST_USER)) {

‎src/main/java/org/opensearch/security/http/HTTPBasicAuthenticator.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ public AuthCredentials extractCredentials(final SecurityRequest request, final T
6868
@Override
6969
public Optional<SecurityResponse> reRequestAuthentication(final SecurityRequest request, AuthCredentials creds) {
7070
return Optional.of(
71-
new SecurityResponse(HttpStatus.SC_UNAUTHORIZED, Map.of("WWW-Authenticate", "Basic realm=\"OpenSearch Security\""), "")
71+
new SecurityResponse(
72+
HttpStatus.SC_UNAUTHORIZED,
73+
Map.of("WWW-Authenticate", "Basic realm=\"OpenSearch Security\""),
74+
"Unauthorized"
75+
)
7276
);
7377
}
7478

0 commit comments

Comments
 (0)
Please sign in to comment.