From e80f9c4c44343ec0e85183196eae0a4e71da6012 Mon Sep 17 00:00:00 2001 From: Nils Martin Sande Date: Mon, 27 May 2024 14:08:13 +0200 Subject: [PATCH] =?UTF-8?q?Endret=20til=20=C3=A5=20bruke=20kafka=20key=20c?= =?UTF-8?q?lient=20fra=20lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api-start-stopp-perioder/build.gradle.kts | 1 + .../RequestHandlerFactory.kt | 6 +-- .../application/OpplysningerRequestHandler.kt | 3 +- .../application/StartStoppRequestHandler.kt | 2 +- .../arbeidssokerregisteret/config/Config.kt | 9 ++-- .../services/kafkakeys/Factory.kt | 31 ------------- .../services/kafkakeys/KafkaKeysClient.kt | 43 ------------------- .../services/kafkakeys/Mock.kt | 20 --------- .../paw/kafkakeygenerator/client/Factory.kt | 2 +- .../client/KafkaKeysClient.kt | 7 ++- .../nav/paw/kafkakeygenerator/client/Mock.kt | 2 +- 11 files changed, 17 insertions(+), 109 deletions(-) delete mode 100644 apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Factory.kt delete mode 100644 apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/KafkaKeysClient.kt delete mode 100644 apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Mock.kt diff --git a/apps/api-start-stopp-perioder/build.gradle.kts b/apps/api-start-stopp-perioder/build.gradle.kts index b8460641..cb05a0b2 100644 --- a/apps/api-start-stopp-perioder/build.gradle.kts +++ b/apps/api-start-stopp-perioder/build.gradle.kts @@ -25,6 +25,7 @@ dependencies { implementation(otel.annotations) implementation(project(":lib:kafka")) implementation(project(":lib:hoplite-config")) + implementation(project(":lib:kafka-key-generator-client")) implementation(hoplite.hopliteCore) implementation(hoplite.hopliteToml) implementation(hoplite.hopliteYaml) diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/RequestHandlerFactory.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/RequestHandlerFactory.kt index f4c0aab1..d1ce510c 100644 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/RequestHandlerFactory.kt +++ b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/RequestHandlerFactory.kt @@ -8,10 +8,10 @@ import no.nav.paw.arbeidssokerregisteret.application.RequestValidator import no.nav.paw.arbeidssokerregisteret.config.Config import no.nav.paw.arbeidssokerregisteret.services.AutorisasjonService import no.nav.paw.arbeidssokerregisteret.services.PersonInfoService -import no.nav.paw.arbeidssokerregisteret.services.kafkakeys.kafkaKeysKlient import no.nav.paw.arbeidssokerregisteret.utils.azureAdM2MTokenClient import no.nav.paw.config.kafka.KafkaFactory -import no.nav.paw.migrering.app.kafkakeys.KafkaKeysClient +import no.nav.paw.kafkakeygenerator.client.KafkaKeysClient +import no.nav.paw.kafkakeygenerator.client.kafkaKeysClient import no.nav.paw.pdl.PdlClient import no.nav.poao_tilgang.client.PoaoTilgangHttpClient import org.apache.kafka.common.serialization.LongSerializer @@ -59,7 +59,7 @@ private fun clientsFactory(config: Config): Clients { config.poaoTilgangClientConfig.url, { createMachineToMachineToken(config.poaoTilgangClientConfig.scope) } ) - val kafkaKeysClient = kafkaKeysKlient(config.kafkaKeysConfig) { + val kafkaKeysClient = kafkaKeysClient(config.kafkaKeysConfig) { createMachineToMachineToken(config.kafkaKeysConfig.scope) } return Clients( diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/OpplysningerRequestHandler.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/OpplysningerRequestHandler.kt index 657a6d0a..1c9d670d 100644 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/OpplysningerRequestHandler.kt +++ b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/OpplysningerRequestHandler.kt @@ -9,7 +9,7 @@ import no.nav.paw.arbeidssokerregisteret.domain.http.ValidationResultOk import no.nav.paw.arbeidssokerregisteret.domain.http.validerOpplysninger import no.nav.paw.arbeidssokerregisteret.intern.v1.Hendelse import no.nav.paw.config.kafka.sendDeferred -import no.nav.paw.migrering.app.kafkakeys.KafkaKeysClient +import no.nav.paw.kafkakeygenerator.client.KafkaKeysClient import org.apache.kafka.clients.producer.Producer import org.apache.kafka.clients.producer.ProducerRecord import org.slf4j.LoggerFactory @@ -38,6 +38,7 @@ class OpplysningerRequestHandler( return Right(validerOpplysninger) } val (id, key) = kafkaKeysClient.getIdAndKey(identitetsnummer.verdi) + val hendelse = opplysningerHendelse(id, opplysningerRequest) val record = ProducerRecord( hendelseTopic, diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/StartStoppRequestHandler.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/StartStoppRequestHandler.kt index 40207a5b..26c5b436 100644 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/StartStoppRequestHandler.kt +++ b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/application/StartStoppRequestHandler.kt @@ -7,7 +7,7 @@ import no.nav.paw.arbeidssokerregisteret.RequestScope import no.nav.paw.arbeidssokerregisteret.domain.Identitetsnummer import no.nav.paw.arbeidssokerregisteret.intern.v1.Hendelse import no.nav.paw.config.kafka.sendDeferred -import no.nav.paw.migrering.app.kafkakeys.KafkaKeysClient +import no.nav.paw.kafkakeygenerator.client.KafkaKeysClient import org.apache.kafka.clients.producer.Producer import org.apache.kafka.clients.producer.ProducerRecord import org.apache.kafka.clients.producer.internals.BuiltInPartitioner.partitionForKey diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/config/Config.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/config/Config.kt index 41612cae..79dbabac 100644 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/config/Config.kt +++ b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/config/Config.kt @@ -1,12 +1,14 @@ package no.nav.paw.arbeidssokerregisteret.config +import no.nav.paw.kafkakeygenerator.client.KafkaKeyConfig + const val CONFIG_FILE_NAME = "application.yaml" data class Config( val authProviders: AuthProviders, val pdlClientConfig: ServiceClientConfig, val poaoTilgangClientConfig: ServiceClientConfig, - val kafkaKeysConfig: KafkaKeysConfig, + val kafkaKeysConfig: KafkaKeyConfig, val eventLogTopic: String, val naisEnv: NaisEnv = currentNaisEnv ) @@ -28,8 +30,3 @@ data class ServiceClientConfig( val url: String, val scope: String ) - -data class KafkaKeysConfig( - val url: String, - val scope: String -) diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Factory.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Factory.kt deleted file mode 100644 index 2ba80405..00000000 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Factory.kt +++ /dev/null @@ -1,31 +0,0 @@ -package no.nav.paw.arbeidssokerregisteret.services.kafkakeys - -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule -import com.fasterxml.jackson.module.kotlin.KotlinFeature -import com.fasterxml.jackson.module.kotlin.KotlinModule -import io.ktor.client.* -import io.ktor.client.plugins.contentnegotiation.* -import io.ktor.serialization.jackson.jackson -import no.nav.paw.arbeidssokerregisteret.config.KafkaKeysConfig -import no.nav.paw.migrering.app.kafkakeys.KafkaKeysClient -import no.nav.paw.migrering.app.kafkakeys.StandardKafkaKeysClient -import no.nav.paw.migrering.app.kafkakeys.inMemoryKafkaKeysMock - - -fun kafkaKeysKlient(konfigurasjon: KafkaKeysConfig, m2mTokenFactory: () -> String): KafkaKeysClient = - when (konfigurasjon.url) { - "MOCK" -> inMemoryKafkaKeysMock() - else -> kafkaKeysMedHttpClient(konfigurasjon, m2mTokenFactory) - } - -private fun kafkaKeysMedHttpClient(config: KafkaKeysConfig, m2mTokenFactory: () -> String): KafkaKeysClient { - val httpClient = HttpClient { - install(ContentNegotiation) { - jackson() - } - } - return StandardKafkaKeysClient( - httpClient, - config.url - ) { m2mTokenFactory() } -} diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/KafkaKeysClient.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/KafkaKeysClient.kt deleted file mode 100644 index 655d26da..00000000 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/KafkaKeysClient.kt +++ /dev/null @@ -1,43 +0,0 @@ -package no.nav.paw.migrering.app.kafkakeys - -import io.ktor.client.HttpClient -import io.ktor.client.call.body -import io.ktor.client.request.header -import io.ktor.client.request.post -import io.ktor.client.request.setBody -import io.ktor.http.ContentType -import io.ktor.http.contentType -import io.opentelemetry.instrumentation.annotations.WithSpan - -data class KafkaKeysResponse( - val id: Long, - val key: Long -) - -data class KafkaKeysRequest( - val ident: String -) - -interface KafkaKeysClient { - suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse -} - -class StandardKafkaKeysClient( - private val httpClient: HttpClient, - private val kafkaKeysUrl: String, - private val getAccessToken: () -> String -) : KafkaKeysClient { - @WithSpan - override suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse = - httpClient.post(kafkaKeysUrl) { - header("Authorization", "Bearer ${getAccessToken()}") - contentType(ContentType.Application.Json) - setBody(KafkaKeysRequest(identitetsnummer)) - }.let { response -> - if (response.status == io.ktor.http.HttpStatusCode.OK) { - response.body() - } else { - throw Exception("Kunne ikke hente kafka key, http_status=${response.status}, melding=${response.body()}") - } - } -} diff --git a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Mock.kt b/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Mock.kt deleted file mode 100644 index c4bfa8b5..00000000 --- a/apps/api-start-stopp-perioder/src/main/kotlin/no/nav/paw/arbeidssokerregisteret/services/kafkakeys/Mock.kt +++ /dev/null @@ -1,20 +0,0 @@ -package no.nav.paw.migrering.app.kafkakeys - -import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.ConcurrentMap -import java.util.concurrent.atomic.AtomicLong - -fun inMemoryKafkaKeysMock(): KafkaKeysClient { - val naisClusterName = System.getenv("NAIS_CLUSTER_NAME") - if (naisClusterName != null) { - throw IllegalStateException("Kan ikke bruke inMemoryKafkaKeysMock i $naisClusterName") - } - val sekvens = AtomicLong(0) - val map: ConcurrentMap = ConcurrentHashMap() - return object: KafkaKeysClient { - override suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse { - val id = map.computeIfAbsent(identitetsnummer) { sekvens.incrementAndGet() } - return KafkaKeysResponse(id, id % 2) - } - } -} diff --git a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Factory.kt b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Factory.kt index 090908b1..f16be7a4 100644 --- a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Factory.kt +++ b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Factory.kt @@ -5,7 +5,7 @@ import io.ktor.client.plugins.contentnegotiation.* import io.ktor.serialization.jackson.* -fun kafkaKeysKlient(konfigurasjon: KafkaKeyConfig, m2mTokenFactory: () -> String): KafkaKeysClient = +fun kafkaKeysClient(konfigurasjon: KafkaKeyConfig, m2mTokenFactory: () -> String): KafkaKeysClient = when (konfigurasjon.url) { "MOCK" -> inMemoryKafkaKeysMock() else -> kafkaKeysMedHttpClient(konfigurasjon, m2mTokenFactory) diff --git a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/KafkaKeysClient.kt b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/KafkaKeysClient.kt index cce4ad5b..c3a588da 100644 --- a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/KafkaKeysClient.kt +++ b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/KafkaKeysClient.kt @@ -7,6 +7,7 @@ import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.ContentType import io.ktor.http.contentType +import java.lang.IllegalStateException data class KafkaKeysResponse( val id: Long, @@ -18,7 +19,9 @@ data class KafkaKeysRequest( ) interface KafkaKeysClient { - suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse? + suspend fun getIdAndKeyOrNull(identitetsnummer: String): KafkaKeysResponse? + suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse = + getIdAndKeyOrNull(identitetsnummer) ?: throw IllegalStateException("Kafka-key-client: Uventet feil mot server: http-status=404") } class StandardKafkaKeysClient( @@ -26,7 +29,7 @@ class StandardKafkaKeysClient( private val kafkaKeysUrl: String, private val getAccessToken: () -> String ) : KafkaKeysClient { - override suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse? = + override suspend fun getIdAndKeyOrNull(identitetsnummer: String): KafkaKeysResponse? = httpClient.post(kafkaKeysUrl) { header("Authorization", "Bearer ${getAccessToken()}") contentType(ContentType.Application.Json) diff --git a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Mock.kt b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Mock.kt index 0dc7db4f..2ecbb1d8 100644 --- a/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Mock.kt +++ b/lib/kafka-key-generator-client/src/main/kotlin/no/nav/paw/kafkakeygenerator/client/Mock.kt @@ -12,7 +12,7 @@ fun inMemoryKafkaKeysMock(): KafkaKeysClient { val sekvens = AtomicLong(0) val map: ConcurrentMap = ConcurrentHashMap() return object: KafkaKeysClient { - override suspend fun getIdAndKey(identitetsnummer: String): KafkaKeysResponse { + override suspend fun getIdAndKeyOrNull(identitetsnummer: String): KafkaKeysResponse { val id = map.computeIfAbsent(identitetsnummer) { sekvens.incrementAndGet() } return KafkaKeysResponse(id, id % 2) }