From 02580b17fc5614966d950153b5611863e69ef573 Mon Sep 17 00:00:00 2001 From: Anthony Britton Date: Mon, 30 Oct 2023 16:00:55 +0000 Subject: [PATCH] PI-1597 --- libs/commons/build.gradle.kts | 2 +- libs/messaging/build.gradle.kts | 1 - .../telemetry/ClientTrackingInterceptor.kt | 29 +++++++++++++++++++ .../domain-events-and-delius/build.gradle.kts | 1 - 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 libs/oauth-server/src/main/kotlin/uk/gov/justice/digital/hmpps/config/telemetry/ClientTrackingInterceptor.kt diff --git a/libs/commons/build.gradle.kts b/libs/commons/build.gradle.kts index ef67056a62..b191446d7a 100644 --- a/libs/commons/build.gradle.kts +++ b/libs/commons/build.gradle.kts @@ -1,11 +1,11 @@ import uk.gov.justice.digital.hmpps.extensions.ClassPathExtension dependencies { + api(libs.bundles.telemetry) compileOnly("org.springframework.boot:spring-boot-starter-data-ldap") implementation("org.springframework.boot:spring-boot-starter-security") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation(libs.bundles.telemetry) implementation(libs.flipt) testImplementation("org.springframework.boot:spring-boot-starter-data-ldap") diff --git a/libs/messaging/build.gradle.kts b/libs/messaging/build.gradle.kts index a92cd0a765..4d7cefde61 100644 --- a/libs/messaging/build.gradle.kts +++ b/libs/messaging/build.gradle.kts @@ -4,7 +4,6 @@ dependencies { implementation(project(":libs:commons")) implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation(libs.bundles.telemetry) compileOnly(libs.openfeign) compileOnly("org.springframework.boot:spring-boot-starter-data-jpa") diff --git a/libs/oauth-server/src/main/kotlin/uk/gov/justice/digital/hmpps/config/telemetry/ClientTrackingInterceptor.kt b/libs/oauth-server/src/main/kotlin/uk/gov/justice/digital/hmpps/config/telemetry/ClientTrackingInterceptor.kt new file mode 100644 index 0000000000..29f6e53e08 --- /dev/null +++ b/libs/oauth-server/src/main/kotlin/uk/gov/justice/digital/hmpps/config/telemetry/ClientTrackingInterceptor.kt @@ -0,0 +1,29 @@ +package uk.gov.justice.digital.hmpps.config.telemetry + +import com.nimbusds.jwt.SignedJWT +import io.opentelemetry.api.trace.Span +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.springframework.context.annotation.Configuration +import org.springframework.http.HttpHeaders +import org.springframework.web.servlet.HandlerInterceptor + +@Configuration +class ClientTrackingInterceptor : HandlerInterceptor { + override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { + val token = request.getHeader(HttpHeaders.AUTHORIZATION) + if (token.startsWith(BEARER)) { + try { + val jwtBody = SignedJWT.parse(token.replace(BEARER, "")).jwtClaimsSet + Span.current().setAttribute("clientId", jwtBody.getClaim("client_id").toString()) + } catch (ignored: Exception) { + // Do nothing - don't create client id span + } + } + return true + } + + companion object { + private const val BEARER = "Bearer " + } +} diff --git a/projects/domain-events-and-delius/build.gradle.kts b/projects/domain-events-and-delius/build.gradle.kts index 75baff2a70..7956ce8bae 100644 --- a/projects/domain-events-and-delius/build.gradle.kts +++ b/projects/domain-events-and-delius/build.gradle.kts @@ -16,7 +16,6 @@ dependencies { implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation(libs.bundles.telemetry) dev(project(":libs:dev-tools")) dev("com.h2database:h2")