Search in sources :

Example 6 with VTrxLogAttr

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

the class RangerServiceService method processFieldToCreateTrxLog.

@SuppressWarnings("unchecked")
private XXTrxLog processFieldToCreateTrxLog(Field field, String objectName, Field nameField, RangerService vObj, XXService mObj, int action) {
    String actionString = "";
    field.setAccessible(true);
    String fieldName = field.getName();
    XXTrxLog xTrxLog = new XXTrxLog();
    try {
        VTrxLogAttr vTrxLogAttr = trxLogAttrs.get(fieldName);
        xTrxLog.setAttributeName(vTrxLogAttr.getAttribUserFriendlyName());
        String value = null;
        boolean isEnum = vTrxLogAttr.isEnum();
        if (isEnum) {
        } else if ("configs".equalsIgnoreCase(fieldName)) {
            Map<String, String> configs = (field.get(vObj) != null) ? (Map<String, String>) field.get(vObj) : new HashMap<String, String>();
            value = jsonUtil.readMapToString(configs);
        } else {
            value = "" + field.get(vObj);
        }
        if (action == OPERATION_CREATE_CONTEXT) {
            if (stringUtil.isEmpty(value)) {
                return null;
            }
            xTrxLog.setNewValue(value);
            actionString = actionCreate;
        } else if (action == OPERATION_DELETE_CONTEXT) {
            xTrxLog.setPreviousValue(value);
            actionString = actionDelete;
        } else if (action == OPERATION_UPDATE_CONTEXT) {
            actionString = actionUpdate;
            String oldValue = null;
            Field[] mFields = mObj.getClass().getSuperclass().getDeclaredFields();
            for (Field mField : mFields) {
                mField.setAccessible(true);
                String mFieldName = mField.getName();
                if (fieldName.equalsIgnoreCase(mFieldName)) {
                    if (isEnum) {
                    } else {
                        oldValue = mField.get(mObj) + "";
                    }
                    break;
                }
            }
            if ("configs".equalsIgnoreCase(fieldName)) {
                Map<String, String> vConfig = jsonUtil.jsonToMap(value);
                RangerService oldService = this.populateViewBean(mObj);
                Map<String, String> xConfig = oldService.getConfigs();
                Map<String, String> newConfig = new HashMap<String, String>();
                Map<String, String> oldConfig = new HashMap<String, String>();
                for (Entry<String, String> entry : vConfig.entrySet()) {
                    String key = entry.getKey();
                    if (!xConfig.containsKey(key)) {
                        if (StringUtils.isNotEmpty(entry.getValue())) {
                            newConfig.put(key, entry.getValue());
                        }
                    } else if (!entry.getValue().equalsIgnoreCase(xConfig.get(key))) {
                        if ("password".equalsIgnoreCase(key) && entry.getValue().equalsIgnoreCase(hiddenPasswordString)) {
                            continue;
                        }
                        newConfig.put(key, entry.getValue());
                        oldConfig.put(key, xConfig.get(key));
                    }
                }
                for (Entry<String, String> entry : xConfig.entrySet()) {
                    String key = entry.getKey();
                    if (!vConfig.containsKey(key)) {
                        oldConfig.put(key, entry.getValue());
                        newConfig.put(key, null);
                    }
                }
                oldValue = jsonUtil.readMapToString(oldConfig);
                value = jsonUtil.readMapToString(newConfig);
            }
            if ("tagService".equalsIgnoreCase(fieldName)) {
                if (!StringUtils.isEmpty(oldValue) && !"null".equalsIgnoreCase(oldValue)) {
                    RangerService oldService = this.populateViewBean(mObj);
                    oldValue = oldService.getTagService();
                }
            }
            if (oldValue == null || value.equalsIgnoreCase(oldValue)) {
                return null;
            }
            xTrxLog.setPreviousValue(oldValue);
            xTrxLog.setNewValue(value);
        }
    } catch (IllegalArgumentException | IllegalAccessException e) {
        LOG.error("Process field to create trx log failure.", e);
    }
    xTrxLog.setAction(actionString);
    xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_SERVICE);
    xTrxLog.setObjectId(vObj.getId());
    xTrxLog.setObjectName(objectName);
    XXServiceDef parentObj = daoMgr.getXXServiceDef().findByName(vObj.getType());
    xTrxLog.setParentObjectClassType(AppConstants.CLASS_TYPE_XA_SERVICE_DEF);
    xTrxLog.setParentObjectId(parentObj.getId());
    xTrxLog.setParentObjectName(parentObj.getName());
    return xTrxLog;
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) HashMap(java.util.HashMap) XXTrxLog(org.apache.ranger.entity.XXTrxLog) VTrxLogAttr(org.apache.ranger.common.view.VTrxLogAttr) Field(java.lang.reflect.Field) RangerService(org.apache.ranger.plugin.model.RangerService) HashMap(java.util.HashMap) XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) Map(java.util.Map)

