From 293cbd230881a3eb641203e83a0772fa347198fe Mon Sep 17 00:00:00 2001
From: Paul McPhee <paul.mcphee@digital.justice.gov.uk>
Date: Mon, 5 Aug 2024 11:59:10 +0100
Subject: [PATCH] PI-2377: Get client name and set to clientId

---
 .../telemetry/ClientTrackingInterceptor.kt    | 36 +++++++++++++++++++
 .../ClientTrackingInterceptorTest.kt          | 21 +++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptor.kt
 create mode 100644 src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptorTest.kt

diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptor.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptor.kt
new file mode 100644
index 000000000..d835a9e11
--- /dev/null
+++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptor.kt
@@ -0,0 +1,36 @@
+package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry
+
+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.stereotype.Component
+import org.springframework.web.servlet.HandlerInterceptor
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
+
+@Configuration
+class ClientTrackingConfiguration(private val clientTrackingInterceptor: ClientTrackingInterceptor) : WebMvcConfigurer {
+  override fun addInterceptors(registry: InterceptorRegistry) {
+    registry.addInterceptor(clientTrackingInterceptor).addPathPatterns("/**")
+  }
+}
+
+@Component
+class ClientTrackingInterceptor : HandlerInterceptor {
+  override fun preHandle(
+    request: HttpServletRequest,
+    response: HttpServletResponse,
+    handler: Any,
+  ): Boolean {
+    val subjectDistinguishedName = request.getAttribute("clientName") as String?
+    subjectDistinguishedName?.let {
+      try {
+        Span.current().setAttribute("clientId", it)
+      } catch (ignored: Exception) {
+        // Do nothing - don't create client id span
+      }
+    }
+    return true
+  }
+}
diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptorTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptorTest.kt
new file mode 100644
index 000000000..b8c9e12db
--- /dev/null
+++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/config/telemetry/ClientTrackingInterceptorTest.kt
@@ -0,0 +1,21 @@
+package uk.gov.justice.digital.hmpps.hmppsintegrationapi.config.telemetry
+
+import jakarta.servlet.http.HttpServletRequest
+import jakarta.servlet.http.HttpServletResponse
+import org.junit.jupiter.api.Assertions.assertTrue
+import org.junit.jupiter.api.Test
+import org.mockito.Mockito.mock
+import org.mockito.kotlin.whenever
+
+class ClientTrackingInterceptorTest {
+  private val request: HttpServletRequest = mock(HttpServletRequest::class.java)
+  private val response: HttpServletResponse = mock(HttpServletResponse::class.java)
+  private val interceptor = ClientTrackingInterceptor()
+
+  @Test
+  fun `handles missing clientName attribute`() {
+    whenever(request.getAttribute("clientName")).thenReturn(null)
+    val result = interceptor.preHandle(request, response, "")
+    assertTrue(result)
+  }
+}