From c3514a908cbc21da134aa4f98a704c9b56178d5e Mon Sep 17 00:00:00 2001 From: Ilmari Vacklin Date: Tue, 17 Dec 2024 17:18:35 +0200 Subject: [PATCH 1/2] feat(kotoutumiskoulutus): add audit logging This implementation is based on the Spring Data EntityCallback API. --- .../kitu/kotoutumiskoulutus/KoealustaService.kt | 16 ++++++++++++++-- server/src/main/resources/application.properties | 1 + server/src/main/resources/logback-spring.xml | 5 +++-- server/src/test/resources/application.properties | 1 + 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaService.kt b/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaService.kt index 5622019d..26fea798 100644 --- a/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaService.kt +++ b/server/src/main/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaService.kt @@ -3,6 +3,7 @@ package fi.oph.kitu.kotoutumiskoulutus import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import fi.oph.kitu.AUDIT_LOGGER_NAME import fi.oph.kitu.PeerService import fi.oph.kitu.logging.add import fi.oph.kitu.logging.addHttpResponse @@ -24,6 +25,7 @@ class KoealustaService( private val mappingService: KoealustaMappingService, ) { private val logger = LoggerFactory.getLogger(javaClass) + private val auditLogger = LoggerFactory.getLogger(AUDIT_LOGGER_NAME) @Value("\${kitu.kotoutumiskoulutus.koealusta.wstoken}") lateinit var koealustaToken: String @@ -92,9 +94,19 @@ class KoealustaService( throw ex } - val result = kielitestiSuoritusRepository.saveAll(suoritukset) + val savedSuoritukset = kielitestiSuoritusRepository.saveAll(suoritukset) - event.add("db.saved" to result.count()) + event.add("db.saved" to savedSuoritukset.count()) + + for (suoritus in savedSuoritukset) { + auditLogger + .atInfo() + .add( + "principal" to "koealusta.import", + "peer.service" to PeerService.Koealusta.value, + "suoritus.id" to suoritus.id, + ).log("Kielitesti suoritus imported") + } return@withEvent suoritukset.maxOfOrNull { it.timeCompleted } ?: from } 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 From ab3de39765bdb834d1f87dd6a90e0a11c5292580 Mon Sep 17 00:00:00 2001 From: Ilmari Vacklin Date: Wed, 8 Jan 2025 09:06:21 +0200 Subject: [PATCH 2/2] chore: suppress and clean up some warnings in test code These issues are highlighted by IDEA's built-in linting. - The testcontainers values are unused in code since we rely on Spring's automatic data source lookup. Suppress the `unused` warning for the whole companion object (in case we add more testcontainers). - Tell Kotlin we assume the value returned from PostgreSQLContainer::withUrlParam() is non-nullable. --- server/src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt | 1 + .../fi/oph/kitu/kotoutumiskoulutus/KoealustaServiceTests.kt | 1 + server/src/test/kotlin/fi/oph/kitu/yki/YkiServiceTests.kt | 3 ++- .../test/kotlin/fi/oph/kitu/yki/YkiSuoritusRepositoryTest.kt | 3 ++- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/server/src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt b/server/src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt index fdbd0158..c29765de 100644 --- a/server/src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt +++ b/server/src/test/kotlin/fi/oph/kitu/KituApplicationTests.kt @@ -10,6 +10,7 @@ import org.testcontainers.junit.jupiter.Testcontainers @SpringBootTest @Testcontainers class KituApplicationTests { + @Suppress("unused") companion object { @JvmStatic @Container diff --git a/server/src/test/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaServiceTests.kt b/server/src/test/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaServiceTests.kt index e0aa3ef9..5a5c6f82 100644 --- a/server/src/test/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaServiceTests.kt +++ b/server/src/test/kotlin/fi/oph/kitu/kotoutumiskoulutus/KoealustaServiceTests.kt @@ -20,6 +20,7 @@ import kotlin.test.assertEquals @SpringBootTest @Testcontainers class KoealustaServiceTests { + @Suppress("unused") companion object { @JvmStatic @Container diff --git a/server/src/test/kotlin/fi/oph/kitu/yki/YkiServiceTests.kt b/server/src/test/kotlin/fi/oph/kitu/yki/YkiServiceTests.kt index 5d7f00f5..1a5c8df1 100644 --- a/server/src/test/kotlin/fi/oph/kitu/yki/YkiServiceTests.kt +++ b/server/src/test/kotlin/fi/oph/kitu/yki/YkiServiceTests.kt @@ -29,13 +29,14 @@ class YkiServiceTests( @Autowired private val ykiSuoritusRepository: YkiSuoritusRepository, @Autowired private val ykiArvioijaRepository: YkiArvioijaRepository, ) { + @Suppress("unused") companion object { @JvmStatic @Container @ServiceConnection val postgres = PostgreSQLContainer("postgres:16") - .withUrlParam("stringtype", "unspecified") + .withUrlParam("stringtype", "unspecified")!! } @BeforeEach diff --git a/server/src/test/kotlin/fi/oph/kitu/yki/YkiSuoritusRepositoryTest.kt b/server/src/test/kotlin/fi/oph/kitu/yki/YkiSuoritusRepositoryTest.kt index 7a005bcb..36e62c65 100644 --- a/server/src/test/kotlin/fi/oph/kitu/yki/YkiSuoritusRepositoryTest.kt +++ b/server/src/test/kotlin/fi/oph/kitu/yki/YkiSuoritusRepositoryTest.kt @@ -22,13 +22,14 @@ import kotlin.test.assertTrue class YkiSuoritusRepositoryTest( @Autowired private val ykiSuoritusRepository: YkiSuoritusRepository, ) { + @Suppress("unused") companion object { @JvmStatic @Container @ServiceConnection val postgres = PostgreSQLContainer("postgres:16") - .withUrlParam("stringtype", "unspecified") + .withUrlParam("stringtype", "unspecified")!! } @BeforeEach