This is an automated email from the ASF dual-hosted git repository.

pradeepagrawal8184 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ranger.git


The following commit(s) were added to refs/heads/master by this push:
     new 69e31ba97 RANGER-5691: Incorrect response seen when admin tries to 
create a key (#1077)
69e31ba97 is described below

commit 69e31ba976cb3d1b9f3ff63ea4d7a189805f33fc
Author: Chinmay Hegde <[email protected]>
AuthorDate: Fri Jul 17 18:07:03 2026 +0530

    RANGER-5691: Incorrect response seen when admin tries to create a key 
(#1077)
    
    * RANGER-5691: Propagate KMS service-not-found exception to caller in 
KMSKeyMgr
    
    * RANGER-5692 : Added debug logs to KMSMDCFilter dofilter method
---
 .../hadoop/crypto/key/kms/server/KMSMDCFilter.java |  3 +
 .../main/java/org/apache/ranger/biz/KmsKeyMgr.java |  5 ++
 .../main/java/org/apache/ranger/rest/XKeyREST.java |  7 ++
 .../java/org/apache/ranger/biz/TestKmsKeyMgr.java  | 96 +++++++++++-----------
 4 files changed, 65 insertions(+), 46 deletions(-)

diff --git 
a/kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSMDCFilter.java 
b/kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSMDCFilter.java
index 203f9078d..e02f7a94b 100644
--- 
a/kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSMDCFilter.java
+++ 
b/kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSMDCFilter.java
@@ -78,7 +78,10 @@ public void doFilter(ServletRequest request, ServletResponse 
response, FilterCha
             HttpServletRequest  req  = (HttpServletRequest) request;
             HttpServletResponse resp = (HttpServletResponse) response;
 
+            logger.debug("==> KMSMDCFilter:doFilter() path = {}", path);
+
             if (path.startsWith(RANGER_KMS_REST_API_PATH)) {
+                logger.debug("==> KMSMDCFilter: calling chain.doFilter() for 
path = {}", path);
                 chain.doFilter(request, resp);
             } else {
                 DATA_TL.remove();
diff --git a/security-admin/src/main/java/org/apache/ranger/biz/KmsKeyMgr.java 
b/security-admin/src/main/java/org/apache/ranger/biz/KmsKeyMgr.java
index 741312bb7..ee16980d7 100755
--- a/security-admin/src/main/java/org/apache/ranger/biz/KmsKeyMgr.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/KmsKeyMgr.java
@@ -114,6 +114,7 @@ public VXKmsKeyList searchKeys(HttpServletRequest request, 
String repoName) thro
             providers = getKMSURL(repoName);
         } catch (Exception e) {
             logger.error("getKey({}) failed", repoName, e);
+            throw restErrorUtil.createRESTException("Provider '" + repoName + 
"' not found or invalid: " + e.getMessage(), MessageEnums.DATA_NOT_FOUND);
         }
 
         List<VXKmsKey> vXKeys       = new ArrayList<>();
@@ -242,6 +243,7 @@ public VXKmsKey rolloverKey(String provider, VXKmsKey 
vXKey) throws Exception {
             providers = getKMSURL(provider);
         } catch (Exception e) {
             logger.error("rolloverKey({}, {}) failed", provider, 
vXKey.getName(), e);
+            throw restErrorUtil.createRESTException("Provider '" + provider + 
"' not found or invalid: " + e.getMessage(), MessageEnums.DATA_NOT_FOUND);
         }
 
         VXKmsKey ret        = null;
@@ -310,6 +312,7 @@ public void deleteKey(String provider, String name) throws 
Exception {
             providers = getKMSURL(provider);
         } catch (Exception e) {
             logger.error("deleteKey({}, {}) failed", provider, name, e);
+            throw restErrorUtil.createRESTException("Provider '" + provider + 
"' not found or invalid: " + e.getMessage(), MessageEnums.DATA_NOT_FOUND);
         }
 
         boolean isKerberos = false;
@@ -371,6 +374,7 @@ public VXKmsKey createKey(String provider, VXKmsKey vXKey) 
throws Exception {
             providers = getKMSURL(provider);
         } catch (Exception e) {
             logger.error("createKey({}, {}) failed", provider, 
vXKey.getName(), e);
+            throw restErrorUtil.createRESTException("Provider '" + provider + 
"' not found or invalid: " + e.getMessage(), MessageEnums.DATA_NOT_FOUND);
         }
 
         VXKmsKey ret        = null;
@@ -439,6 +443,7 @@ public VXKmsKey getKey(String provider, String name) throws 
Exception {
             providers = getKMSURL(provider);
         } catch (Exception e) {
             logger.error("getKey({}, {}) failed", provider, name, e);
+            throw restErrorUtil.createRESTException("Provider '" + provider + 
"' not found or invalid: " + e.getMessage(), MessageEnums.DATA_NOT_FOUND);
         }
 
         boolean isKerberos = false;
diff --git a/security-admin/src/main/java/org/apache/ranger/rest/XKeyREST.java 
b/security-admin/src/main/java/org/apache/ranger/rest/XKeyREST.java
index 525ba142a..30c743cfe 100755
--- a/security-admin/src/main/java/org/apache/ranger/rest/XKeyREST.java
+++ b/security-admin/src/main/java/org/apache/ranger/rest/XKeyREST.java
@@ -225,6 +225,13 @@ public VXKmsKey getKey(@PathParam("alias") String name, 
@QueryParam("provider")
     private void handleError(Exception e) {
         String message = e.getMessage();
 
+        if (e instanceof WebApplicationException) {
+            WebApplicationException wae = (WebApplicationException) e;
+            if (wae.getResponse() != null && wae.getResponse().hasEntity()) {
+                throw wae;
+            }
+        }
+
         if (e instanceof ClientErrorException || e instanceof 
ServerErrorException || e instanceof WebApplicationException) {
             logger.error(e.getMessage());
 
diff --git 
a/security-admin/src/test/java/org/apache/ranger/biz/TestKmsKeyMgr.java 
b/security-admin/src/test/java/org/apache/ranger/biz/TestKmsKeyMgr.java
index 574af7957..9b9a38dfd 100644
--- a/security-admin/src/test/java/org/apache/ranger/biz/TestKmsKeyMgr.java
+++ b/security-admin/src/test/java/org/apache/ranger/biz/TestKmsKeyMgr.java
@@ -272,7 +272,7 @@ public void testSearchKeys_WithKerberos() throws Exception {
     }
 
     @Test
-    public void testSearchKeys_ExceptionHandling() throws Exception {
+    public void testSearchKeys_ProviderNotFound() throws Exception {
         try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
                 MockedStatic<StringUtil> stringUtilMock = 
Mockito.mockStatic(StringUtil.class);
                 MockedStatic<PropertiesUtil> propertiesUtilMock = 
Mockito.mockStatic(PropertiesUtil.class)) {
@@ -282,11 +282,15 @@ public void testSearchKeys_ExceptionHandling() throws 
Exception {
 
             
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
 
-            VXKmsKeyList result = kmsKeyMgr.searchKeys(request, 
TEST_REPO_NAME);
+            Exception mockException = new RuntimeException("Provider 
'testKmsRepo' not found or invalid: Service not found");
+            Mockito.when(restErrorUtil.createRESTException(
+                    Mockito.contains("Provider 'testKmsRepo' not found or 
invalid"),
+                    Mockito.eq(MessageEnums.DATA_NOT_FOUND)
+            )).thenThrow(mockException);
 
-            Assertions.assertNotNull(result);
-            Assertions.assertNotNull(result.getVXKeys());
-            Assertions.assertTrue(result.getVXKeys().isEmpty());
+            Assertions.assertThrows(Exception.class, () -> {
+                kmsKeyMgr.searchKeys(request, TEST_REPO_NAME);
+            });
         }
     }
 
@@ -552,23 +556,6 @@ public void testGetKey_WithException() throws Exception {
         }
     }
 
-    @Test
-    public void testGetKey_ReturnsNull() throws Exception {
-        try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
-                MockedStatic<StringUtil> stringUtilMock = 
Mockito.mockStatic(StringUtil.class);
-                MockedStatic<PropertiesUtil> propertiesUtilMock = 
Mockito.mockStatic(PropertiesUtil.class)) {
-            
contextUtilMock.when(ContextUtil::getCurrentUserLoginId).thenReturn(TEST_USERNAME);
-            stringUtilMock.when(() -> 
StringUtil.getUTFEncodedString(TEST_USERNAME)).thenReturn(TEST_USERNAME);
-            propertiesUtilMock.when(() -> 
PropertiesUtil.getProperty("hadoop.security.authentication", 
"simple")).thenReturn("simple");
-
-            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
-
-            VXKmsKey result = kmsKeyMgr.getKey(TEST_REPO_NAME, TEST_KEY_NAME);
-
-            Assertions.assertNull(result);
-        }
-    }
-
     @Test
     public void testGetKeyFromUri_Success() throws Exception {
         VXKmsKey resultKey = createMockVXKmsKey();
@@ -1012,15 +999,6 @@ public void testExtractCommonCriteriasForFilter() throws 
Exception {
         Assertions.assertEquals("asc", result.getSortType());
     }
 
-    @Test
-    public void testSearchKeys_NullRequest() throws Exception {
-        VXKmsKeyList result = kmsKeyMgr.searchKeys(null, TEST_REPO_NAME);
-
-        Assertions.assertNotNull(result);
-        Assertions.assertNotNull(result.getVXKeys());
-        Assertions.assertTrue(result.getVXKeys().isEmpty());
-    }
-
     @Test
     public void testSearchKeys_EmptyKeyList() throws Exception {
         RangerService rangerService = createMockRangerService();
@@ -1071,7 +1049,7 @@ public void testSearchKeys_EmptyKeyList() throws 
Exception {
     }
 
     @Test
-    public void testRolloverKey_NullProviders() throws Exception {
+    public void testRolloverKey_ProviderNotFound() throws Exception {
         VXKmsKey inputKey = createMockVXKmsKey();
 
         try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
@@ -1081,17 +1059,24 @@ public void testRolloverKey_NullProviders() throws 
Exception {
             stringUtilMock.when(() -> 
StringUtil.getUTFEncodedString(TEST_USERNAME)).thenReturn(TEST_USERNAME);
             propertiesUtilMock.when(() -> 
PropertiesUtil.getProperty("hadoop.security.authentication", 
"simple")).thenReturn("simple");
 
-            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenReturn(null);
+            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
 
-            VXKmsKey result = kmsKeyMgr.rolloverKey(TEST_REPO_NAME, inputKey);
+            Exception mockException = new RuntimeException("Provider 
'testKmsRepo' not found or invalid: Service not found");
+            Mockito.when(restErrorUtil.createRESTException(
+                    Mockito.contains("Provider 'testKmsRepo' not found or 
invalid"),
+                    Mockito.eq(MessageEnums.DATA_NOT_FOUND)
+            )).thenThrow(mockException);
+
+            Assertions.assertThrows(Exception.class, () -> {
+                kmsKeyMgr.rolloverKey(TEST_REPO_NAME, inputKey);
+            });
 
-            Assertions.assertNull(result);
             Mockito.verify(rangerBizUtil).blockAuditorRoleUser();
         }
     }
 
     @Test
-    public void testCreateKey_NullProviders() throws Exception {
+    public void testCreateKey_ProviderNotFound() throws Exception {
         VXKmsKey inputKey = createMockVXKmsKey();
 
         try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
@@ -1101,17 +1086,24 @@ public void testCreateKey_NullProviders() throws 
Exception {
             stringUtilMock.when(() -> 
StringUtil.getUTFEncodedString(TEST_USERNAME)).thenReturn(TEST_USERNAME);
             propertiesUtilMock.when(() -> 
PropertiesUtil.getProperty("hadoop.security.authentication", 
"simple")).thenReturn("simple");
 
-            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenReturn(null);
+            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
+
+            Exception mockException = new RuntimeException("Provider 
'testKmsRepo' not found or invalid: Service not found");
+            Mockito.when(restErrorUtil.createRESTException(
+                    Mockito.contains("Provider 'testKmsRepo' not found or 
invalid"),
+                    Mockito.eq(MessageEnums.DATA_NOT_FOUND)
+            )).thenThrow(mockException);
 
-            VXKmsKey result = kmsKeyMgr.createKey(TEST_REPO_NAME, inputKey);
+            Assertions.assertThrows(Exception.class, () -> {
+                kmsKeyMgr.createKey(TEST_REPO_NAME, inputKey);
+            });
 
-            Assertions.assertNull(result);
             Mockito.verify(rangerBizUtil).blockAuditorRoleUser();
         }
     }
 
     @Test
-    public void testDeleteKey_NullProviders() throws Exception {
+    public void testDeleteKey_ProviderNotFound() throws Exception {
         try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
                 MockedStatic<StringUtil> stringUtilMock = 
Mockito.mockStatic(StringUtil.class);
                 MockedStatic<PropertiesUtil> propertiesUtilMock = 
Mockito.mockStatic(PropertiesUtil.class)) {
@@ -1119,9 +1111,15 @@ public void testDeleteKey_NullProviders() throws 
Exception {
             stringUtilMock.when(() -> 
StringUtil.getUTFEncodedString(TEST_USERNAME)).thenReturn(TEST_USERNAME);
             propertiesUtilMock.when(() -> 
PropertiesUtil.getProperty("hadoop.security.authentication", 
"simple")).thenReturn("simple");
 
-            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenReturn(null);
+            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
+
+            Exception mockException = new RuntimeException("Provider 
'testKmsRepo' not found or invalid: Service not found");
+            Mockito.when(restErrorUtil.createRESTException(
+                    Mockito.contains("Provider 'testKmsRepo' not found or 
invalid"),
+                    Mockito.eq(MessageEnums.DATA_NOT_FOUND)
+            )).thenThrow(mockException);
 
-            Assertions.assertDoesNotThrow(() -> {
+            Assertions.assertThrows(Exception.class, () -> {
                 kmsKeyMgr.deleteKey(TEST_REPO_NAME, TEST_KEY_NAME);
             });
 
@@ -1130,7 +1128,7 @@ public void testDeleteKey_NullProviders() throws 
Exception {
     }
 
     @Test
-    public void testGetKey_NullProviders() throws Exception {
+    public void testGetKey_ProviderNotFound() throws Exception {
         try (MockedStatic<ContextUtil> contextUtilMock = 
Mockito.mockStatic(ContextUtil.class);
                 MockedStatic<StringUtil> stringUtilMock = 
Mockito.mockStatic(StringUtil.class);
                 MockedStatic<PropertiesUtil> propertiesUtilMock = 
Mockito.mockStatic(PropertiesUtil.class)) {
@@ -1138,11 +1136,17 @@ public void testGetKey_NullProviders() throws Exception 
{
             stringUtilMock.when(() -> 
StringUtil.getUTFEncodedString(TEST_USERNAME)).thenReturn(TEST_USERNAME);
             propertiesUtilMock.when(() -> 
PropertiesUtil.getProperty("hadoop.security.authentication", 
"simple")).thenReturn("simple");
 
-            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenReturn(null);
+            
Mockito.when(svcStore.getServiceByName(TEST_REPO_NAME)).thenThrow(new 
RuntimeException("Service not found"));
 
-            VXKmsKey result = kmsKeyMgr.getKey(TEST_REPO_NAME, TEST_KEY_NAME);
+            Exception mockException = new RuntimeException("Provider 
'testKmsRepo' not found or invalid: Service not found");
+            Mockito.when(restErrorUtil.createRESTException(
+                    Mockito.contains("Provider 'testKmsRepo' not found or 
invalid"),
+                    Mockito.eq(MessageEnums.DATA_NOT_FOUND)
+            )).thenThrow(mockException);
 
-            Assertions.assertNull(result);
+            Assertions.assertThrows(Exception.class, () -> {
+                kmsKeyMgr.getKey(TEST_REPO_NAME, TEST_KEY_NAME);
+            });
         }
     }
 

Reply via email to