From 89d154d5aa1e25c271f0cf250498e8057ae37001 Mon Sep 17 00:00:00 2001 From: Qi Chen Date: Thu, 1 Jun 2023 07:45:28 -0500 Subject: [PATCH] FIX: extract reason from error in AWSSdk2Transport (#493) * MAINT: extract reason from error Signed-off-by: George Chen * MAINT: add change log Signed-off-by: George Chen * MAINT: remove unwanted string Signed-off-by: George Chen * TST: AwsSdk2SecurityIT Signed-off-by: George Chen * MAINT: header Signed-off-by: George Chen --------- Signed-off-by: George Chen --- CHANGELOG.md | 1 + .../transport/aws/AwsSdk2Transport.java | 8 ++++- .../integTest/aws/AwsSdk2SecurityIT.java | 31 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 2605031e6d..c0758fb207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -131,6 +131,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fix AwsSdk2TransportOptions.responseCompression ([#322](https://github.com/opensearch-project/opensearch-java/pull/322)) - Fix missing Highlight and SourceConfig in the MultisearchBody ([#442](https://github.com/opensearch-project/opensearch-java/pull/442)) - Fix parsing /_alias error response for not existing alias ([#476](https://github.com/opensearch-project/opensearch-java/pull/476)) +- Fix missing cause message in missing permission to call Fine Grained Access Control Amazon OpenSearch domain ([#473](https://github.com/opensearch-project/opensearch-java/issues/473)) - Fix catching JsonParsingException ([#494](https://github.com/opensearch-project/opensearch-java/issues/494)) - Fix StoryStats numeric value out of range of int ([#489](https://github.com/opensearch-project/opensearch-java/pull/489)) diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java index 8fd1dfa9b4..4609fbd6a1 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java @@ -457,7 +457,13 @@ private ResponseT parseResponse( JsonObject val = JsonpDeserializer.jsonValueDeserializer() .deserialize(parser, mapper) .asJsonObject(); - String message = val.getString("Message", null); + String message = null; + if (val.get("error") instanceof JsonObject) { + message = val.get("error").asJsonObject().getString("reason", null); + } + if (message == null) { + message = val.getString("Message", null); + } if (message == null) { message = val.getString("message", null); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java new file mode 100644 index 0000000000..aef59a3abf --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SecurityIT.java @@ -0,0 +1,31 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.client.opensearch.integTest.aws; + +import org.junit.Test; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.opensearch._types.OpenSearchException; +import org.opensearch.client.opensearch.cluster.GetClusterSettingsRequest; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; + +public class AwsSdk2SecurityIT extends AwsSdk2TransportTestCase { + private static final String DEFAULT_MESSAGE = "authentication/authorization failure"; + @Test + public void testUnAuthorizedException() { + final OpenSearchClient client = getClient(false, null, null); + final GetClusterSettingsRequest request = new GetClusterSettingsRequest.Builder() + .includeDefaults(true) + .build(); + final OpenSearchException ex = assertThrows( + OpenSearchException.class, () -> client.cluster().getSettings(request)); + assertFalse(ex.getMessage().contains(DEFAULT_MESSAGE)); + } +}