Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggle på å gå mot Saf direkte enn via dp-proxy #149

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .nais/dev-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ oppgave:
app: oppgave-q1
ingress: oppgave-q1.dev-fss-pub.nais.io
scope: api://dev-fss.oppgavehandtering.oppgave-q1/.default
saf:
app: saf-q1
ingress: saf.dev-fss-pub.nais.io
scope: api://dev-fss.oppgavehandtering.saf/.default

6 changes: 6 additions & 0 deletions .nais/nais.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ spec:
value: {{oppgave.scope}}
- name: OPPGAVE_URL
value: {{oppgave.ingress}}
- name: SAF_URL
value: {{saf.ingress}}
- name: SAF_SCOPE
value: {{saf.scope}}
envFrom:
- secret: dp-mottak-unleash-api-token
accessPolicy:
Expand All @@ -60,6 +64,8 @@ spec:
namespace: nom
- application: {{ oppgave.app }}
namespace: oppgavehandtering
- application: {{ saf.app }}
namespace: teamdokumenthandtering
external:
- host: {{dp_proxy.ingress}}
- host: {{pdl.ingress}}
Expand Down
6 changes: 5 additions & 1 deletion .nais/prod-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ dokarkiv:
oppgave:
app: oppgave
ingress: oppgave.prod-fss-pub.nais.io
scope: api://prod-fss.oppgavehandtering.oppgave/.default
scope: api://prod-fss.oppgavehandtering.oppgave/.default
saf:
app: saf
ingress: saf.prod-fss-pub.nais.io
scope: api://prod-fss.oppgavehandtering.saf/.default
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import no.nav.dagpenger.mottak.behov.journalpost.FerdigstillJournalpostBehovLøs
import no.nav.dagpenger.mottak.behov.journalpost.JournalpostApiClient
import no.nav.dagpenger.mottak.behov.journalpost.JournalpostBehovLøser
import no.nav.dagpenger.mottak.behov.journalpost.OppdaterJournalpostBehovLøser
import no.nav.dagpenger.mottak.behov.journalpost.SafClient
import no.nav.dagpenger.mottak.behov.journalpost.SøknadsdataBehovLøser
import no.nav.dagpenger.mottak.behov.journalpost.UnleashSafClient
import no.nav.dagpenger.mottak.behov.person.PdlPersondataOppslag
import no.nav.dagpenger.mottak.behov.person.PersondataBehovLøser
import no.nav.dagpenger.mottak.behov.person.SkjermingOppslag
Expand All @@ -32,7 +32,7 @@ private val logg = KotlinLogging.logger {}

internal class ApplicationBuilder(env: Map<String, String>) : RapidsConnection.StatusListener {
private val innsendingRepository = InnsendingPostgresRepository(PostgresDataSourceBuilder.dataSource)
private val safClient = SafClient(Config.properties)
private val safClient = UnleashSafClient(Config.properties, unleash)
private val arenaApiClient = ArenaApiClient(Config.properties)
private val journalpostApiClient = JournalpostApiClient(tokenProvider = Config.properties.dokarkivTokenProvider)
private val gosysOppslag = GosysClient(Config.properties)
Expand Down
8 changes: 8 additions & 0 deletions mediator/src/main/kotlin/no/nav/dagpenger/mottak/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ internal object Config {
}
}

val Configuration.safTokenProvider: () -> String by lazy {
{
cachedTokenProvider.clientCredentials(properties[Key("SAF_SCOPE", stringType)]).accessToken
}
}