Example 7 with VTrxLogAttr

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

the class XAssetService method getTransactionLog.

public List<XXTrxLog> getTransactionLog(VXAsset vObj, XXAsset 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 {
        Field 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;
            }
            VTrxLogAttr vTrxLogAttr = trxLogAttrs.get(fieldName);
            XXTrxLog xTrxLog = new XXTrxLog();
            xTrxLog.setAttributeName(vTrxLogAttr.getAttribUserFriendlyName());
            String value = null;
            boolean isEnum = vTrxLogAttr.isEnum();
            if (isEnum) {
                String enumName = XXAsset.getEnumName(fieldName);
                int enumValue = field.get(vObj) == null ? 0 : Integer.parseInt("" + field.get(vObj));
                value = xaEnumUtil.getLabel(enumName, enumValue);
            } else {
                value = "" + field.get(vObj);
            }
            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 = XXAsset.getEnumName(mFieldName);
                            int enumValue = mField.get(mObj) == null ? 0 : Integer.parseInt("" + mField.get(mObj));
                            oldValue = xaEnumUtil.getLabel(enumName, enumValue);
                        } else {
                            oldValue = mField.get(mObj) + "";
                        }
                        break;
                    }
                }
                if ("config".equalsIgnoreCase(fieldName)) {
                    Map<String, String> vConfig = jsonUtil.jsonToMap(value);
                    Map<String, String> xConfig = jsonUtil.jsonToMap(oldValue);
                    Map<String, String> newConfig = new HashMap<String, String>();
                    Map<String, String> oldConfig = new HashMap<String, String>();
                    for (Entry<String, String> entry : vConfig.entrySet()) {
                        String key = entry.getKey();
                        if (!xConfig.containsKey(key)) {
                            newConfig.put(key, entry.getValue());
                        } else if (!entry.getValue().equalsIgnoreCase(xConfig.get(key))) {
                            if ("password".equalsIgnoreCase(key) && entry.getValue().equalsIgnoreCase(hiddenPasswordString)) {
                                continue;
                            }
                            newConfig.put(key, entry.getValue());
                            oldConfig.put(key, xConfig.get(key));
                        }
                    }
                    oldValue = jsonUtil.readMapToString(oldConfig);
                    value = jsonUtil.readMapToString(newConfig);
                }
                if (value.equalsIgnoreCase(oldValue)) {
                    continue;
                }
                xTrxLog.setPreviousValue(oldValue);
                xTrxLog.setNewValue(value);
            }
            xTrxLog.setAction(action);
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_ASSET);
            xTrxLog.setObjectId(vObj.getId());
            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();
    }
    return trxLogList;
}
Also used : HashMap(java.util.HashMap) 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)

Example 8 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 9 with VTrxLogAttr

use of org.apache.ranger.common.view.VTrxLogAttr 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 10 with VTrxLogAttr

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

the class XUserService method getTransactionLog.

