Skip to content

Commit

Permalink
Bumper aap-libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Borge committed Feb 9, 2024
1 parent 5fb6cec commit 5dd7d4e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 105 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ application {
mainClass.set("api.AppKt")
}

val aapLibVersion = "3.7.161"
val aapLibVersion = "4.0.0"
val ktorVersion = "2.3.8"

dependencies {
implementation("com.github.navikt.aap-libs:kafka-2:$aapLibVersion")
implementation("com.github.navikt.aap-libs:ktor-utils:$aapLibVersion")
implementation("com.github.navikt.aap-libs:ktor-auth-azuread:$aapLibVersion")
implementation("com.github.navikt.aap-libs:ktor-auth:$aapLibVersion")

implementation("io.ktor:ktor-serialization-jackson:$ktorVersion")

Expand Down
6 changes: 2 additions & 4 deletions app/main/api/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package api

import api.afp.fellesordningen.afp
import api.afp.afp
import api.arena.ArenaoppslagRestClient
import api.auth.MASKINPORTEN_AFP_OFFENTLIG
import api.auth.MASKINPORTEN_AFP_PRIVAT
Expand Down Expand Up @@ -37,6 +37,7 @@ fun Application.api() {
val config = Config()
val prometheus = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)
val sporingsloggKafkaClient = SporingsloggKafkaClient(config.kafka, config.sporingslogg)
val arenaRestClient = ArenaoppslagRestClient(config.arenaoppslag, config.azure)

install(CallLogging) {
logging()
Expand Down Expand Up @@ -67,13 +68,10 @@ fun Application.api() {
allowHeader(HttpHeaders.ContentType)
}

val arenaRestClient = ArenaoppslagRestClient(config.arenaoppslag, config.azure)

routing {
actuator(prometheus)
swaggerUI(path = "swagger", swaggerFile = "openapi.yaml")

afp(config, arenaRestClient, sporingsloggKafkaClient, prometheus)

}
}
88 changes: 5 additions & 83 deletions app/main/api/afp/Routes.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
package api.afp.fellesordningen
package api.afp

import api.arena.ArenaoppslagRestClient
import api.auth.MASKINPORTEN_AFP_OFFENTLIG
import api.auth.MASKINPORTEN_AFP_PRIVAT
import api.sporingslogg.Spor
import api.sporingslogg.SporingsloggKafkaClient
import api.util.Config
import api.util.httpCallCounter
import api.util.httpFailedCallCounter
import api.util.sporingsloggFailCounter
import api.auth.hentConsumerId
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*

import io.ktor.server.routing.*
import io.micrometer.prometheus.PrometheusMeterRegistry
import org.slf4j.LoggerFactory
import java.lang.Exception
import java.util.*

private val secureLog = LoggerFactory.getLogger("secureLog")
private val logger = LoggerFactory.getLogger("App")
private const val consumerTag = "fellesordningen"

fun Route.afp(
config: Config,
Expand All @@ -37,6 +24,7 @@ fun Route.afp(
Afp.doWorkFellesOrdningen(call, config, arenaoppslagRestClient, sporingsloggClient, prometheus)
}
}

authenticate(MASKINPORTEN_AFP_OFFENTLIG) {
post("/offentlig") {
Afp.doWorkOffentlig(
Expand All @@ -46,80 +34,14 @@ fun Route.afp(
sporingsloggClient,
prometheus,
orgnr = call.hentConsumerId()
) //TODO: hent orgnr fra token
)
}
}
}

authenticate(MASKINPORTEN_AFP_PRIVAT) {
post("/fellesordning-for-afp") {
Afp.doWorkFellesOrdningen(call, config, arenaoppslagRestClient, sporingsloggClient, prometheus)
}
}
}

