Search in sources :

Example 11 with XXAsset

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())
}
Also used : XXAsset(org.apache.ranger.entity.XXAsset)

Example 12 with XXAsset

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;
}
Also used : XXAsset(org.apache.ranger.entity.XXAsset) ArrayList(java.util.ArrayList) XXTrxLog(org.apache.ranger.entity.XXTrxLog) VTrxLogAttr(org.apache.ranger.common.view.VTrxLogAttr) SearchField(org.apache.ranger.common.SearchField) Field(java.lang.reflect.Field) SortField(org.apache.ranger.common.SortField)

Example 13 with XXAsset

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);
}
Also used : XXAsset(org.apache.ranger.entity.XXAsset) Test(org.junit.Test)

Example 14 with XXAsset

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);
}
Also used : VXAsset(org.apache.ranger.view.VXAsset) BaseDao(org.apache.ranger.common.db.BaseDao) XXAsset(org.apache.ranger.entity.XXAsset) XXDBBase(org.apache.ranger.entity.XXDBBase) Test(org.junit.Test)

Example 15 with XXAsset

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);
}
Also used : XXAsset(org.apache.ranger.entity.XXAsset) Test(org.junit.Test)

Aggregations

XXAsset (org.apache.ranger.entity.XXAsset)15 Test (org.junit.Test)6 XXResource (org.apache.ranger.entity.XXResource)4 ArrayList (java.util.ArrayList)3 BaseDao (org.apache.ranger.common.db.BaseDao)2 XXDBBase (org.apache.ranger.entity.XXDBBase)2 RangerService (org.apache.ranger.plugin.model.RangerService)2 VXResource (org.apache.ranger.view.VXResource)2 VXResponse (org.apache.ranger.view.VXResponse)2 Field (java.lang.reflect.Field)1 List (java.util.List)1 SearchCriteria (org.apache.ranger.common.SearchCriteria)1 SearchField (org.apache.ranger.common.SearchField)1 SortField (org.apache.ranger.common.SortField)1 VTrxLogAttr (org.apache.ranger.common.view.VTrxLogAttr)1 XXAssetDao (org.apache.ranger.db.XXAssetDao)1 XXPortalUserDao (org.apache.ranger.db.XXPortalUserDao)1 XXResourceDao (org.apache.ranger.db.XXResourceDao)1 XXUserDao (org.apache.ranger.db.XXUserDao)1 XXPolicy (org.apache.ranger.entity.XXPolicy)1