From e2e5c7dc6de1cd7d0520889dcdf2574a393b721b Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Thu, 18 Mar 2021 11:50:51 -0700 Subject: [PATCH] Catch and respond invalid index name exception when an index with invalid name is mentioned (#865) (#1081) (cherry picked from commit 7c5dbb947e2c2ecb3fa51a267b968556428a3cff) --- .../security/filter/OpenDistroSecurityFilter.java | 7 +++++++ .../security/IndexIntegrationTests.java | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/com/amazon/opendistroforelasticsearch/security/filter/OpenDistroSecurityFilter.java b/src/main/java/com/amazon/opendistroforelasticsearch/security/filter/OpenDistroSecurityFilter.java index 3b64253946..be3070cdfb 100644 --- a/src/main/java/com/amazon/opendistroforelasticsearch/security/filter/OpenDistroSecurityFilter.java +++ b/src/main/java/com/amazon/opendistroforelasticsearch/security/filter/OpenDistroSecurityFilter.java @@ -336,6 +336,13 @@ public void onFailure(Exception e) { log.debug("no permissions for {}", pres.getMissingPrivileges()); listener.onFailure(new ElasticsearchSecurityException("no permissions for " + pres.getMissingPrivileges()+ " and "+user, RestStatus.FORBIDDEN)); } + } catch (ElasticsearchException e) { + if (task != null) { + log.debug("Failed to apply filter. Task id: {} ({}). Action: {}", task.getId(), task.getDescription(), action, e); + } else { + log.debug("Failed to apply filter. Action: {}", action, e); + } + listener.onFailure(e); } catch (Throwable e) { log.error("Unexpected exception "+e, e); listener.onFailure(new ElasticsearchSecurityException("Unexpected exception " + action, RestStatus.INTERNAL_SERVER_ERROR)); diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/security/IndexIntegrationTests.java b/src/test/java/com/amazon/opendistroforelasticsearch/security/IndexIntegrationTests.java index ceb9472f2d..ca00ce2a89 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/security/IndexIntegrationTests.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/security/IndexIntegrationTests.java @@ -30,6 +30,7 @@ package com.amazon.opendistroforelasticsearch.security; +import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; @@ -455,6 +456,17 @@ public void testAliasResolution() throws Exception { Assert.assertEquals(HttpStatus.SC_OK, rh.executeGetRequest(".ki*ana/_search?pretty", encodeBasicHeader("aliastest", "nagilum")).getStatusCode()); Assert.assertEquals(HttpStatus.SC_OK, rh.executeGetRequest(".kibana/_search?pretty", encodeBasicHeader("aliastest", "nagilum")).getStatusCode()); } + + @Test + public void testIndexResolveInvalidIndexName() throws Exception { + setup(); + final RestHelper rh = nonSslRestHelper(); + + // invalid_index_name_exception should be thrown and responded when invalid index name is mentioned in requests. + HttpResponse res = rh.executeGetRequest(URLEncoder.encode("_##pdt_data/_search", "UTF-8"), encodeBasicHeader("ccsresolv", "nagilum")); + Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); + Assert.assertTrue(res.getBody().contains("invalid_index_name_exception")); + } @Test public void testCCSIndexResolve() throws Exception {