From 306b7405d1bac8eb90d3ca5af5412190d62e134d Mon Sep 17 00:00:00 2001 From: Vincenzo Ingenito <54367888+vincenzo-ingenito@users.noreply.github.com> Date: Sun, 10 Dec 2023 19:14:48 +0100 Subject: [PATCH] feat: Add subject and issuer config --- .../client/routes/base/ClientRoutes.java | 3 ++ .../gtw/dispatcher/logging/LoggerHelper.java | 47 +++++++++++++++---- .../ms/gtw/dispatcher/service/IConfigSRV.java | 4 ++ .../dispatcher/service/impl/ConfigSRV.java | 31 ++++++++++++ 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java index f23e31b0..88bc8759 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/client/routes/base/ClientRoutes.java @@ -48,6 +48,9 @@ public static final class Config { // VALUES public static final String PROPS_NAME_AUDIT_ENABLED = "audit-enabled"; public static final String PROPS_NAME_CONTROL_LOG_ENABLED = "control-log-persistence-enabled"; + public static final String PROPS_NAME_ISSUER_CF = "issuer-cf-cleaning"; + public static final String PROPS_NAME_SUBJECT = "subject-persistence"; + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/logging/LoggerHelper.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/logging/LoggerHelper.java index e4afe95f..56e43aed 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/logging/LoggerHelper.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/logging/LoggerHelper.java @@ -66,7 +66,6 @@ public void trace(String logType,String workflowInstanceId, String message, ILog jwtPayloadDTO = new JWTPayloadDTO(); } LogDTO logDTO = LogDTO.builder(). - op_issuer(jwtPayloadDTO.getIss()). op_locality(jwtPayloadDTO.getLocality()). message(message). operation(operation.getCode()). @@ -74,7 +73,6 @@ public void trace(String logType,String workflowInstanceId, String message, ILog op_timestamp_start(dateFormat.format(startDateOperation)). op_timestamp_end(dateFormat.format(new Date())). op_role(jwtPayloadDTO.getSubject_role()). - op_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())). gateway_name(getGatewayName()). microservice_name(msName). op_application_id(jwtPayloadDTO.getSubject_application_id()). @@ -84,6 +82,14 @@ public void trace(String logType,String workflowInstanceId, String message, ILog workflow_instance_id(workflowInstanceId). build(); + if(configSRV.isSubjectPersistenceEnabled()) { + logDTO.setOp_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())); + } + + if(configSRV.isCfOnIssuerNotAllowed()) { + logDTO.setOp_issuer(jwtPayloadDTO.getIss()); + } + final String logMessage = StringUtility.toJSON(logDTO); log.trace(logMessage); @@ -102,7 +108,6 @@ public void debug(String logType,String workflowInstanceId, String message, ILo jwtPayloadDTO = new JWTPayloadDTO(); } LogDTO logDTO = LogDTO.builder(). - op_issuer(jwtPayloadDTO.getIss()). op_locality(jwtPayloadDTO.getLocality()). message(message). operation(operation.getCode()). @@ -110,7 +115,6 @@ public void debug(String logType,String workflowInstanceId, String message, ILo op_timestamp_start(dateFormat.format(startDateOperation)). op_timestamp_end(dateFormat.format(new Date())). op_role(jwtPayloadDTO.getSubject_role()). - op_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())). gateway_name(getGatewayName()). microservice_name(msName). op_application_id(jwtPayloadDTO.getSubject_application_id()). @@ -120,6 +124,14 @@ public void debug(String logType,String workflowInstanceId, String message, ILo workflow_instance_id(workflowInstanceId). build(); + if(configSRV.isSubjectPersistenceEnabled()) { + logDTO.setOp_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())); + } + + if(configSRV.isCfOnIssuerNotAllowed()) { + logDTO.setOp_issuer(jwtPayloadDTO.getIss()); + } + final String logMessage = StringUtility.toJSON(logDTO); log.debug(logMessage); if (Boolean.TRUE.equals(kafkaLogEnable)) { @@ -137,7 +149,6 @@ public void info(String logType,String workflowInstanceId, String message, ILogE jwtPayloadDTO = new JWTPayloadDTO(); } LogDTO logDTO = LogDTO.builder(). - op_issuer(jwtPayloadDTO.getIss()). op_locality(jwtPayloadDTO.getLocality()). message(message). operation(operation.getCode()). @@ -146,7 +157,6 @@ public void info(String logType,String workflowInstanceId, String message, ILogE op_timestamp_end(dateFormat.format(new Date())). op_document_type(documentType). op_role(jwtPayloadDTO.getSubject_role()). - op_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())). gateway_name(getGatewayName()). microservice_name(msName). op_application_id(jwtPayloadDTO.getSubject_application_id()). @@ -157,6 +167,14 @@ public void info(String logType,String workflowInstanceId, String message, ILogE typeIdExtension(typeIdExtension). build(); + if(configSRV.isSubjectPersistenceEnabled()) { + logDTO.setOp_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadDTO.getSub())); + } + + if(configSRV.isCfOnIssuerNotAllowed()) { + logDTO.setOp_issuer(jwtPayloadDTO.getIss()); + } + final String logMessage = StringUtility.toJSON(logDTO); log.info(logMessage); @@ -173,7 +191,6 @@ public void warn(String logType,String workflowInstanceId, String message, ILogE jwtPayloadToken = new JWTPayloadDTO(); } LogDTO logDTO = LogDTO.builder(). - op_issuer(jwtPayloadToken.getIss()). op_locality(jwtPayloadToken.getLocality()). message(message). operation(operation.getCode()). @@ -181,7 +198,6 @@ public void warn(String logType,String workflowInstanceId, String message, ILogE op_timestamp_start(dateFormat.format(startDateOperation)). op_timestamp_end(dateFormat.format(new Date())). op_role(jwtPayloadToken.getSubject_role()). - op_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadToken.getSub())). gateway_name(getGatewayName()). microservice_name(msName). op_application_id(jwtPayloadToken.getSubject_application_id()). @@ -191,6 +207,13 @@ public void warn(String logType,String workflowInstanceId, String message, ILogE workflow_instance_id(workflowInstanceId). build(); + if(configSRV.isSubjectPersistenceEnabled()) { + logDTO.setOp_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadToken.getSub())); + } + + if(configSRV.isCfOnIssuerNotAllowed()) { + logDTO.setOp_issuer(jwtPayloadToken.getIss()); + } final String logMessage = StringUtility.toJSON(logDTO); log.warn(logMessage); if (Boolean.TRUE.equals(kafkaLogEnable)) { @@ -208,7 +231,6 @@ public void error(String logType,String workflowInstanceId, String message, ILog jwtPayloadToken = new JWTPayloadDTO(); } LogDTO logDTO = LogDTO.builder(). - op_issuer(jwtPayloadToken.getIss()). op_locality(jwtPayloadToken.getLocality()). message(message). operation(operation.getCode()). @@ -219,7 +241,6 @@ public void error(String logType,String workflowInstanceId, String message, ILog op_error_description(error.getDescription()). op_document_type(documentType). op_role(jwtPayloadToken.getSubject_role()). - op_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadToken.getSub())). gateway_name(getGatewayName()). microservice_name(msName). op_application_id(jwtPayloadToken.getSubject_application_id()). @@ -229,6 +250,12 @@ public void error(String logType,String workflowInstanceId, String message, ILog workflow_instance_id(workflowInstanceId). build(); + if(configSRV.isSubjectPersistenceEnabled()) { + logDTO.setOp_fiscal_code(CfUtility.extractFiscalCodeFromJwtSub(jwtPayloadToken.getSub())); + } + if(configSRV.isCfOnIssuerNotAllowed()) { + logDTO.setOp_issuer(jwtPayloadToken.getIss()); + } final String logMessage = StringUtility.toJSON(logDTO); log.error(logMessage); if (Boolean.TRUE.equals(kafkaLogEnable)) { diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java index 3fbaea72..4b70e241 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/IConfigSRV.java @@ -6,4 +6,8 @@ public interface IConfigSRV { Boolean isControlLogPersistenceEnable(); + Boolean isCfOnIssuerNotAllowed(); + + Boolean isSubjectPersistenceEnabled(); + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java index a09b0413..7a1b8892 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/dispatcher/service/impl/ConfigSRV.java @@ -17,6 +17,8 @@ import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_AUDIT_ENABLED; import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_ISSUER_CF; +import static it.finanze.sanita.fse2.ms.gtw.dispatcher.client.routes.base.ClientRoutes.Config.PROPS_NAME_SUBJECT; import static it.finanze.sanita.fse2.ms.gtw.dispatcher.dto.ConfigItemDTO.*; import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.DISPATCHER; import static it.finanze.sanita.fse2.ms.gtw.dispatcher.enums.ConfigItemTypeEnum.GENERIC; @@ -88,4 +90,33 @@ private void refresh(ConfigItemTypeEnum type, String name) { props.put(name, Pair.of(new Date().getTime(), prop)); } + @Override + public Boolean isSubjectPersistenceEnabled() { + long lastUpdate = props.get(PROPS_NAME_SUBJECT).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + synchronized (PROPS_NAME_SUBJECT) { + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(GENERIC, PROPS_NAME_SUBJECT); + } + } + } + return Boolean.parseBoolean( + props.get(PROPS_NAME_SUBJECT).getValue() + ); + } + + @Override + public Boolean isCfOnIssuerNotAllowed() { + long lastUpdate = props.get(PROPS_NAME_ISSUER_CF).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + synchronized(PROPS_NAME_ISSUER_CF) { + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(GENERIC, PROPS_NAME_ISSUER_CF); + } + } + } + return Boolean.parseBoolean( + props.get(PROPS_NAME_ISSUER_CF).getValue() + ); + } }