val Configuration.pdlApiTokenProvider: () -> String by lazy {
{
cachedTokenProvider.clientCredentials(properties[Key("PDL_API_SCOPE", stringType)]).accessToken
Expand Down Expand Up @@ -127,6 +133,8 @@ internal object Config {

fun Configuration.gosysUrl(): String = this[Key("OPPGAVE_URL", stringType)].addHttpsrotocoll()

fun Configuration.safUrl(): String = this[Key("SAF_URL", stringType)].addHttpsrotocoll()

fun asMap(): Map<String, String> =
properties.list().reversed().fold(emptyMap()) { map, pair ->
map + pair.second
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package no.nav.dagpenger.mottak.behov.journalpost

import com.fasterxml.jackson.annotation.JsonIgnore
import no.nav.dagpenger.mottak.behov.GraphqlQuery

internal data class JournalPostQuery(
@JsonIgnore val journalpostId: String,
) : GraphqlQuery(
//language=Graphql
query =
"""
query(${'$'}journalpostId: String!) {
journalpost(journalpostId: ${'$'}journalpostId) {
journalstatus
journalpostId
journalfoerendeEnhet
datoOpprettet
behandlingstema
bruker {
type
id
}
relevanteDatoer {
dato
datotype
}
dokumenter {
tittel
dokumentInfoId
brevkode
}
}
}
""".trimIndent(),
variables =
mapOf(
"journalpostId" to journalpostId,
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.nav.dagpenger.mottak.behov.journalpost

internal interface JournalpostArkiv {
suspend fun hentJournalpost(journalpostId: String): SafGraphQL.Journalpost
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.nav.dagpenger.mottak.behov.journalpost

import com.fasterxml.jackson.annotation.JsonIgnore
import com.natpryce.konfig.Configuration
import io.ktor.client.HttpClient
import io.ktor.client.plugins.ClientRequestException
Expand All @@ -12,76 +11,29 @@ import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import mu.KotlinLogging
import no.nav.dagpenger.mottak.Config.dpProxyTokenProvider
import no.nav.dagpenger.mottak.Config.dpProxyUrl
import no.nav.dagpenger.mottak.behov.GraphqlQuery

internal interface JournalpostArkiv {
suspend fun hentJournalpost(journalpostId: String): SafGraphQL.Journalpost
}

internal interface SøknadsArkiv {
suspend fun hentSøknadsData(
journalpostId: String,
dokumentInfoId: String,
): SafGraphQL.SøknadsData
}

internal data class JournalPostQuery(
@JsonIgnore val journalpostId: String,
) : GraphqlQuery(
//language=Graphql
query =
"""
query(${'$'}journalpostId: String!) {
journalpost(journalpostId: ${'$'}journalpostId) {
journalstatus
journalpostId
journalfoerendeEnhet
datoOpprettet
behandlingstema
bruker {
type
id
}
relevanteDatoer {
dato
datotype
}
dokumenter {
tittel
dokumentInfoId
brevkode
}
}
}
""".trimIndent(),
variables =
mapOf(
"journalpostId" to journalpostId,
),
)
import no.nav.dagpenger.mottak.Config.safTokenProvider
import no.nav.dagpenger.mottak.Config.safUrl

internal class SafClient(config: Configuration) : JournalpostArkiv, SøknadsArkiv {
companion object {
private val logger = KotlinLogging.logger {}
}

private val tokenProvider = config.dpProxyTokenProvider
private val dpProxyUrl = config.dpProxyUrl()
private val tokenProvider = config.safTokenProvider
private val safUrl = config.safUrl()

private val proxyJoarkClient =
private val joarkClient =
HttpClient {
expectSuccess = true
}

private val proxySøknadsDataClient =
private val søknadsDataClient =
HttpClient {
expectSuccess = true
}

override suspend fun hentJournalpost(journalpostId: String): SafGraphQL.Journalpost =
proxyJoarkClient.request("$dpProxyUrl//proxy/v1/saf/graphql") {
joarkClient.request("$safUrl/graphql") {
header("Content-Type", "application/json")
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
method = HttpMethod.Post
Expand All @@ -95,7 +47,7 @@ internal class SafClient(config: Configuration) : JournalpostArkiv, SøknadsArki
dokumentInfoId: String,
): SafGraphQL.SøknadsData =
try {
proxySøknadsDataClient.request("$dpProxyUrl/proxy/v1/saf/rest/hentdokument/$journalpostId/$dokumentInfoId/ORIGINAL") {
søknadsDataClient.request("$safUrl/rest/hentdokument/$journalpostId/$dokumentInfoId/ORIGINAL") {
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
method = HttpMethod.Get
}.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package no.nav.dagpenger.mottak.behov.journalpost

import com.natpryce.konfig.Configuration
import io.ktor.client.HttpClient
import io.ktor.client.plugins.ClientRequestException
import io.ktor.client.request.header
import io.ktor.client.request.request
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import mu.KotlinLogging
import no.nav.dagpenger.mottak.Config.dpProxyTokenProvider
import no.nav.dagpenger.mottak.Config.dpProxyUrl

internal class SafProxyClient(config: Configuration) : JournalpostArkiv, SøknadsArkiv {
companion object {
private val logger = KotlinLogging.logger {}
}

private val tokenProvider = config.dpProxyTokenProvider
private val dpProxyUrl = config.dpProxyUrl()

private val proxyJoarkClient =
HttpClient {
expectSuccess = true
}

private val proxySøknadsDataClient =
HttpClient {
expectSuccess = true
}

override suspend fun hentJournalpost(journalpostId: String): SafGraphQL.Journalpost =
proxyJoarkClient.request("$dpProxyUrl//proxy/v1/saf/graphql") {
header("Content-Type", "application/json")
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
method = HttpMethod.Post
setBody(JournalPostQuery(journalpostId).toJson())
}.let {
SafGraphQL.Journalpost.fromGraphQlJson(it.bodyAsText())
}

override suspend fun hentSøknadsData(
journalpostId: String,
dokumentInfoId: String,
): SafGraphQL.SøknadsData =
try {
proxySøknadsDataClient.request("$dpProxyUrl/proxy/v1/saf/rest/hentdokument/$journalpostId/$dokumentInfoId/ORIGINAL") {
header(HttpHeaders.Authorization, "Bearer ${tokenProvider.invoke()}")
method = HttpMethod.Get
}.let {
SafGraphQL.SøknadsData.fromJson(it.bodyAsText())
}
} catch (exception: ClientRequestException) {
if (exception.response.status == HttpStatusCode.NotFound) {
logger.warn(exception) { "Fant ikke dokumentInfo for journalpostId $journalpostId med dokumentinfoId $dokumentInfoId" }
SafGraphQL.SøknadsData.fromJson("{}")
} else {
throw exception
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.nav.dagpenger.mottak.behov.journalpost

internal interface SøknadsArkiv {
suspend fun hentSøknadsData(
journalpostId: String,
dokumentInfoId: String,
): SafGraphQL.SøknadsData
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package no.nav.dagpenger.mottak.behov.journalpost

import com.natpryce.konfig.Configuration
import io.getunleash.Unleash

internal class UnleashSafClient(config: Configuration, private val unleash: Unleash) : JournalpostArkiv, SøknadsArkiv {
private val proxy = SafProxyClient(config)
private val safClient = SafClient(config)

companion object {
const val TOGGLE = "bruk-saf-client"
}

override suspend fun hentJournalpost(journalpostId: String): SafGraphQL.Journalpost {
return if (unleash.isEnabled(TOGGLE)) {
safClient.hentJournalpost(journalpostId)
} else {
proxy.hentJournalpost(journalpostId)
}
}

override suspend fun hentSøknadsData(
journalpostId: String,
dokumentInfoId: String,
): SafGraphQL.SøknadsData {
return if (unleash.isEnabled(TOGGLE)) {
safClient.hentSøknadsData(journalpostId, dokumentInfoId)
} else {
proxy.hentSøknadsData(journalpostId, dokumentInfoId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotliquery.queryOf
import kotliquery.sessionOf
import kotliquery.using
import mu.KotlinLogging
import no.nav.dagpenger.mottak.behov.journalpost.SafClient
import no.nav.dagpenger.mottak.behov.journalpost.SafProxyClient
import no.nav.dagpenger.mottak.db.PostgresDataSourceBuilder
import org.postgresql.util.PGobject
import javax.sql.DataSource
Expand All @@ -15,7 +15,7 @@ private val logger = KotlinLogging.logger {}
private val sikkerlogg = KotlinLogging.logger("tjenestekall.SøknadsDataVaktmester")

internal class SøknadsDataVaktmester(
private val safClient: SafClient,
private val safProxyClient: SafProxyClient,
private val ds: DataSource = PostgresDataSourceBuilder.dataSource,
) {
fun fixSoknadsData(jp: Int) {
Expand All @@ -24,7 +24,7 @@ internal class SøknadsDataVaktmester(
if (session.lås(jp)) {
val (innsendingId, dokumentinfoId) = session.innsendingIdOgDokumentinfoId(jp)
runBlocking {
val data = safClient.hentSøknadsData(jp.toString(), dokumentinfoId.toString()).data.toString()
val data = safProxyClient.hentSøknadsData(jp.toString(), dokumentinfoId.toString()).data.toString()
session.insertSøknadsData(innsendingId, data)
logger.info { "Oppdatert søknads data for journalpost id $jp" }
sikkerlogg.info { "Oppdatert søknads data for journalpost id $jp. Søknadsdata: $data" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import kotliquery.sessionOf
import kotliquery.using
import no.nav.dagpenger.innsendingData
import no.nav.dagpenger.mottak.behov.JsonMapper
import no.nav.dagpenger.mottak.behov.journalpost.SafClient
import no.nav.dagpenger.mottak.behov.journalpost.SafGraphQL
import no.nav.dagpenger.mottak.behov.journalpost.SafProxyClient
import no.nav.dagpenger.mottak.db.InnsendingPostgresRepository
import no.nav.dagpenger.mottak.db.PostgresDataSourceBuilder
import no.nav.dagpenger.mottak.db.PostgresTestHelper
Expand All @@ -24,8 +24,8 @@ class SøknadsDataVaktmesterTest {
InnsendingPostgresRepository().lagre(innsending)

SøknadsDataVaktmester(
safClient =
mockk<SafClient>().also {
safProxyClient =
mockk<SafProxyClient>().also {
coEvery {
it.hentSøknadsData(innsending.journalpostId(), "12345678")
} returns SafGraphQL.SøknadsData.fromJson("""{"hubba": "bubba"}""")
Expand Down
Loading