use of org.apache.ranger.view.VXPermObj in project ranger by apache.
the class ServiceUtil method toGrantRevokeRequest.
public GrantRevokeRequest toGrantRevokeRequest(VXPolicy vXPolicy) {
String serviceType = null;
RangerService service = null;
GrantRevokeRequest ret = new GrantRevokeRequest();
if (vXPolicy != null) {
String serviceName = vXPolicy.getRepositoryName();
try {
service = svcStore.getServiceByName(serviceName);
} catch (Exception e) {
LOG.error(HttpServletResponse.SC_BAD_REQUEST + "No Service Found for ServiceName:" + serviceName);
throw restErrorUtil.createRESTException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage() + serviceName, true);
}
if (service != null) {
serviceType = service.getType();
} else {
LOG.error(HttpServletResponse.SC_BAD_REQUEST + "No Service Found for ServiceName" + serviceName);
throw restErrorUtil.createRESTException(HttpServletResponse.SC_BAD_REQUEST, "No Service Found for ServiceName" + serviceName, true);
}
if (vXPolicy.getGrantor() != null) {
ret.setGrantor(vXPolicy.getGrantor());
}
ret.setEnableAudit(Boolean.TRUE);
ret.setIsRecursive(Boolean.FALSE);
ret.setReplaceExistingPermissions(toBooleanReplacePerm(vXPolicy.isReplacePerm()));
Integer assetType = toAssetType(serviceType);
if (assetType == RangerCommonEnums.ASSET_HIVE) {
String database = StringUtils.isEmpty(vXPolicy.getDatabases()) ? "*" : vXPolicy.getDatabases();
String table = getTableOrUdf(vXPolicy);
String column = StringUtils.isEmpty(vXPolicy.getColumns()) ? "*" : vXPolicy.getColumns();
Map<String, String> mapResource = new HashMap<String, String>();
mapResource.put("database", database);
mapResource.put("table", table);
mapResource.put("column", column);
ret.setResource(mapResource);
} else if (assetType == RangerCommonEnums.ASSET_HBASE) {
String tableName = vXPolicy.getTables();
tableName = StringUtil.isEmpty(tableName) ? "*" : tableName;
String colFamily = vXPolicy.getColumnFamilies();
colFamily = StringUtil.isEmpty(colFamily) ? "*" : colFamily;
String qualifier = vXPolicy.getColumns();
qualifier = StringUtil.isEmpty(qualifier) ? "*" : qualifier;
Map<String, String> mapResource = new HashMap<String, String>();
mapResource.put("table", tableName);
mapResource.put("column-family", colFamily);
mapResource.put("column", qualifier);
ret.setResource(mapResource);
}
List<VXPermObj> vXPermObjList = vXPolicy.getPermMapList();
if (vXPermObjList != null) {
for (VXPermObj vXPermObj : vXPermObjList) {
boolean delegatedAdmin = false;
if (vXPermObj.getUserList() != null) {
for (String user : vXPermObj.getUserList()) {
if (user.contains(getUserName(user))) {
ret.getUsers().add(user);
}
}
}
if (vXPermObj.getGroupList() != null) {
for (String group : vXPermObj.getGroupList()) {
if (group.contains(getGroupName(group))) {
ret.getGroups().add(group);
}
}
}
if (vXPermObj.getPermList() != null) {
for (String perm : vXPermObj.getPermList()) {
if (AppConstants.getEnumFor_XAPermType(perm) != 0) {
if ("Admin".equalsIgnoreCase(perm)) {
delegatedAdmin = true;
if (assetType != null && assetType.intValue() != RangerCommonEnums.ASSET_HBASE) {
continue;
}
}
ret.getAccessTypes().add(perm);
}
}
}
if (delegatedAdmin) {
ret.setDelegateAdmin(Boolean.TRUE);
} else {
ret.setDelegateAdmin(Boolean.FALSE);
}
}
}
}
return ret;
}
use of org.apache.ranger.view.VXPermObj in project ranger by apache.
the class TestServiceUtil method testToRangerPolicy.
@Test
public void testToRangerPolicy() {
Date date = new Date();
List<String> userList = new ArrayList<String>();
userList.add("rangerAdmin");
List<String> groupList = new ArrayList<String>();
groupList.add("rangerGroup");
List<String> permObjList = new ArrayList<String>();
permObjList.add("Admin");
Map<String, RangerPolicyResource> resourceMap = new HashMap<String, RangerPolicyResource>();
List<String> valuesList = new ArrayList<String>();
valuesList.add("resource");
RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
rangerPolicyResource.setIsExcludes(false);
rangerPolicyResource.setIsRecursive(true);
rangerPolicyResource.setValues(valuesList);
resourceMap.put("path", rangerPolicyResource);
List<RangerPolicyItem> rangerPolicyItemList = new ArrayList<RangerPolicy.RangerPolicyItem>();
RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
rangerPolicyItem.setUsers(userList);
rangerPolicyItem.setGroups(groupList);
List<RangerPolicyItemCondition> rangerPolicyItemConditionList = new ArrayList<RangerPolicy.RangerPolicyItemCondition>();
RangerPolicyItemCondition rangerPolicyItemCondition = new RangerPolicyItemCondition();
rangerPolicyItemCondition.setType("ipaddress");
List<String> conditionValueList = new ArrayList<String>();
conditionValueList.add("10.129.35.86");
rangerPolicyItemCondition.setValues(conditionValueList);
rangerPolicyItemConditionList.add(rangerPolicyItemCondition);
rangerPolicyItem.setConditions(rangerPolicyItemConditionList);
rangerPolicyItem.setDelegateAdmin(true);
rangerPolicyItemList.add(rangerPolicyItem);
RangerPolicy expectedRangerPolicy = new RangerPolicy();
expectedRangerPolicy.setId(1L);
expectedRangerPolicy.setName("hdfs");
expectedRangerPolicy.setCreatedBy("rangerAdmin");
expectedRangerPolicy.setCreateTime(date);
expectedRangerPolicy.setDescription("hdfs policy description");
expectedRangerPolicy.setIsAuditEnabled(true);
expectedRangerPolicy.setResources(resourceMap);
expectedRangerPolicy.setPolicyItems(rangerPolicyItemList);
VXPolicy vXPolicy = new VXPolicy();
vXPolicy.setId(1L);
vXPolicy.setCreateDate(date);
vXPolicy.setUpdateDate(date);
vXPolicy.setOwner("rangerAdmin");
vXPolicy.setUpdatedBy("rangerAdmin");
vXPolicy.setPolicyName("hdfs");
vXPolicy.setDescription("hdfs policy description");
vXPolicy.setIsEnabled(true);
vXPolicy.setIsAuditEnabled(true);
vXPolicy.setIsRecursive(true);
vXPolicy.setResourceName("resource");
RangerService service = new RangerService();
service.setId(1L);
service.setName("hdfsService");
service.setType("hdfs");
List<VXPermObj> vXPermObjList = new ArrayList<VXPermObj>();
VXPermObj vXPermObj = new VXPermObj();
vXPermObj.setUserList(userList);
vXPermObj.setGroupList(groupList);
vXPermObj.setPermList(permObjList);
vXPermObj.setIpAddress("10.129.35.86");
vXPermObjList.add(vXPermObj);
vXPolicy.setPermMapList(vXPermObjList);
RangerPolicy actualRangerPolicy = serviceUtil.toRangerPolicy(vXPolicy, service);
Assert.assertNotNull(actualRangerPolicy);
Assert.assertEquals(expectedRangerPolicy.getId(), actualRangerPolicy.getId());
Assert.assertEquals(expectedRangerPolicy.getName(), actualRangerPolicy.getName());
Assert.assertEquals(expectedRangerPolicy.getDescription(), actualRangerPolicy.getDescription());
Assert.assertEquals(expectedRangerPolicy.getCreatedBy(), actualRangerPolicy.getCreatedBy());
Assert.assertTrue(actualRangerPolicy.getIsAuditEnabled());
Assert.assertEquals(expectedRangerPolicy.getResources(), actualRangerPolicy.getResources());
Assert.assertEquals(expectedRangerPolicy.getPolicyItems(), actualRangerPolicy.getPolicyItems());
}
use of org.apache.ranger.view.VXPermObj in project ranger by apache.
the class XPolicyService method mapPermObjToPermList.
private List<VXPermMap> mapPermObjToPermList(List<VXPermObj> permObjList) {
List<VXPermMap> permMapList = new ArrayList<VXPermMap>();
Random rand = new Random();
for (VXPermObj permObj : permObjList) {
String ipAddress = permObj.getIpAddress();
if (!stringUtil.isEmpty(permObj.getUserList())) {
String permGrp = new Date() + " : " + rand.nextInt(9999);
for (String user : permObj.getUserList()) {
XXUser xxUser = xaDaoMgr.getXXUser().findByUserName(user);
if (xxUser == null) {
logger.error("No User found with this name : " + user);
throw restErrorUtil.createRESTException("No User found with name : " + user, MessageEnums.DATA_NOT_FOUND);
}
for (String permission : permObj.getPermList()) {
VXPermMap vXPermMap = new VXPermMap();
int permType = AppConstants.getEnumFor_XAPermType(permission);
vXPermMap.setPermFor(AppConstants.XA_PERM_FOR_USER);
vXPermMap.setPermGroup(permGrp);
vXPermMap.setPermType(permType);
vXPermMap.setUserId(xxUser.getId());
vXPermMap.setIpAddress(ipAddress);
permMapList.add(vXPermMap);
}
}
}
if (!stringUtil.isEmpty(permObj.getGroupList())) {
String permGrp = new Date() + " : " + rand.nextInt(9999);
for (String group : permObj.getGroupList()) {
XXGroup xxGroup = xaDaoMgr.getXXGroup().findByGroupName(group);
if (xxGroup == null) {
logger.error("No UserGroup found with this name : " + group);
throw restErrorUtil.createRESTException("No User found with name : " + group, MessageEnums.DATA_NOT_FOUND);
}
for (String permission : permObj.getPermList()) {
VXPermMap vXPermMap = new VXPermMap();
int permType = AppConstants.getEnumFor_XAPermType(permission);
vXPermMap.setPermFor(AppConstants.XA_PERM_FOR_GROUP);
vXPermMap.setPermGroup(permGrp);
vXPermMap.setPermType(permType);
vXPermMap.setGroupId(xxGroup.getId());
vXPermMap.setIpAddress(ipAddress);
permMapList.add(vXPermMap);
}
}
}
}
return permMapList;
}
use of org.apache.ranger.view.VXPermObj in project ranger by apache.
the class XPolicyService method mapPermObjToPermList.
private List<VXPermMap> mapPermObjToPermList(List<VXPermObj> permObjList, VXPolicy vXPolicy) {
Long resId = vXPolicy.getId();
List<VXPermMap> permMapList = new ArrayList<VXPermMap>();
List<VXPermMap> updPermMapList = new ArrayList<VXPermMap>();
Map<String, VXPermMap> newPermMap = new LinkedHashMap<String, VXPermMap>();
Random rand = new Random();
Map<String, XXPermMap> prevPermMap = getPrevPermMap(resId);
if (permObjList == null) {
permObjList = new ArrayList<VXPermObj>();
}
for (VXPermObj permObj : permObjList) {
String permGrp = new Date() + " : " + rand.nextInt(9999);
String ipAddress = permObj.getIpAddress();
if (!stringUtil.isEmpty(permObj.getUserList())) {
int permFor = AppConstants.XA_PERM_FOR_USER;
for (String user : permObj.getUserList()) {
XXUser xxUser = xaDaoMgr.getXXUser().findByUserName(user);
if (xxUser == null) {
logger.error("No User found with this name : " + user);
throw restErrorUtil.createRESTException("No User found with name : " + user, MessageEnums.DATA_NOT_FOUND);
}
Long userId = xxUser.getId();
for (String permission : permObj.getPermList()) {
int permType = AppConstants.getEnumFor_XAPermType(permission);
VXPermMap vXPermMap = new VXPermMap();
vXPermMap.setPermFor(AppConstants.XA_PERM_FOR_USER);
vXPermMap.setPermGroup(permGrp);
vXPermMap.setPermType(permType);
vXPermMap.setUserId(xxUser.getId());
vXPermMap.setResourceId(resId);
vXPermMap.setIpAddress(ipAddress);
permMapList.add(vXPermMap);
StringBuilder uniqueKey = new StringBuilder();
uniqueKey.append(resId + uniqueKeySeparator);
uniqueKey.append(permFor + uniqueKeySeparator);
uniqueKey.append(userId + uniqueKeySeparator);
uniqueKey.append(permType);
newPermMap.put(uniqueKey.toString(), vXPermMap);
}
}
}
if (!stringUtil.isEmpty(permObj.getGroupList())) {
int permFor = AppConstants.XA_PERM_FOR_GROUP;
for (String group : permObj.getGroupList()) {
XXGroup xxGroup = xaDaoMgr.getXXGroup().findByGroupName(group);
if (xxGroup == null) {
logger.error("No UserGroup found with this name : " + group);
throw restErrorUtil.createRESTException("No Group found with name : " + group, MessageEnums.DATA_NOT_FOUND);
}
Long grpId = xxGroup.getId();
for (String permission : permObj.getPermList()) {
int permType = AppConstants.getEnumFor_XAPermType(permission);
VXPermMap vXPermMap = new VXPermMap();
vXPermMap.setPermFor(AppConstants.XA_PERM_FOR_GROUP);
vXPermMap.setPermGroup(permGrp);
vXPermMap.setPermType(permType);
vXPermMap.setGroupId(xxGroup.getId());
vXPermMap.setResourceId(resId);
vXPermMap.setIpAddress(ipAddress);
permMapList.add(vXPermMap);
StringBuilder uniqueKey = new StringBuilder();
uniqueKey.append(resId + uniqueKeySeparator);
uniqueKey.append(permFor + uniqueKeySeparator);
uniqueKey.append(grpId + uniqueKeySeparator);
uniqueKey.append(permType);
newPermMap.put(uniqueKey.toString(), vXPermMap);
}
}
}
}
// Create Newly added permissions and Remove deleted permissions from DB
if (prevPermMap.isEmpty()) {
updPermMapList.addAll(permMapList);
} else {
for (Entry<String, VXPermMap> entry : newPermMap.entrySet()) {
if (!prevPermMap.containsKey(entry.getKey())) {
updPermMapList.add(entry.getValue());
} else {
VXPermMap vPMap = xPermMapService.populateViewBean(prevPermMap.get(entry.getKey()));
VXPermMap vPMapNew = entry.getValue();
vPMap.setIpAddress(vPMapNew.getIpAddress());
updPermMapList.add(vPMap);
}
}
}
return updPermMapList;
}
use of org.apache.ranger.view.VXPermObj in project ranger by apache.
the class XPolicyService method mapPermMapToPermObj.
public List<VXPermObj> mapPermMapToPermObj(List<VXPermMap> permMapList) {
List<VXPermObj> permObjList = new ArrayList<VXPermObj>();
HashMap<String, List<VXPermMap>> sortedPemMap = new HashMap<String, List<VXPermMap>>();
if (permMapList != null) {
for (VXPermMap vXPermMap : permMapList) {
String permGrp = vXPermMap.getPermGroup();
List<VXPermMap> sortedList = sortedPemMap.get(permGrp);
if (sortedList == null) {
sortedList = new ArrayList<VXPermMap>();
sortedPemMap.put(permGrp, sortedList);
}
sortedList.add(vXPermMap);
}
}
for (Entry<String, List<VXPermMap>> entry : sortedPemMap.entrySet()) {
VXPermObj vXPermObj = new VXPermObj();
List<String> userList = new ArrayList<String>();
List<String> groupList = new ArrayList<String>();
List<String> permList = new ArrayList<String>();
String ipAddress = "";
List<VXPermMap> permListForGrp = entry.getValue();
for (VXPermMap permMap : permListForGrp) {
if (permMap.getPermFor() == AppConstants.XA_PERM_FOR_USER) {
if (!userList.contains(permMap.getUserName())) {
userList.add(permMap.getUserName());
}
} else if (permMap.getPermFor() == AppConstants.XA_PERM_FOR_GROUP) {
if (!groupList.contains(permMap.getGroupName())) {
groupList.add(permMap.getGroupName());
}
}
String perm = AppConstants.getLabelFor_XAPermType(permMap.getPermType());
if (!permList.contains(perm)) {
permList.add(perm);
}
ipAddress = permMap.getIpAddress();
}
if (!userList.isEmpty()) {
vXPermObj.setUserList(userList);
}
if (!groupList.isEmpty()) {
vXPermObj.setGroupList(groupList);
}
vXPermObj.setPermList(permList);
vXPermObj.setIpAddress(ipAddress);
permObjList.add(vXPermObj);
}
return permObjList;
}
Aggregations