Skip to content

Commit

Permalink
fix: add custom logger for keycloak status
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Jan 22, 2024
1 parent 5cb0146 commit 77ec949
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ public Map<String, String> fullVersionInformation() {
map.put("version", getVersion());
map.put("buildDate", getVersionDate());
map.put("gitCommitId", getGitCommitId());
map.put("databaseUrl", getDatabaseUrl());
map.put("databaseUserName", getDatabaseUsername());
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions arc-web/src/main/java/fr/insee/arc/web/WebInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,21 +12,27 @@
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;

@Configuration
@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public ResponseEntity<Map<String, Object>> healthcheckAction() {
return healthcheck();
}

@RequestMapping("/version")
public ResponseEntity<Map<String, String>> versionAction(){
return version();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public ResponseEntity<Map<String, Object>> healthcheck() {
return new ResponseEntity<>(map, HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<>(map, HttpStatus.OK);

}

public ResponseEntity<Map<String, String>> version() {
Map<String, String> map = WebAttributesName.fullVersionInformation();
return new ResponseEntity<>(map, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 77ec949

Please sign in to comment.