use of org.apache.ranger.entity.XXAsset in project ranger by apache.
the class XResourceService method validateForCreate.
@Override
protected void validateForCreate(VXResource vObj) {
if (vObj == null) {
throw restErrorUtil.createRESTException("Policy not provided.", MessageEnums.DATA_NOT_FOUND);
}
Long assetId = vObj.getAssetId();
if (assetId != null) {
XXAsset xAsset = daoManager.getXXAsset().getById(assetId);
if (xAsset == null) {
throw restErrorUtil.createRESTException("The repository for which " + "the policy is created, doesn't exist in the system.", MessageEnums.OPER_NOT_ALLOWED_FOR_STATE);
}
} else {
logger.debug("Asset id not provided.");
throw restErrorUtil.createRESTException("Please provide repository" + " id for policy.", MessageEnums.OPER_NOT_ALLOWED_FOR_STATE);
}
String resourceName = vObj.getName();
// int isRecursive = vObj.getIsRecursive();
if (stringUtil.isEmpty(resourceName)) {
logger.error("Resource name not found for : " + vObj.toString());
throw restErrorUtil.createRESTException("Please provide valid resources.", MessageEnums.INVALID_INPUT_DATA);
}
// String[] resourceNameList = stringUtil.split(resourceName, ",");
// for(String resName : resourceNameList){
// List<XXResource> xXResourceList = null;
// if (assetType == AppConstants.ASSET_HDFS) {
// xXResourceList = appDaoManager.getXXResource()
// .findByResourceNameAndAssetIdAndRecursiveFlag(resName, assetId, isRecursive);
// } else {
// xXResourceList = appDaoManager.getXXResource()
// .findByResourceNameAndAssetIdAndResourceType(vObj.getName(),
// vObj.getAssetId(), vObj.getResourceType());
// }
//
// if (xXResourceList != null) {
// boolean similarPolicyFound = false;
// for(XXResource xxResource : xXResourceList){
// String dbResourceName = xxResource.getName();
// // Not checking dbResourceName to be null or empty
// // as this should never be the case
// String[] resources = stringUtil.split(dbResourceName, ",");
// for(String dbResource: resources){
// if(dbResource.equalsIgnoreCase(resName)){
// if(resourceId!=null){
// Long dbResourceId = xxResource.getId();
// if(!resourceId.equals(dbResourceId)){
// similarPolicyFound = true;
// break;
// }
// } else {
// similarPolicyFound = true;
// break;
// }
// }
// }
// if(similarPolicyFound){
// break;
// }
// }
// if(similarPolicyFound){
// throw restErrorUtil.createRESTException(
// "Similar policy already exists for the resource : " + resName,
// MessageEnums.ERROR_DUPLICATE_OBJECT);
// }
// }
// }
// if(vObj.getAssetType())
}
use of org.apache.ranger.entity.XXAsset in project ranger by apache.
the class XResourceService method getTransactionLog.
public List<XXTrxLog> getTransactionLog(VXResource vObj, XXResource mObj, String action) {
if (vObj == null || action == null || ("update".equalsIgnoreCase(action) && mObj == null)) {
return null;
}
XXAsset xAsset = daoManager.getXXAsset().getById(vObj.getAssetId());
String parentObjectName = xAsset.getName();
List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
Field[] fields = vObj.getClass().getDeclaredFields();
Field nameField;
try {
nameField = vObj.getClass().getDeclaredField("name");
nameField.setAccessible(true);
String objectName = "" + nameField.get(vObj);
for (Field field : fields) {
field.setAccessible(true);
String fieldName = field.getName();
if (!trxLogAttrs.containsKey(fieldName)) {
continue;
}
int policyType = vObj.getAssetType();
if (policyType == AppConstants.ASSET_HDFS) {
String[] ignoredAttribs = { "tableType", "columnType", "isEncrypt", "databases", "tables", "columnFamilies", "columns", "udfs" };
if (ArrayUtils.contains(ignoredAttribs, fieldName)) {
continue;
}
} else if (policyType == AppConstants.ASSET_HIVE) {
String[] ignoredAttribs = { "name", "isRecursive", "isEncrypt", "columnFamilies" };
if (ArrayUtils.contains(ignoredAttribs, fieldName)) {
continue;
}
} else if (policyType == AppConstants.ASSET_HBASE) {
String[] ignoredAttribs = { "name", "tableType", "columnType", "isRecursive", "databases", "udfs" };
if (ArrayUtils.contains(ignoredAttribs, fieldName)) {
continue;
}
} else if (policyType == AppConstants.ASSET_KNOX || policyType == AppConstants.ASSET_STORM) {
String[] ignoredAttribs = { "name", "tableType", "columnType", "isEncrypt", "databases", "tables", "columnFamilies", "columns", "udfs" };
if (ArrayUtils.contains(ignoredAttribs, fieldName)) {
continue;
}
}
VTrxLogAttr vTrxLogAttr = trxLogAttrs.get(fieldName);
XXTrxLog xTrxLog = new XXTrxLog();
xTrxLog.setAttributeName(vTrxLogAttr.getAttribUserFriendlyName());
String value = null;
boolean isEnum = vTrxLogAttr.isEnum();
if (isEnum) {
String enumName = XXResource.getEnumName(fieldName);
if (enumName == null && "assetType".equals(fieldName)) {
enumName = "CommonEnums.AssetType";
}
int enumValue = field.get(vObj) == null ? 0 : Integer.parseInt("" + field.get(vObj));
value = xaEnumUtil.getLabel(enumName, enumValue);
} else {
value = "" + field.get(vObj);
if (value == null || "null".equalsIgnoreCase(value)) {
continue;
}
}
if ("create".equalsIgnoreCase(action)) {
if (stringUtil.isEmpty(value)) {
continue;
}
xTrxLog.setNewValue(value);
} else if ("delete".equalsIgnoreCase(action)) {
xTrxLog.setPreviousValue(value);
} else if ("update".equalsIgnoreCase(action)) {
String oldValue = null;
Field[] mFields = mObj.getClass().getDeclaredFields();
for (Field mField : mFields) {
mField.setAccessible(true);
String mFieldName = mField.getName();
if (fieldName.equalsIgnoreCase(mFieldName)) {
if (isEnum) {
String enumName = XXResource.getEnumName(mFieldName);
if (enumName == null && "assetType".equals(mFieldName)) {
enumName = "CommonEnums.AssetType";
}
int enumValue = mField.get(mObj) == null ? 0 : Integer.parseInt("" + mField.get(mObj));
oldValue = xaEnumUtil.getLabel(enumName, enumValue);
} else {
oldValue = mField.get(mObj) + "";
}
break;
}
}
if (value.equalsIgnoreCase(oldValue) && !"policyName".equals(fieldName)) {
continue;
}
xTrxLog.setPreviousValue(oldValue);
xTrxLog.setNewValue(value);
}
xTrxLog.setAction(action);
xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_RESOURCE);
xTrxLog.setObjectId(vObj.getId());
xTrxLog.setParentObjectClassType(AppConstants.CLASS_TYPE_XA_ASSET);
xTrxLog.setParentObjectId(vObj.getAssetId());
xTrxLog.setParentObjectName(parentObjectName);
xTrxLog.setObjectName(objectName);
trxLogList.add(xTrxLog);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
if (trxLogList.isEmpty()) {
XXTrxLog xTrxLog = new XXTrxLog();
xTrxLog.setAction(action);
xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_RESOURCE);
xTrxLog.setObjectId(vObj.getId());
xTrxLog.setObjectName(vObj.getName());
xTrxLog.setParentObjectClassType(AppConstants.CLASS_TYPE_XA_ASSET);
xTrxLog.setParentObjectId(vObj.getAssetId());
xTrxLog.setParentObjectName(parentObjectName);
trxLogList.add(xTrxLog);
}
return trxLogList;
}
use of org.apache.ranger.entity.XXAsset in project ranger by apache.
the class TestRangerBizUtil method testGetDisplayName_AssetName.
@Test
public void testGetDisplayName_AssetName() {
XXAsset obj = new XXAsset();
obj.setDescription(resourceName);
String displayNameChk = rangerBizUtil.getDisplayName(obj);
Assert.assertEquals(resourceName, displayNameChk);
}
use of org.apache.ranger.entity.XXAsset in project ranger by apache.
the class TestRangerBizUtil method testGetMObject_VXDataObject.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testGetMObject_VXDataObject() {
VXAsset vXDataObject = new VXAsset();
vXDataObject.setId(id);
BaseDao baseDao = Mockito.mock(BaseDao.class);
Mockito.when(daoManager.getDaoForClassType(vXDataObject.getMyClassType())).thenReturn(baseDao);
Mockito.when(baseDao.getById(vXDataObject.getId())).thenReturn(new XXAsset());
XXDBBase xXDBaseChk = rangerBizUtil.getMObject(vXDataObject);
Assert.assertNotNull(xXDBaseChk);
}
use of org.apache.ranger.entity.XXAsset in project ranger by apache.
the class TestRangerBizUtil method testGetDisplayNameForClassName.
@Test
public void testGetDisplayNameForClassName() {
XXAsset obj = new XXAsset();
String displayNameChk = rangerBizUtil.getDisplayNameForClassName(obj);
Assert.assertEquals("Asset", displayNameChk);
}
Aggregations