From afd1204a22858b569c3a69a8a4d13efbc8d3fe1e Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Wed, 14 Jun 2023 18:31:58 +0530 Subject: [PATCH] Add logs for policy priority change and apply changes to devices --- .../notification/logger/PolicyLogContext.java | 32 +++++++++++++++++++ .../logger/util/MDCContextUtil.java | 6 ++++ .../impl/PolicyAdministratorPointImpl.java | 10 +++++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 27 ++++++++++++++-- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java index 073c4f3587..9dbd10a059 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/PolicyLogContext.java @@ -23,12 +23,16 @@ import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; public class PolicyLogContext extends LogContext { private final String policyName; private final String payload; + private final String actionTag; + private final String userName; private final String tenantID; private final String tenantDomain; private PolicyLogContext(Builder builder) { this.policyName = builder.policyName; this.payload = builder.payload; + this.actionTag = builder.actionTag; + this.userName = builder.userName; this.tenantID = builder.tenantID; this.tenantDomain = builder.tenantDomain; } @@ -41,6 +45,14 @@ public class PolicyLogContext extends LogContext { return payload; } + public String getActionTag() { + return actionTag; + } + + public String getUserName() { + return userName; + } + public String getTenantID() { return tenantID; } @@ -52,6 +64,8 @@ public class PolicyLogContext extends LogContext { public static class Builder { private String policyName; private String payload; + private String actionTag; + private String userName; private String tenantID; private String tenantDomain; @@ -76,6 +90,24 @@ public class PolicyLogContext extends LogContext { return this; } + public String getActionTag() { + return actionTag; + } + + public Builder setActionTag(String actionTag) { + this.actionTag = actionTag; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + public String getTenantID() { return tenantID; } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java index e7e01b45e6..dc0de89db9 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/util/MDCContextUtil.java @@ -69,6 +69,12 @@ public final class MDCContextUtil { if (mdcContext.getPayload() != null) { MDC.put("Payload", mdcContext.getPayload()); } + if (mdcContext.getActionTag() != null) { + MDC.put("ActionTag", mdcContext.getActionTag()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } if (mdcContext.getTenantDomain() != null) { MDC.put("TenantDomain", mdcContext.getTenantDomain()); } diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 2521eab3a1..7ead0213c9 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.policy.mgt.core.impl; import io.entgra.device.mgt.core.device.mgt.common.PolicyPaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.PolicyLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraPolicyLoggerImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -50,7 +53,8 @@ import java.util.Set; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { - private static final Log log = LogFactory.getLog(PolicyAdministratorPointImpl.class); + PolicyLogContext.Builder policyLogContextBuilder = new PolicyLogContext.Builder(); + private static final EntgraLogger log = new EntgraPolicyLoggerImpl(PolicyAdministratorPointImpl.class); private PolicyManager policyManager; private ProfileManager profileManager; @@ -131,6 +135,8 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); if (log.isDebugEnabled()) { @@ -164,11 +170,13 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.scheduleTask(taskInfo.getName()); + log.info("Apply changes to device", policyLogContextBuilder.setActionTag("PUBLISH_CHANGES").setUserName(userName).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).build()); } else { if (!taskManager.isTaskScheduled(taskName)) { TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); taskManager.scheduleTask(taskInfo.getName()); + log.info("Apply changes to device", policyLogContextBuilder.setActionTag("PUBLISH_CHANGES").setUserName(userName).setTenantID(String.valueOf(tenantId)).setTenantDomain(tenantDomain).build()); } else { throw new PolicyManagementException("There is a task already running for policy changes. Please try " + "to apply " + diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 385fcaf165..9d362bcc9e 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/main/java/io/entgra/device/mgt/core/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -96,6 +96,7 @@ public class PolicyManagerImpl implements PolicyManager { public Policy addPolicy(Policy policy) throws PolicyManagementException { String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getProfile() != null && policy.getProfile().getProfileId() == 0) { @@ -178,7 +179,7 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.closeConnection(); } String stringPayload = new Gson().toJson(policy); - log.info("Policy Created", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); + log.info("Policy created", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("ADD_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return policy; } @@ -186,6 +187,7 @@ public class PolicyManagerImpl implements PolicyManager { public Policy updatePolicy(Policy policy) throws PolicyManagementException { String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); try { // Previous policy needs to be obtained before beginning the transaction Policy previousPolicy = this.getPolicy(policy.getId()); @@ -328,7 +330,7 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.closeConnection(); } String stringPayload = new Gson().toJson(policy); - log.info("Policy Updated", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); + log.info("Policy updated", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("UPDATE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return policy; } @@ -518,6 +520,10 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); + String stringPayload = new Gson().toJson(policies); boolean bool; try { List existingPolicies; @@ -546,18 +552,24 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } + log.info("Policy priorities updated", policyLogContextBuilder.setPayload(stringPayload).setActionTag("UPDATE_POLICY_PRIORITIES").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return bool; } @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { try { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); PolicyManagementDAOFactory.beginTransaction(); policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); policyDAO.deletePolicy(policy.getId()); featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); + String stringPayload = new Gson().toJson(policy); + log.info("Policy deleted", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("DELETE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return true; } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -578,6 +590,9 @@ public class PolicyManagerImpl implements PolicyManager { @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); boolean bool; List policies = this.getPolicies(); Policy pol = null; @@ -608,6 +623,8 @@ public class PolicyManagerImpl implements PolicyManager { featureDAO.deleteFeaturesOfProfile(policy.getProfileId()); profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); + String stringPayload = new Gson().toJson(policy); + log.info("Policy deleted", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(stringPayload).setActionTag("DELETE_POLICY").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); return bool; } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -666,6 +683,9 @@ public class PolicyManagerImpl implements PolicyManager { List deviceList = new ArrayList<>(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder .getInstance().getDeviceManagementService(); + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { try { Device device = deviceManagementService.getDevice(deviceIdentifier, false); @@ -694,6 +714,9 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setDevices(deviceList); } + String policyPayload = new Gson().toJson(policy); + String deviceListPayload = new Gson().toJson(deviceList); + log.info("Policy added to devices", policyLogContextBuilder.setPolicyName(policy.getPolicyName()).setPayload(" Policy: " + policyPayload + " DeviceList: " + deviceListPayload).setActionTag("ADD_POLICY_TO_DEVICE").setUserName(userName).setTenantID(tenantId).setTenantDomain(tenantDomain).build()); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while adding the policy ("