From aba3fc6a2fdd6fe7d0d7292a11242423140c3ce3 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Tue, 18 Feb 2025 11:01:54 +0100 Subject: [PATCH 1/2] Improve the revisions with more data. --- .../eventlisteners/OurRevisionListener.kt | 46 ++++++++++++++++--- .../no/nav/klage/oppgave/util/TokenUtil.kt | 5 ++ 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt b/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt index fd58f5e2f..5b70e1ccb 100644 --- a/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt +++ b/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt @@ -26,19 +26,51 @@ class OurRevisionListener( override fun newRevision(revisionEntity: Any?) { revisionEntity as OurRevision - try { + val request = try { val requestAttributes = RequestContextHolder.getRequestAttributes() if (requestAttributes != null) { val request = (requestAttributes as ServletRequestAttributes).request - revisionEntity.actor = tokenUtil.getIdent() - revisionEntity.request = request.method + " " + request.requestURI + request.method + " " + request.requestURI } else { - revisionEntity.actor = systembrukerIdent + null } - revisionEntity.traceId = Span.current().spanContext.traceId } catch (e: Exception) { - logger.warn("Failed to set correct actor and/or request on revision entity. Setting 'unknown'.", e) - revisionEntity.actor = "unknown" + logger.debug("No request found to set on revision entity. Setting to null.", e) + null } + + val actor = if (request == null) { + systembrukerIdent + } else { + val navIdentFromToken = try { + tokenUtil.getIdent() + } catch (e: Exception) { + logger.debug("No NAVIdent found in token.", e) + null + } + val callingApplication = try { + tokenUtil.getCallingApplication() + } catch (e: Exception) { + logger.warn("Failed to get calling application from token.", e) + null + } + + if (navIdentFromToken == null && callingApplication == null) { + logger.warn("Neither NAVIdent nor calling application could be found from token. Setting 'unknown'.") + } + + navIdentFromToken ?: callingApplication ?: "unknown" + } + + val traceId = try { + Span.current().spanContext.traceId + } catch (e: Exception) { + logger.warn("Failed to set traceId on revision entity. Setting 'unknown'.", e) + "unknown" + } + + revisionEntity.request = request + revisionEntity.actor = actor + revisionEntity.traceId = traceId } } \ No newline at end of file diff --git a/src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt b/src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt index 7141771f4..88d4080ef 100644 --- a/src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt +++ b/src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt @@ -218,4 +218,9 @@ class TokenUtil( tokenValidationContextHolder.getTokenValidationContext().getJwtToken(SecurityConfiguration.ISSUER_AAD) ?.jwtTokenClaims?.get("NAVident")?.toString() ?: throw RuntimeException("Ident not found in token") + + fun getCallingApplication(): String = + tokenValidationContextHolder.getTokenValidationContext().getJwtToken(SecurityConfiguration.ISSUER_AAD) + ?.jwtTokenClaims?.get("azp_name")?.toString() + ?: throw RuntimeException("Application not found in token") } From e699ab1362ba6e3f464a0fc1dd5030f8a20a5883 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Tue, 18 Feb 2025 12:31:11 +0100 Subject: [PATCH 2/2] Improved logic for setting `actor` in revisions. --- .../eventlisteners/OurRevisionListener.kt | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt b/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt index 5b70e1ccb..0058689df 100644 --- a/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt +++ b/src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt @@ -26,12 +26,16 @@ class OurRevisionListener( override fun newRevision(revisionEntity: Any?) { revisionEntity as OurRevision + var actor: String? = null + val request = try { val requestAttributes = RequestContextHolder.getRequestAttributes() if (requestAttributes != null) { val request = (requestAttributes as ServletRequestAttributes).request request.method + " " + request.requestURI } else { + //no exception occurred, we just don't have a request + actor = systembrukerIdent null } } catch (e: Exception) { @@ -39,27 +43,22 @@ class OurRevisionListener( null } - val actor = if (request == null) { - systembrukerIdent - } else { - val navIdentFromToken = try { - tokenUtil.getIdent() - } catch (e: Exception) { - logger.debug("No NAVIdent found in token.", e) - null - } - val callingApplication = try { - tokenUtil.getCallingApplication() - } catch (e: Exception) { - logger.warn("Failed to get calling application from token.", e) - null - } + val navIdentFromToken = try { + tokenUtil.getIdent() + } catch (e: Exception) { + logger.debug("No NAVIdent found in token.", e) + null + } - if (navIdentFromToken == null && callingApplication == null) { - logger.warn("Neither NAVIdent nor calling application could be found from token. Setting 'unknown'.") - } + val callingApplication = try { + tokenUtil.getCallingApplication() + } catch (e: Exception) { + logger.warn("Failed to get calling application from token.", e) + null + } - navIdentFromToken ?: callingApplication ?: "unknown" + if (navIdentFromToken != null || callingApplication != null) { + actor = navIdentFromToken ?: callingApplication } val traceId = try { @@ -70,7 +69,7 @@ class OurRevisionListener( } revisionEntity.request = request - revisionEntity.actor = actor + revisionEntity.actor = actor ?: "unknown" revisionEntity.traceId = traceId } } \ No newline at end of file