From e176f146cc7d62bb4849494e0149030c555c7dfd Mon Sep 17 00:00:00 2001 From: Johan Blomgren Date: Tue, 21 Jan 2025 10:08:38 +0100 Subject: [PATCH] =?UTF-8?q?Fikset=20tester,=20setter=20userId=20f=C3=B8rst?= =?UTF-8?q?e=20request=20ogs=C3=A5=20hvis=20userId=20mangler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../libs/log/filter/LogFilter.kt | 8 ++++---- .../libs/log/filter/LogFilterTest.kt | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt b/log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt index 80cd6b7..7757911 100644 --- a/log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt +++ b/log/main/no/nav/tilleggsstonader/libs/log/filter/LogFilter.kt @@ -50,12 +50,13 @@ class LogFilter(val sporBruker: Boolean) : HttpFilter() { if (!sporBruker) { return } - val userId = resolveUserId(request) + var userId = resolveUserId(request) if (userId.isNullOrEmpty()) { // user-id tracking only works if the client is stateful and supports cookies. // if no user-id is found, generate one for any following requests but do not use it on the // current request to avoid generating large numbers of useless user-ids. - generateUserIdCookie(response) + userId = IdUtils.generateId() + generateUserIdCookie(response, userId) } MDC.put(MDC_USER_ID, userId) } @@ -95,8 +96,7 @@ class LogFilter(val sporBruker: Boolean) : HttpFilter() { ?: IdUtils.generateId() } - private fun generateUserIdCookie(response: HttpServletResponse) { - val userId = IdUtils.generateId() + private fun generateUserIdCookie(response: HttpServletResponse, userId: String) { val cookie = Cookie(RANDOM_USER_ID_COOKIE_NAME, userId).apply { path = "/" maxAge = ONE_MONTH_IN_SECONDS diff --git a/log/test/no/nav/tilleggsstonader/libs/log/filter/LogFilterTest.kt b/log/test/no/nav/tilleggsstonader/libs/log/filter/LogFilterTest.kt index 764375f..59f37eb 100644 --- a/log/test/no/nav/tilleggsstonader/libs/log/filter/LogFilterTest.kt +++ b/log/test/no/nav/tilleggsstonader/libs/log/filter/LogFilterTest.kt @@ -5,6 +5,7 @@ import io.mockk.mockk import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import no.nav.tilleggsstonader.libs.log.NavHttpHeaders +import no.nav.tilleggsstonader.libs.log.mdc.MDCConstants import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -14,7 +15,7 @@ class LogFilterTest { private lateinit var httpServletRequest: HttpServletRequest private lateinit var httpServletResponse: HttpServletResponse - private val logFilter = LogFilter() + private val logFilter = LogFilter(true) @BeforeEach fun setup() { @@ -52,6 +53,22 @@ class LogFilterTest { .isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR) } + @Test + fun `skal ikke logge bruker hvis sporBruker=true`() { + logFilter.doFilter(httpServletRequest, httpServletResponse) { _, _ -> + assertThat(MDC.get(MDCConstants.MDC_USER_ID)).isNotNull() + } + } + + @Test + fun `skal ikke logge bruker hvis sporBruker=false`() { + val logFilter = LogFilter(sporBruker = false) + + logFilter.doFilter(httpServletRequest, httpServletResponse) { _, _ -> + assertThat(MDC.get(MDCConstants.MDC_USER_ID)).isNull() + } + } + private val mockHttpServletRequest = mockk(relaxed = true).also { every { it.method } returns "GET" every { it.requestURI } returns "/test/path"