Skip to content

Commit aba3fc6

Browse files
committed
Improve the revisions with more data.
1 parent d9dad08 commit aba3fc6

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/main/kotlin/no/nav/klage/oppgave/eventlisteners/OurRevisionListener.kt

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,51 @@ class OurRevisionListener(
2626
override fun newRevision(revisionEntity: Any?) {
2727
revisionEntity as OurRevision
2828

29-
try {
29+
val request = try {
3030
val requestAttributes = RequestContextHolder.getRequestAttributes()
3131
if (requestAttributes != null) {
3232
val request = (requestAttributes as ServletRequestAttributes).request
33-
revisionEntity.actor = tokenUtil.getIdent()
34-
revisionEntity.request = request.method + " " + request.requestURI
33+
request.method + " " + request.requestURI
3534
} else {
36-
revisionEntity.actor = systembrukerIdent
35+
null
3736
}
38-
revisionEntity.traceId = Span.current().spanContext.traceId
3937
} catch (e: Exception) {
40-
logger.warn("Failed to set correct actor and/or request on revision entity. Setting 'unknown'.", e)
41-
revisionEntity.actor = "unknown"
38+
logger.debug("No request found to set on revision entity. Setting to null.", e)
39+
null
4240
}
41+
42+
val actor = if (request == null) {
43+
systembrukerIdent
44+
} else {
45+
val navIdentFromToken = try {
46+
tokenUtil.getIdent()
47+
} catch (e: Exception) {
48+
logger.debug("No NAVIdent found in token.", e)
49+
null
50+
}
51+
val callingApplication = try {
52+
tokenUtil.getCallingApplication()
53+
} catch (e: Exception) {
54+
logger.warn("Failed to get calling application from token.", e)
55+
null
56+
}
57+
58+
if (navIdentFromToken == null && callingApplication == null) {
59+
logger.warn("Neither NAVIdent nor calling application could be found from token. Setting 'unknown'.")
60+
}
61+
62+
navIdentFromToken ?: callingApplication ?: "unknown"
63+
}
64+
65+
val traceId = try {
66+
Span.current().spanContext.traceId
67+
} catch (e: Exception) {
68+
logger.warn("Failed to set traceId on revision entity. Setting 'unknown'.", e)
69+
"unknown"
70+
}
71+
72+
revisionEntity.request = request
73+
revisionEntity.actor = actor
74+
revisionEntity.traceId = traceId
4375
}
4476
}

src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ class TokenUtil(
218218
tokenValidationContextHolder.getTokenValidationContext().getJwtToken(SecurityConfiguration.ISSUER_AAD)
219219
?.jwtTokenClaims?.get("NAVident")?.toString()
220220
?: throw RuntimeException("Ident not found in token")
221+
222+
fun getCallingApplication(): String =
223+
tokenValidationContextHolder.getTokenValidationContext().getJwtToken(SecurityConfiguration.ISSUER_AAD)
224+
?.jwtTokenClaims?.get("azp_name")?.toString()
225+
?: throw RuntimeException("Application not found in token")
221226
}

0 commit comments

Comments
 (0)