object Afp {
private const val AFP_FELLERORDNINGEN_ORGNR = "987414502"

suspend fun doWorkOffentlig(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry,
orgnr: String
) {
doWork(
call, config, arenaoppslagRestClient, sporingsloggClient, prometheus, orgnr
)
}

suspend fun doWorkFellesOrdningen(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry
) {
doWork(
call, config, arenaoppslagRestClient, sporingsloggClient, prometheus, AFP_FELLERORDNINGEN_ORGNR
)
}


private suspend fun doWork(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry,
orgnr: String
) {
prometheus.httpCallCounter(consumerTag, call.request.path()).increment()
val body = call.receive<VedtakRequest>()
val callId = requireNotNull(call.request.header("x-callid")) { "x-callid ikke satt" }
runCatching {
arenaoppslagRestClient.hentVedtakFellesordning(UUID.fromString(callId), body)
}.onFailure { ex ->
prometheus.httpFailedCallCounter(consumerTag, call.request.path()).increment()
secureLog.error("Klarte ikke hente vedtak fra Arena", ex)
throw ex
}.onSuccess { res ->
if (config.sporingslogg.enabled) {
try {
sporingsloggClient.send(Spor.opprett(body.personidentifikator, res, orgnr))
call.respond(res)
} catch (e: Exception) {
prometheus.sporingsloggFailCounter(consumerTag).increment()
secureLog.error("Klarte ikke produsere til kafka sporingslogg og kan derfor ikke returnere data", e)
call.respond(
HttpStatusCode.ServiceUnavailable,
"Feilet sporing av oppslag, kan derfor ikke returnere data. Feilen er på vår side, prøv igjen senere."
)
}
} else {
logger.warn("Sporingslogg er skrudd av, returnerer data uten å sende til kafka")
call.respond(res)
}
}
}
}
2 changes: 1 addition & 1 deletion app/main/api/afp/VedtakRequest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api.afp.fellesordningen
package api.afp

import java.time.LocalDate

Expand Down
2 changes: 1 addition & 1 deletion app/main/api/afp/VedtakResponse.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api.afp.fellesordningen
package api.afp

import java.time.LocalDate

Expand Down
93 changes: 93 additions & 0 deletions app/main/api/afp/Worker.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package api.afp

import api.arena.ArenaoppslagRestClient
import api.sporingslogg.Spor
import api.sporingslogg.SporingsloggKafkaClient
import api.util.Config
import api.util.httpCallCounter
import api.util.httpFailedCallCounter
import api.util.sporingsloggFailCounter
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.micrometer.prometheus.PrometheusMeterRegistry
import org.slf4j.LoggerFactory
import java.lang.Exception
import java.util.*

private val secureLog = LoggerFactory.getLogger("secureLog")
private val logger = LoggerFactory.getLogger("App")
private const val AFP_FELLERORDNINGEN_ORGNR = "987414502"

private val consumerTags = mapOf(
AFP_FELLERORDNINGEN_ORGNR to "fellesordningen"
)

object Afp {
private fun getConsumerTag(orgnr: String) = consumerTags[orgnr] ?: orgnr

suspend fun doWorkOffentlig(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry,
orgnr: String
) {
doWork(
call, config, arenaoppslagRestClient, sporingsloggClient, prometheus, orgnr
)
}

suspend fun doWorkFellesOrdningen(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry
) {
doWork(
call, config, arenaoppslagRestClient, sporingsloggClient, prometheus, AFP_FELLERORDNINGEN_ORGNR
)
}

private suspend fun doWork(
call: ApplicationCall,
config: Config,
arenaoppslagRestClient: ArenaoppslagRestClient,
sporingsloggClient: SporingsloggKafkaClient,
prometheus: PrometheusMeterRegistry,
orgnr: String
) {
val consumerTag = getConsumerTag(orgnr)

prometheus.httpCallCounter(consumerTag, call.request.path()).increment()
val body = call.receive<VedtakRequest>()
val callId = requireNotNull(call.request.header("x-callid")) { "x-callid ikke satt" }
runCatching {
arenaoppslagRestClient.hentVedtakFellesordning(UUID.fromString(callId), body)
}.onFailure { ex ->
prometheus.httpFailedCallCounter(consumerTag, call.request.path()).increment()
secureLog.error("Klarte ikke hente vedtak fra Arena", ex)
throw ex
}.onSuccess { res ->
if (config.sporingslogg.enabled) {
try {
sporingsloggClient.send(Spor.opprett(body.personidentifikator, res, orgnr))
call.respond(res)
} catch (e: Exception) {
prometheus.sporingsloggFailCounter(consumerTag).increment()
secureLog.error("Klarte ikke produsere til kafka sporingslogg og kan derfor ikke returnere data", e)
call.respond(
HttpStatusCode.ServiceUnavailable,
"Feilet sporing av oppslag, kan derfor ikke returnere data. Feilen er på vår side, prøv igjen senere."
)
}
} else {
logger.warn("Sporingslogg er skrudd av, returnerer data uten å sende til kafka")
call.respond(res)
}
}
}
}
20 changes: 8 additions & 12 deletions app/main/api/arena/ArenaoppslagRestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api.arena

