diff --git a/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KielitestiSuoritusRepository.kt b/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KielitestiSuoritusRepository.kt index b7afcd88..886cf72f 100644 --- a/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KielitestiSuoritusRepository.kt +++ b/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KielitestiSuoritusRepository.kt @@ -1,7 +1,45 @@ package fi.oph.kitu.kotoutumiskoulutus +import com.fasterxml.jackson.databind.ObjectMapper +import fi.oph.kitu.AUDIT_LOGGER_NAME +import fi.oph.kitu.logging.add +import fi.oph.kitu.logging.tryAddUser +import org.slf4j.LoggerFactory +import org.springframework.data.relational.core.mapping.event.AfterDeleteCallback +import org.springframework.data.relational.core.mapping.event.AfterSaveCallback import org.springframework.data.repository.CrudRepository +import org.springframework.stereotype.Component import org.springframework.stereotype.Repository @Repository interface KielitestiSuoritusRepository : CrudRepository + +@Component +class AuditLoggerEntityListener( + private val jacksonObjectMapper: ObjectMapper, +) : AfterSaveCallback, + AfterDeleteCallback { + private val auditLogger = LoggerFactory.getLogger(AUDIT_LOGGER_NAME) + + override fun onAfterSave(aggregate: KielitestiSuoritus): KielitestiSuoritus { + auditLogger + .atInfo() + .tryAddUser() + .add( + "entity" to jacksonObjectMapper.writeValueAsString(aggregate), + "operation" to "save", + ).log("Saved kielitesti suoritus") + return aggregate + } + + override fun onAfterDelete(aggregate: KielitestiSuoritus): KielitestiSuoritus { + auditLogger + .atInfo() + .tryAddUser() + .add( + "entity" to jacksonObjectMapper.writeValueAsString(aggregate), + "operation" to "delete", + ).log("Deleted kielitesti suoritus") + return aggregate + } +} diff --git a/server/src/main/kotlin/fi/oph/kitu/logging/LoggerExtensions.kt b/server/src/main/kotlin/fi/oph/kitu/logging/LoggerExtensions.kt index 57ffc9cf..49b6b103 100644 --- a/server/src/main/kotlin/fi/oph/kitu/logging/LoggerExtensions.kt +++ b/server/src/main/kotlin/fi/oph/kitu/logging/LoggerExtensions.kt @@ -1,6 +1,7 @@ package fi.oph.kitu.logging import fi.oph.kitu.PeerService +import fi.oph.kitu.auth.CasUserDetails import io.opentelemetry.semconv.HttpAttributes import io.opentelemetry.semconv.UrlAttributes import io.opentelemetry.semconv.UserAgentAttributes @@ -11,6 +12,7 @@ import org.slf4j.spi.LoggingEventBuilder import org.springframework.dao.DuplicateKeyException import org.springframework.http.HttpHeaders import org.springframework.http.ResponseEntity +import org.springframework.security.core.context.SecurityContextHolder import java.net.http.HttpResponse fun LoggingEventBuilder.addServletRequest(request: HttpServletRequest): LoggingEventBuilder = @@ -120,3 +122,12 @@ fun LoggingEventBuilder.withEvent( log() } } + +fun LoggingEventBuilder.tryAddUser(): LoggingEventBuilder { + val userDetails = SecurityContextHolder.getContext().authentication?.principal as? CasUserDetails + if (userDetails == null) { + return this + } + add("user" to userDetails) + return this +} diff --git a/server/src/main/resources/application.properties b/server/src/main/resources/application.properties index 95089f42..a1647740 100644 --- a/server/src/main/resources/application.properties +++ b/server/src/main/resources/application.properties @@ -30,6 +30,7 @@ kitu.yki.password=${YKI_API_PASSWORD} spring.mustache.suffix=.mustache logging.structured.format.console=ecs +logging.structured.format.file=ecs # Uncomment this to debug authentication/authorization issues. # Ref: https://docs.spring.io/spring-security/reference/servlet/architecture.html#servlet-logging diff --git a/server/src/main/resources/logback-spring.xml b/server/src/main/resources/logback-spring.xml index 444132f2..d4460dab 100644 --- a/server/src/main/resources/logback-spring.xml +++ b/server/src/main/resources/logback-spring.xml @@ -5,8 +5,9 @@ logs/audit.log - - ${LOG_PATTERN} + + ${FILE_LOG_STRUCTURED_FORMAT} + ${FILE_LOG_CHARSET} logs/audit.%d{yyyy-MM-dd}.log diff --git a/server/src/test/resources/application.properties b/server/src/test/resources/application.properties index adf2b46a..e80239cf 100644 --- a/server/src/test/resources/application.properties +++ b/server/src/test/resources/application.properties @@ -22,3 +22,4 @@ kitu.yki.username= kitu.yki.password= logging.structured.format.console=ecs +logging.structured.format.file=ecs