Skip to content

Commit

Permalink
Gjør henting av tilgangstoken likt i klienter (#576)
Browse files Browse the repository at this point in the history
* Gjør henting av tilgangstoken likt i klienter

* Endre skrivefeil i test
  • Loading branch information
bjerga authored Feb 20, 2024
1 parent d8f5de3 commit a351300
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 116 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/no/nav/syfo/client/OppgaveClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const val BEHANDLINGSTYPE_UTLAND = "ae0106"
const val BEHANDLINGSTYPE_NORMAL = "ab0061"

class OppgaveClient(
val oppgavebehndlingUrl: String,
val oppgavebehandlingUrl: String,
val httpClient: HttpClient,
val metrikk: Metrikk,
val getAccessToken: () -> String
Expand All @@ -38,7 +38,7 @@ class OppgaveClient(
private val sikkerlogger = sikkerLogger()

private suspend fun opprettOppgave(opprettOppgaveRequest: OpprettOppgaveRequest): OpprettOppgaveResponse = retry("opprett_oppgave") {
httpClient.post<OpprettOppgaveResponse>(oppgavebehndlingUrl) {
httpClient.post<OpprettOppgaveResponse>(oppgavebehandlingUrl) {
contentType(ContentType.Application.Json)
this.header("Authorization", "Bearer ${getAccessToken()}")
this.header("X-Correlation-ID", MdcUtils.getCallId())
Expand All @@ -50,7 +50,7 @@ class OppgaveClient(
return retry("hent_oppgave") {
val callId = MdcUtils.getCallId()
logger.info("Henter oppgave med CallId $callId")
httpClient.get<OppgaveResponse>(oppgavebehndlingUrl) {
httpClient.get<OppgaveResponse>(oppgavebehandlingUrl) {
this.header("Authorization", "Bearer ${getAccessToken()}")
this.header("X-Correlation-ID", callId)
parameter("tema", TEMA)
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/no/nav/syfo/client/dokarkiv/DokArkivClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import no.nav.helse.arbeidsgiver.integrasjoner.AccessTokenProvider
import no.nav.helsearbeidsgiver.utils.log.logger
import no.nav.helsearbeidsgiver.utils.log.sikkerLogger
import no.nav.syfo.helpers.retry
Expand All @@ -22,8 +21,8 @@ val AUTOMATISK_JOURNALFOERING_ENHET = "9999"

class DokArkivClient(
private val url: String,
private val accessTokenProvider: AccessTokenProvider,
private val httpClient: HttpClient
private val httpClient: HttpClient,
private val getAccessToken: () -> String
) {
private val logger = this.logger()
private val sikkerlogger = sikkerLogger()
Expand Down Expand Up @@ -53,7 +52,7 @@ class DokArkivClient(
return httpClient.patch<String>("$url/journalpost/$journalpostId/ferdigstill") {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
header("Authorization", "Bearer ${accessTokenProvider.getToken()}")
header("Authorization", "Bearer ${getAccessToken()}")
header("Nav-Callid", msgId)
body = ferdigstillRequest
}.also { logger.info("Ferdigstilte journalpost {}", journalpostId) }
Expand Down Expand Up @@ -97,7 +96,7 @@ class DokArkivClient(
httpClient.put<HttpResponse>("$url/journalpost/$journalpostId") {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
header("Authorization", "Bearer ${accessTokenProvider.getToken()}")
header("Authorization", "Bearer ${getAccessToken()}")
header("Nav-Callid", msgId)
body = oppdaterJournalpostRequest
}.also { logger.info("Oppdatering av journalpost ok for journalpostid {}, msgId {}", journalpostId, msgId) }
Expand All @@ -124,7 +123,7 @@ class DokArkivClient(
httpClient.patch<HttpResponse>("$url/journalpost/$journalpostId/feilregistrer/feilregistrerSakstilknytning") {
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
header("Authorization", "Bearer ${accessTokenProvider.getToken()}")
header("Authorization", "Bearer ${getAccessToken()}")
header("Nav-Callid", msgId)
}.also { logger.info("Feilregistrerte journalpost {}", journalpostId) }
} catch (e: Exception) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/no/nav/syfo/client/saf/SafDokumentClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.util.toByteArray
import kotlinx.coroutines.runBlocking
import no.nav.helse.arbeidsgiver.integrasjoner.AccessTokenProvider
import no.nav.helsearbeidsgiver.utils.log.MdcUtils
import no.nav.helsearbeidsgiver.utils.log.logger

Expand All @@ -21,7 +20,7 @@ import no.nav.helsearbeidsgiver.utils.log.logger
class SafDokumentClient(
private val url: String,
private val httpClient: HttpClient,
private val stsClient: AccessTokenProvider
private val getAccessToken: () -> String
) {
private val logger = this.logger()

Expand All @@ -33,7 +32,7 @@ class SafDokumentClient(
val response = runBlocking {
httpClient.get<HttpStatement>("$url/hentdokument/$journalpostId/$dokumentInfoId/ORIGINAL") {
accept(ContentType.Application.Xml)
header("Authorization", "Bearer ${stsClient.getToken()}")
header("Authorization", "Bearer ${getAccessToken()}")
header("Nav-Callid", MdcUtils.getCallId())
header("Nav-Consumer-Id", "syfoinntektsmelding")
}.execute()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.client.request.post
import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.coroutines.runBlocking
import no.nav.helse.arbeidsgiver.integrasjoner.AccessTokenProvider
import no.nav.helsearbeidsgiver.utils.log.logger
import no.nav.syfo.client.saf.model.GetJournalpostRequest
import no.nav.syfo.client.saf.model.JournalResponse
Expand All @@ -15,17 +14,17 @@ import no.nav.syfo.client.saf.model.Journalpost
class SafJournalpostClient(
private val httpClient: HttpClient,
private val basePath: String,
private val stsClient: AccessTokenProvider
private val getAccessToken: () -> String
) {
private val logger = this.logger()

fun getJournalpostMetadata(journalpostId: String): Journalpost? {
val token = stsClient.getToken()
logger.info("Henter journalpostmetadata for $journalpostId with token size " + token.length)
val accessToken = getAccessToken()
logger.info("Henter journalpostmetadata for $journalpostId with token size " + accessToken.length)
val response = runBlocking {
httpClient.post<JournalResponse>(basePath) {
contentType(ContentType.Application.Json)
header("Authorization", "Bearer $token")
header("Authorization", "Bearer $accessToken")
header("X-Correlation-ID", journalpostId)
body = GetJournalpostRequest(query = lagQuery(journalpostId))
}
Expand Down
21 changes: 13 additions & 8 deletions src/main/kotlin/no/nav/syfo/koin/DevKoinProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ fun devConfig(config: ApplicationConfig) = module {
}

single { UtsattOppgaveDAO(UtsattOppgaveRepositoryImp(get())) }
single {
val tokenProvider = get<AccessTokenProvider>(qualifier = named(AccessScope.OPPGAVE))
OppgaveClient(config.getString("oppgavebehandling_url"), get(), get(), tokenProvider::getToken)
}
single { UtsattOppgaveService(get(), get(), get(), get(), get(), get()) }
single { FeiletUtsattOppgaveMeldingProsessor(get(), get()) }

Expand All @@ -125,6 +121,15 @@ fun devConfig(config: ApplicationConfig) = module {
single { PostgresBakgrunnsjobbRepository(get()) } bind BakgrunnsjobbRepository::class
single { BakgrunnsjobbService(get(), bakgrunnsvarsler = MetrikkVarsler()) }

single {
OppgaveClient(
config.getString("oppgavebehandling_url"),
get(),
get(),
get<AccessTokenProvider>(qualifier = named(AccessScope.OPPGAVE))::getToken
)
}

single {
PdlClientImpl(
config.getString("pdl_url"),
Expand All @@ -146,23 +151,23 @@ fun devConfig(config: ApplicationConfig) = module {
SafJournalpostClient(
get(),
config.getString("saf_journal_url"),
get(qualifier = named(AccessScope.SAF)),
get<AccessTokenProvider>(qualifier = named(AccessScope.SAF))::getToken,
)
}

single {
SafDokumentClient(
config.getString("saf_dokument_url"),
get(),
get(qualifier = named(AccessScope.SAF)),
get<AccessTokenProvider>(qualifier = named(AccessScope.SAF))::getToken,
)
}

single {
DokArkivClient(
config.getString("dokarkiv_url"),
get(qualifier = named(AccessScope.DOKARKIV)),
get()
get(),
get<AccessTokenProvider>(qualifier = named(AccessScope.DOKARKIV))::getToken
)
}

Expand Down
22 changes: 1 addition & 21 deletions src/main/kotlin/no/nav/syfo/koin/Fakes.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package no.nav.syfo.koin

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import no.nav.helse.arbeidsgiver.integrasjoner.AccessTokenProvider
import no.nav.helse.arbeidsgiver.integrasjoner.altinn.AltinnOrganisasjon
import no.nav.helse.arbeidsgiver.integrasjoner.dokarkiv.DokarkivKlient
import no.nav.helse.arbeidsgiver.integrasjoner.dokarkiv.JournalpostRequest
import no.nav.helse.arbeidsgiver.integrasjoner.dokarkiv.JournalpostResponse
Expand All @@ -18,8 +14,6 @@ import no.nav.helse.arbeidsgiver.integrasjoner.pdl.PdlHentFullPerson
import no.nav.helse.arbeidsgiver.integrasjoner.pdl.PdlHentPersonNavn
import no.nav.helse.arbeidsgiver.integrasjoner.pdl.PdlIdent
import no.nav.helse.arbeidsgiver.integrasjoner.pdl.PdlPersonNavnMetadata
import no.nav.helse.arbeidsgiver.utils.loadFromResources
import no.nav.helse.arbeidsgiver.web.auth.AltinnOrganisationsRepository
import no.nav.syfo.client.norg.ArbeidsfordelingRequest
import no.nav.syfo.client.norg.ArbeidsfordelingResponse
import no.nav.syfo.client.norg.Norg2Client
Expand All @@ -30,14 +24,6 @@ import java.time.LocalDate.now
import java.time.LocalDateTime

fun Module.mockExternalDependecies() {
// single { MockAltinnRepo(get()) } bind AltinnOrganisationsRepository::class

single {
object : AccessTokenProvider {
override fun getToken(): String = "fake token"
}
} bind AccessTokenProvider::class

single {
object : DokarkivKlient {
override fun journalførDokument(
Expand Down Expand Up @@ -113,7 +99,7 @@ fun Module.mockExternalDependecies() {
object : Norg2Client(
"",
get(),
get<AccessTokenProvider>()::getToken
{ "fake token" }
) {
override suspend fun hentAlleArbeidsfordelinger(
request: ArbeidsfordelingRequest,
Expand Down Expand Up @@ -142,9 +128,3 @@ fun Module.mockExternalDependecies() {
}
} bind Norg2Client::class
}

class MockAltinnRepo(om: ObjectMapper) : AltinnOrganisationsRepository {
private val mockList = "altinn-mock/organisasjoner-med-rettighet.json".loadFromResources()
private val mockAcl = om.readValue<Set<AltinnOrganisasjon>>(mockList)
override fun hentOrgMedRettigheterForPerson(identitetsnummer: String): Set<AltinnOrganisasjon> = mockAcl
}
68 changes: 23 additions & 45 deletions src/main/kotlin/no/nav/syfo/koin/LocalKoinProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,59 +61,34 @@ fun localDevConfig(config: ApplicationConfig) = module {
} bind DataSource::class
single { UtsattOppgaveRepositoryImp(get()) } bind UtsattOppgaveRepository::class

single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get(), get(), get()) } bind FinnAlleUtgaandeOppgaverProcessor::class
single { FinnAlleUtgaandeOppgaverProcessor(get(), get(), get(), get(), get(), get()) }

single {
JournalpostHendelseConsumer(
joarkLocalProperties().toMutableMap(),
config.getString("kafka_joark_hendelse_topic"),
get(),
get()
joarkLocalProperties(), config.getString("kafka_joark_hendelse_topic"), get(), get()
)
}
single {
UtsattOppgaveConsumer(
utsattOppgaveLocalProperties().toMutableMap(),
config.getString("kafka_utsatt_oppgave_topic"),
get(),
get(),
get()
)
UtsattOppgaveConsumer(utsattOppgaveLocalProperties(), config.getString("kafka_utsatt_oppgave_topic"), get(), get(), get())
}
single { PostgresBakgrunnsjobbRepository(get()) } bind BakgrunnsjobbRepository::class
single { BakgrunnsjobbService(get()) }
single { BehandlendeEnhetConsumer(get(), get(), get()) } bind BehandlendeEnhetConsumer::class
single { BehandlendeEnhetConsumer(get(), get(), get()) }
single { UtsattOppgaveDAO(UtsattOppgaveRepositoryMockk()) }
single { OppgaveClient(config.getString("oppgavebehandling_url"), get(), get()) { "local token" } } bind OppgaveClient::class
single { UtsattOppgaveService(get(), get(), get(), get(), get(), get()) } bind UtsattOppgaveService::class
single { OppgaveClient(config.getString("oppgavebehandling_url"), get(), get()) { "local token" } }
single { UtsattOppgaveService(get(), get(), get(), get(), get(), get()) }
single { FeiletUtsattOppgaveMeldingProsessor(get(), get()) }

single { FjernInntektsmeldingByBehandletProcessor(get(), 1) } bind FjernInntektsmeldingByBehandletProcessor::class
single { FjernInntektsmeldingByBehandletProcessor(get(), 1) }

single {
InntektsmeldingBehandler(
get(),
get(),
get(),
get(),
get(),
get()
)
} bind InntektsmeldingBehandler::class
single { InngaaendeJournalConsumer(get()) } bind InngaaendeJournalConsumer::class
single { BehandleInngaaendeJournalConsumer(get()) } bind BehandleInngaaendeJournalConsumer::class
single { JournalConsumer(get(), get(), get()) } bind JournalConsumer::class
single { InntektsmeldingBehandler(get(), get(), get(), get(), get(), get()) }
single { InngaaendeJournalConsumer(get()) }
single { BehandleInngaaendeJournalConsumer(get()) }
single { JournalConsumer(get(), get(), get()) }
single { Metrikk() } bind Metrikk::class
single { JournalpostService(get(), get(), get(), get(), get()) } bind JournalpostService::class
single { InntektsmeldingService(InntektsmeldingRepositoryImp(get()), get()) } bind InntektsmeldingService::class
single {
JoarkInntektsmeldingHendelseProsessor(
get(),
get(),
get(),
get()
)
} bind JoarkInntektsmeldingHendelseProsessor::class
single { JournalpostService(get(), get(), get(), get(), get()) }
single { InntektsmeldingService(InntektsmeldingRepositoryImp(get()), get()) }
single { JoarkInntektsmeldingHendelseProsessor(get(), get(), get(), get()) }

single {
InntektsmeldingAivenProducer(producerLocalProperties(config.getString("kafka_bootstrap_servers")))
Expand All @@ -123,25 +98,25 @@ fun localDevConfig(config: ApplicationConfig) = module {
SafJournalpostClient(
get(),
"http://localhost",
get()
::fakeToken
)
} bind SafJournalpostClient::class
}

single {
SafDokumentClient(
config.getString("saf_dokument_url"),
get(),
get()
::fakeToken
)
} bind SafDokumentClient::class
}

single {
DokArkivClient(
config.getString("dokarkiv_url"),
get(),
get()
::fakeToken
)
} bind DokArkivClient::class
}

single { ArbeidsgiverperiodeRepositoryImp(get()) } bind ArbeidsgiverperiodeRepository::class

Expand All @@ -156,3 +131,6 @@ fun localDevConfig(config: ApplicationConfig) = module {
)
}
}

private fun fakeToken(): String =
"fake token"
Loading

0 comments on commit a351300

Please sign in to comment.