From 77ec949bd2ba56554b7e70ce385183f241baed98 Mon Sep 17 00:00:00 2001 From: Manuel Soulier Date: Mon, 22 Jan 2024 21:40:49 +0100 Subject: [PATCH] fix: add custom logger for keycloak status --- .../fr/insee/arc/core/util/StaticLoggerDispatcher.java | 5 +++++ .../insee/arc/utils/ressourceUtils/PropertiesHandler.java | 2 -- .../main/java/fr/insee/arc/utils/utils/LoggerHelper.java | 6 ++++++ .../java/fr/insee/arc/web/Oauth2ClientForKeycloak.java | 5 ++++- .../src/main/java/fr/insee/arc/web/WebInitializer.java | 4 ++-- .../java/fr/insee/arc/web/WebSecurityConfiguration.java | 8 ++++++++ .../arc/web/gui/index/controller/ControllerIndex.java | 5 +++++ .../fr/insee/arc/web/gui/index/service/ServiceIndex.java | 6 +++++- .../ws/services/restServices/WsSecurityConfiguration.java | 7 +++++++ 9 files changed, 42 insertions(+), 6 deletions(-) diff --git a/arc-core/src/main/java/fr/insee/arc/core/util/StaticLoggerDispatcher.java b/arc-core/src/main/java/fr/insee/arc/core/util/StaticLoggerDispatcher.java index 073118c74..42124756c 100644 --- a/arc-core/src/main/java/fr/insee/arc/core/util/StaticLoggerDispatcher.java +++ b/arc-core/src/main/java/fr/insee/arc/core/util/StaticLoggerDispatcher.java @@ -10,6 +10,11 @@ @Component public class StaticLoggerDispatcher { + + public static void custom(Logger logger, Object message) { + LoggerHelper.custom(logger, message); + } + /** Transition class. Use a LoggerDispatcher as an @Autowired attribute instead. */ public static void error(Logger logger, Exception e) { diff --git a/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java b/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java index 4fada6403..53bb93e69 100644 --- a/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java +++ b/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java @@ -320,8 +320,6 @@ public Map fullVersionInformation() { map.put("version", getVersion()); map.put("buildDate", getVersionDate()); map.put("gitCommitId", getGitCommitId()); - map.put("databaseUrl", getDatabaseUrl()); - map.put("databaseUserName", getDatabaseUsername()); return map; } diff --git a/arc-utils/src/main/java/fr/insee/arc/utils/utils/LoggerHelper.java b/arc-utils/src/main/java/fr/insee/arc/utils/utils/LoggerHelper.java index 9e506872f..97cf397bf 100644 --- a/arc-utils/src/main/java/fr/insee/arc/utils/utils/LoggerHelper.java +++ b/arc-utils/src/main/java/fr/insee/arc/utils/utils/LoggerHelper.java @@ -26,6 +26,8 @@ private LoggerHelper() { throw new IllegalStateException("Logger utility class"); } + private static final String CUSTOM_LOGGER="OPERATION"; + /** * @see Logger#log(Priority, Object) * @param aLogger @@ -355,4 +357,8 @@ public static final void infoGenTextAsComment(Class clazz, String method, Log logAsComment(aLogger, Level.INFO, t, "Lors de l'exécution de", clazz.getCanonicalName(), IConstanteCaractere.sharp, method); } + + public static void custom(Logger aLogger, Object message) { + log(aLogger, Level.forName("CUSTOM_LOGGER", 200), message); + } } diff --git a/arc-web/src/main/java/fr/insee/arc/web/Oauth2ClientForKeycloak.java b/arc-web/src/main/java/fr/insee/arc/web/Oauth2ClientForKeycloak.java index 453fa4fb4..5ed34709e 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/Oauth2ClientForKeycloak.java +++ b/arc-web/src/main/java/fr/insee/arc/web/Oauth2ClientForKeycloak.java @@ -4,6 +4,8 @@ import java.util.List; import java.util.Set; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -14,10 +16,11 @@ import org.springframework.security.oauth2.core.oidc.OidcUserInfo; import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority; +import fr.insee.arc.core.util.StaticLoggerDispatcher; import fr.insee.arc.utils.webutils.WebAttributesName; public class Oauth2ClientForKeycloak { - + @Value(WebAttributesName.KEYCLOAK_ATTRIBUTE_REALM) protected String keycloakRealm; diff --git a/arc-web/src/main/java/fr/insee/arc/web/WebInitializer.java b/arc-web/src/main/java/fr/insee/arc/web/WebInitializer.java index 9416b5954..992ddbb1a 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/WebInitializer.java +++ b/arc-web/src/main/java/fr/insee/arc/web/WebInitializer.java @@ -9,12 +9,12 @@ public class WebInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { - return new Class[] { WebConfiguration.class }; + return new Class[0]; } @Override protected Class[] getServletConfigClasses() { - return new Class[] { WebSecurityConfiguration.class }; + return new Class[] { WebConfiguration.class, WebSecurityConfiguration.class }; } @Override diff --git a/arc-web/src/main/java/fr/insee/arc/web/WebSecurityConfiguration.java b/arc-web/src/main/java/fr/insee/arc/web/WebSecurityConfiguration.java index 25575503e..d9d694579 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/WebSecurityConfiguration.java +++ b/arc-web/src/main/java/fr/insee/arc/web/WebSecurityConfiguration.java @@ -1,5 +1,7 @@ package fr.insee.arc.web; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; @@ -10,6 +12,7 @@ import org.springframework.security.oauth2.core.ClientAuthenticationMethod; import org.springframework.security.web.SecurityFilterChain; +import fr.insee.arc.core.util.StaticLoggerDispatcher; import fr.insee.arc.utils.ressourceUtils.PropertiesHandler; import fr.insee.arc.utils.webutils.WebAttributesName; @@ -17,14 +20,19 @@ @EnableWebSecurity @EnableMethodSecurity public class WebSecurityConfiguration extends Oauth2ClientForKeycloak { + + private static final Logger LOGGER = LogManager.getLogger(WebSecurityConfiguration.class); // register Keycloak oauth2 client for authentification flow @Bean public ClientRegistrationRepository clientRegistrationRepository() { if (WebAttributesName.isKeycloakActive(keycloakRealm)) { + StaticLoggerDispatcher.custom(LOGGER, "Keycloak is set for arc-web"); + return new InMemoryClientRegistrationRepository( keycloakClientRegistration(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)); } + StaticLoggerDispatcher.custom(LOGGER, "Keycloak is NOT set for arc-web"); return null; } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/index/controller/ControllerIndex.java b/arc-web/src/main/java/fr/insee/arc/web/gui/index/controller/ControllerIndex.java index ad71bc331..57cf4c778 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/index/controller/ControllerIndex.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/index/controller/ControllerIndex.java @@ -22,4 +22,9 @@ public ResponseEntity> healthcheckAction() { return healthcheck(); } + @RequestMapping("/version") + public ResponseEntity> versionAction(){ + return version(); + } + } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/index/service/ServiceIndex.java b/arc-web/src/main/java/fr/insee/arc/web/gui/index/service/ServiceIndex.java index 46e94f10f..fd19040be 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/index/service/ServiceIndex.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/index/service/ServiceIndex.java @@ -26,7 +26,11 @@ public ResponseEntity> healthcheck() { return new ResponseEntity<>(map, HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>(map, HttpStatus.OK); - + } + + public ResponseEntity> version() { + Map map = WebAttributesName.fullVersionInformation(); + return new ResponseEntity<>(map, HttpStatus.OK); } } diff --git a/arc-ws/src/main/java/fr/insee/arc/ws/services/restServices/WsSecurityConfiguration.java b/arc-ws/src/main/java/fr/insee/arc/ws/services/restServices/WsSecurityConfiguration.java index 2cd723aea..aae73bfe4 100644 --- a/arc-ws/src/main/java/fr/insee/arc/ws/services/restServices/WsSecurityConfiguration.java +++ b/arc-ws/src/main/java/fr/insee/arc/ws/services/restServices/WsSecurityConfiguration.java @@ -5,6 +5,8 @@ import java.util.Map; import java.util.stream.Collectors; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -21,6 +23,7 @@ import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; +import fr.insee.arc.core.util.StaticLoggerDispatcher; import fr.insee.arc.utils.ressourceUtils.PropertiesHandler; import fr.insee.arc.utils.webutils.WebAttributesName; @@ -29,6 +32,8 @@ @EnableMethodSecurity public class WsSecurityConfiguration { + private static final Logger LOGGER = LogManager.getLogger(WsSecurityConfiguration.class); + @Value(WebAttributesName.KEYCLOAK_ATTRIBUTE_REALM) private String keycloakRealm; @@ -65,8 +70,10 @@ SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { @Bean public JwtDecoder jwtDecoder() { if (WebAttributesName.isKeycloakActive(keycloakRealm)) { + StaticLoggerDispatcher.custom(LOGGER, "Keycloak is set for arc-ws"); return JwtDecoders.fromIssuerLocation(keycloakServer + "/realms/" + keycloakRealm); } + StaticLoggerDispatcher.custom(LOGGER, "Keycloak is NOT set for arc-ws"); return null; }