Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE-24246 : Fix for Ranger Deny policy overriding policy with same r… #1566

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is the target db name derived from source? When both are same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no when you don't pass target db name

RangerLoadWork rangerLoadWork = new RangerLoadWork(rangerLoadRoot, work.getSourceDbName(), targetDbName,
work.getMetricCollector());
Task<RangerLoadWork> rangerLoadTask = TaskFactory.get(rangerLoadWork, conf);
if (childTasks == null) {
Expand Down
Expand Up @@ -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();
}
Expand All @@ -277,7 +277,7 @@ private synchronized Client getRangerClient() {
@Override
public List<RangerPolicy> changeDataSet(List<RangerPolicy> 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)) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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<RangerPolicy> 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<RangerPolicy> expectedRangerPolicies,
List<RangerPolicy> 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<RangerPolicy> expectedRangerPolicies,
List<RangerPolicy> 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);
}
}
}
}