use of org.apache.ranger.plugin.model.RangerPolicy in project nifi by apache.
the class TestRangerBasePluginWithPolicies method testDisabledPolicy.
@Test
public void testDisabledPolicy() {
final String resourceIdentifier1 = "/resource-1";
RangerPolicyResource resource1 = new RangerPolicyResource(resourceIdentifier1);
final Map<String, RangerPolicyResource> policy1Resources = new HashMap<>();
policy1Resources.put(resourceIdentifier1, resource1);
final RangerPolicyItem policy1Item = new RangerPolicyItem();
policy1Item.setAccesses(Stream.of(new RangerPolicyItemAccess("READ")).collect(Collectors.toList()));
final RangerPolicy policy1 = new RangerPolicy();
policy1.setIsEnabled(false);
policy1.setResources(policy1Resources);
policy1.setPolicyItems(Stream.of(policy1Item).collect(Collectors.toList()));
final List<RangerPolicy> policies = new ArrayList<>();
policies.add(policy1);
final RangerServiceDef serviceDef = new RangerServiceDef();
serviceDef.setName("nifi");
final ServicePolicies servicePolicies = new ServicePolicies();
servicePolicies.setPolicies(policies);
servicePolicies.setServiceDef(serviceDef);
// set all the policies in the plugin
final RangerBasePluginWithPolicies pluginWithPolicies = new RangerBasePluginWithPolicies("nifi", "nifi");
pluginWithPolicies.setPolicies(servicePolicies);
// ensure the policy was skipped
assertFalse(pluginWithPolicies.doesPolicyExist(resourceIdentifier1, RequestAction.READ));
assertTrue(pluginWithPolicies.getAccessPolicies().isEmpty());
assertNull(pluginWithPolicies.getAccessPolicy(resourceIdentifier1, RequestAction.READ));
}
use of org.apache.ranger.plugin.model.RangerPolicy in project ranger by apache.
the class ServiceDBStore method writeExcel.
private void writeExcel(List<RangerPolicy> policies, String excelFileName, HttpServletResponse response) throws IOException {
Workbook workbook = null;
OutputStream outStream = null;
try {
workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
createHeaderRow(sheet);
int rowCount = 0;
if (!CollectionUtils.isEmpty(policies)) {
for (RangerPolicy policy : policies) {
List<RangerPolicyItem> policyItems = policy.getPolicyItems();
List<RangerRowFilterPolicyItem> rowFilterPolicyItems = policy.getRowFilterPolicyItems();
List<RangerDataMaskPolicyItem> dataMaskPolicyItems = policy.getDataMaskPolicyItems();
List<RangerPolicyItem> allowExceptions = policy.getAllowExceptions();
List<RangerPolicyItem> denyExceptions = policy.getDenyExceptions();
List<RangerPolicyItem> denyPolicyItems = policy.getDenyPolicyItems();
XXService xxservice = daoMgr.getXXService().findByName(policy.getService());
String serviceType = "";
if (xxservice != null) {
Long ServiceId = xxservice.getType();
XXServiceDef xxservDef = daoMgr.getXXServiceDef().getById(ServiceId);
if (xxservDef != null) {
serviceType = xxservDef.getName();
}
}
if (CollectionUtils.isNotEmpty(policyItems)) {
for (RangerPolicyItem policyItem : policyItems) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
}
} else if (CollectionUtils.isNotEmpty(dataMaskPolicyItems)) {
for (RangerDataMaskPolicyItem dataMaskPolicyItem : dataMaskPolicyItems) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, null, dataMaskPolicyItem, null, row, null);
}
} else if (CollectionUtils.isNotEmpty(rowFilterPolicyItems)) {
for (RangerRowFilterPolicyItem rowFilterPolicyItem : rowFilterPolicyItems) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, null, null, rowFilterPolicyItem, row, null);
}
} else if (serviceType.equalsIgnoreCase(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME)) {
if (CollectionUtils.isEmpty(policyItems)) {
Row row = sheet.createRow(++rowCount);
RangerPolicyItem policyItem = new RangerPolicyItem();
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
}
} else if (CollectionUtils.isEmpty(policyItems)) {
Row row = sheet.createRow(++rowCount);
RangerPolicyItem policyItem = new RangerPolicyItem();
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_INCLUDE);
}
if (CollectionUtils.isNotEmpty(allowExceptions)) {
for (RangerPolicyItem policyItem : allowExceptions) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_ALLOW_EXCLUDE);
}
}
if (CollectionUtils.isNotEmpty(denyExceptions)) {
for (RangerPolicyItem policyItem : denyExceptions) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_DENY_EXCLUDE);
}
}
if (CollectionUtils.isNotEmpty(denyPolicyItems)) {
for (RangerPolicyItem policyItem : denyPolicyItems) {
Row row = sheet.createRow(++rowCount);
writeBookForPolicyItems(policy, policyItem, null, null, row, POLICY_DENY_INCLUDE);
}
}
}
}
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
workbook.write(outByteStream);
byte[] outArray = outByteStream.toByteArray();
response.setContentType("application/ms-excel");
response.setContentLength(outArray.length);
response.setHeader("Expires:", "0");
response.setHeader("Content-Disposition", "attachment; filename=" + excelFileName);
response.setStatus(HttpServletResponse.SC_OK);
outStream = response.getOutputStream();
outStream.write(outArray);
outStream.flush();
} catch (IOException ex) {
LOG.error("Failed to create report file " + excelFileName, ex);
} catch (Exception ex) {
LOG.error("Error while generating report file " + excelFileName, ex);
} finally {
if (outStream != null) {
outStream.close();
}
if (workbook != null) {
workbook.close();
}
}
}
use of org.apache.ranger.plugin.model.RangerPolicy in project ranger by apache.
the class ServiceDBStore method getPolicyFromEventTime.
public RangerPolicy getPolicyFromEventTime(String eventTime, Long policyId) {
XXDataHist xDataHist = daoMgr.getXXDataHist().findObjByEventTimeClassTypeAndId(eventTime, AppConstants.CLASS_TYPE_RANGER_POLICY, policyId);
if (xDataHist == null) {
String errMsg = "No policy history found for given time: " + eventTime;
LOG.error(errMsg);
throw restErrorUtil.createRESTException(errMsg, MessageEnums.DATA_NOT_FOUND);
}
String content = xDataHist.getContent();
RangerPolicy policy = (RangerPolicy) dataHistService.writeJsonToJavaObject(content, RangerPolicy.class);
return policy;
}
use of org.apache.ranger.plugin.model.RangerPolicy in project ranger by apache.
the class ServiceDBStore method deletePolicy.
@Override
public void deletePolicy(Long policyId) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.deletePolicy(" + policyId + ")");
}
RangerPolicy policy = getPolicy(policyId);
if (policy == null) {
throw new Exception("no policy exists with ID=" + policyId);
}
String policyName = policy.getName();
RangerService service = getServiceByName(policy.getService());
if (service == null) {
throw new Exception("service does not exist - name='" + policy.getService());
}
Long version = policy.getVersion();
if (version == null) {
version = Long.valueOf(1);
LOG.info("Found Version Value: `null`, so setting value of version to 1, While updating object, version should not be null.");
} else {
version = Long.valueOf(version.longValue() + 1);
}
policy.setVersion(version);
List<XXTrxLog> trxLogList = policyService.getTransactionLog(policy, RangerPolicyService.OPERATION_DELETE_CONTEXT);
deleteExistingPolicyItems(policy);
deleteExistingPolicyResources(policy);
deleteExistingPolicyLabel(policy);
policyService.delete(policy);
handlePolicyUpdate(service, true);
dataHistService.createObjectDataHistory(policy, RangerDataHistService.ACTION_DELETE);
bizUtil.createTrxLog(trxLogList);
LOG.info("Policy Deleted Successfully. PolicyName : " + policyName);
}
use of org.apache.ranger.plugin.model.RangerPolicy in project ranger by apache.
the class ServiceDBStore method getServicePolicies.
@Override
public List<RangerPolicy> getServicePolicies(Long serviceId, SearchFilter filter) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.getServicePolicies(" + serviceId + ")");
}
XXService service = daoMgr.getXXService().getById(serviceId);
if (service == null) {
throw new Exception("service does not exist - id='" + serviceId);
}
List<RangerPolicy> ret = getServicePolicies(service, filter);
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceDBStore.getServicePolicies(" + serviceId + ") : policy-count=" + (ret == null ? 0 : ret.size()));
}
return ret;
}
Aggregations