From e27209a315e76e9c94d7453b22f96aae4867b6c7 Mon Sep 17 00:00:00 2001 From: Daniel Hillwig Date: Fri, 4 Dec 2020 10:32:05 +0100 Subject: [PATCH 1/6] fix: added extended logging for registration --- .../api/facade/CreateUserFacade.java | 30 +++++++++--- .../helper/KeycloakAdminClientHelper.java | 49 ++++++++++++++----- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java index 7221a6a1f..32d48eba4 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java @@ -1,5 +1,7 @@ package de.caritas.cob.userservice.api.facade; +import static java.util.Objects.nonNull; + import de.caritas.cob.userservice.api.container.RocketChatCredentials; import de.caritas.cob.userservice.api.exception.SaveUserException; import de.caritas.cob.userservice.api.exception.httpresponses.BadRequestException; @@ -26,6 +28,7 @@ import de.caritas.cob.userservice.api.service.helper.KeycloakAdminClientHelper; import lombok.NonNull; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -36,6 +39,7 @@ * new session). * */ +@Slf4j @Service @RequiredArgsConstructor public class CreateUserFacade { @@ -56,13 +60,27 @@ public class CreateUserFacade { /** * Creates a user in Keycloak and MariaDB. Then creates a session or chat account depending on the * provided {@link ConsultingType}. - * + * * @param user {@link UserDTO} * @return {@link KeycloakCreateUserResponseDTO} - * + * */ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDTO user) { + log.warn("=== UserDTO ==="); + log.warn("addictiveDrugs: " + user.getAddictiveDrugs()); + log.warn("age: " + user.getAge()); + log.warn("consultingType: " + user.getConsultingType()); + log.warn("email:" + ((nonNull(user.getEmail())) ? "not null" : "null")); + log.warn("gender: " + user.getGender()); + log.warn("postcode: " + user.getPostcode()); + log.warn("relation: " + user.getRelation()); + log.warn("state: " + user.getState()); + log.warn("termsAccepted: " + user.getTermsAccepted()); + log.warn("username: " + user.getUsername()); + log.warn("agencyId: " + user.getAgencyId()); + log.warn("==============="); + KeycloakCreateUserResponseDTO response; String userId; @@ -103,7 +121,7 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT /** * Update the Keycloak account data (roles, password, e-mail address), create the user in MariaDB * and initialize a session or chat relation (depending on {@link ConsultingType}). - * + * * @param userId Keycloak user ID * @param user {@link UserDTO} from registration form * @param consultingType {@link ConsultingType} @@ -146,7 +164,7 @@ private void updateAccountData(String userId, UserDTO user, ConsultingType consu /** * Initializes the provided {@link User} account depending on the consulting type. Consulting type * KREUZBUND will get a chat/agency relation, all others will be provided with a session. - * + * * @param user {@link UserDTO} * @param dbUser {@link User} * @param consultingTypeSettings {@link ConsultingTypeSettings} @@ -166,7 +184,7 @@ private void initializeUserAccount(UserDTO user, User dbUser, /** * Creates a new session for the provided {@link User}. - * + * * @param user {@link UserDTO} * @param dbUser {@link User} * @param consultingTypeSettings {@link ConsultingTypeSettings} @@ -251,7 +269,7 @@ private void createUserChatAgencyRelation(UserDTO user, User dbUser) { /** * Deletes the provided user in Keycloak and MariaDB and its related session or user <-> * chat/agency relations. - * + * * @param userId Keycloak user ID * @param session {@link Session} * @param dbUser {@link User} diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index 83bb0e10b..ec67bdbd1 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -1,11 +1,30 @@ package de.caritas.cob.userservice.api.service.helper; +import static java.util.Objects.nonNull; + +import de.caritas.cob.userservice.api.authorization.Authority; +import de.caritas.cob.userservice.api.authorization.UserRole; +import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException; +import de.caritas.cob.userservice.api.helper.UserHelper; +import de.caritas.cob.userservice.api.model.CreateUserResponseDTO; +import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO; +import de.caritas.cob.userservice.api.model.registration.UserDTO; +import de.caritas.cob.userservice.api.service.LogService; +import de.caritas.cob.userservice.api.service.helper.aspect.KeycloakAdminClientLogout; import java.net.URI; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.Set; +import javax.ws.rs.core.Link; import javax.ws.rs.core.Response; import lombok.Synchronized; +import lombok.extern.slf4j.Slf4j; import org.keycloak.admin.client.Keycloak; import org.keycloak.admin.client.resource.RealmResource; import org.keycloak.admin.client.resource.UserResource; @@ -14,20 +33,11 @@ import org.keycloak.representations.idm.ErrorRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.UserRepresentation; +import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; -import de.caritas.cob.userservice.api.authorization.Authority; -import de.caritas.cob.userservice.api.authorization.UserRole; -import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException; -import de.caritas.cob.userservice.api.helper.UserHelper; -import de.caritas.cob.userservice.api.model.CreateUserResponseDTO; -import de.caritas.cob.userservice.api.model.registration.UserDTO; -import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO; -import de.caritas.cob.userservice.api.service.LogService; -import de.caritas.cob.userservice.api.service.helper.aspect.KeycloakAdminClientLogout; -import lombok.extern.slf4j.Slf4j; /** * Helper class for the KeycloakService. Communicates to the Keycloak Admin API over the Keycloak @@ -37,6 +47,7 @@ @Slf4j @Service public class KeycloakAdminClientHelper { + @Value("${keycloak.auth-server-url}") private String KEYCLOAK_SERVER_URL; @@ -237,8 +248,20 @@ public void updateRole(final String userId, final String roleName) { boolean isRoleUpdated = false; // Assign role + RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation(); + log.warn("=== roleRepresentation ==="); + log.warn("containerId: " + roleRepresentation.getContainerId()); + log.warn("description: " + roleRepresentation.getDescription()); + log.warn("id: " + roleRepresentation.getId()); + log.warn("name: " + roleRepresentation.getName()); + log.warn("attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation.getAttributes().toString() : "null")); + log.warn("clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation.getClientRole().toString() : "null")); + log.warn("composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation.getComposites().toString() : "null")); + log.warn("isComposite: " + roleRepresentation.isComposite()); + log.warn("=========================="); + user.roles().realmLevel() - .add(Arrays.asList(realmResource.roles().get(roleName).toRepresentation())); + .add(Arrays.asList(roleRepresentation)); // Check if role has been assigned successfully List userRoles = user.roles().realmLevel().listAll(); @@ -308,7 +331,7 @@ public void rollBackUser(String userId) { /** * Returns true if the given user has the provided authority. * - * @param userId Keycloak user ID + * @param userId Keycloak user ID * @param authority Keycloak authority * @return true if user hast provided authority */ @@ -385,4 +408,4 @@ public void closeInstance() { // this.keycloakInstance = null; // } } -} \ No newline at end of file +} From a9a7258f9bcad3674835f2f403fbb8515fdd9834 Mon Sep 17 00:00:00 2001 From: Daniel Hillwig Date: Fri, 4 Dec 2020 10:40:24 +0100 Subject: [PATCH 2/6] fix: codestyle violation --- .../api/service/helper/KeycloakAdminClientHelper.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index ec67bdbd1..f822ada62 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -244,8 +244,6 @@ public void updateRole(final String userId, final String roleName) { // Get realm and user resources RealmResource realmResource = getInstance().realm(KEYCLOAK_REALM); UsersResource userRessource = realmResource.users(); - UserResource user = userRessource.get(userId); - boolean isRoleUpdated = false; // Assign role RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation(); @@ -260,6 +258,9 @@ public void updateRole(final String userId, final String roleName) { log.warn("isComposite: " + roleRepresentation.isComposite()); log.warn("=========================="); + UserResource user = userRessource.get(userId); + boolean isRoleUpdated = false; + user.roles().realmLevel() .add(Arrays.asList(roleRepresentation)); From 2e44af1faf7ecbe19f80c8a1acf66d33b309445f Mon Sep 17 00:00:00 2001 From: Daniel Hillwig Date: Fri, 4 Dec 2020 13:52:46 +0100 Subject: [PATCH 3/6] fix: added userid to extended logging --- .../api/facade/CreateUserFacade.java | 57 +++++++++++-------- .../helper/KeycloakAdminClientHelper.java | 25 ++++---- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java index 32d48eba4..dd75a0a1c 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java @@ -12,8 +12,8 @@ import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeManager; import de.caritas.cob.userservice.api.manager.consultingType.ConsultingTypeSettings; import de.caritas.cob.userservice.api.model.CreateUserResponseDTO; -import de.caritas.cob.userservice.api.model.registration.UserDTO; import de.caritas.cob.userservice.api.model.keycloak.KeycloakCreateUserResponseDTO; +import de.caritas.cob.userservice.api.model.registration.UserDTO; import de.caritas.cob.userservice.api.model.rocketchat.login.LoginResponseDTO; import de.caritas.cob.userservice.api.repository.session.ConsultingType; import de.caritas.cob.userservice.api.repository.session.Session; @@ -37,7 +37,6 @@ /** * Facade to encapsulate the steps to initialize an user account (create chat/agency relation or a * new session). - * */ @Slf4j @Service @@ -63,23 +62,25 @@ public class CreateUserFacade { * * @param user {@link UserDTO} * @return {@link KeycloakCreateUserResponseDTO} - * */ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDTO user) { - log.warn("=== UserDTO ==="); - log.warn("addictiveDrugs: " + user.getAddictiveDrugs()); - log.warn("age: " + user.getAge()); - log.warn("consultingType: " + user.getConsultingType()); - log.warn("email:" + ((nonNull(user.getEmail())) ? "not null" : "null")); - log.warn("gender: " + user.getGender()); - log.warn("postcode: " + user.getPostcode()); - log.warn("relation: " + user.getRelation()); - log.warn("state: " + user.getState()); - log.warn("termsAccepted: " + user.getTermsAccepted()); - log.warn("username: " + user.getUsername()); - log.warn("agencyId: " + user.getAgencyId()); - log.warn("==============="); + String lineSeparator = System.getProperty("line.separator"); + StringBuilder logStringBuilder = new StringBuilder(); + logStringBuilder.append("=== UserDTO ===" + lineSeparator); + logStringBuilder.append("addictiveDrugs: " + user.getAddictiveDrugs() + lineSeparator); + logStringBuilder.append("age: " + user.getAge() + lineSeparator); + logStringBuilder.append("consultingType: " + user.getConsultingType() + lineSeparator); + logStringBuilder + .append("email:" + ((nonNull(user.getEmail())) ? "not null" : "null") + lineSeparator); + logStringBuilder.append("gender: " + user.getGender() + lineSeparator); + logStringBuilder.append("postcode: " + user.getPostcode() + lineSeparator); + logStringBuilder.append("relation: " + user.getRelation() + lineSeparator); + logStringBuilder.append("state: " + user.getState() + lineSeparator); + logStringBuilder.append("termsAccepted: " + user.getTermsAccepted() + lineSeparator); + logStringBuilder.append("username: " + user.getUsername() + lineSeparator); + logStringBuilder.append("agencyId: " + user.getAgencyId() + lineSeparator); + KeycloakCreateUserResponseDTO response; String userId; @@ -112,6 +113,11 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT return response; } + logStringBuilder.append("userId " + userId + lineSeparator); + logStringBuilder.append("===============" + lineSeparator); + + log.warn(logStringBuilder.toString()); + // Update Keycloak account data and create user and session in MariaDB updateAccountData(userId, user, consultingType); @@ -122,8 +128,8 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT * Update the Keycloak account data (roles, password, e-mail address), create the user in MariaDB * and initialize a session or chat relation (depending on {@link ConsultingType}). * - * @param userId Keycloak user ID - * @param user {@link UserDTO} from registration form + * @param userId Keycloak user ID + * @param user {@link UserDTO} from registration form * @param consultingType {@link ConsultingType} */ private void updateAccountData(String userId, UserDTO user, ConsultingType consultingType) { @@ -165,8 +171,8 @@ private void updateAccountData(String userId, UserDTO user, ConsultingType consu * Initializes the provided {@link User} account depending on the consulting type. Consulting type * KREUZBUND will get a chat/agency relation, all others will be provided with a session. * - * @param user {@link UserDTO} - * @param dbUser {@link User} + * @param user {@link UserDTO} + * @param dbUser {@link User} * @param consultingTypeSettings {@link ConsultingTypeSettings} */ private void initializeUserAccount(UserDTO user, User dbUser, @@ -185,8 +191,8 @@ private void initializeUserAccount(UserDTO user, User dbUser, /** * Creates a new session for the provided {@link User}. * - * @param user {@link UserDTO} - * @param dbUser {@link User} + * @param user {@link UserDTO} + * @param dbUser {@link User} * @param consultingTypeSettings {@link ConsultingTypeSettings} */ private void createUserSession(UserDTO user, User dbUser, @@ -262,7 +268,8 @@ private void createUserChatAgencyRelation(UserDTO user, User dbUser) { } catch (InternalServerErrorException serviceException) { rollBackUserAccount(dbUser.getUserId(), dbUser, null, userAgency); - throw new InternalServerErrorException("Could not create user-agency relation for Kreuzbund registration"); + throw new InternalServerErrorException( + "Could not create user-agency relation for Kreuzbund registration"); } } @@ -270,9 +277,9 @@ private void createUserChatAgencyRelation(UserDTO user, User dbUser) { * Deletes the provided user in Keycloak and MariaDB and its related session or user <-> * chat/agency relations. * - * @param userId Keycloak user ID + * @param userId Keycloak user ID * @param session {@link Session} - * @param dbUser {@link User} + * @param dbUser {@link User} */ private void rollBackUserAccount(String userId, User dbUser, Session session, UserAgency userAgency) { diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index f822ada62..3f60b2474 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -247,16 +247,21 @@ public void updateRole(final String userId, final String roleName) { // Assign role RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation(); - log.warn("=== roleRepresentation ==="); - log.warn("containerId: " + roleRepresentation.getContainerId()); - log.warn("description: " + roleRepresentation.getDescription()); - log.warn("id: " + roleRepresentation.getId()); - log.warn("name: " + roleRepresentation.getName()); - log.warn("attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation.getAttributes().toString() : "null")); - log.warn("clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation.getClientRole().toString() : "null")); - log.warn("composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation.getComposites().toString() : "null")); - log.warn("isComposite: " + roleRepresentation.isComposite()); - log.warn("=========================="); + String lineSeparator = System.getProperty("line.separator"); + StringBuilder logStringBuilder = new StringBuilder(); + logStringBuilder.append("=== roleRepresentation ===" + lineSeparator); + logStringBuilder.append("userId: " + userId + lineSeparator); + logStringBuilder.append("containerId: " + roleRepresentation.getContainerId() + lineSeparator); + logStringBuilder.append("description: " + roleRepresentation.getDescription() + lineSeparator); + logStringBuilder.append("id: " + roleRepresentation.getId() + lineSeparator); + logStringBuilder.append("name: " + roleRepresentation.getName() + lineSeparator); + logStringBuilder.append("attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation.getAttributes().toString() : "null") + lineSeparator); + logStringBuilder.append("clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation.getClientRole().toString() : "null") + lineSeparator); + logStringBuilder.append("composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation.getComposites().toString() : "null") + lineSeparator); + logStringBuilder.append("isComposite: " + roleRepresentation.isComposite() + lineSeparator); + logStringBuilder.append("=========================="); + + log.warn(logStringBuilder.toString()); UserResource user = userRessource.get(userId); boolean isRoleUpdated = false; From 98119b3cf3046f7851dd4e02b1c41f27998bb938 Mon Sep 17 00:00:00 2001 From: Daniel Hillwig Date: Fri, 4 Dec 2020 13:56:14 +0100 Subject: [PATCH 4/6] fix: codestyle violation --- .../api/facade/CreateUserFacade.java | 1 - .../helper/KeycloakAdminClientHelper.java | 22 +++++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java index dd75a0a1c..a1e5d7f85 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java @@ -81,7 +81,6 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT logStringBuilder.append("username: " + user.getUsername() + lineSeparator); logStringBuilder.append("agencyId: " + user.getAgencyId() + lineSeparator); - KeycloakCreateUserResponseDTO response; String userId; diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index 3f60b2474..18170d581 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -12,16 +12,9 @@ import de.caritas.cob.userservice.api.service.LogService; import de.caritas.cob.userservice.api.service.helper.aspect.KeycloakAdminClientLogout; import java.net.URI; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.Set; -import javax.ws.rs.core.Link; import javax.ws.rs.core.Response; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; @@ -33,7 +26,6 @@ import org.keycloak.representations.idm.ErrorRepresentation; import org.keycloak.representations.idm.RoleRepresentation; import org.keycloak.representations.idm.UserRepresentation; -import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; @@ -243,7 +235,6 @@ public void updateUserRole(final String userId) throws Exception { public void updateRole(final String userId, final String roleName) { // Get realm and user resources RealmResource realmResource = getInstance().realm(KEYCLOAK_REALM); - UsersResource userRessource = realmResource.users(); // Assign role RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation(); @@ -255,14 +246,21 @@ public void updateRole(final String userId, final String roleName) { logStringBuilder.append("description: " + roleRepresentation.getDescription() + lineSeparator); logStringBuilder.append("id: " + roleRepresentation.getId() + lineSeparator); logStringBuilder.append("name: " + roleRepresentation.getName() + lineSeparator); - logStringBuilder.append("attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation.getAttributes().toString() : "null") + lineSeparator); - logStringBuilder.append("clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation.getClientRole().toString() : "null") + lineSeparator); - logStringBuilder.append("composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation.getComposites().toString() : "null") + lineSeparator); + logStringBuilder.append( + "attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation + .getAttributes().toString() : "null") + lineSeparator); + logStringBuilder.append( + "clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation + .getClientRole().toString() : "null") + lineSeparator); + logStringBuilder.append( + "composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation + .getComposites().toString() : "null") + lineSeparator); logStringBuilder.append("isComposite: " + roleRepresentation.isComposite() + lineSeparator); logStringBuilder.append("=========================="); log.warn(logStringBuilder.toString()); + UsersResource userRessource = realmResource.users(); UserResource user = userRessource.get(userId); boolean isRoleUpdated = false; From e6cada7622643a088188355ac43084e12809e3be Mon Sep 17 00:00:00 2001 From: mebo4b Date: Fri, 11 Dec 2020 10:42:23 +0100 Subject: [PATCH 5/6] chore: add extended logging for user resource --- .../helper/KeycloakAdminClientHelper.java | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index 18170d581..f2eeb8ac8 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -1,7 +1,7 @@ package de.caritas.cob.userservice.api.service.helper; -import static java.util.Objects.nonNull; - +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import de.caritas.cob.userservice.api.authorization.Authority; import de.caritas.cob.userservice.api.authorization.UserRole; import de.caritas.cob.userservice.api.exception.keycloak.KeycloakException; @@ -108,9 +108,9 @@ public KeycloakCreateUserResponseDTO createKeycloakUser(final UserDTO user) thro /** * Creates a user with firstname and lastname in Keycloak and returns its Keycloak user ID. * - * @param user {@link UserDTO} + * @param user {@link UserDTO} * @param firstName first name of user - * @param lastName last name of user + * @param lastName last name of user * @return {@link KeycloakCreateUserResponseDTO} */ @KeycloakAdminClientLogout @@ -228,7 +228,7 @@ public void updateUserRole(final String userId) throws Exception { /** * Assigns the role with the given name to the given user ID. * - * @param userId Keycloak user ID + * @param userId Keycloak user ID * @param roleName Keycloak role name */ @KeycloakAdminClientLogout @@ -237,31 +237,35 @@ public void updateRole(final String userId, final String roleName) { RealmResource realmResource = getInstance().realm(KEYCLOAK_REALM); // Assign role + ObjectMapper objectMapper = new ObjectMapper(); RoleRepresentation roleRepresentation = realmResource.roles().get(roleName).toRepresentation(); String lineSeparator = System.getProperty("line.separator"); StringBuilder logStringBuilder = new StringBuilder(); - logStringBuilder.append("=== roleRepresentation ===" + lineSeparator); - logStringBuilder.append("userId: " + userId + lineSeparator); - logStringBuilder.append("containerId: " + roleRepresentation.getContainerId() + lineSeparator); - logStringBuilder.append("description: " + roleRepresentation.getDescription() + lineSeparator); - logStringBuilder.append("id: " + roleRepresentation.getId() + lineSeparator); - logStringBuilder.append("name: " + roleRepresentation.getName() + lineSeparator); - logStringBuilder.append( - "attributes: " + ((nonNull(roleRepresentation.getAttributes())) ? roleRepresentation - .getAttributes().toString() : "null") + lineSeparator); - logStringBuilder.append( - "clientRole: " + ((nonNull(roleRepresentation.getClientRole())) ? roleRepresentation - .getClientRole().toString() : "null") + lineSeparator); - logStringBuilder.append( - "composites: " + ((nonNull(roleRepresentation.getComposites())) ? roleRepresentation - .getComposites().toString() : "null") + lineSeparator); - logStringBuilder.append("isComposite: " + roleRepresentation.isComposite() + lineSeparator); + logStringBuilder.append("=== roleRepresentation ===").append(lineSeparator); + try { + logStringBuilder.append("object: ") + .append(objectMapper.writeValueAsString(roleRepresentation)) + .append(lineSeparator); + } catch (JsonProcessingException e) { + log.warn("Could not parse object of role representation"); + } logStringBuilder.append("=========================="); - log.warn(logStringBuilder.toString()); - UsersResource userRessource = realmResource.users(); UserResource user = userRessource.get(userId); + + logStringBuilder.append("=== userRessource ===").append(lineSeparator); + try { + logStringBuilder.append("object: ") + .append(objectMapper.writeValueAsString(user.toRepresentation())) + .append(lineSeparator); + } catch (JsonProcessingException e) { + log.warn("Could not parse object of user resource"); + } + logStringBuilder.append("=========================="); + + log.warn(logStringBuilder.toString()); + boolean isRoleUpdated = false; user.roles().realmLevel() @@ -284,7 +288,7 @@ public void updateRole(final String userId, final String roleName) { /** * Updates the Keycloak password for a user. * - * @param userId Keycloak user ID + * @param userId Keycloak user ID * @param password user password */ @KeycloakAdminClientLogout @@ -301,7 +305,7 @@ public void updatePassword(final String userId, final String password) throws Ex * success/error status possible, because the Keycloak Client doesn't provide one either. * * * @param userId Keycloak user ID - * @param user {@link UserDTO} + * @param user {@link UserDTO} * @return the (dummy) email address * @throws Exception {@link Exception} */ @@ -335,7 +339,7 @@ public void rollBackUser(String userId) { /** * Returns true if the given user has the provided authority. * - * @param userId Keycloak user ID + * @param userId Keycloak user ID * @param authority Keycloak authority * @return true if user hast provided authority */ From b79da5b23325976fbfe3c0c37d97272ba4900f89 Mon Sep 17 00:00:00 2001 From: mebo4b Date: Fri, 11 Dec 2020 10:55:48 +0100 Subject: [PATCH 6/6] chore: added user id --- .../userservice/api/facade/CreateUserFacade.java | 15 +-------------- .../service/helper/KeycloakAdminClientHelper.java | 2 ++ 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java index a1e5d7f85..25dc37ba8 100644 --- a/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java +++ b/src/main/java/de/caritas/cob/userservice/api/facade/CreateUserFacade.java @@ -1,7 +1,5 @@ package de.caritas.cob.userservice.api.facade; -import static java.util.Objects.nonNull; - import de.caritas.cob.userservice.api.container.RocketChatCredentials; import de.caritas.cob.userservice.api.exception.SaveUserException; import de.caritas.cob.userservice.api.exception.httpresponses.BadRequestException; @@ -68,18 +66,7 @@ public KeycloakCreateUserResponseDTO createUserAndInitializeAccount(final UserDT String lineSeparator = System.getProperty("line.separator"); StringBuilder logStringBuilder = new StringBuilder(); logStringBuilder.append("=== UserDTO ===" + lineSeparator); - logStringBuilder.append("addictiveDrugs: " + user.getAddictiveDrugs() + lineSeparator); - logStringBuilder.append("age: " + user.getAge() + lineSeparator); - logStringBuilder.append("consultingType: " + user.getConsultingType() + lineSeparator); - logStringBuilder - .append("email:" + ((nonNull(user.getEmail())) ? "not null" : "null") + lineSeparator); - logStringBuilder.append("gender: " + user.getGender() + lineSeparator); - logStringBuilder.append("postcode: " + user.getPostcode() + lineSeparator); - logStringBuilder.append("relation: " + user.getRelation() + lineSeparator); - logStringBuilder.append("state: " + user.getState() + lineSeparator); - logStringBuilder.append("termsAccepted: " + user.getTermsAccepted() + lineSeparator); - logStringBuilder.append("username: " + user.getUsername() + lineSeparator); - logStringBuilder.append("agencyId: " + user.getAgencyId() + lineSeparator); + logStringBuilder.append("object: " + user.toString()); KeycloakCreateUserResponseDTO response; String userId; diff --git a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java index f2eeb8ac8..a15a2a548 100644 --- a/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java +++ b/src/main/java/de/caritas/cob/userservice/api/service/helper/KeycloakAdminClientHelper.java @@ -244,6 +244,8 @@ public void updateRole(final String userId, final String roleName) { logStringBuilder.append("=== roleRepresentation ===").append(lineSeparator); try { logStringBuilder.append("object: ") + .append("user ID: ") + .append(userId) .append(objectMapper.writeValueAsString(roleRepresentation)) .append(lineSeparator); } catch (JsonProcessingException e) {