Skip to content

Commit

Permalink
Merge pull request #1345 from navikt/improve_audit_values
Browse files Browse the repository at this point in the history
Improve the revisions with more data.
  • Loading branch information
flexable777 authored Feb 18, 2025
2 parents e09922b + d2aa532 commit dafa01f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,50 @@ class OurRevisionListener(
override fun newRevision(revisionEntity: Any?) {
revisionEntity as OurRevision

try {
var actor: String? = null

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
//no exception occurred, we just don't have a request
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 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) {
actor = navIdentFromToken ?: callingApplication
}

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 ?: "unknown"
revisionEntity.traceId = traceId
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/no/nav/klage/oppgave/util/TokenUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit dafa01f

Please sign in to comment.