Skip to content

Commit dafa01f

Browse files
authored
Merge pull request #1345 from navikt/improve_audit_values
Improve the revisions with more data.
2 parents e09922b + d2aa532 commit dafa01f

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

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

+38-7
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,50 @@ class OurRevisionListener(
2626
override fun newRevision(revisionEntity: Any?) {
2727
revisionEntity as OurRevision
2828

29-
try {
29+
var actor: String? = null
30+
31+
val request = try {
3032
val requestAttributes = RequestContextHolder.getRequestAttributes()
3133
if (requestAttributes != null) {
3234
val request = (requestAttributes as ServletRequestAttributes).request
33-
revisionEntity.actor = tokenUtil.getIdent()
34-
revisionEntity.request = request.method + " " + request.requestURI
35+
request.method + " " + request.requestURI
3536
} else {
36-
revisionEntity.actor = systembrukerIdent
37+
//no exception occurred, we just don't have a request
38+
actor = systembrukerIdent
39+
null
3740
}
38-
revisionEntity.traceId = Span.current().spanContext.traceId
3941
} catch (e: Exception) {
40-
logger.warn("Failed to set correct actor and/or request on revision entity. Setting 'unknown'.", e)
41-
revisionEntity.actor = "unknown"
42+
logger.debug("No request found to set on revision entity. Setting to null.", e)
43+
null
44+
}
45+
46+
val navIdentFromToken = try {
47+
tokenUtil.getIdent()
48+
} catch (e: Exception) {
49+
logger.debug("No NAVIdent found in token.", e)
50+
null
51+
}
52+
53+
val callingApplication = try {
54+
tokenUtil.getCallingApplication()
55+
} catch (e: Exception) {
56+
logger.warn("Failed to get calling application from token.", e)
57+
null
4258
}
59+
60+
if (navIdentFromToken != null || callingApplication != null) {
61+
actor = navIdentFromToken ?: callingApplication
62+
}
63+
64+
val traceId = try {
65+
Span.current().spanContext.traceId
66+
} catch (e: Exception) {
67+
logger.warn("Failed to set traceId on revision entity. Setting 'unknown'.", e)
68+
"unknown"
69+
}
70+
71+
revisionEntity.request = request
72+
revisionEntity.actor = actor ?: "unknown"
73+
revisionEntity.traceId = traceId
4374
}
4475
}

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

+5
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)