Skip to content

Commit 970b478

Browse files
Fix SQL client parsing of array header values (elastic#143408)
1 parent cb95c77 commit 970b478

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

docs/changelog/143408.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
area: SQL
2+
issues:
3+
- 143018
4+
- 143019
5+
pr: 143408
6+
summary: Fix SQL client parsing of array header values
7+
type: bug

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/RemoteFailure.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,20 @@ private static Map<String, String> parseHeaders(JsonParser parser) throws IOExce
269269
}
270270
String name = parser.getText();
271271
token = parser.nextToken();
272-
if (token != JsonToken.VALUE_STRING) {
272+
String value;
273+
if (token == JsonToken.VALUE_STRING) {
274+
value = parser.getText();
275+
} else if (token == JsonToken.START_ARRAY) {
276+
List<String> values = new ArrayList<>();
277+
while ((token = parser.nextToken()) != JsonToken.END_ARRAY) {
278+
if (token == JsonToken.VALUE_STRING) {
279+
values.add(parser.getText());
280+
}
281+
}
282+
value = String.join(", ", values);
283+
} else {
273284
throw new IOException("expected header value but was [" + token + "][" + parser.getText() + "]");
274285
}
275-
String value = parser.getText();
276286
headers.put(name, value);
277287
}
278288

x-pack/plugin/sql/sql-client/src/test/java/org/elasticsearch/xpack/sql/client/RemoteFailureTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public void testParseMissingAuth() throws IOException {
6464
assertEquals(singletonMap("WWW-Authenticate", "Basic realm=\"security\", charset=\"UTF-8\""), failure.headers());
6565
}
6666

67+
public void testParseMissingAuthMultipleSchemes() throws IOException {
68+
RemoteFailure failure = parse("missing_auth_multiple_schemes.json");
69+
assertEquals("security_exception", failure.type());
70+
assertEquals(
71+
"unable to authenticate with provided credentials and anonymous access is not allowed for this request",
72+
failure.reason()
73+
);
74+
assertThat(failure.remoteTrace(), containsString("AuthenticationService.authenticate"));
75+
assertNull(failure.cause());
76+
assertEquals(singletonMap("WWW-Authenticate", "Basic realm=\"security\", charset=\"UTF-8\", ApiKey"), failure.headers());
77+
}
78+
6779
public void testNoError() {
6880
IOException e = expectThrows(IOException.class, () -> parse("no_error.json"));
6981
assertEquals(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"error" : {
3+
"root_cause" : [
4+
{
5+
"type" : "security_exception",
6+
"reason" : "unable to authenticate with provided credentials and anonymous access is not allowed for this request",
7+
"additional_unsuccessful_credentials" : "API key: Illegal base64 character 5f",
8+
"header" : {
9+
"WWW-Authenticate" : ["Basic realm=\"security\", charset=\"UTF-8\"", "ApiKey"]
10+
},
11+
"stack_trace" : "ElasticsearchSecurityException[unable to authenticate]\n\tat org.elasticsearch.xpack.security.authc.AuthenticationService.authenticate(AuthenticationService.java:99)"
12+
}
13+
],
14+
"type" : "security_exception",
15+
"reason" : "unable to authenticate with provided credentials and anonymous access is not allowed for this request",
16+
"additional_unsuccessful_credentials" : "API key: Illegal base64 character 5f",
17+
"header" : {
18+
"WWW-Authenticate" : ["Basic realm=\"security\", charset=\"UTF-8\"", "ApiKey"]
19+
},
20+
"stack_trace" : "ElasticsearchSecurityException[unable to authenticate]\n\tat org.elasticsearch.xpack.security.authc.AuthenticationService.authenticate(AuthenticationService.java:99)"
21+
},
22+
"status" : 401
23+
}

0 commit comments

Comments
 (0)