From 654b4341e11ef536baf1d2b35c62c0bd4f2d79ec Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Sat, 16 Dec 2023 11:27:41 +0530 Subject: [PATCH] Add custom logs for roles,users,groups,login --- .../impl/GroupManagementServiceImpl.java | 97 +++++- .../impl/RoleManagementServiceImpl.java | 55 ++- .../impl/UserManagementServiceImpl.java | 58 +++- .../impl/GroupManagementServiceImplTest.java | 20 +- .../impl/UserManagementServiceImplTest.java | 23 ++ .../logger/GroupMgtLogContext.java | 181 ++++++++++ .../logger/RoleMgtLogContext.java | 149 ++++++++ ...gContext.java => UserLoginLogContext.java} | 26 +- .../logger/UserMgtLogContext.java | 181 ++++++++++ .../logger/impl/EntgraGroupMgtLoggerImpl.java | 318 +++++++++++++++++ ...Impl.java => EntgraRoleMgtLoggerImpl.java} | 91 +++-- .../impl/EntgraUserLoginLoggerImpl.java | 323 ++++++++++++++++++ .../logger/impl/EntgraUserMgtLoggerImpl.java | 318 +++++++++++++++++ .../logger/util/MDCContextUtil.java | 90 ++++- .../ui/request/interceptor/UserHandler.java | 16 +- 15 files changed, 1864 insertions(+), 82 deletions(-) create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/GroupMgtLogContext.java create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/RoleMgtLogContext.java rename components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/{UserLogContext.java => UserLoginLogContext.java} (84%) create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserMgtLogContext.java create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraGroupMgtLoggerImpl.java rename components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/{EntgraUserLoggerImpl.java => EntgraRoleMgtLoggerImpl.java} (66%) create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoginLoggerImpl.java create mode 100644 components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserMgtLoggerImpl.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java index f825a59c0b..6a7350a977 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; +import com.google.gson.Gson; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; @@ -26,8 +27,9 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.GroupMgtLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraGroupMgtLoggerImpl; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -64,7 +66,8 @@ import java.util.List; public class GroupManagementServiceImpl implements GroupManagementService { - private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); + GroupMgtLogContext.Builder groupMgtContextBuilder = new GroupMgtLogContext.Builder(); + private static final EntgraLogger log = new EntgraGroupMgtLoggerImpl(GroupManagementServiceImpl.class); private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", @@ -168,6 +171,8 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Override public Response createGroup(DeviceGroup group) { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (group == null) { return Response.status(Response.Status.BAD_REQUEST).build(); @@ -176,6 +181,22 @@ public class GroupManagementServiceImpl implements GroupManagementService { group.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); try { DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + int deviceCount = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(group.getGroupId()); + String stringDevices = new Gson().toJson(DeviceMgtAPIUtils.getGroupManagementProviderService().getAllDevicesOfGroup(group.getName(), false)); + log.info( + "Group " + group.getName() + " created", + groupMgtContextBuilder + .setActionTag("ADD_GROUP") + .setGroupId(String.valueOf(group.getGroupId())) + .setName(group.getName()) + .setOwner(group.getOwner()) + .setDeviceCount(String.valueOf(deviceCount)) + .setDevices(stringDevices) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(owner) + .build() + ); return Response.status(Response.Status.CREATED).build(); } catch (GroupManagementException e) { String msg = "Error occurred while adding new group."; @@ -225,11 +246,30 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Override public Response updateGroup(int groupId, DeviceGroup deviceGroup) { + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); if (deviceGroup == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } try { DeviceMgtAPIUtils.getGroupManagementProviderService().updateGroup(deviceGroup, groupId); + int deviceCount = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(groupId); + String stringDevices = new Gson().toJson(DeviceMgtAPIUtils.getGroupManagementProviderService().getAllDevicesOfGroup(deviceGroup.getName(), false)); + log.info( + "Group " + deviceGroup.getName() + " updated", + groupMgtContextBuilder + .setActionTag("UPDATE_GROUP") + .setGroupId(String.valueOf(deviceGroup.getGroupId())) + .setName(deviceGroup.getName()) + .setOwner(deviceGroup.getOwner()) + .setDeviceCount(String.valueOf(deviceCount)) + .setDevices(stringDevices) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(username) + .build() + ); return Response.status(Response.Status.OK).build(); } catch (GroupManagementException e) { String msg = "Error occurred while adding new group."; @@ -249,7 +289,20 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Override public Response deleteGroup(int groupId, boolean isDeleteChildren) { try { + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId, isDeleteChildren)) { + log.info( + "Group with group id " + groupId + " deleted", + groupMgtContextBuilder + .setActionTag("DELETE_GROUP") + .setGroupId(String.valueOf(groupId)) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(username) + .build() + ); return Response.status(Response.Status.OK).build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); @@ -335,17 +388,36 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Override public Response addDevicesToGroup(int groupId, List deviceIdentifiers) { try { + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + List devices = new ArrayList<>(); for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) { Device device = dms.getDevice(deviceIdentifier, false); + devices.add(device); if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { pap.removePolicyUsed(deviceIdentifier); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier); } } pap.publishChanges(); + int deviceCount = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(groupId); + String stringDevices = new Gson().toJson(devices); + log.info( + "Devices added for group id " + groupId, + groupMgtContextBuilder + .setActionTag("ADD_DEVICES") + .setGroupId(String.valueOf(groupId)) + .setDeviceCount(String.valueOf(deviceCount)) + .setDevices(stringDevices) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(username) + .build() + ); return Response.status(Response.Status.OK).build(); } catch (GroupManagementException e) { String msg = "Error occurred while adding devices to group."; @@ -467,6 +539,9 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Path("/roles/share") @Override public Response createGroupWithRoles(DeviceGroupRoleWrapper groups) { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (groups == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } @@ -478,6 +553,22 @@ public class GroupManagementServiceImpl implements GroupManagementService { PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername().isEmpty()); if (group != null) { DeviceMgtAPIUtils.getGroupManagementProviderService().manageGroupSharing(group.getGroupId(), groups.getUserRoles()); + int deviceCount = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(group.getGroupId()); + String stringDevices = new Gson().toJson(DeviceMgtAPIUtils.getGroupManagementProviderService().getAllDevicesOfGroup(group.getName(), false)); + log.info( + "Group " + group.getName() + " created", + groupMgtContextBuilder + .setActionTag("ADD_GROUP") + .setGroupId(String.valueOf(group.getGroupId())) + .setName(group.getName()) + .setOwner(group.getOwner()) + .setDeviceCount(String.valueOf(deviceCount)) + .setDevices(stringDevices) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(username) + .build() + ); return Response.status(Response.Status.CREATED).entity(group.getGroupId()).build(); } else { String msg = "Error occurred while retrieving newly created group."; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java index c52b40c082..49cd588b82 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -17,16 +17,18 @@ */ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; +import com.google.gson.Gson; import io.entgra.device.mgt.core.apimgt.webapp.publisher.exception.APIManagerPublisherException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; -import org.apache.commons.logging.Log; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.RoleMgtLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraRoleMgtLoggerImpl; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -67,7 +69,8 @@ import static io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants.PRIM public class RoleManagementServiceImpl implements RoleManagementService { private static final String API_BASE_PATH = "/roles"; - private static final Log log = LogFactory.getLog(RoleManagementServiceImpl.class); + RoleMgtLogContext.Builder roleMgtContextBuilder = new RoleMgtLogContext.Builder(); + private static final EntgraLogger log = new EntgraRoleMgtLoggerImpl(RoleManagementServiceImpl.class); @GET @Override @@ -404,6 +407,9 @@ public class RoleManagementServiceImpl implements RoleManagementService { RequestValidationUtil.validateRoleDetails(roleInfo); RequestValidationUtil.validateRoleName(roleInfo.getRoleName()); try { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (log.isDebugEnabled()) { log.debug("Persisting the role in the underlying user store"); @@ -429,7 +435,19 @@ public class RoleManagementServiceImpl implements RoleManagementService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } - + String stringUsers = new Gson().toJson(roleInfo.getUsers()); + log.info( + "Role " + roleInfo.getRoleName().split("/")[1] + " created", + roleMgtContextBuilder + .setActionTag("ADD_ROLE") + .setUserStoreDomain(roleInfo.getRoleName().split("/")[0]) + .setRoleName(roleInfo.getRoleName().split("/")[1]) + .setUsers(stringUsers) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(userName) + .build() + ); //TODO fix what's returned in the entity return Response.created(new URI(API_BASE_PATH + "/" + URLEncoder.encode(roleInfo.getRoleName(), "UTF-8"))). entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been" @@ -535,6 +553,9 @@ public class RoleManagementServiceImpl implements RoleManagementService { RequestValidationUtil.validateRoleName(roleName); RequestValidationUtil.validateRoleDetails(roleInfo); try { + String tenantId = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserStoreManager userStoreManager = userRealm.getUserStoreManager(); if (!userStoreManager.isExistingRole(roleName)) { @@ -566,6 +587,19 @@ public class RoleManagementServiceImpl implements RoleManagementService { String[] roleDetails = roleName.split("/"); updatePermissions(roleDetails[roleDetails.length - 1], roleInfo, userRealm); } + String stringUsers = new Gson().toJson(roleInfo.getUsers()); + log.info( + "Role " + roleInfo.getRoleName().split("/")[1] + " updated", + roleMgtContextBuilder + .setActionTag("UPDATE_ROLE") + .setUserStoreDomain(roleInfo.getRoleName().split("/")[0]) + .setRoleName(roleInfo.getRoleName().split("/")[1]) + .setUsers(stringUsers) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(userName) + .build() + ); //TODO: Need to send the updated role information in the entity back to the client return Response.status(Response.Status.OK).entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been updated").build(); @@ -601,6 +635,8 @@ public class RoleManagementServiceImpl implements RoleManagementService { } RequestValidationUtil.validateRoleName(roleName); try { + String tenantDomain = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String userName = String.valueOf(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); final UserRealm userRealm = DeviceMgtAPIUtils.getUserRealm(); final UserStoreManager userStoreManager = userRealm.getUserStoreManager(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -614,6 +650,17 @@ public class RoleManagementServiceImpl implements RoleManagementService { log.debug("Deleting the role in user store"); } DeviceMgtAPIUtils.getGroupManagementProviderService().deleteRoleAndRoleGroupMapping(roleName, roleToDelete, tenantId, userStoreManager, authorizationManager); + log.info( + "Role " + roleName.split("/")[1] + " deleted", + roleMgtContextBuilder + .setActionTag("DELETE_ROLE") + .setUserStoreDomain(userStoreName) + .setRoleName(roleName.split("/")[1]) + .setTenantID(String.valueOf(tenantId)) + .setTenantDomain(tenantDomain) + .setUserName(userName) + .build() + ); return Response.status(Response.Status.OK).build(); } catch (UserStoreException e) { String msg = "Error occurred while deleting the role '" + roleName + "'"; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java index bc3a26b88d..1d80b1288c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImpl.java @@ -17,11 +17,13 @@ */ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.UserMgtLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraUserMgtLoggerImpl; import org.apache.http.HttpStatus; import org.eclipse.wst.common.uriresolver.internal.util.URIEncoder; import org.wso2.carbon.context.CarbonContext; @@ -113,7 +115,8 @@ public class UserManagementServiceImpl implements UserManagementService { private static final String ROLE_EVERYONE = "Internal/everyone"; private static final String API_BASE_PATH = "/users"; - private static final Log log = LogFactory.getLog(UserManagementServiceImpl.class); + UserMgtLogContext.Builder userMgtContextBuilder = new UserMgtLogContext.Builder(); + private static final EntgraLogger log = new EntgraUserMgtLoggerImpl(UserManagementServiceImpl.class); // Permissions that are given for a normal device user. private static final Permission[] PERMISSIONS_FOR_DEVICE_USER = { @@ -158,7 +161,10 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("User '" + userInfo.getUsername() + "' has successfully been added."); } - + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String loggeduserName = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getUsername()); + String stringRoles = new Gson().toJson(userInfo.getRoles()); BasicUserInfo createdUserInfo = this.getBasicUserInfo(userInfo.getUsername()); // Outputting debug message upon successful retrieval of user if (log.isDebugEnabled()) { @@ -173,6 +179,20 @@ public class UserManagementServiceImpl implements UserManagementService { props.setProperty("last-name", userInfo.getLastname()); props.setProperty("username", username); props.setProperty("password", initialUserPassword); + log.info( + "User " + username + " created", + userMgtContextBuilder + .setActionTag("ADD_USER") + .setUserStoreDomain(userInfo.getUsername().split("/")[0]) + .setFirstName(userInfo.getFirstname()) + .setLastName(userInfo.getLastname()) + .setEmail(userInfo.getEmailAddress()) + .setUserRoles(stringRoles) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(loggeduserName) + .build() + ); EmailMetaInfo metaInfo = new EmailMetaInfo(recipient, props); BasicUserInfoWrapper userInfoWrapper = new BasicUserInfoWrapper(); @@ -247,6 +267,9 @@ public class UserManagementServiceImpl implements UserManagementService { username = domain + '/' + username; } try { + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); + String loggeduserName = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getUsername()); UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); if (!userStoreManager.isExistingUser(username)) { if (log.isDebugEnabled()) { @@ -293,6 +316,21 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("User by username: " + username + " was successfully updated."); } + String stringRoles = new Gson().toJson(newRoles); + log.info( + "User " + username + " updated", + userMgtContextBuilder + .setActionTag("UPDATE_USER") + .setUserStoreDomain(username.split("/")[0]) + .setFirstName(userInfo.getFirstname()) + .setLastName(userInfo.getLastname()) + .setEmail(userInfo.getEmailAddress()) + .setUserRoles(stringRoles) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(loggeduserName) + .build() + ); BasicUserInfo updatedUserInfo = this.getBasicUserInfo(username); return Response.ok().entity(updatedUserInfo).build(); @@ -326,6 +364,8 @@ public class UserManagementServiceImpl implements UserManagementService { username = domain + '/' + username; nameWithDomain = true; } + String tenantId = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + String tenantDomain = String.valueOf(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()); try { int deviceCount; UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); @@ -347,6 +387,16 @@ public class UserManagementServiceImpl implements UserManagementService { if (log.isDebugEnabled()) { log.debug("User '" + username + "' was successfully removed."); } + log.info( + "User " + username + " removed", + userMgtContextBuilder + .setActionTag("REMOVE_USER") + .setUserStoreDomain(domain) + .setTenantID(tenantId) + .setTenantDomain(tenantDomain) + .setUserName(username) + .build() + ); return Response.status(Response.Status.OK).build(); } else { String msg = "Before deleting this user, ensure there are no devices assigned to the user. You can either remove the devices or change their owner through an update enrollment operation."; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImplTest.java index 4c069623c7..1f88b3b77c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -57,7 +57,7 @@ import java.util.List; */ @PowerMockIgnore({"javax.ws.rs.*", "org.apache.log4j.*", "javax.xml.parsers"}) @SuppressStaticInitializationFor({"io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils", - "org.wso2.carbon.context.PrivilegedCarbonContext"}) + "org.wso2.carbon.context.CarbonContext", "org.wso2.carbon.context.PrivilegedCarbonContext"}) @PrepareForTest({DeviceMgtAPIUtils.class, CarbonContext.class}) public class GroupManagementServiceImplTest { private GroupManagementService groupManagementService; @@ -191,6 +191,12 @@ public class GroupManagementServiceImplTest { public void testUpdateGroup() throws GroupManagementException, GroupNotExistException, GroupAlreadyExistException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); DeviceGroup deviceGroup = new DeviceGroup(); deviceGroup.setGroupId(1); Mockito.doNothing().when(groupManagementProviderService).updateGroup(deviceGroup, 1); @@ -215,6 +221,12 @@ public class GroupManagementServiceImplTest { public void testDeleteGroup() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.doReturn(true).when(groupManagementProviderService).deleteGroup(1, false); Mockito.doReturn(false).when(groupManagementProviderService).deleteGroup(2, false); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).deleteGroup(3, false); @@ -308,6 +320,12 @@ public class GroupManagementServiceImplTest { .toReturn(policyManagerService); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(deviceManagementProviderService); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); List deviceIdentifiers = new ArrayList<>(); Mockito.doNothing().when(groupManagementProviderService).addDevices(1, deviceIdentifiers); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).addDevices(2, diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java index d032194a1f..3c5e5e3fa1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/UserManagementServiceImplTest.java @@ -115,6 +115,12 @@ public class UserManagementServiceImplTest { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); Mockito.doReturn(true).when(userStoreManager).isExistingUser("admin"); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.doAnswer(new Answer() { private int count = 0; @@ -170,6 +176,12 @@ public class UserManagementServiceImplTest { public void testUpdateUser() throws UserStoreException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Response response = userManagementService.updateUser(TEST2_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Non-existing user was successfully updated"); @@ -273,6 +285,11 @@ public class UserManagementServiceImplTest { .toReturn(this.userStoreManager); PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) .toReturn(this.deviceManagementProviderService); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.doReturn(0).when(deviceManagementProviderService).getDeviceCount(TEST_USERNAME); Mockito.doNothing().when(userStoreManager).deleteUser(Mockito.anyString()); Response response = userManagementService.removeUser(TEST_USERNAME, null); @@ -325,6 +342,12 @@ public class UserManagementServiceImplTest { public void testNegativeScenarios2() throws UserStoreException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getUserStoreManager")) .toReturn(this.userStoreManager); + CarbonContext carbonContext = Mockito.mock(CarbonContext.class, Mockito.RETURNS_MOCKS); + PowerMockito.stub(PowerMockito.method(CarbonContext.class, "getThreadLocalCarbonContext")) + .toReturn(carbonContext); + Mockito.when(carbonContext.getTenantId()).thenReturn(-1234); + Mockito.when(carbonContext.getUsername()).thenReturn("admin"); + Mockito.when(carbonContext.getTenantDomain()).thenReturn("carbon.super"); Mockito.doThrow(new UserStoreException()).when(userStoreManager).isExistingUser(TEST3_USERNAME); Response response = userManagementService.getUser(TEST3_USERNAME, null, null); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/GroupMgtLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/GroupMgtLogContext.java new file mode 100644 index 0000000000..3eeb4f66bc --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/GroupMgtLogContext.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.notification.logger; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; + +public class GroupMgtLogContext extends LogContext { + private final String groupId; + private final String name; + private final String owner; + private final String actionTag; + private final String deviceCount; + private final String devices; + private final String userName; + private final String tenantID; + private final String tenantDomain; + + private GroupMgtLogContext(Builder builder) { + this.groupId = builder.groupId; + this.name = builder.name; + this.owner = builder.owner; + this.actionTag = builder.actionTag; + this.deviceCount = builder.deviceCount; + this.devices = builder.devices; + this.userName = builder.userName; + this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + } + + public String getGroupId() { + return groupId; + } + + public String getName() { + return name; + } + + public String getOwner() { + return owner; + } + + public String getActionTag() { + return actionTag; + } + + public String getDeviceCount() { + return deviceCount; + } + + public String getDevices() { + return devices; + } + + public String getUserName() { + return userName; + } + + public String getTenantID() { + return tenantID; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public static class Builder { + private String groupId; + private String name; + private String owner; + private String actionTag; + private String deviceCount; + private String devices; + private String userName; + private String tenantID; + private String tenantDomain; + + public Builder() { + } + + public String getGroupId() { + return groupId; + } + + public Builder setGroupId(String groupId) { + this.groupId = groupId; + return this; + } + + public String getName() { + return name; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public String getOwner() { + return owner; + } + + public Builder setOwner(String owner) { + this.owner = owner; + return this; + } + + public String getActionTag() { + return actionTag; + } + + public Builder setActionTag(String actionTag) { + this.actionTag = actionTag; + return this; + } + + public String getDeviceCount() { + return deviceCount; + } + + public Builder setDeviceCount(String deviceCount) { + this.deviceCount = deviceCount; + return this; + } + + public String getDevices() { + return devices; + } + + public Builder setDevices(String devices) { + this.devices = devices; + return this; + } + + public String getUserName() { + return userName; + } + + public Builder setUserName(String userName) { + this.userName = userName; + return this; + } + + public String getTenantID() { + return tenantID; + } + + public Builder setTenantID(String tenantID) { + this.tenantID = tenantID; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public GroupMgtLogContext build() { + return new GroupMgtLogContext(this); + } + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/RoleMgtLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/RoleMgtLogContext.java new file mode 100644 index 0000000000..2fbeac4583 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/RoleMgtLogContext.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.notification.logger; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; + +public class RoleMgtLogContext extends LogContext { + private final String userStoreDomain; + private final String roleName; + private final String users; + private final String actionTag; + private final String userName; + private final String tenantID; + private final String tenantDomain; + + private RoleMgtLogContext(Builder builder) { + this.userStoreDomain = builder.userStoreDomain; + this.roleName = builder.roleName; + this.users = builder.users; + this.actionTag = builder.actionTag; + this.userName = builder.userName; + this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + } + + public String getRoleName() { + return roleName; + } + + public String getUserStoreDomain() { + return userStoreDomain; + } + + public String getUsers() { + return users; + } + + public String getActionTag() { + return actionTag; + } + + public String getUserName() { + return userName; + } + + public String getTenantID() { + return tenantID; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public static class Builder { + private String userStoreDomain; + private String roleName; + private String users; + private String actionTag; + private String userName; + private String tenantID; + private String tenantDomain; + + public Builder() { + } + + public String getUserStoreDomain() { + return userStoreDomain; + } + + public Builder setUserStoreDomain(String userStoreDomain) { + this.userStoreDomain = userStoreDomain; + return this; + } + + public String getRoleName() { + return roleName; + } + + public Builder setRoleName(String roleName) { + this.roleName = roleName; + return this; + } + + public String getUsers() { + return users; + } + + public Builder setUsers(String users) { + this.users = users; + 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; + } + + public Builder setTenantID(String tenantID) { + this.tenantID = tenantID; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public RoleMgtLogContext build() { + return new RoleMgtLogContext(this); + } + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLoginLogContext.java similarity index 84% rename from components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLogContext.java rename to components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLoginLogContext.java index 25ed4fbc05..30ad6c4c6c 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLogContext.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserLoginLogContext.java @@ -20,22 +20,22 @@ package io.entgra.device.mgt.core.notification.logger; import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; -public class UserLogContext extends LogContext { +public class UserLoginLogContext extends LogContext { private final String userName; private final String userEmail; private final String metaInfo; private final String tenantID; private final boolean isUserRegistered; - private final boolean isDeviceRegisterged; + private final boolean isDeviceRegistered; private final String tenantDomain; - private UserLogContext(Builder builder) { + private UserLoginLogContext(Builder builder) { this.userEmail = builder.userEmail; this.userName = builder.userName; this.metaInfo = builder.metaInfo; this.tenantID = builder.tenantID; this.isUserRegistered = builder.isUserRegistered; - this.isDeviceRegisterged = builder.isDeviceRegisterged; + this.isDeviceRegistered = builder.isDeviceRegistered; this.tenantDomain = builder.tenantDomain; } @@ -59,8 +59,8 @@ public class UserLogContext extends LogContext { return isUserRegistered; } - public boolean isDeviceRegisterged() { - return isDeviceRegisterged; + public boolean isDeviceRegistered() { + return isDeviceRegistered; } public String getTenantDomain() { @@ -73,7 +73,7 @@ public class UserLogContext extends LogContext { private String metaInfo; private String tenantID; private boolean isUserRegistered; - private boolean isDeviceRegisterged; + private boolean isDeviceRegistered; private String tenantDomain; public Builder() { @@ -124,12 +124,12 @@ public class UserLogContext extends LogContext { return this; } - public boolean getIsDeviceRegisterged() { - return isDeviceRegisterged; + public boolean getIsDeviceRegistered() { + return isDeviceRegistered; } - public Builder setDeviceRegisterged(boolean deviceRegisterged) { - isDeviceRegisterged = deviceRegisterged; + public Builder setDeviceRegistered(boolean deviceRegistered) { + isDeviceRegistered = deviceRegistered; return this; } @@ -142,8 +142,8 @@ public class UserLogContext extends LogContext { return this; } - public UserLogContext build() { - return new UserLogContext(this); + public UserLoginLogContext build() { + return new UserLoginLogContext(this); } } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserMgtLogContext.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserMgtLogContext.java new file mode 100644 index 0000000000..ffd2408abb --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/UserMgtLogContext.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.notification.logger; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; + +public class UserMgtLogContext extends LogContext { + private final String userStoreDomain; + private final String firstName; + private final String lastName; + private final String email; + private final String userRoles; + private final String actionTag; + private final String userName; + private final String tenantID; + private final String tenantDomain; + + private UserMgtLogContext(Builder builder) { + this.userStoreDomain = builder.userStoreDomain; + this.firstName = builder.firstName; + this.lastName = builder.lastName; + this.email = builder.email; + this.userRoles = builder.userRoles; + this.actionTag = builder.actionTag; + this.userName = builder.userName; + this.tenantID = builder.tenantID; + this.tenantDomain = builder.tenantDomain; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getUserStoreDomain() { + return userStoreDomain; + } + + public String getEmail() { + return email; + } + + public String getUserRoles() { + return userRoles; + } + + public String getActionTag() { + return actionTag; + } + + public String getUserName() { + return userName; + } + + public String getTenantID() { + return tenantID; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public static class Builder { + private String userStoreDomain; + private String firstName; + private String lastName; + private String email; + private String userRoles; + private String actionTag; + private String userName; + private String tenantID; + private String tenantDomain; + + public Builder() { + } + + public String getUserStoreDomain() { + return userStoreDomain; + } + + public Builder setUserStoreDomain(String userStoreDomain) { + this.userStoreDomain = userStoreDomain; + return this; + } + + public String getFirstName() { + return firstName; + } + + public Builder setFirstName(String firstName) { + this.firstName = firstName; + return this; + } + + public String getLastName() { + return lastName; + } + + public Builder setLastName(String lastName) { + this.lastName = lastName; + return this; + } + + public String getEmail() { + return email; + } + + public Builder setEmail(String email) { + this.email = email; + return this; + } + + public String getUserRoles() { + return userRoles; + } + + public Builder setUserRoles(String userRoles) { + this.userRoles = userRoles; + 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; + } + + public Builder setTenantID(String tenantID) { + this.tenantID = tenantID; + return this; + } + + public String getTenantDomain() { + return tenantDomain; + } + + public Builder setTenantDomain(String tenantDomain) { + this.tenantDomain = tenantDomain; + return this; + } + + public UserMgtLogContext build() { + return new UserMgtLogContext(this); + } + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraGroupMgtLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraGroupMgtLoggerImpl.java new file mode 100644 index 0000000000..e845acc2d2 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraGroupMgtLoggerImpl.java @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.notification.logger.impl; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.GroupMgtLogContext; +import io.entgra.device.mgt.core.notification.logger.util.MDCContextUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.MDC; + +public class EntgraGroupMgtLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraGroupMgtLoggerImpl(Class clazz) { + log = LogFactory.getLog(clazz); + } + + public void info(String message) { + log.info(message); + } + + public void info(String message, Throwable t) { + log.info(message, t); + } + + @Override + public void info(Object o) { + log.info(o); + } + + @Override + public void info(Object o, Throwable throwable) { + log.info(o, throwable); + } + + @Override + public void info(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.info(object, t); + } + + public void debug(String message) { + log.debug(message); + } + + public void debug(String message, Throwable t) { + log.debug(message, t); + } + + @Override + public void debug(Object o) { + log.debug(o); + } + + @Override + public void debug(Object o, Throwable throwable) { + log.debug(o, throwable); + } + + @Override + public void debug(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.debug(object, t); + } + + public void error(String message) { + log.error(message); + } + + public void error(String message, Throwable t) { + log.error(message, t); + } + + @Override + public void error(Object o) { + log.error(o); + } + + @Override + public void error(Object o, Throwable throwable) { + log.error(o, throwable); + } + + @Override + public void error(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.error(object, t); + } + + public void warn(String message) { + log.warn(message); + } + + public void warn(String message, Throwable t) { + log.warn(message, t); + } + + @Override + public void warn(Object o) { + log.warn(o); + } + + @Override + public void warn(Object o, Throwable throwable) { + log.warn(o, throwable); + } + + @Override + public void warn(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.warn(object, t); + } + + public void trace(String message) { + log.trace(message); + } + + public void trace(String message, Throwable t) { + log.trace(message, t); + } + + @Override + public void trace(Object o) { + log.trace(o); + } + + @Override + public void trace(Object o, Throwable throwable) { + log.trace(o, throwable); + } + + @Override + public void trace(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.trace(object, t); + } + + public void fatal(String message) { + log.fatal(message); + } + + public void fatal(String message, Throwable t) { + log.fatal(message, t); + } + + @Override + public void fatal(Object o) { + log.fatal(0); + } + + @Override + public void fatal(Object o, Throwable throwable) { + log.fatal(0, throwable); + } + + @Override + public void fatal(String message, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + GroupMgtLogContext groupMgtLogContext = (GroupMgtLogContext) logContext; + MDCContextUtil.populateGroupMgtMDCContext(groupMgtLogContext); + log.fatal(object, t); + } + + @Override + public boolean isDebugEnabled() { + return log.isDebugEnabled(); + } + + @Override + public boolean isErrorEnabled() { + return log.isErrorEnabled(); + } + + @Override + public boolean isFatalEnabled() { + return log.isFatalEnabled(); + } + + @Override + public boolean isInfoEnabled() { + return log.isInfoEnabled(); + } + + @Override + public boolean isTraceEnabled() { + return log.isTraceEnabled(); + } + + @Override + public boolean isWarnEnabled() { + return log.isWarnEnabled(); + } + + @Override + public void clearLogContext() { + MDC.clear(); + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraRoleMgtLoggerImpl.java similarity index 66% rename from components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java rename to components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraRoleMgtLoggerImpl.java index fa482605f6..3e3a831466 100644 --- a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoggerImpl.java +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraRoleMgtLoggerImpl.java @@ -19,17 +19,17 @@ package io.entgra.device.mgt.core.notification.logger.impl; import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; -import io.entgra.device.mgt.core.notification.logger.UserLogContext; +import io.entgra.device.mgt.core.notification.logger.RoleMgtLogContext; import io.entgra.device.mgt.core.notification.logger.util.MDCContextUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.MDC; -public class EntgraUserLoggerImpl implements EntgraLogger { +public class EntgraRoleMgtLoggerImpl implements EntgraLogger { private static Log log = null; - public EntgraUserLoggerImpl(Class clazz) { + public EntgraRoleMgtLoggerImpl(Class clazz) { log = LogFactory.getLog(clazz); } @@ -53,26 +53,25 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void info(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.info(message); } @Override public void info(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.info(object); } @Override public void info(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.info(object, t); } - public void debug(String message) { log.debug(message); } @@ -93,26 +92,25 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void debug(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.debug(message); } @Override public void debug(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.debug(object); } @Override public void debug(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.debug(object, t); } - public void error(String message) { log.error(message); } @@ -133,33 +131,32 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void error(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.error(message); } @Override public void error(String message, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.error(message, t); } @Override public void error(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.error(object); } @Override public void error(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.error(object, t); } - public void warn(String message) { log.warn(message); } @@ -180,29 +177,29 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void warn(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.warn(message); } @Override public void warn(String message, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.warn(message, t); } @Override public void warn(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.warn(object); } @Override public void warn(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.warn(object, t); } @@ -226,27 +223,25 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void trace(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.trace(message); } @Override public void trace(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.trace(object); } @Override public void trace(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.trace(object, t); - } - public void fatal(String message) { log.fatal(message); } @@ -267,22 +262,22 @@ public class EntgraUserLoggerImpl implements EntgraLogger { @Override public void fatal(String message, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.fatal(message); } @Override public void fatal(Object object, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.fatal(object); } @Override public void fatal(Object object, Throwable t, LogContext logContext) { - UserLogContext userLogContext = (UserLogContext) logContext; - MDCContextUtil.populateUserMDCContext(userLogContext); + RoleMgtLogContext roleMgtLogContext = (RoleMgtLogContext) logContext; + MDCContextUtil.populateRoleMgtMDCContext(roleMgtLogContext); log.fatal(object, t); } diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoginLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoginLoggerImpl.java new file mode 100644 index 0000000000..087fc45eec --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserLoginLoggerImpl.java @@ -0,0 +1,323 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.notification.logger.impl; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.UserLoginLogContext; +import io.entgra.device.mgt.core.notification.logger.util.MDCContextUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.MDC; + +public class EntgraUserLoginLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraUserLoginLoggerImpl(Class clazz) { + log = LogFactory.getLog(clazz); + } + + public void info(String message) { + log.info(message); + } + + public void info(String message, Throwable t) { + log.info(message, t); + } + + @Override + public void info(Object o) { + log.info(o); + } + + @Override + public void info(Object o, Throwable throwable) { + log.info(o, throwable); + } + + @Override + public void info(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.info(object, t); + } + + + public void debug(String message) { + log.debug(message); + } + + public void debug(String message, Throwable t) { + log.debug(message, t); + } + + @Override + public void debug(Object o) { + log.debug(o); + } + + @Override + public void debug(Object o, Throwable throwable) { + log.debug(o, throwable); + } + + @Override + public void debug(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.debug(object, t); + } + + + public void error(String message) { + log.error(message); + } + + public void error(String message, Throwable t) { + log.error(message, t); + } + + @Override + public void error(Object o) { + log.error(o); + } + + @Override + public void error(Object o, Throwable throwable) { + log.error(o, throwable); + } + + @Override + public void error(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.error(object, t); + } + + + public void warn(String message) { + log.warn(message); + } + + public void warn(String message, Throwable t) { + log.warn(message, t); + } + + @Override + public void warn(Object o) { + log.warn(o); + } + + @Override + public void warn(Object o, Throwable throwable) { + log.warn(o, throwable); + } + + @Override + public void warn(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.warn(object, t); + } + + public void trace(String message) { + log.trace(message); + } + + public void trace(String message, Throwable t) { + log.trace(message, t); + } + + @Override + public void trace(Object o) { + log.trace(o); + } + + @Override + public void trace(Object o, Throwable throwable) { + log.trace(o, throwable); + } + + @Override + public void trace(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.trace(object, t); + + } + + + public void fatal(String message) { + log.fatal(message); + } + + public void fatal(String message, Throwable t) { + log.fatal(message, t); + } + + @Override + public void fatal(Object o) { + log.fatal(0); + } + + @Override + public void fatal(Object o, Throwable throwable) { + log.fatal(0, throwable); + } + + @Override + public void fatal(String message, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + UserLoginLogContext userLoginLogContext = (UserLoginLogContext) logContext; + MDCContextUtil.populateUserMDCContext(userLoginLogContext); + log.fatal(object, t); + } + + @Override + public boolean isDebugEnabled() { + return log.isDebugEnabled(); + } + + @Override + public boolean isErrorEnabled() { + return log.isErrorEnabled(); + } + + @Override + public boolean isFatalEnabled() { + return log.isFatalEnabled(); + } + + @Override + public boolean isInfoEnabled() { + return log.isInfoEnabled(); + } + + @Override + public boolean isTraceEnabled() { + return log.isTraceEnabled(); + } + + @Override + public boolean isWarnEnabled() { + return log.isWarnEnabled(); + } + + @Override + public void clearLogContext() { + MDC.clear(); + } +} diff --git a/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserMgtLoggerImpl.java b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserMgtLoggerImpl.java new file mode 100644 index 0000000000..fe9fd0ee01 --- /dev/null +++ b/components/logger/io.entgra.device.mgt.core.notification.logger/src/main/java/io/entgra/device/mgt/core/notification/logger/impl/EntgraUserMgtLoggerImpl.java @@ -0,0 +1,318 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.device.mgt.core.notification.logger.impl; + +import io.entgra.device.mgt.core.device.mgt.extensions.logger.LogContext; +import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; +import io.entgra.device.mgt.core.notification.logger.UserMgtLogContext; +import io.entgra.device.mgt.core.notification.logger.util.MDCContextUtil; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.log4j.MDC; + +public class EntgraUserMgtLoggerImpl implements EntgraLogger { + + private static Log log = null; + + public EntgraUserMgtLoggerImpl(Class clazz) { + log = LogFactory.getLog(clazz); + } + + public void info(String message) { + log.info(message); + } + + public void info(String message, Throwable t) { + log.info(message, t); + } + + @Override + public void info(Object o) { + log.info(o); + } + + @Override + public void info(Object o, Throwable throwable) { + log.info(o, throwable); + } + + @Override + public void info(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.info(message); + } + + @Override + public void info(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.info(object); + } + + @Override + public void info(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.info(object, t); + } + + public void debug(String message) { + log.debug(message); + } + + public void debug(String message, Throwable t) { + log.debug(message, t); + } + + @Override + public void debug(Object o) { + log.debug(o); + } + + @Override + public void debug(Object o, Throwable throwable) { + log.debug(o, throwable); + } + + @Override + public void debug(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.debug(message); + } + + @Override + public void debug(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.debug(object); + } + + @Override + public void debug(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.debug(object, t); + } + + public void error(String message) { + log.error(message); + } + + public void error(String message, Throwable t) { + log.error(message, t); + } + + @Override + public void error(Object o) { + log.error(o); + } + + @Override + public void error(Object o, Throwable throwable) { + log.error(o, throwable); + } + + @Override + public void error(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.error(message); + } + + @Override + public void error(String message, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.error(message, t); + } + + @Override + public void error(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.error(object); + } + + @Override + public void error(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.error(object, t); + } + + public void warn(String message) { + log.warn(message); + } + + public void warn(String message, Throwable t) { + log.warn(message, t); + } + + @Override + public void warn(Object o) { + log.warn(o); + } + + @Override + public void warn(Object o, Throwable throwable) { + log.warn(o, throwable); + } + + @Override + public void warn(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.warn(message); + } + + @Override + public void warn(String message, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.warn(message, t); + } + + @Override + public void warn(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.warn(object); + } + + @Override + public void warn(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.warn(object, t); + } + + public void trace(String message) { + log.trace(message); + } + + public void trace(String message, Throwable t) { + log.trace(message, t); + } + + @Override + public void trace(Object o) { + log.trace(o); + } + + @Override + public void trace(Object o, Throwable throwable) { + log.trace(o, throwable); + } + + @Override + public void trace(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.trace(message); + } + + @Override + public void trace(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.trace(object); + } + + @Override + public void trace(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.trace(object, t); + } + + public void fatal(String message) { + log.fatal(message); + } + + public void fatal(String message, Throwable t) { + log.fatal(message, t); + } + + @Override + public void fatal(Object o) { + log.fatal(0); + } + + @Override + public void fatal(Object o, Throwable throwable) { + log.fatal(0, throwable); + } + + @Override + public void fatal(String message, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.fatal(message); + } + + @Override + public void fatal(Object object, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.fatal(object); + } + + @Override + public void fatal(Object object, Throwable t, LogContext logContext) { + UserMgtLogContext userMgtLogContext = (UserMgtLogContext) logContext; + MDCContextUtil.populateUserMgtMDCContext(userMgtLogContext); + log.fatal(object, t); + } + + @Override + public boolean isDebugEnabled() { + return log.isDebugEnabled(); + } + + @Override + public boolean isErrorEnabled() { + return log.isErrorEnabled(); + } + + @Override + public boolean isFatalEnabled() { + return log.isFatalEnabled(); + } + + @Override + public boolean isInfoEnabled() { + return log.isInfoEnabled(); + } + + @Override + public boolean isTraceEnabled() { + return log.isTraceEnabled(); + } + + @Override + public boolean isWarnEnabled() { + return log.isWarnEnabled(); + } + + @Override + public void clearLogContext() { + MDC.clear(); + } +} 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 dc046e8eeb..1c9afa6948 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 @@ -37,7 +37,7 @@ public final class MDCContextUtil { } } - public static void populateUserMDCContext(final UserLogContext mdcContext) { + public static void populateUserMDCContext(final UserLoginLogContext mdcContext) { if (mdcContext.getUserName() != null) { MDC.put("UserName", mdcContext.getUserName()); } @@ -53,8 +53,8 @@ public final class MDCContextUtil { if (mdcContext.isUserRegistered()) { MDC.put("IsUserRegistered", "Registered"); } - if (mdcContext.isDeviceRegisterged()) { - MDC.put("IsDeviceRegistered", mdcContext.isDeviceRegisterged()); + if (mdcContext.isDeviceRegistered()) { + MDC.put("IsDeviceRegistered", mdcContext.isDeviceRegistered()); } if (mdcContext.getTenantDomain() != null) { MDC.put("TenantDomain", mdcContext.getTenantDomain()); @@ -160,6 +160,88 @@ public final class MDCContextUtil { MDC.put("UserName", mdcContext.getUserName()); } } -} + public static void populateRoleMgtMDCContext(final RoleMgtLogContext mdcContext) { + if (mdcContext.getUserStoreDomain() != null) { + MDC.put("UserStoreDomain", mdcContext.getUserStoreDomain()); + } + if (mdcContext.getRoleName() != null) { + MDC.put("RoleName", mdcContext.getRoleName()); + } + if (mdcContext.getUsers() != null) { + MDC.put("Users", mdcContext.getUsers()); + } + 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()); + } + if (mdcContext.getTenantID() != null) { + MDC.put("TenantId", mdcContext.getTenantID()); + } + } + public static void populateUserMgtMDCContext(final UserMgtLogContext mdcContext) { + if (mdcContext.getUserStoreDomain() != null) { + MDC.put("UserStoreDomain", mdcContext.getUserStoreDomain()); + } + if (mdcContext.getFirstName() != null) { + MDC.put("FirstName", mdcContext.getFirstName()); + } + if (mdcContext.getLastName() != null) { + MDC.put("LastName", mdcContext.getLastName()); + } + if (mdcContext.getEmail() != null) { + MDC.put("Email", mdcContext.getEmail()); + } + if (mdcContext.getUserRoles() != null) { + MDC.put("UserRoles", mdcContext.getUserRoles()); + } + 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()); + } + if (mdcContext.getTenantID() != null) { + MDC.put("TenantId", mdcContext.getTenantID()); + } + } + + public static void populateGroupMgtMDCContext(final GroupMgtLogContext mdcContext) { + if (mdcContext.getGroupId() != null) { + MDC.put("GroupId", mdcContext.getGroupId()); + } + if (mdcContext.getName() != null) { + MDC.put("Name", mdcContext.getName()); + } + if (mdcContext.getOwner() != null) { + MDC.put("Owner", mdcContext.getOwner()); + } + if (mdcContext.getActionTag() != null) { + MDC.put("ActionTag", mdcContext.getActionTag()); + } + if (mdcContext.getDeviceCount() != null) { + MDC.put("DeviceCount", mdcContext.getDeviceCount()); + } + if (mdcContext.getDevices() != null) { + MDC.put("Devices", mdcContext.getDevices()); + } + if (mdcContext.getUserName() != null) { + MDC.put("UserName", mdcContext.getUserName()); + } + if (mdcContext.getTenantDomain() != null) { + MDC.put("TenantDomain", mdcContext.getTenantDomain()); + } + if (mdcContext.getTenantID() != null) { + MDC.put("TenantId", mdcContext.getTenantID()); + } + } +} diff --git a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java index 5c60a16558..011215bc2d 100644 --- a/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.device.mgt.core.ui.request.interceptor/src/main/java/io/entgra/device/mgt/core/ui/request/interceptor/UserHandler.java @@ -27,8 +27,8 @@ import io.entgra.device.mgt.core.ui.request.interceptor.beans.ProxyResponse; import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerConstants; import io.entgra.device.mgt.core.ui.request.interceptor.util.HandlerUtil; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; -import io.entgra.device.mgt.core.notification.logger.UserLogContext; -import io.entgra.device.mgt.core.notification.logger.impl.EntgraUserLoggerImpl; +import io.entgra.device.mgt.core.notification.logger.UserLoginLogContext; +import io.entgra.device.mgt.core.notification.logger.impl.EntgraUserLoginLoggerImpl; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpPost; @@ -49,8 +49,8 @@ import java.util.Base64; @MultipartConfig @WebServlet("/user") public class UserHandler extends HttpServlet { - private static final EntgraLogger log = new EntgraUserLoggerImpl(UserHandler.class); - UserLogContext.Builder userLogContextBuilder = new UserLogContext.Builder(); + private static final EntgraLogger log = new EntgraUserLoginLoggerImpl(UserHandler.class); + UserLoginLogContext.Builder userLoginLogContextBuilder = new UserLoginLogContext.Builder(); private static final long serialVersionUID = 9050048549140517002L; @Override @@ -121,7 +121,13 @@ public class UserHandler extends HttpServlet { jTokenResultAsJsonObject.get("username").getAsString().replaceAll("@carbon.super", "")); HandlerUtil.handleSuccess(resp, proxyResponse); httpSession.setAttribute(HandlerConstants.USERNAME_WITH_DOMAIN, jTokenResultAsJsonObject.get("username").getAsString()); - log.info("Customer login", userLogContextBuilder.setUserName(proxyResponse.getData()).setUserRegistered(true).build()); + log.info( + "User " + proxyResponse.getData() + " logged in", + userLoginLogContextBuilder + .setUserName(proxyResponse.getData()) + .setUserRegistered(true) + .build() + ); } } catch (IOException e) { log.error("Error occurred while sending the response into the socket. ", e);