Skip to content

Commit

Permalink
Merge pull request #52 from navikt/dev/flytte-kotlin-clients
Browse files Browse the repository at this point in the history
Dev/flytte kotlin clients
  • Loading branch information
naviktthomas authored Oct 25, 2024
2 parents e93b5b2 + dbb4098 commit 35548ae
Show file tree
Hide file tree
Showing 52 changed files with 2,531 additions and 37 deletions.
10 changes: 4 additions & 6 deletions apps/api-start-stopp-perioder/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import org.gradle.configurationcache.extensions.capitalized
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
Expand All @@ -23,6 +21,7 @@ dependencies {
implementation(project(":lib:kafka"))
implementation(project(":lib:hoplite-config"))
implementation(project(":lib:kafka-key-generator-client"))
implementation(project(":lib:pdl-client"))
implementation(project(":domain:arbeidssoeker-regler"))
implementation(libs.bundles.ktorServerWithNettyAndMicrometer)
implementation(libs.micrometer.registryPrometheus)
Expand All @@ -36,7 +35,6 @@ dependencies {
implementation(libs.nav.common.auditLog)
implementation(libs.nav.common.log)
implementation(libs.poao.tilgangClient)
implementation(libs.paw.pdl.client)
implementation(libs.logbackClassic)
implementation(libs.logstashLogbackEncoder)
implementation(libs.kafka.clients)
Expand Down Expand Up @@ -93,8 +91,8 @@ jib {
environment = mapOf(
"IMAGE_WITH_VERSION" to "${image ?: project.name}:${project.version}",
"OTEL_INSTRUMENTATION_METHODS_INCLUDE" to ("io.ktor.server.routing.Routing[interceptor,executeResult];" +
"io.ktor.server.netty.NettyApplicationCallHandler[handleRequest,exceptionCaught];") +
"io.ktor.serialization.jackson.JacksonConverter[deserialize,serializeNullable]"
"io.ktor.server.netty.NettyApplicationCallHandler[handleRequest,exceptionCaught];") +
"io.ktor.serialization.jackson.JacksonConverter[deserialize,serializeNullable]"
)
jvmFlags = listOf("-XX:ActiveProcessorCount=4", "-XX:+UseZGC", "-XX:+ZGenerational")
}
Expand All @@ -107,7 +105,7 @@ mapOf(
"${layout.projectDirectory}/src/main/resources/openapi/opplysninger.yaml" to "${generatedCodePackageName}.opplysningermottatt",
"${layout.projectDirectory}/src/main/resources/openapi/startstopp.yaml" to "${generatedCodePackageName}.startstopp"
).map { (openApiDocFile, pkgName) ->
val taskName = "generate${pkgName.capitalized()}"
val taskName = "generate${pkgName.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }}"
tasks.register(taskName, GenerateTask::class) {
generatorName.set("kotlin-server")
library = "ktor"
Expand Down
2 changes: 1 addition & 1 deletion apps/bekreftelse-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {

// Serialization
implementation(libs.ktor.serialization.jackson)
implementation(libs.ktor.serialization.json)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.jackson.datatypeJsr310)

// Authentication
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package no.nav.paw.bekreftelse.api.model

import no.nav.paw.bekreftelse.api.exception.UgyldigBearerTokenException
import no.nav.paw.bekreftelse.api.model.ResolveToken.AzureToken
import no.nav.paw.bekreftelse.api.model.ResolveToken.IdPortenToken
import no.nav.paw.bekreftelse.api.model.ResolveToken.TokenXToken
import no.nav.security.token.support.core.context.TokenValidationContext
import no.nav.security.token.support.core.jwt.JwtToken
import java.util.*
Expand Down Expand Up @@ -39,14 +36,11 @@ data object OID : Claim<UUID>("oid", UUID::fromString)
data object Name : Claim<String>("name", { it })
data object NavIdent : Claim<String>("NAVident", { it })

private sealed class ResolveToken(
val issuer: Issuer,
val claims: List<Claim<*>>
) {
data object IdPortenToken : ResolveToken(IdPorten, listOf(PID))
data object TokenXToken : ResolveToken(TokenX, listOf(PID))
data object AzureToken : ResolveToken(Azure, listOf(OID, Name, NavIdent))
}
sealed class ResolveToken(val issuer: Issuer, val claims: List<Claim<*>>)

data object IdPortenToken : ResolveToken(IdPorten, listOf(PID))
data object TokenXToken : ResolveToken(TokenX, listOf(PID))
data object AzureToken : ResolveToken(Azure, listOf(OID, Name, NavIdent))

private val validTokens: List<ResolveToken> = listOf(IdPortenToken, TokenXToken, AzureToken)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ val KafkaConsumerPlugin: ApplicationPlugin<KafkaConsumerPluginConfig> =
application.log.info("Stopper Kafka Consumer")
pollingFlag.set(false)
consumeJob?.cancel()
consumer.unsubscribe()
consumer.close(shutDownTimeout)
}

Expand Down
2 changes: 1 addition & 1 deletion apps/bekreftelse-tjeneste/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {

// Serialization
implementation(libs.ktor.serialization.jackson)
implementation(libs.ktor.serialization.json)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.jackson.datatypeJsr310)

// Tooling
Expand Down
2 changes: 1 addition & 1 deletion apps/bekreftelse-utgang/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {

// Serialization
implementation(libs.ktor.serialization.jackson)
implementation(libs.ktor.serialization.json)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.jackson.datatypeJsr310)

// Tooling
Expand Down
5 changes: 2 additions & 3 deletions apps/hendelselogg-backup/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import org.gradle.configurationcache.extensions.capitalized
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask
Expand Down Expand Up @@ -107,7 +106,7 @@ val generatedCodeOutputDir = "${layout.buildDirectory.get()}/generated/"
mapOf(
"${layout.projectDirectory}/src/main/resources/openapi/Brukerstoette.yaml" to "${generatedCodePackageName}.brukerstoette"
).map { (openApiDocFile, pkgName) ->
val taskName = "generate${pkgName.capitalized()}"
val taskName = "generate${pkgName.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }}"
tasks.register(taskName, GenerateTask::class) {
generatorName.set("kotlin-server")
library = "ktor"
Expand Down Expand Up @@ -142,7 +141,7 @@ mapOf(
mapOf(
"${layout.projectDirectory}/src/main/resources/openapi/oppslags-api.yaml" to "${generatedCodePackageName}.oppslagsapi"
).map { (openApiDocFile, pkgName) ->
val taskName = "generate${pkgName.capitalized()}"
val taskName = "generate${pkgName.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }}"
tasks.register(taskName, GenerateTask::class) {
generatorName.set("kotlin")
library = "jvm-ktor"
Expand Down
2 changes: 1 addition & 1 deletion apps/kafka-key-generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val jvmMajorVersion: String by project
dependencies {
// PAW
implementation(project(":lib:hoplite-config"))
implementation(libs.paw.pdl.client)
implementation(project(":lib:pdl-client"))

// NAV
implementation(libs.nav.common.log)
Expand Down
6 changes: 3 additions & 3 deletions apps/utgang-pdl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ dependencies {
implementation(project(":domain:arbeidssoekerregisteret-kotlin"))
implementation(project(":domain:arbeidssoeker-regler"))

implementation(project(":lib:kafka-key-generator-client"))
implementation(project(":lib:hoplite-config"))
implementation(project(":lib:kafka"))
implementation(project(":lib:kafka-streams"))
implementation(project(":lib:hoplite-config"))
implementation(project(":lib:kafka-key-generator-client"))
implementation(project(":lib:pdl-client"))

api(libs.arrow.core.core)

implementation(libs.kafka.streams.core)
implementation(libs.paw.pdl.client)

implementation(libs.bundles.ktorServerWithNettyAndMicrometer)
implementation(libs.micrometer.registryPrometheus)
Expand Down
4 changes: 1 addition & 3 deletions domain/arbeidssoeker-regler/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
id("org.openapi.generator")
Expand All @@ -11,8 +9,8 @@ val baseImage: String by project
val jvmMajorVersion: String by project

dependencies {
implementation(project(":lib:pdl-client"))
implementation(project(":domain:interne-hendelser"))
implementation(libs.paw.pdl.client)
api(libs.micrometer.registryPrometheus)
api(libs.arrow.core.core)
testImplementation(libs.ktor.server.testJvm)
Expand Down
17 changes: 10 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
[versions]
pawPdlClientVersion = "24.10.18.41-1"
pawAaregClientVersion = "24.07.04.18-1"
noNavSecurityVersion = "5.0.5"
noNavCommonVersion = "3.2024.05.23_05.46-2b29fa343e8e"
arbeidssokerregisteretVersion = "1.9348086045.48-1"
bekreftelseSchemaVersion = "24.10.21.12-1"
arrowVersion = "1.2.4"
arrowJacksonIntegrationVersion = "0.14.1"
noNavCommonVersion = "3.2024.05.23_05.46-2b29fa343e8e"
noNavSecurityVersion = "5.0.5"
comSksamuelHopliteVersion = "2.8.2"
graphqlClientVersion = "7.1.1"
orgApacheKafkaVersion = "3.7.0"
ioConfluentKafkaVersion = "7.7.1"
orgApacheAvroVersion = "1.12.0"
Expand All @@ -27,6 +26,8 @@ coroutinesVersion = "1.9.0"
postgresDriverVersion = "42.7.4"
flywayVersion = "10.20.0"
hikariVersion = "6.0.0"
kotlinxCoroutinesVersion = "1.8.1"
kotlinxSerializationJsonVersion = "1.7.3"
ktorVersion = "2.3.12"

[libraries]
Expand All @@ -36,6 +37,8 @@ arrow-core-serialization = { group = "io.arrow-kt", name = "arrow-core-serializa
arrow-integration-jackson = { group = "io.arrow-kt", name = "arrow-integrations-jackson-module", version.ref = "arrowJacksonIntegrationVersion" }
logbackClassic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logbackVersion" }
logstashLogbackEncoder = { group = "net.logstash.logback", name = "logstash-logback-encoder", version.ref = "logstashVersion" }
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "kotlinxCoroutinesVersion" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinxSerializationJsonVersion" }
ktor-client-contentNegotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktorVersion" }
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktorVersion" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktorVersion" }
Expand All @@ -57,14 +60,12 @@ ktor-server-testJvm = { group = "io.ktor", name = "ktor-server-tests-jvm", versi
ktor-serialization-core = { group = "io.ktor", name = "ktor-serialization", version.ref = "ktorVersion" }
ktor-serialization-jvm = { group = "io.ktor", name = "ktor-serialization-jvm", version.ref = "ktorVersion" }
ktor-serialization-jackson = { group = "io.ktor", name = "ktor-serialization-jackson", version.ref = "ktorVersion" }
ktor-serialization-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktorVersion" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktorVersion" }
opentelemetry-api = { group = "io.opentelemetry", name = "opentelemetry-api", version.ref = "otelTargetSdkVersion" }
opentelemetry-ktor = { group = "io.opentelemetry.instrumentation", name = "opentelemetry-ktor-2.0", version.ref = "otelInstrumentationKtorVersion" }
opentelemetry-annotations = { group = "io.opentelemetry.instrumentation", name = "opentelemetry-instrumentation-annotations", version.ref = "otelInstrumentationVersion" }
micrometerCore = { group = "io.micrometer", name = "micrometer-core", version.ref = "micrometerVersion" }
micrometer-registryPrometheus = { group = "io.micrometer", name = "micrometer-registry-prometheus", version.ref = "micrometerVersion" }
paw-pdl-client = { group = "no.nav.paw", name = "pdl-client", version.ref = "pawPdlClientVersion" }
paw-aareg-client = { group = "no.nav.paw", name = "aareg-client", version.ref = "pawAaregClientVersion" }
paw-schema-main = { group = "no.nav.paw.arbeidssokerregisteret.api", name = "main-avro-schema", version.ref = "arbeidssokerregisteretVersion" }
kafka-clients = { group = "org.apache.kafka", name = "kafka-clients", version.ref = "orgApacheKafkaVersion" }
kafka-streams-core = { group = "org.apache.kafka", name = "kafka-streams", version.ref = "orgApacheKafkaVersion" }
Expand All @@ -80,6 +81,8 @@ nav-common-log = { group = "no.nav.common", name = "log", version.ref = "noNavCo
nav-common-auditLog = { group = "no.nav.common", name = "audit-log", version.ref = "noNavCommonVersion" }
nav-security-tokenValidationKtorV2 = { group = "no.nav.security", name = "token-validation-ktor-v2", version.ref = "noNavSecurityVersion" }
nav-security-tokenClientCore = { group = "no.nav.security", name = "token-client-core", version.ref = "noNavSecurityVersion" }
graphql-client = { group = "com.expediagroup", name = "graphql-kotlin-client", version.ref = "graphqlClientVersion" }
graphql-ktor-client = { group = "com.expediagroup", name = "graphql-kotlin-ktor-client", version.ref = "graphqlClientVersion" }
hoplite-core = { group = "com.sksamuel.hoplite", name = "hoplite-core", version.ref = "comSksamuelHopliteVersion" }
hoplite-toml = { group = "com.sksamuel.hoplite", name = "hoplite-toml", version.ref = "comSksamuelHopliteVersion" }
hoplite-yaml = { group = "com.sksamuel.hoplite", name = "hoplite-yaml", version.ref = "comSksamuelHopliteVersion" }
Expand Down
41 changes: 41 additions & 0 deletions lib/aareg-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# paw-aareg-client

Henter arbeidsforhold fra Arbeidsgiver- og arbeidstakerregisteret ([aareg](https://navikt.github.io/aareg/)).

Se URL-er på https://github.com/navikt/aareg-services

Se dokumentasjon for aareg på https://aareg-services.dev.intern.nav.no/swagger-ui/index.html

### Bruk av paw-aareg-client

**_gradle.build.kts_**

```kts
val tokenproviderVersion: String by project
val aaregClientVersion: String by project

dependencies {
implementation("no.nav.paw:tokenprovider:$tokenproviderVersion")
implementation("no.nav.paw:aareg-client:$aaregClientVersion")
}
```

### Klienten instansieres slik

```kt
import kotlinx.coroutines.runBlocking
import no.nav.paw.tokenprovider.OAuth2TokenProvider
import no.nav.paw.aareg.AaregClient

fun main() {
val url = "https://modapp-q1.adeo.no/aareg-services"
val tokenProvider = OAuth2TokenProvider(
// Token config
)

val aaregClient = AaregClient(url) { tokenProvider.getToken() }

val arbeidsforhold = runBlocking { aaregClient.hentArbeidsforhold("fnr", "callId") }
println(arbeidsforhold)
}
```
28 changes: 28 additions & 0 deletions lib/aareg-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
kotlin("jvm")
kotlin("plugin.serialization")
}

val jvmMajorVersion: String by project

dependencies {
api(libs.kotlinx.serialization.json)

implementation(libs.ktor.client.contentNegotiation)
implementation(libs.ktor.client.okhttp)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktor.client.logging)

testImplementation(libs.bundles.testLibsWithUnitTesting)
testImplementation(libs.ktor.client.mock)
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(jvmMajorVersion))
}
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
53 changes: 53 additions & 0 deletions lib/aareg-client/src/main/kotlin/no/nav/paw/aareg/AaregClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package no.nav.paw.aareg

import io.ktor.client.call.body
import io.ktor.client.plugins.ResponseException
import io.ktor.client.request.bearerAuth
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.statement.bodyAsText
import io.ktor.http.ContentType
import io.ktor.http.contentType
import io.ktor.serialization.JsonConvertException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.net.ConnectException

/**
* klient for å hente ut aktive arbeidsforhold på en person
*/

class AaregClient(
private val url: String,
private val getAccessToken: () -> String
) {
private val sikkerLogger: Logger = LoggerFactory.getLogger("tjenestekall")
private val logger: Logger = LoggerFactory.getLogger("paw-aareg-client")
private val httpClient = createHttpClient()

suspend fun hentArbeidsforhold(ident: String, callId: String): List<Arbeidsforhold> {
val token = getAccessToken()
try {
val payload = httpClient.get(url) {
contentType(ContentType.Application.Json)
bearerAuth(token)
header("Nav-Call-Id", callId)
header("Nav-Personident", ident)
}.also {
logger.info("Hentet arbeidsforhold fra aareg med status=${it.status}")
sikkerLogger.debug("Svar fra aareg-API: " + it.bodyAsText())
}.body<List<Arbeidsforhold>>()
return payload
} catch (responseException: ResponseException) {
logger.error("Hente arbeidsforhold callId=[$callId] feilet med http-kode ${responseException.response.status}")
throw responseException
} catch (connectException: ConnectException) {
logger.error("Hente arbeidsforhold callId=[$callId] feilet:", connectException)
throw connectException
} catch (jsonConvertException: JsonConvertException) {
logger.error("Hente arbeidsforhold callId=[$callId] feilet, kunne ikke lese JSON")
sikkerLogger.error("Hente arbeidsforhold callId=[$callId] feilet", jsonConvertException)
throw jsonConvertException
}
}
}
Loading

0 comments on commit 35548ae

Please sign in to comment.