Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #690 from navikt/pdl-for-system
Browse files Browse the repository at this point in the history
Pdl for system
  • Loading branch information
AuroraSmil authored Jan 24, 2023
2 parents 149077d + e32a5df commit 79f70df
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class AktorIdCacheService(
private val pdlOppslagGateway: PdlOppslagGateway,
private val aktorIdCacheRepository: AktorIdCacheRepository,
) {
fun hentAktorIdFraPDLHvisIkkeFinnes(foedselsnummer: Foedselsnummer) {
fun hentAktorIdFraPDLHvisIkkeFinnes(foedselsnummer: Foedselsnummer, erSystemKontekst: Boolean = false) {
if (aktorIdCacheRepository.hentAktørId(foedselsnummer) != null) return

// hente ut
val identer = pdlOppslagGateway.hentIdenter(foedselsnummer)
val identer = pdlOppslagGateway.hentIdenter(foedselsnummer, erSystemKontekst)
val aktorId = identer.identer.first { !it.isHistorisk && it.gruppe == Gruppe.AKTORID }.ident
// sette inn

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FormidlingsgruppeMottakService(

formidlingsgruppeRepository.lagre(formidlingsgruppeEndretEvent)

//aktorIdCacheService.hentAktorIdFraPDLHvisIkkeFinnes(formidlingsgruppeEndretEvent.foedselsnummer)
aktorIdCacheService.hentAktorIdFraPDLHvisIkkeFinnes(formidlingsgruppeEndretEvent.foedselsnummer, true)

if (skalUtledeAvslutningAvPeriode) {
arbeidssokerperiodeAvsluttetService.behandleAvslutningAvArbeidssokerperiode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class TokenExchangeService(private val tokenResolver: TokenResolver) {
}
}

fun createMachineToMachineToken(api: DownstreamApi): String {
return createAadMachineToMachineToken(api)
}

private fun exchangeTokenXToken(api: DownstreamApi, opprinneligToken: String): String {
logger.info("Veksler TokenX-token mot ${api.appName}")
return tokendingsClient.exchangeOnBehalfOfToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package no.nav.fo.veilarbregistrering.bruker

interface PdlOppslagGateway {
fun hentPerson(aktorid: AktorId): Person?
fun hentIdenter(fnr: Foedselsnummer): Identer
fun hentIdenter(fnr: Foedselsnummer, erSystemKontekst: Boolean = false): Identer
fun hentIdenter(aktorId: AktorId): Identer
fun hentIdenterBolk(fnrListe: List<Foedselsnummer>): Map<Foedselsnummer, AktorId>
fun hentGeografiskTilknytning(aktorId: AktorId): GeografiskTilknytning?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import java.nio.charset.StandardCharsets

open class PdlOppslagClient(
private val baseUrl: String,
private val tokenProvider: () -> String = { "default" }
private val tokenProvider: () -> String = { "default" },
private val systemTokenProvider: () -> String = { "default" },
): HealthCheck {

private val mapper: ObjectMapper = jacksonObjectMapper().findAndRegisterModules()
Expand All @@ -39,17 +40,28 @@ open class PdlOppslagClient(
return response.data.hentIdenter
}

fun hentIdenterForSystemkontekst(fnr: Foedselsnummer): PdlIdenter {
val request = PdlHentIdenterRequest(hentIdenterQuery(), HentIdenterVariables(fnr.stringValue()))
val json = hentIdenterRequest(fnr.stringValue(), request, true)
val response = mapAndValidateResponse<PdlHentIdenterResponse>(json)
return response.data.hentIdenter
}

fun hentIdenter(aktorId: AktorId): PdlIdenter {
val request = PdlHentIdenterRequest(hentIdenterQuery(), HentIdenterVariables(aktorId.aktorId))
val json = hentIdenterRequest(aktorId.aktorId, request)
val response = mapAndValidateResponse<PdlHentIdenterResponse>(json)
return response.data.hentIdenter
}

open fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest): String {
return hentFraPdl(pdlHentIdenterRequest, ekstraHeaders = mapOf(
NAV_PERSONIDENT_HEADER to personident,
))
open fun hentIdenterRequest(
personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest, erSystemKontekst: Boolean = false
): String {
return hentFraPdl(
pdlHentIdenterRequest, ekstraHeaders = mapOf(
NAV_PERSONIDENT_HEADER to personident,
), erSystemKontekst
)
}

fun hentIdenterBolk(fnrListe: List<Foedselsnummer>): List<PdlIdenterForFoedselsnummer> {
Expand All @@ -66,10 +78,11 @@ open class PdlOppslagClient(

private fun hentFraPdl(
graphqlRequest: Any,
ekstraHeaders: Map<String, String> = emptyMap()
ekstraHeaders: Map<String, String> = emptyMap(),
erSystemKontekst: Boolean = false
): String {
val requestBody = RestUtils.toJsonRequestBody(graphqlRequest)
val authHeaders = lagAuthHeaders()
val authHeaders = if (erSystemKontekst) lagAuthHeadersForSystem() else lagAuthHeaders()
val request = Request.Builder()
.url(UrlUtils.joinPaths(baseUrl, "/graphql"))
.headers(Headers.of(authHeaders + ekstraHeaders))
Expand Down Expand Up @@ -124,6 +137,13 @@ open class PdlOppslagClient(
)
}

private fun lagAuthHeadersForSystem(): Map<String, String> {
val aadToken = systemTokenProvider()
return mapOf(
"Authorization" to "Bearer $aadToken",
)
}

private fun hentIdenterQuery() = hentRessursfil("pdl/hentIdenter.graphql")
private fun hentIdenterBolkQuery() = hentRessursfil("pdl/hentIdenterBolk.graphql")
private fun hentPersonQuery() = hentRessursfil("pdl/hentPerson.graphql")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class PdlOppslagConfig {
fun pdlOppslagClient(tokenExchangeService: TokenExchangeService): PdlOppslagClient {
val baseUrl = requireProperty("PDL_URL")

return PdlOppslagClient(baseUrl) {
tokenExchangeService.exchangeToken(pdlApi)
}
return PdlOppslagClient(
baseUrl,
{ tokenExchangeService.exchangeToken(pdlApi) },
{ tokenExchangeService.createMachineToMachineToken(pdlApi) }
)
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ open class PdlOppslagGatewayImpl(private val pdlOppslagClient: PdlOppslagClient)
}

@Cacheable(CacheConfig.HENT_PERSONIDENTER)
override fun hentIdenter(fnr: Foedselsnummer): Identer {
override fun hentIdenter(fnr: Foedselsnummer, erSystemKontekst: Boolean): Identer {
return try {
val pdlIdenter = pdlOppslagClient.hentIdenter(fnr)
val pdlIdenter = if (erSystemKontekst) {
LOG.info("Henter identer fra PDL med systemkontekst")
pdlOppslagClient.hentIdenterForSystemkontekst(fnr)
} else {
pdlOppslagClient.hentIdenter(fnr)
}
map(pdlIdenter)
} catch (e: BrukerIkkeFunnetException) {
throw e
Expand All @@ -63,11 +68,11 @@ open class PdlOppslagGatewayImpl(private val pdlOppslagClient: PdlOppslagClient)
return pdlAktorIdListe
.filter { it.identer != null }
.associate {
Foedselsnummer(it.ident) to AktorId(it.identer?.first()?.ident
?: throw BrukerIkkeFunnetException("Fant ikke AktørId for fødselsnummer i hentIdenterBolk").also {_ ->
secureLogger.warn("Fant ikke aktor_id for foedselsnummer ${it.ident} ")
})
}
Foedselsnummer(it.ident) to AktorId(it.identer?.first()?.ident
?: throw BrukerIkkeFunnetException("Fant ikke AktørId for fødselsnummer i hentIdenterBolk").also { _ ->
secureLogger.warn("Fant ikke aktor_id for foedselsnummer ${it.ident} ")
})
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package no.nav.fo.veilarbregistrering.aktorIdCache

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import no.nav.fo.veilarbregistrering.bruker.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

internal class AktorIdCacheServiceTest {

private lateinit var aktorIdCacheService: AktorIdCacheService
private val aktorIdCacheRepository: AktorIdCacheRepository = mockk(relaxed = true)

@BeforeEach
fun setup() {
every { aktorIdCacheRepository.hentAktørId(any()) } returns null

val pdlOppslagGateway: PdlOppslagGateway = mockk(relaxed = true)
every {
pdlOppslagGateway.hentIdenter(
any(),
erSystemKontekst = true
)
} returns Identer(listOf(Ident(ident = "1234", isHistorisk = false, gruppe = Gruppe.AKTORID)))

aktorIdCacheService = AktorIdCacheService(pdlOppslagGateway, aktorIdCacheRepository)
}

@Test
fun `skal hente aktørId fra PDL og sette inn hvis den ikke finnes i cache`() {
val fnr = Foedselsnummer("01234567890")

aktorIdCacheService.hentAktorIdFraPDLHvisIkkeFinnes(fnr, true)

verify(exactly = 1) { aktorIdCacheRepository.lagre(any()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ class PdlOppslagClientGraphqlTest(private val mockServer: ClientAndServer) {

private fun buildClient(): PdlOppslagClient {
val baseUrl = "http://${mockServer.remoteAddress().address.hostName}:${mockServer.remoteAddress().port}"
return PdlOppslagClient(baseUrl) { authToken }
return PdlOppslagClient(baseUrl = baseUrl, tokenProvider = { authToken })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import no.nav.fo.veilarbregistrering.bruker.feil.PdlException
import no.nav.fo.veilarbregistrering.bruker.pdl.endepunkt.*
import org.approvaltests.Approvals
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -102,8 +102,8 @@ class PdlOppslagClientTest {

@Test
fun `skal hente identer til person`() {
val client = object : PdlOppslagClient("", { TOKEN }) {
override fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest): String {
val client = object : PdlOppslagClient("", { TOKEN }, { TOKEN }) {
override fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest, erSystemKontekst: Boolean): String {
return toJson(HENT_IDENTER_OK_JSON)
}
}
Expand All @@ -115,10 +115,25 @@ class PdlOppslagClientTest {
.any { pdlIdent: PdlIdent -> pdlIdent.gruppe == PdlGruppe.AKTORID && !pdlIdent.historisk })
}

@Test
fun `skal hente identer til person for systemkontekst`() {
val client = object : PdlOppslagClient("", { TOKEN }, { SYSTEMTOKEN }) {
override fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest, erSystemKontekst: Boolean): String {
return toJson(HENT_IDENTER_OK_JSON)
}
}
val pdlIdenter = client.hentIdenterForSystemkontekst(Foedselsnummer("12345678910"))
assertThat(pdlIdenter.identer).hasSize(2)
assertTrue(pdlIdenter.identer
.any { pdlIdent: PdlIdent -> pdlIdent.gruppe == PdlGruppe.FOLKEREGISTERIDENT && !pdlIdent.historisk })
assertTrue(pdlIdenter.identer
.any { pdlIdent: PdlIdent -> pdlIdent.gruppe == PdlGruppe.AKTORID && !pdlIdent.historisk })
}

@Test
fun `skal hente identer med historikk til person`() {
val client = object : PdlOppslagClient("", { TOKEN }) {
override fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest): String {
val client = object : PdlOppslagClient("", { TOKEN }, { TOKEN }) {
override fun hentIdenterRequest(personident: String, pdlHentIdenterRequest: PdlHentIdenterRequest, erSystemKontekst: Boolean): String {
return toJson(HENT_IDENTER_MED_HISTORISK_OK_JSON)
}
}
Expand Down Expand Up @@ -152,6 +167,7 @@ class PdlOppslagClientTest {

companion object {
private const val TOKEN = "Token"
private const val SYSTEMTOKEN = "SystemToken"
internal const val HENT_PERSON_OK_JSON = "/pdl/hentPersonOk.json"
internal const val HENT_PERSON_FEIL_JSON = "/pdl/hentPersonError.json"
internal const val HENT_PERSON_NOT_FOUND_JSON = "/pdl/hentPersonNotFound.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class OppgaveRouterTest {
private class StubPdlOppslagGateway(private val geografiskTilknytning: GeografiskTilknytning? = null ,private val users: Map<AktorId, Person> = emptyMap()) : PdlOppslagGateway {
override fun hentPerson(aktorid: AktorId) = users[aktorid]

override fun hentIdenter(fnr: Foedselsnummer): Identer {
override fun hentIdenter(fnr: Foedselsnummer, erSystemKontekst: Boolean): Identer {
TODO("Not yet implemented")
}

Expand Down

0 comments on commit 79f70df

Please sign in to comment.