Skip to content

Commit

Permalink
Lagt til felles http-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
naviktthomas committed Nov 20, 2024
1 parent 77cd3f8 commit 27fce72
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/http-client-utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
kotlin("jvm")
}

val jvmMajorVersion: String by project
val jvmVersion = JavaVersion.valueOf("VERSION_$jvmMajorVersion")

dependencies {
implementation(project(":lib:hoplite-config"))
implementation(libs.ktor.serialization.jackson)
implementation(libs.jackson.datatypeJsr310)
implementation(libs.jackson.kotlin)
implementation(libs.ktor.client.contentNegotiation)
implementation(libs.ktor.client.cio)
api(libs.nav.common.tokenClient)
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(jvmVersion.majorVersion)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.nav.paw.client.config

const val AZURE_M2M_CONFIG = "azure_m2m_config.toml"

data class AzureAdM2MConfig(
val tokenEndpointUrl: String,
val clientId: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package no.nav.paw.client.factory

import com.nimbusds.jose.jwk.KeyUse
import com.nimbusds.jose.jwk.RSAKey
import no.nav.common.token_client.builder.AzureAdTokenClientBuilder
import no.nav.common.token_client.cache.CaffeineTokenCache
import no.nav.common.token_client.client.AzureAdMachineToMachineTokenClient
import no.nav.paw.client.config.AzureAdM2MConfig
import no.nav.paw.config.env.Local
import no.nav.paw.config.env.RuntimeEnvironment
import no.nav.paw.config.env.currentRuntimeEnvironment
import java.security.KeyPairGenerator
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey

fun createAzureAdM2MTokenClient(
runtimeEnvironment: RuntimeEnvironment = currentRuntimeEnvironment,
azureProviderConfig: AzureAdM2MConfig
): AzureAdMachineToMachineTokenClient =
when (runtimeEnvironment) {
is Local -> AzureAdTokenClientBuilder.builder()
.withClientId(azureProviderConfig.clientId)
.withPrivateJwk(createMockRSAKey("azure"))
.withTokenEndpointUrl(azureProviderConfig.tokenEndpointUrl)
.buildMachineToMachineTokenClient()

else -> AzureAdTokenClientBuilder.builder()
.withNaisDefaults()
.withCache(CaffeineTokenCache())
.buildMachineToMachineTokenClient()
}

private fun createMockRSAKey(keyID: String): String? = KeyPairGenerator
.getInstance("RSA").let {
it.initialize(2048)
it.generateKeyPair()
}.let {
RSAKey.Builder(it.public as RSAPublicKey)
.privateKey(it.private as RSAPrivateKey)
.keyUse(KeyUse.SIGNATURE)
.keyID(keyID)
.build()
.toJSONString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package no.nav.paw.client.factory

import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.HttpClientEngine
import io.ktor.client.engine.HttpClientEngineConfig
import io.ktor.client.engine.HttpClientEngineFactory
import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.serialization.jackson.jackson

fun createHttpClient(
engineFactory: HttpClientEngineFactory<HttpClientEngineConfig> = CIO,
block: HttpClientConfig<*>.() -> Unit = {
install(ContentNegotiation) {
jackson {
configureJackson()
}
}
}
): HttpClient {
return HttpClient(engineFactory, block)
}

fun createHttpClient(
engine: HttpClientEngine,
block: HttpClientConfig<*>.() -> Unit = {
install(ContentNegotiation) {
jackson {
configureJackson()
}
}
}
): HttpClient {
return HttpClient(engine, block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package no.nav.paw.client.factory

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule

fun createObjectMapper(): ObjectMapper {
return jacksonObjectMapper().apply {
configureJackson()
}
}

fun ObjectMapper.configureJackson() {
setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL)
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
registerModule(JavaTimeModule())
kotlinModule {
withReflectionCacheSize(512)
disable(KotlinFeature.NullIsSameAsDefault)
disable(KotlinFeature.SingletonSupport)
disable(KotlinFeature.StrictNullChecks)
enable(KotlinFeature.NullToEmptyCollection)
enable(KotlinFeature.NullToEmptyMap)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clientId = "${AZURE_APP_CLIENT_ID}"
tokenEndpointUrl = "${AZURE_OPENID_CONFIG_TOKEN_ENDPOINT}"
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include(
"lib:hoplite-config",
"lib:error-handling",
"lib:security",
"lib:http-client-utils",
"lib:kafka",
"lib:kafka-streams",
"lib:kafka-key-generator-client",
Expand Down

0 comments on commit 27fce72

Please sign in to comment.