diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ReplLoadTask.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ReplLoadTask.java index 6265df1de7..61b3652829 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ReplLoadTask.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ReplLoadTask.java @@ -162,7 +162,8 @@ private void initiateAuthorizationLoadTask() throws SemanticException { if (RANGER_AUTHORIZER.equalsIgnoreCase(conf.getVar(HiveConf.ConfVars.REPL_AUTHORIZATION_PROVIDER_SERVICE))) { Path rangerLoadRoot = new Path(new Path(work.dumpDirectory).getParent(), ReplUtils.REPL_RANGER_BASE_DIR); LOG.info("Adding Import Ranger Metadata Task from {} ", rangerLoadRoot); - RangerLoadWork rangerLoadWork = new RangerLoadWork(rangerLoadRoot, work.getSourceDbName(), work.dbNameToLoadIn, + String targetDbName = StringUtils.isEmpty(work.dbNameToLoadIn) ? work.getSourceDbName() : work.dbNameToLoadIn; + RangerLoadWork rangerLoadWork = new RangerLoadWork(rangerLoadRoot, work.getSourceDbName(), targetDbName, work.getMetricCollector()); Task rangerLoadTask = TaskFactory.get(rangerLoadWork, conf); if (childTasks == null) { diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ranger/RangerRestClientImpl.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ranger/RangerRestClientImpl.java index 87a239591f..31081ab1e0 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ranger/RangerRestClientImpl.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/repl/ranger/RangerRestClientImpl.java @@ -260,7 +260,7 @@ private RangerExportPolicyList importRangerPoliciesPlain(String jsonRangerExport public String getRangerImportUrl(String rangerUrl, String dbName) throws URISyntaxException { URIBuilder uriBuilder = new URIBuilder(rangerUrl); uriBuilder.setPath(RANGER_REST_URL_IMPORTJSONFILE); - uriBuilder.addParameter("updateIfExists", "true"); + uriBuilder.addParameter("mergeIfExists", "true"); uriBuilder.addParameter("polResource", dbName); return uriBuilder.build().toString(); } @@ -277,7 +277,7 @@ private synchronized Client getRangerClient() { @Override public List changeDataSet(List rangerPolicies, String sourceDbName, String targetDbName) { - if (targetDbName.equals(sourceDbName)) { + if (StringUtils.isEmpty(sourceDbName) || StringUtils.isEmpty(targetDbName) || targetDbName.equals(sourceDbName)) { return rangerPolicies; } if (CollectionUtils.isNotEmpty(rangerPolicies)) { diff --git a/ql/src/test/org/apache/hadoop/hive/ql/exec/repl/TestRangerLoadTask.java b/ql/src/test/org/apache/hadoop/hive/ql/exec/repl/TestRangerLoadTask.java index d8141d4db4..c6bc99ed0d 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/exec/repl/TestRangerLoadTask.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/exec/repl/TestRangerLoadTask.java @@ -40,6 +40,7 @@ import java.net.MalformedURLException; import java.net.URL; +import java.util.List; import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.REPL_RANGER_ADD_DENY_POLICY_TARGET; import static org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils.RANGER_HIVE_SERVICE_NAME; @@ -283,11 +284,96 @@ public void testRangerEndpointCreation() throws Exception { Assert.assertTrue(rangerRestClient.getRangerImportUrl("http://ranger.apache.org:6080/", "dbname").equals("http://ranger.apache.org:6080/service/plugins/policies/importPoliciesFromFile" - + "?updateIfExists=true&polResource=dbname")); + + "?mergeIfExists=true&polResource=dbname")); Assert.assertTrue(rangerRestClient.getRangerImportUrl("http://ranger.apache.org:6080", "dbname").equals("http://ranger.apache.org:6080/service/plugins/policies/importPoliciesFromFile" - + "?updateIfExists=true&polResource=dbname")); + + "?mergeIfExists=true&polResource=dbname")); } + + @Test + public void testChangeDataSet() throws Exception { + RangerRestClientImpl rangerRestClient = new RangerRestClientImpl(); + String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\"," + + "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\"" + + ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"cm_hive\",\"name\":\"db-level\",\"policyType\":0," + + "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"]," + + "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false," + + "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}}," + + "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\"," + + "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[]," + + "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[]," + + "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":" + + "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}"; + RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class); + List rangerPolicies = rangerPolicyList.getPolicies(); + rangerRestClient.changeDataSet(rangerPolicies, null, null); + assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies, + null, null), "aa"); + assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies, + "aa", null), "aa"); + assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies, + null, "aa"), "aa"); + assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies, + "aa", "aa"), "aa"); + assertNotEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies, + "aa", "tgt_aa"), "tgt_aa"); + } + + private void assertNotEqualsRangerPolicies(List expectedRangerPolicies, + List actualRangerPolicies, String targetName) { + Assert.assertEquals(expectedRangerPolicies.size(), actualRangerPolicies.size()); + for (int index = 0; index < expectedRangerPolicies.size(); index++) { + Assert.assertEquals(expectedRangerPolicies.get(index).getName(), actualRangerPolicies.get(index).getName()); + Assert.assertEquals(expectedRangerPolicies.get(index).getService(), actualRangerPolicies.get(index).getService()); + Assert.assertEquals(expectedRangerPolicies.get(index).getDescription(), + actualRangerPolicies.get(index).getDescription()); + Assert.assertEquals(expectedRangerPolicies.get(index).getPolicyType(), + actualRangerPolicies.get(index).getPolicyType()); + Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(), + actualRangerPolicies.get(index).getResources().size()); + Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(), + actualRangerPolicies.get(index).getResources().size()); + RangerPolicy.RangerPolicyResource expectedRangerPolicyResource = expectedRangerPolicies.get(index) + .getResources().get("database"); + RangerPolicy.RangerPolicyResource actualRangerPolicyResource = actualRangerPolicies.get(index) + .getResources().get("database"); + Assert.assertEquals(expectedRangerPolicyResource.getValues().size(), + actualRangerPolicyResource.getValues().size()); + for (int resourceIndex = 0; resourceIndex < expectedRangerPolicyResource.getValues().size(); resourceIndex++) { + Assert.assertEquals(actualRangerPolicyResource.getValues().get(index), + targetName); + } + } + } + + private void assertEqualsRangerPolicies(List expectedRangerPolicies, + List actualRangerPolicies, String sourceName) { + Assert.assertEquals(expectedRangerPolicies.size(), actualRangerPolicies.size()); + for (int index = 0; index < expectedRangerPolicies.size(); index++) { + Assert.assertEquals(expectedRangerPolicies.get(index).getName(), actualRangerPolicies.get(index).getName()); + Assert.assertEquals(expectedRangerPolicies.get(index).getService(), actualRangerPolicies.get(index).getService()); + Assert.assertEquals(expectedRangerPolicies.get(index).getDescription(), + actualRangerPolicies.get(index).getDescription()); + Assert.assertEquals(expectedRangerPolicies.get(index).getPolicyType(), + actualRangerPolicies.get(index).getPolicyType()); + Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(), + actualRangerPolicies.get(index).getResources().size()); + Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(), + actualRangerPolicies.get(index).getResources().size()); + RangerPolicy.RangerPolicyResource expectedRangerPolicyResource = expectedRangerPolicies.get(index) + .getResources().get("database"); + RangerPolicy.RangerPolicyResource actualRangerPolicyResource = actualRangerPolicies.get(index) + .getResources().get("database"); + Assert.assertEquals(expectedRangerPolicyResource.getValues().size(), + actualRangerPolicyResource.getValues().size()); + for (int resourceIndex = 0; resourceIndex < expectedRangerPolicyResource.getValues().size(); resourceIndex++) { + Assert.assertEquals(expectedRangerPolicyResource.getValues().get(index), + actualRangerPolicyResource.getValues().get(index)); + Assert.assertEquals(actualRangerPolicyResource.getValues().get(index), + sourceName); + } + } + } }