public List<XXTrxLog> getTransactionLog(VXUser vObj, VXPortalUser mObj, String action) {
    if (vObj == null || action == null || ("update".equalsIgnoreCase(action) && mObj == null)) {
        return null;
    }
    List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>();
    try {
        Field nameField = vObj.getClass().getDeclaredField("name");
        nameField.setAccessible(true);
        String objectName = "" + nameField.get(vObj);
        Field[] fields = vObj.getClass().getDeclaredFields();
        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;
            if (vTrxLogAttr.isEnum()) {
                String enumName = XXUser.getEnumName(fieldName);
                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)) && !"update".equalsIgnoreCase(action)) {
                    continue;
                }
            }
            if ("password".equalsIgnoreCase(fieldName)) {
                if (value.equalsIgnoreCase(hiddenPasswordString)) {
                    continue;
                }
            }
            if ("create".equalsIgnoreCase(action)) {
                if (stringUtil.isEmpty(value) || ("emailAddress".equalsIgnoreCase(fieldName) && !stringUtil.validateEmail(value))) {
                    continue;
                }
                xTrxLog.setNewValue(value);
            } else if ("delete".equalsIgnoreCase(action)) {
                if ("emailAddress".equalsIgnoreCase(fieldName) && !stringUtil.validateEmail(value)) {
                    continue;
                }
                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 ("loginId".equalsIgnoreCase(mFieldName)) {
                        mFieldName = "name";
                    }
                    if (fieldName.equalsIgnoreCase(mFieldName)) {
                        oldValue = mField.get(mObj) + "";
                        break;
                    }
                }
                if (oldValue == null || oldValue.equalsIgnoreCase(value)) {
                    continue;
                }
                if ("emailAddress".equalsIgnoreCase(fieldName)) {
                    if (stringUtil.validateEmail(oldValue)) {
                        xTrxLog.setPreviousValue(oldValue);
                    }
                    if (stringUtil.validateEmail(value)) {
                        xTrxLog.setNewValue(value);
                    }
                } else {
                    xTrxLog.setPreviousValue(oldValue);
                    xTrxLog.setNewValue(value);
                }
            }
            xTrxLog.setAction(action);
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_USER);
            xTrxLog.setObjectId(vObj.getId());
            xTrxLog.setObjectName(objectName);
            trxLogList.add(xTrxLog);
        }
        if (trxLogList.isEmpty()) {
            XXTrxLog xTrxLog = new XXTrxLog();
            xTrxLog.setAction(action);
            xTrxLog.setObjectClassType(AppConstants.CLASS_TYPE_XA_USER);
            xTrxLog.setObjectId(vObj.getId());
            xTrxLog.setObjectName(objectName);
            trxLogList.add(xTrxLog);
        }
    } catch (IllegalArgumentException e) {
        logger.error("Transaction log failure.", e);
    } catch (IllegalAccessException e) {
        logger.error("Transaction log failure.", e);
    } catch (NoSuchFieldException e) {
        logger.error("Transaction log failure.", e);
    } catch (SecurityException e) {
        logger.error("Transaction log failure.", e);
    }
    return trxLogList;
}
Also used : VTrxLogAttr(org.apache.ranger.common.view.VTrxLogAttr) SearchField(org.apache.ranger.common.SearchField) Field(java.lang.reflect.Field) SortField(org.apache.ranger.common.SortField) ArrayList(java.util.ArrayList) XXTrxLog(org.apache.ranger.entity.XXTrxLog)

Aggregations

Field (java.lang.reflect.Field)10 VTrxLogAttr (org.apache.ranger.common.view.VTrxLogAttr)10 XXTrxLog (org.apache.ranger.entity.XXTrxLog)10 ArrayList (java.util.ArrayList)9 SearchField (org.apache.ranger.common.SearchField)7 SortField (org.apache.ranger.common.SortField)3 XXUser (org.apache.ranger.entity.XXUser)3 HashMap (java.util.HashMap)2 XXGroup (org.apache.ranger.entity.XXGroup)2 List (java.util.List)1 Map (java.util.Map)1 XXAsset (org.apache.ranger.entity.XXAsset)1 XXDataMaskTypeDef (org.apache.ranger.entity.XXDataMaskTypeDef)1 XXService (org.apache.ranger.entity.XXService)1 XXServiceConfigMap (org.apache.ranger.entity.XXServiceConfigMap)1 XXServiceDef (org.apache.ranger.entity.XXServiceDef)1 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)1 RangerDataMaskPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem)1 RangerService (org.apache.ranger.plugin.model.RangerService)1