use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.
the class ServiceDBStore method writeCSVForPolicyItems.
private void writeCSVForPolicyItems(RangerPolicy policy, RangerPolicyItem policyItem, RangerDataMaskPolicyItem dataMaskPolicyItem, RangerRowFilterPolicyItem rowFilterPolicyItem, StringBuilder csvBuffer, String policyConditionType) {
if (LOG.isDebugEnabled()) {
// To avoid PMD violation
LOG.debug("policyConditionType:[" + policyConditionType + "]");
}
final String COMMA_DELIMITER = "|";
final String LINE_SEPARATOR = "\n";
List<String> groups = new ArrayList<String>();
List<String> users = new ArrayList<String>();
String groupNames = "";
String userNames = "";
String policyLabelName = "";
String accessType = "";
String policyStatus = "";
String policyType = "";
Boolean delegateAdmin = false;
String isRecursive = "";
String isExcludes = "";
String serviceName = "";
String description = "";
Boolean isAuditEnabled = true;
String isExcludesValue = "";
String maskingInfo = "";
List<RangerPolicyItemAccess> accesses = new ArrayList<RangerPolicyItemAccess>();
List<RangerPolicyItemCondition> conditionsList = new ArrayList<RangerPolicyItemCondition>();
String conditionKeyValue = "";
String resValue = "";
String resourceKeyVal = "";
String isRecursiveValue = "";
String resKey = "";
String ServiceType = "";
String filterExpr = "";
String policyName = "";
List<String> policyLabels = new ArrayList<String>();
String policyConditionTypeValue = "";
serviceName = policy.getService();
description = policy.getDescription();
isAuditEnabled = policy.getIsAuditEnabled();
policyLabels = policy.getPolicyLabels();
StringBuffer sb = new StringBuffer();
StringBuffer sbIsRecursive = new StringBuffer();
StringBuffer sbIsExcludes = new StringBuffer();
Map<String, RangerPolicyResource> resources = policy.getResources();
RangerPolicyItemDataMaskInfo dataMaskInfo = new RangerPolicyItemDataMaskInfo();
RangerPolicyItemRowFilterInfo filterInfo = new RangerPolicyItemRowFilterInfo();
policyName = policy.getName();
policyName = policyName.replace("|", "");
if (resources != null) {
for (Entry<String, RangerPolicyResource> resource : resources.entrySet()) {
resKey = resource.getKey();
RangerPolicyResource policyResource = resource.getValue();
List<String> resvalueList = policyResource.getValues();
isExcludes = policyResource.getIsExcludes().toString();
isRecursive = policyResource.getIsRecursive().toString();
resValue = resvalueList.toString();
sb = sb.append(resourceKeyVal).append(" ").append(resKey).append("=").append(resValue);
sbIsExcludes = sbIsExcludes.append(resourceKeyVal).append(" ").append(resKey).append("=[").append(isExcludes).append("]");
sbIsRecursive = sbIsRecursive.append(resourceKeyVal).append(" ").append(resKey).append("=[").append(isRecursive).append("]");
}
isExcludesValue = sbIsExcludes.toString();
isExcludesValue = isExcludesValue.substring(1);
isRecursiveValue = sbIsRecursive.toString();
isRecursiveValue = isRecursiveValue.substring(1);
resourceKeyVal = sb.toString();
resourceKeyVal = resourceKeyVal.substring(1);
if (policyItem != null && dataMaskPolicyItem == null && rowFilterPolicyItem == null) {
groups = policyItem.getGroups();
users = policyItem.getUsers();
accesses = policyItem.getAccesses();
delegateAdmin = policyItem.getDelegateAdmin();
conditionsList = policyItem.getConditions();
} else if (dataMaskPolicyItem != null && policyItem == null && rowFilterPolicyItem == null) {
groups = dataMaskPolicyItem.getGroups();
users = dataMaskPolicyItem.getUsers();
accesses = dataMaskPolicyItem.getAccesses();
delegateAdmin = dataMaskPolicyItem.getDelegateAdmin();
conditionsList = dataMaskPolicyItem.getConditions();
dataMaskInfo = dataMaskPolicyItem.getDataMaskInfo();
String dataMaskType = dataMaskInfo.getDataMaskType();
String conditionExpr = dataMaskInfo.getConditionExpr();
String valueExpr = dataMaskInfo.getValueExpr();
maskingInfo = "dataMasktype=[" + dataMaskType + "]";
if (conditionExpr != null && !conditionExpr.isEmpty() && valueExpr != null && !valueExpr.isEmpty()) {
maskingInfo = maskingInfo + "; conditionExpr=[" + conditionExpr + "]";
}
} else if (rowFilterPolicyItem != null && policyItem == null && dataMaskPolicyItem == null) {
groups = rowFilterPolicyItem.getGroups();
users = rowFilterPolicyItem.getUsers();
accesses = rowFilterPolicyItem.getAccesses();
delegateAdmin = rowFilterPolicyItem.getDelegateAdmin();
conditionsList = rowFilterPolicyItem.getConditions();
filterInfo = rowFilterPolicyItem.getRowFilterInfo();
filterExpr = filterInfo.getFilterExpr();
}
if (CollectionUtils.isNotEmpty(accesses)) {
for (RangerPolicyItemAccess access : accesses) {
accessType = accessType + access.getType().replace("#", "").replace("|", "") + "#";
}
accessType = accessType.substring(0, accessType.lastIndexOf("#"));
}
if (CollectionUtils.isNotEmpty(groups)) {
for (String group : groups) {
group = group.replace("|", "");
group = group.replace("#", "");
groupNames = groupNames + group + "#";
}
groupNames = groupNames.substring(0, groupNames.lastIndexOf("#"));
}
if (CollectionUtils.isNotEmpty(users)) {
for (String user : users) {
user = user.replace("|", "");
user = user.replace("#", "");
userNames = userNames + user + "#";
}
userNames = userNames.substring(0, userNames.lastIndexOf("#"));
}
String conditionValue = "";
for (RangerPolicyItemCondition conditions : conditionsList) {
String conditionType = conditions.getType();
List<String> conditionList = conditions.getValues();
conditionValue = conditionList.toString();
conditionKeyValue = conditionType + "=" + conditionValue;
}
XXService xxservice = daoMgr.getXXService().findByName(policy.getService());
if (xxservice != null) {
Long ServiceId = xxservice.getType();
XXServiceDef xxservDef = daoMgr.getXXServiceDef().getById(ServiceId);
if (xxservDef != null) {
ServiceType = xxservDef.getName();
}
}
}
if (policyConditionType != null) {
policyConditionTypeValue = policyConditionType;
}
if (policyConditionType == null && ServiceType.equalsIgnoreCase("tag")) {
policyConditionTypeValue = POLICY_ALLOW_INCLUDE;
} else if (policyConditionType == null) {
policyConditionTypeValue = "";
}
if (policy.getIsEnabled()) {
policyStatus = "Enabled";
} else {
policyStatus = "Disabled";
}
int policyTypeInt = policy.getPolicyType();
switch(policyTypeInt) {
case RangerPolicy.POLICY_TYPE_ACCESS:
policyType = POLICY_TYPE_ACCESS;
break;
case RangerPolicy.POLICY_TYPE_DATAMASK:
policyType = POLICY_TYPE_DATAMASK;
break;
case RangerPolicy.POLICY_TYPE_ROWFILTER:
policyType = POLICY_TYPE_ROWFILTER;
break;
}
if (CollectionUtils.isNotEmpty(policyLabels)) {
for (String policyLabel : policyLabels) {
policyLabel = policyLabel.replace("|", "");
policyLabel = policyLabel.replace("#", "");
policyLabelName = policyLabelName + policyLabel + "#";
}
policyLabelName = policyLabelName.substring(0, policyLabelName.lastIndexOf("#"));
}
csvBuffer.append(policy.getId());
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(policyName);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(resourceKeyVal);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(groupNames);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(userNames);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(accessType.trim());
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(ServiceType);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(policyStatus);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(policyType);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(delegateAdmin.toString().toUpperCase());
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(isRecursiveValue);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(isExcludesValue);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(serviceName);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(description);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(isAuditEnabled.toString().toUpperCase());
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(conditionKeyValue.trim());
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(policyConditionTypeValue);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(maskingInfo);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(filterExpr);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(policyLabelName);
csvBuffer.append(COMMA_DELIMITER);
csvBuffer.append(LINE_SEPARATOR);
}
use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.
the class ServiceDBStore method getServiceDefByName.
@Override
public RangerServiceDef getServiceDefByName(String name) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.getServiceDefByName(" + name + ")");
}
RangerServiceDef ret = null;
XXServiceDef xServiceDef = daoMgr.getXXServiceDef().findByName(name);
if (xServiceDef != null) {
ret = serviceDefService.getPopulatedViewObject(xServiceDef);
}
if (LOG.isDebugEnabled()) {
LOG.debug("== ServiceDBStore.getServiceDefByName(" + name + "): " + ret);
}
return ret;
}
use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.
the class ServiceDBStore method writeCSV.
private StringBuilder writeCSV(List<RangerPolicy> policies, String cSVFileName, HttpServletResponse response) {
response.setContentType("text/csv");
final String LINE_SEPARATOR = "\n";
final String FILE_HEADER = "ID|Name|Resources|Groups|Users|Accesses|Service Type|Status|Policy Type|Delegate Admin|isRecursive|" + "isExcludes|Service Name|Description|isAuditEnabled|Policy Conditions|Policy Condition Type|Masking Options|Row Filter Expr|Policy Label Name";
StringBuilder csvBuffer = new StringBuilder();
csvBuffer.append(FILE_HEADER);
csvBuffer.append(LINE_SEPARATOR);
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) {
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
}
} else if (CollectionUtils.isNotEmpty(dataMaskPolicyItems)) {
for (RangerDataMaskPolicyItem dataMaskPolicyItem : dataMaskPolicyItems) {
writeCSVForPolicyItems(policy, null, dataMaskPolicyItem, null, csvBuffer, null);
}
} else if (CollectionUtils.isNotEmpty(rowFilterPolicyItems)) {
for (RangerRowFilterPolicyItem rowFilterPolicyItem : rowFilterPolicyItems) {
writeCSVForPolicyItems(policy, null, null, rowFilterPolicyItem, csvBuffer, null);
}
} else if (serviceType.equalsIgnoreCase(EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME)) {
if (CollectionUtils.isEmpty(policyItems)) {
RangerPolicyItem policyItem = new RangerPolicyItem();
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
}
} else if (CollectionUtils.isEmpty(policyItems)) {
RangerPolicyItem policyItem = new RangerPolicyItem();
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_INCLUDE);
}
if (CollectionUtils.isNotEmpty(allowExceptions)) {
for (RangerPolicyItem policyItem : allowExceptions) {
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_ALLOW_EXCLUDE);
}
}
if (CollectionUtils.isNotEmpty(denyExceptions)) {
for (RangerPolicyItem policyItem : denyExceptions) {
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_DENY_EXCLUDE);
}
}
if (CollectionUtils.isNotEmpty(denyPolicyItems)) {
for (RangerPolicyItem policyItem : denyPolicyItems) {
writeCSVForPolicyItems(policy, policyItem, null, null, csvBuffer, POLICY_DENY_INCLUDE);
}
}
}
}
response.setHeader("Content-Disposition", "attachment; filename=" + cSVFileName);
response.setStatus(HttpServletResponse.SC_OK);
return csvBuffer;
}
use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.
the class TagDBStore method createResourceForServiceResource.
private void createResourceForServiceResource(Long resourceId, RangerServiceResource serviceResource) {
String serviceName = serviceResource.getServiceName();
XXService xService = daoManager.getXXService().findByName(serviceName);
if (xService == null) {
throw errorUtil.createRESTException("No Service found with name: " + serviceName, MessageEnums.ERROR_CREATING_OBJECT);
}
XXServiceDef xServiceDef = daoManager.getXXServiceDef().getById(xService.getType());
if (xServiceDef == null) {
throw errorUtil.createRESTException("No Service-Def found with ID: " + xService.getType(), MessageEnums.ERROR_CREATING_OBJECT);
}
Map<String, RangerPolicy.RangerPolicyResource> resElements = serviceResource.getResourceElements();
for (Map.Entry<String, RangerPolicyResource> resElement : resElements.entrySet()) {
XXResourceDef xResDef = daoManager.getXXResourceDef().findByNameAndServiceDefId(resElement.getKey(), xServiceDef.getId());
if (xResDef == null) {
LOG.error("TagDBStore.createResource: ResourceType is not valid [" + resElement.getKey() + "]");
throw errorUtil.createRESTException("Resource Type is not valid [" + resElement.getKey() + "]", MessageEnums.DATA_NOT_FOUND);
}
RangerPolicyResource policyRes = resElement.getValue();
XXServiceResourceElement resourceElement = new XXServiceResourceElement();
resourceElement.setIsExcludes(policyRes.getIsExcludes());
resourceElement.setIsRecursive(policyRes.getIsRecursive());
resourceElement.setResDefId(xResDef.getId());
resourceElement.setResourceId(resourceId);
resourceElement = rangerAuditFields.populateAuditFieldsForCreate(resourceElement);
resourceElement = daoManager.getXXServiceResourceElement().create(resourceElement);
int sortOrder = 1;
for (String resVal : policyRes.getValues()) {
XXServiceResourceElementValue resourceElementValue = new XXServiceResourceElementValue();
resourceElementValue.setResElementId(resourceElement.getId());
resourceElementValue.setValue(resVal);
resourceElementValue.setSortOrder(sortOrder);
resourceElementValue = rangerAuditFields.populateAuditFieldsForCreate(resourceElementValue);
resourceElementValue = daoManager.getXXServiceResourceElementValue().create(resourceElementValue);
sortOrder++;
}
}
}
use of org.apache.ranger.entity.XXServiceDef in project ranger by apache.
the class TestRangerServiceDefServiceBase method test1MapViewToEntityBean.
@Test
public void test1MapViewToEntityBean() {
RangerServiceDef rangerServiceDef = rangerServiceDef();
XXServiceDef serviceDef = serviceDef();
int operationContext = 1;
XXServiceDef dbServiceDef = rangerServiceDefService.mapViewToEntityBean(rangerServiceDef, serviceDef, operationContext);
Assert.assertNotNull(dbServiceDef);
Assert.assertEquals(dbServiceDef, serviceDef);
Assert.assertEquals(dbServiceDef.getDescription(), serviceDef.getDescription());
Assert.assertEquals(dbServiceDef.getGuid(), serviceDef.getGuid());
Assert.assertEquals(dbServiceDef.getName(), serviceDef.getName());
Assert.assertEquals(dbServiceDef.getAddedByUserId(), serviceDef.getAddedByUserId());
Assert.assertEquals(dbServiceDef.getId(), serviceDef.getId());
Assert.assertEquals(dbServiceDef.getVersion(), serviceDef.getVersion());
Assert.assertEquals(dbServiceDef.getImplclassname(), serviceDef.getImplclassname());
Assert.assertEquals(dbServiceDef.getUpdatedByUserId(), serviceDef.getUpdatedByUserId());
}
Aggregations