Skip to content

Commit

Permalink
WIP - tilpasse ny R&R
Browse files Browse the repository at this point in the history
  • Loading branch information
geiralund committed Nov 20, 2024
1 parent 8563fe2 commit a8cc2bd
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package no.nav.dagpenger.behandling

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

val objectMapper =
val objectMapper: ObjectMapper =
jacksonObjectMapper()
.registerModule(JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package no.nav.dagpenger.behandling.mediator

import com.github.navikt.tbd_libs.rapids_and_rivers.KafkaRapid
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.plugins.statuspages.StatusPagesConfig
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import mu.KotlinLogging
import no.nav.dagpenger.behandling.api.models.HttpProblemDTO
import no.nav.dagpenger.behandling.db.PostgresDataSourceBuilder.clean
import no.nav.dagpenger.behandling.db.PostgresDataSourceBuilder.runMigration
import no.nav.dagpenger.behandling.konfigurasjon.Configuration.config
Expand All @@ -19,9 +25,12 @@ import no.nav.dagpenger.behandling.mediator.repository.AvklaringRepositoryPostgr
import no.nav.dagpenger.behandling.mediator.repository.BehandlingRepositoryPostgres
import no.nav.dagpenger.behandling.mediator.repository.OpplysningerRepositoryPostgres
import no.nav.dagpenger.behandling.mediator.repository.PersonRepositoryPostgres
import no.nav.dagpenger.behandling.objectMapper
import no.nav.dagpenger.opplysning.Opplysningstype
import no.nav.dagpenger.regel.RegelverkDagpenger
import no.nav.helse.rapids_rivers.RapidApplication
import org.apache.kafka.common.errors.ResourceNotFoundException
import java.net.URI

internal class ApplicationBuilder(
config: Map<String, String>,
Expand All @@ -34,7 +43,9 @@ internal class ApplicationBuilder(
private val opplysningstyper: Set<Opplysningstype<*>> = RegelverkDagpenger.produserer

private val rapidsConnection: RapidsConnection =
RapidApplication.create(config) { engine, rapidsConnection: KafkaRapid ->
RapidApplication.create(env = config, objectMapper = objectMapper, builder = {
withStatusPagesConfig(statusPages())
}) { engine, rapidsConnection: KafkaRapid ->
val aktivitetsloggMediator = AktivitetsloggMediator()

// Logger bare oppgaver enn så lenge. Bør inn i HendelseMediator
Expand Down Expand Up @@ -67,6 +78,43 @@ internal class ApplicationBuilder(
)
}

private fun statusPages(): StatusPagesConfig.() -> Unit =
{
exception<IllegalArgumentException> { call: ApplicationCall, cause: IllegalArgumentException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Ugyldig forespørsel",
status = HttpStatusCode.BadRequest.value,
detail = cause.message ?: "Ugyldig forespørsel",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.BadRequest, problem)
}
exception<ResourceNotFoundException> { call: ApplicationCall, cause: ResourceNotFoundException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Fant ikke ressurs",
status = HttpStatusCode.NotFound.value,
detail = cause.message ?: "Fant ikke ressurs",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.NotFound, problem)
}
exception<Throwable> { call: ApplicationCall, cause: Throwable ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Noe gikk galt",
status = HttpStatusCode.InternalServerError.value,
detail = cause.message ?: "Noe gikk galt",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.InternalServerError, problem)
}
}

init {
rapidsConnection.register(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,18 @@
package no.nav.dagpenger.behandling.mediator.api

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
import io.ktor.server.auth.AuthenticationConfig
import io.ktor.server.auth.jwt.jwt
import io.ktor.server.plugins.callloging.CallLogging
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.request.path
import io.ktor.server.request.uri
import io.ktor.server.response.respond
import mu.KotlinLogging
import no.nav.dagpenger.behandling.api.models.HttpProblemDTO
import no.nav.dagpenger.behandling.mediator.api.auth.AuthFactory.azureAd
import org.apache.kafka.common.errors.ResourceNotFoundException
import org.slf4j.event.Level
import java.net.URI

private val logger = KotlinLogging.logger {}

internal fun Application.konfigurerApi(
internal fun Application.authenticationConfig(
auth: AuthenticationConfig.() -> Unit = {
jwt("azureAd") { azureAd() }
},
) {
install(ContentNegotiation) {
jackson {
registerModule(JavaTimeModule())
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}
install(CallLogging) {
disableDefaultColors()
filter {
it.request.path() !in setOf("/metrics", "/isalive", "/isready")
}
level = Level.INFO
}
install(Authentication) {
auth()
}
install(StatusPages) {
exception<IllegalArgumentException> { call: ApplicationCall, cause: IllegalArgumentException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Ugyldig forespørsel",
status = HttpStatusCode.BadRequest.value,
detail = cause.message ?: "Ugyldig forespørsel",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.BadRequest, problem)
}
exception<ResourceNotFoundException> { call: ApplicationCall, cause: ResourceNotFoundException ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Fant ikke ressurs",
status = HttpStatusCode.NotFound.value,
detail = cause.message ?: "Fant ikke ressurs",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.NotFound, problem)
}
exception<Throwable> { call: ApplicationCall, cause: Throwable ->
val problem =
HttpProblemDTO(
type = URI.create(call.request.uri),
title = "Noe gikk galt",
status = HttpStatusCode.InternalServerError.value,
detail = cause.message ?: "Noe gikk galt",
)
logger.error(cause) { "Noe gikk galt på ${call.request.uri}" }
call.respond(HttpStatusCode.InternalServerError, problem)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.application.createApplicationPlugin
import io.ktor.server.application.install
import io.ktor.server.auth.authenticate
Expand Down Expand Up @@ -61,7 +60,7 @@ internal fun Application.behandlingApi(
opplysningstyper: Set<Opplysningstype<*>>,
messageContext: (ident: String) -> MessageContext,
) {
konfigurerApi()
authenticationConfig()
install(OtelTraceIdPlugin)

routing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package no.nav.dagpenger.behandling.mediator.api.auth

import io.ktor.http.auth.HttpAuthHeader
import io.ktor.server.application.ApplicationCall
import io.ktor.server.auth.Principal
import io.ktor.server.auth.authentication
import io.ktor.server.auth.jwt.JWTCredential
import io.ktor.server.auth.jwt.JWTPayloadHolder
Expand All @@ -11,7 +10,7 @@ import io.ktor.server.auth.parseAuthorizationHeader
import io.ktor.server.request.ApplicationRequest
import no.nav.dagpenger.behandling.mediator.api.auth.AuthFactory.issuerFromString

internal fun validator(jwtCredential: JWTCredential): Principal {
internal fun validator(jwtCredential: JWTCredential): JWTPayloadHolder {
requirePid(jwtCredential)
return JWTPrincipal(jwtCredential.payload)
}
Expand Down Expand Up @@ -40,4 +39,10 @@ internal fun ApplicationRequest.jwt(): String =
internal fun ApplicationCall.optionalIdent(): String? =
requireNotNull(this.authentication.principal<JWTPrincipal>()).payload.claims["pid"]?.asString()

internal fun ApplicationCall.issuer() = issuerFromString(this.authentication.principal<JWTPrincipal>()?.payload?.issuer)
internal fun ApplicationCall.issuer() =
issuerFromString(
this.authentication
.principal<JWTPrincipal>()
?.payload
?.issuer,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import io.opentelemetry.instrumentation.annotations.WithSpan
import kotliquery.queryOf
import kotliquery.sessionOf
Expand Down Expand Up @@ -40,6 +42,8 @@ internal class ArenaOppgaveMottak(
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
val sakId = packet["after.SAK_ID"].toString()
withLoggingContext("sakId" to sakId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package no.nav.dagpenger.behandling.mediator.mottak
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageProblems
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import io.opentelemetry.api.trace.Span
import io.opentelemetry.instrumentation.annotations.WithSpan
import mu.KotlinLogging
Expand Down Expand Up @@ -33,6 +35,8 @@ internal class AvbrytBehandlingMottak(
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
val behandlingId = packet["behandlingId"].asUUID()
Span.current().apply {
Expand All @@ -51,6 +55,7 @@ internal class AvbrytBehandlingMottak(
override fun onError(
problems: MessageProblems,
context: MessageContext,
metadata: MessageMetadata,
) {
logger.error { problems }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package no.nav.dagpenger.behandling.mediator.mottak
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import mu.KotlinLogging
import mu.withLoggingContext
import no.nav.dagpenger.behandling.mediator.IMessageMediator
Expand Down Expand Up @@ -33,6 +35,8 @@ internal class AvklaringIkkeRelevantMottak(
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
withLoggingContext(
"behovId" to packet["@behovId"].asText(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package no.nav.dagpenger.behandling.mediator.mottak
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageProblems
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import io.opentelemetry.api.trace.Span
import io.opentelemetry.instrumentation.annotations.WithSpan
import mu.KotlinLogging
Expand Down Expand Up @@ -33,6 +35,8 @@ internal class GodkjennBehandlingMottak(
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
val behandlingId = packet["behandlingId"].asUUID()
Span.current().apply {
Expand All @@ -51,6 +55,7 @@ internal class GodkjennBehandlingMottak(
override fun onError(
problems: MessageProblems,
context: MessageContext,
metadata: MessageMetadata,
) {
logger.error { problems }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers.asLocalDateTime
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import io.opentelemetry.instrumentation.annotations.WithSpan
import mu.KotlinLogging
import mu.withLoggingContext
Expand All @@ -30,10 +32,17 @@ internal class InnsendingFerdigstiltMottak(
}.register(this)
}

private companion object {
private val logger = KotlinLogging.logger {}
private val sikkerlogg = KotlinLogging.logger("tjenestekall.SøknadInnsendtMottak")
}

@WithSpan
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
val søknadId = packet["søknadsData"]["søknad_uuid"].asUUID().toString()
withLoggingContext("søknadId" to søknadId) {
Expand All @@ -42,11 +51,6 @@ internal class InnsendingFerdigstiltMottak(
message.publish(context)
}
}

private companion object {
private val logger = KotlinLogging.logger {}
private val sikkerlogg = KotlinLogging.logger("tjenestekall.SøknadInnsendtMottak")
}
}

class InnsendingFerdigstiltMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package no.nav.dagpenger.behandling.mediator.mottak
import com.github.navikt.tbd_libs.rapids_and_rivers.JsonMessage
import com.github.navikt.tbd_libs.rapids_and_rivers.River
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageContext
import com.github.navikt.tbd_libs.rapids_and_rivers_api.MessageMetadata
import com.github.navikt.tbd_libs.rapids_and_rivers_api.RapidsConnection
import io.micrometer.core.instrument.MeterRegistry
import io.opentelemetry.api.trace.Span
import io.opentelemetry.instrumentation.annotations.WithSpan
import mu.withLoggingContext
Expand Down Expand Up @@ -31,6 +33,8 @@ internal class OppgaveReturnertTilSaksbehandler(
override fun onPacket(
packet: JsonMessage,
context: MessageContext,
metadata: MessageMetadata,
meterRegistry: MeterRegistry,
) {
val behandlingId = packet["behandlingId"].asUUID()
Span.current().apply {
Expand Down
Loading

0 comments on commit a8cc2bd

Please sign in to comment.