Skip to content

Commit 3c2b63c

Browse files
committed
Add 'permission denied' handling for index access
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
1 parent 0f0e330 commit 3c2b63c

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

common/src/main/java/org/opensearch/sql/common/error/ErrorCode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public enum ErrorCode {
4040
/** Index or datasource not found */
4141
INDEX_NOT_FOUND,
4242

43+
/** Permission denied or insufficient privileges */
44+
PERMISSION_DENIED,
45+
4346
/** Query planning failed */
4447
PLANNING_ERROR,
4548

integ-test/src/test/java/org/opensearch/sql/security/PPLPermissionsIT.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,29 @@ public void testUserWithoutMappingPermissionCannotGetFieldMappings() throws IOEx
613613
}
614614
}
615615

616+
@Test
617+
public void testUserWithoutMappingPermissionGetsPermissionDeniedErrorCode() throws IOException {
618+
// Test that security exceptions return PERMISSION_DENIED error code, not INDEX_NOT_FOUND
619+
try {
620+
executeQueryAsUser(String.format("describe %s", TEST_INDEX_BANK), NO_MAPPING_USER);
621+
fail("Expected security exception for user without mapping permission");
622+
} catch (ResponseException e) {
623+
assertEquals(403, e.getResponse().getStatusLine().getStatusCode());
624+
String responseBody =
625+
org.opensearch.sql.legacy.TestUtils.getResponseBody(e.getResponse(), false);
626+
JSONObject responseJson = new JSONObject(responseBody);
627+
628+
// Verify the error code is PERMISSION_DENIED, not INDEX_NOT_FOUND
629+
assertTrue("Response should have error field", responseJson.has("error"));
630+
JSONObject error = responseJson.getJSONObject("error");
631+
assertTrue("Error should have code field", error.has("code"));
632+
assertEquals(
633+
"Security exception should return PERMISSION_DENIED error code",
634+
"PERMISSION_DENIED",
635+
error.getString("code"));
636+
}
637+
}
638+
616639
@Test
617640
public void testUserWithoutSettingsPermissionCannotGetSettings() throws IOException {
618641
// Test that user without settings permission gets 403 error

opensearch/src/main/java/org/opensearch/sql/opensearch/client/OpenSearchNodeClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,20 @@ public Map<String, IndexMapping> getIndexMappings(String... indexExpression) {
9999
.collect(
100100
Collectors.toUnmodifiableMap(
101101
Map.Entry::getKey, cursor -> new IndexMapping(cursor.getValue())));
102-
} catch (IndexNotFoundException | OpenSearchSecurityException e) {
102+
} catch (IndexNotFoundException e) {
103103
// Re-throw directly to be treated as client error finally
104104
throw ErrorReport.wrap(e)
105105
.code(ErrorCode.INDEX_NOT_FOUND)
106106
.location("while fetching index mappings")
107107
.context("index_name", indexExpression[0])
108108
.build();
109+
} catch (OpenSearchSecurityException e) {
110+
// Re-throw with permission denied code
111+
throw ErrorReport.wrap(e)
112+
.code(ErrorCode.PERMISSION_DENIED)
113+
.location("while fetching index mappings")
114+
.context("index_name", indexExpression[0])
115+
.build();
109116
} catch (Exception e) {
110117
throw new IllegalStateException(
111118
"Failed to read mapping for index pattern [" + indexExpression + "]", e);

0 commit comments

Comments
 (0)