import api.dsop.DsopRequest
import api.util.ArenaoppslagConfig
import api.afp.fellesordningen.VedtakRequest
import api.afp.fellesordningen.VedtakResponse
import api.afp.VedtakRequest
import api.afp.VedtakResponse
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
Expand All @@ -20,12 +20,11 @@ import io.ktor.http.*
import io.ktor.serialization.jackson.*
import io.prometheus.client.Summary
import kotlinx.coroutines.runBlocking
import no.nav.aap.ktor.client.AzureAdTokenProvider
import no.nav.aap.ktor.client.AzureConfig
import no.nav.aap.ktor.client.auth.azure.AzureAdTokenProvider
import no.nav.aap.ktor.client.auth.azure.AzureConfig
import org.slf4j.LoggerFactory
import java.util.*


private const val ARENAOPPSLAG_CLIENT_SECONDS_METRICNAME = "arenaoppslag_client_seconds"
private val sikkerLogg = LoggerFactory.getLogger("secureLog")
private val clientLatencyStats: Summary = Summary.build()
Expand All @@ -39,20 +38,19 @@ private val objectMapper = jacksonObjectMapper()
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.registerModule(JavaTimeModule())


class ArenaoppslagRestClient(
private val arenaoppslagConfig: ArenaoppslagConfig,
azureConfig: AzureConfig
) {
private val tokenProvider = AzureAdTokenProvider(azureConfig, arenaoppslagConfig.scope)
private val tokenProvider = AzureAdTokenProvider(azureConfig)

fun hentVedtakFellesordning(callId: UUID, vedtakRequest: VedtakRequest): VedtakResponse =
clientLatencyStats.startTimer().use {
runBlocking {
httpClient.post("${arenaoppslagConfig.proxyBaseUrl}/fellesordningen/vedtak"){
accept(ContentType.Application.Json)
header("Nav-Call-Id", callId)
bearerAuth(tokenProvider.getClientCredentialToken())
bearerAuth(tokenProvider.getClientCredentialToken(arenaoppslagConfig.scope))
contentType(ContentType.Application.Json)
setBody(vedtakRequest)
}
Expand All @@ -68,7 +66,7 @@ class ArenaoppslagRestClient(
httpClient.post("${arenaoppslagConfig.proxyBaseUrl}/dsop/vedtak"){
accept(ContentType.Application.Json)
header("Nav-Call-Id", callId)
bearerAuth(tokenProvider.getClientCredentialToken())
bearerAuth(tokenProvider.getClientCredentialToken(arenaoppslagConfig.scope))
contentType(ContentType.Application.Json)
setBody(dsopRequest)
}
Expand All @@ -84,7 +82,7 @@ class ArenaoppslagRestClient(
httpClient.post("${arenaoppslagConfig.proxyBaseUrl}/dsop/meldeplikt"){
accept(ContentType.Application.Json)
header("Nav-Call-Id", callId)
bearerAuth(tokenProvider.getClientCredentialToken())
bearerAuth(tokenProvider.getClientCredentialToken(arenaoppslagConfig.scope))
contentType(ContentType.Application.Json)
setBody(dsopRequest)
}
Expand All @@ -94,8 +92,6 @@ class ArenaoppslagRestClient(
}
}



private val httpClient = HttpClient(CIO) {
install(HttpTimeout)
install(HttpRequestRetry)
Expand Down
4 changes: 2 additions & 2 deletions app/main/api/util/Config.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package api.util

import no.nav.aap.ktor.client.AzureConfig
import no.nav.aap.ktor.client.auth.azure.AzureConfig
import java.net.URI
import java.net.URL

Expand All @@ -10,7 +10,7 @@ data class Config(
val oauth: OauthConfig = OauthConfig(),
val arenaoppslag: ArenaoppslagConfig = ArenaoppslagConfig(),
val azure: AzureConfig = AzureConfig(
tokenEndpoint = URI(getEnvVar("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT")).toURL(),
tokenEndpoint = getEnvVar("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT"),
clientId = getEnvVar("AZURE_APP_CLIENT_ID"),
clientSecret = getEnvVar("AZURE_APP_CLIENT_SECRET")
),
Expand Down

0 comments on commit 5dd7d4e

Please sign in to comment.