Search in sources :

Example 11 with VTrxLogAttr

use of org.apache.ranger.common.view.VTrxLogAttr in project ranger by apache.

the class XAuditMapService method getTransactionLog.

public List<XXTrxLog> getTransactionLog(VXAuditMap vObj, VXAuditMap mObj, String action) {
    if (vObj == null || action == null || ("update".equalsIgnoreCase(action) && mObj == null)) {
        return null;
    }
    List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
    Field[] fields = vObj.getClass().getDeclaredFields();
    try {
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            if (!trxLogAttrs.containsKey(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 = XXAuditMap.getEnumName(fieldName);
                int enumValue = field.get(vObj) == null ? 0 : Integer.parseInt("" + field.get(vObj));
                value = xaEnumUtil.getLabel(enumName, enumValue);
            } else {
                value = "" + field.get(vObj);
                XXUser xUser = daoManager.getXXUser().getById(Long.parseLong(value));
                value = xUser.getName();
            }
            if ("create".equalsIgnoreCase(action)) {
                xTrxLog.setNewValue(value);
            } else if ("delete".equalsIgnoreCase(action)) {
                xTrxLog.setPreviousValue(value);
            } else if ("update".equalsIgnoreCase(action)) {
                // Not Changed.
                xTrxLog.setNewValue(value);
                xTrxLog.setPreviousValue(value);
            }
            xTrxLog.setAction(action);
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_AUDIT_MAP);
            xTrxLog.setObjectId(vObj.getId());
            xTrxLog.setParentObjectClassType(AppConstants.CLASS_TYPE_XA_RESOURCE);
            xTrxLog.setParentObjectId(vObj.getResourceId());
            // xTrxLog.setParentObjectName(vObj.get);
            // xTrxLog.setObjectName(objectName);
            trxLogList.add(xTrxLog);
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return trxLogList;
}
Also used : XXUser(org.apache.ranger.entity.XXUser) ArrayList(java.util.ArrayList) XXTrxLog(org.apache.ranger.entity.XXTrxLog) VTrxLogAttr(org.apache.ranger.common.view.VTrxLogAttr) Field(java.lang.reflect.Field) SearchField(org.apache.ranger.common.SearchField)

Example 12 with VTrxLogAttr

use of org.apache.ranger.common.view.VTrxLogAttr in project ranger by apache.

the class RangerSecurityZoneServiceService method getTransactionLog.

public List<XXTrxLog> getTransactionLog(RangerSecurityZone vSecurityZone, RangerSecurityZone securityZoneDB, String action) {
    if (vSecurityZone == null || action == null || ("update".equalsIgnoreCase(action) && securityZoneDB == null)) {
        return null;
    }
    List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
    Field[] fields = vSecurityZone.getClass().getDeclaredFields();
    try {
        Field nameField = vSecurityZone.getClass().getDeclaredField("name");
        nameField.setAccessible(true);
        String objectName = "" + nameField.get(vSecurityZone);
        for (Field field : fields) {
            String fieldName = field.getName();
            if (!trxLogAttrs.containsKey(fieldName)) {
                continue;
            }
            field.setAccessible(true);
            VTrxLogAttr vTrxLogAttr = trxLogAttrs.get(fieldName);
            XXTrxLog xTrxLog = new XXTrxLog();
            xTrxLog.setAttributeName(vTrxLogAttr.getAttribUserFriendlyName());
            xTrxLog.setAction(action);
            xTrxLog.setObjectId(vSecurityZone.getId());
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_SECURITY_ZONE);
            xTrxLog.setObjectName(objectName);
            String value = null;
            if (vTrxLogAttr.isEnum()) {
                String enumName = XXUser.getEnumName(fieldName);
                int enumValue = field.get(vSecurityZone) == null ? 0 : Integer.parseInt("" + field.get(vSecurityZone));
                value = xaEnumUtil.getLabel(enumName, enumValue);
            } else {
                value = "" + field.get(vSecurityZone);
                if ((value == null || "null".equalsIgnoreCase(value)) && !"update".equalsIgnoreCase(action)) {
                    continue;
                }
            }
            if ("services".equalsIgnoreCase(fieldName)) {
                Gson gson = new Gson();
                value = gson.toJson(vSecurityZone.getServices(), HashMap.class);
            }
            if ("create".equalsIgnoreCase(action)) {
                xTrxLog.setNewValue(value);
                trxLogList.add(xTrxLog);
            } else if ("delete".equalsIgnoreCase(action)) {
                xTrxLog.setPreviousValue(value);
                trxLogList.add(xTrxLog);
            } else if ("update".equalsIgnoreCase(action)) {
                String oldValue = null;
                Field[] mFields = vSecurityZone.getClass().getDeclaredFields();
                for (Field mField : mFields) {
                    mField.setAccessible(true);
                    String mFieldName = mField.getName();
                    if (fieldName.equalsIgnoreCase(mFieldName)) {
                        if ("services".equalsIgnoreCase(mFieldName)) {
                            Gson gson = new Gson();
                            oldValue = gson.toJson(securityZoneDB.getServices(), HashMap.class);
                        } else {
                            oldValue = mField.get(securityZoneDB) + "";
                        }
                        break;
                    }
                }
                if (oldValue == null || oldValue.equalsIgnoreCase(value)) {
                    continue;
                }
                xTrxLog.setPreviousValue(oldValue);
                xTrxLog.setNewValue(value);
                trxLogList.add(xTrxLog);
            }
        }
        if (trxLogList.isEmpty()) {
            XXTrxLog xTrxLog = new XXTrxLog();
            xTrxLog.setAction(action);
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_RANGER_SECURITY_ZONE);
            xTrxLog.setObjectId(vSecurityZone.getId());
            xTrxLog.setObjectName(objectName);
            trxLogList.add(xTrxLog);
        }
    } catch (IllegalAccessException e) {
        logger.error("Transaction log failure.", e);
    } catch (NoSuchFieldException e) {
        logger.error("Transaction log failure.", e);
    }
    return trxLogList;
}
Also used : VTrxLogAttr(org.apache.ranger.common.view.VTrxLogAttr) Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) XXTrxLog(org.apache.ranger.entity.XXTrxLog)

Aggregations

Field (java.lang.reflect.Field)12 VTrxLogAttr (org.apache.ranger.common.view.VTrxLogAttr)12 XXTrxLog (org.apache.ranger.entity.XXTrxLog)12 ArrayList (java.util.ArrayList)11 SearchField (org.apache.ranger.common.SearchField)7 HashMap (java.util.HashMap)3 SortField (org.apache.ranger.common.SortField)3 XXUser (org.apache.ranger.entity.XXUser)3 XXGroup (org.apache.ranger.entity.XXGroup)2 Gson (com.google.gson.Gson)1 List (java.util.List)1 XXAsset (org.apache.ranger.entity.XXAsset)1 XXDataMaskTypeDef (org.apache.ranger.entity.XXDataMaskTypeDef)1 XXPortalUser (org.apache.ranger.entity.XXPortalUser)1 XXService (org.apache.ranger.entity.XXService)1 XXServiceDef (org.apache.ranger.entity.XXServiceDef)1 RangerDataMaskPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem)1 RangerService (org.apache.ranger.plugin.model.RangerService)1