Search in sources :

Example 31 with XXTrxLog

use of org.apache.ranger.entity.XXTrxLog in project ranger by apache.

the class RangerPolicyService method getTransactionLog.

public List<XXTrxLog> getTransactionLog(RangerPolicy vObj, XXPolicy mObj, int action) {
    if (vObj == null || action == 0 || (action == OPERATION_UPDATE_CONTEXT && 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) {
            if (!trxLogAttrs.containsKey(field.getName())) {
                continue;
            }
            XXTrxLog xTrxLog = processFieldToCreateTrxLog(field, objectName, nameField, vObj, mObj, action);
            if (xTrxLog != null) {
                trxLogList.add(xTrxLog);
            }
        }
        Field[] superClassFields = vObj.getClass().getSuperclass().getDeclaredFields();
        for (Field field : superClassFields) {
            if ("isEnabled".equalsIgnoreCase(field.getName())) {
                XXTrxLog xTrx = processFieldToCreateTrxLog(field, objectName, nameField, vObj, mObj, action);
                if (xTrx != null) {
                    trxLogList.add(xTrx);
                }
                break;
            }
        }
    } catch (IllegalAccessException illegalAcc) {
        logger.error("Transaction log failure.", illegalAcc);
    } catch (NoSuchFieldException noSuchField) {
        logger.error("Transaction log failure.", noSuchField);
    }
    return trxLogList;
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) XXTrxLog(org.apache.ranger.entity.XXTrxLog)

Example 32 with XXTrxLog

use of org.apache.ranger.entity.XXTrxLog 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 33 with XXTrxLog

use of org.apache.ranger.entity.XXTrxLog 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 34 with XXTrxLog

use of org.apache.ranger.entity.XXTrxLog 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 35 with XXTrxLog

use of org.apache.ranger.entity.XXTrxLog 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)

Aggregations

XXTrxLog (org.apache.ranger.entity.XXTrxLog)38 ArrayList (java.util.ArrayList)21 VXString (org.apache.ranger.view.VXString)13 Field (java.lang.reflect.Field)12 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)12 RangerService (org.apache.ranger.plugin.model.RangerService)12 VTrxLogAttr (org.apache.ranger.common.view.VTrxLogAttr)10 XXUser (org.apache.ranger.entity.XXUser)9 SearchField (org.apache.ranger.common.SearchField)7 XXPortalUser (org.apache.ranger.entity.XXPortalUser)7 IOException (java.io.IOException)6 UnknownHostException (java.net.UnknownHostException)5 XXPolicy (org.apache.ranger.entity.XXPolicy)5 RangerDataMaskPolicyItem (org.apache.ranger.plugin.model.RangerPolicy.RangerDataMaskPolicyItem)5 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 XXGroup (org.apache.ranger.entity.XXGroup)4 XXService (org.apache.ranger.entity.XXService)4 XXServiceConfigMap (org.apache.ranger.entity.XXServiceConfigMap)4