Skip to content

Commit

Permalink
Endret til å bruke kafka key client fra lib
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsmsa committed May 27, 2024
1 parent 35fb197 commit e80f9c4
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 109 deletions.
1 change: 1 addition & 0 deletions apps/api-start-stopp-perioder/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
Expand All @@ -28,8 +30,3 @@ data class ServiceClientConfig(
val url: String,
val scope: String
)

data class KafkaKeysConfig(
val url: String,
val scope: String
)

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,15 +19,17 @@ 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(
private val httpClient: HttpClient,
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun inMemoryKafkaKeysMock(): KafkaKeysClient {
val sekvens = AtomicLong(0)
val map: ConcurrentMap<String, Long> = 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)
}
Expand Down

0 comments on commit e80f9c4

Please sign in to comment.