Skip to content

Commit

Permalink
Oppdaterer dependencies og gradle. Bytter ut kluent og kotlinx serial…
Browse files Browse the repository at this point in the history
…ization.
  • Loading branch information
chris-santa committed Apr 16, 2024
1 parent 7539b86 commit f04e623
Show file tree
Hide file tree
Showing 50 changed files with 723 additions and 542 deletions.
8 changes: 3 additions & 5 deletions azure-exchange/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
`maven-publish`
`java-library`
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
Expand All @@ -14,13 +13,12 @@ dependencies {
implementation(Ktor.serverAuthJwt)
implementation(Ktor.clientApache)
implementation(Ktor.clientContentNegotiation)
implementation(Ktor.serializationKotlinxJson)
implementation(Ktor.jackson)
implementation(Ktor.clientJson)
implementation(Ktor.serialization)
implementation(Ktor.serverNetty)
implementation(Nimbusds.joseJwt)
testImplementation(kotlin("test-junit5"))
testImplementation(Kluent.kluent)
testImplementation(Mockk.mockk)
testImplementation(Ktor.clientMock)
testImplementation(Ktor.serverTestHost)
Expand Down Expand Up @@ -65,8 +63,8 @@ publishing {
}
}

java {
toolchain {
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.tms.token.support.azure.exchange

// Proxy for System environment which allows for mocking or overwriting default env
object AzureEnvironment {
private val baseEnv = System.getenv()

private val env = mutableMapOf<String, String>()

init {
env.putAll(baseEnv)
}

fun get(name: String) = env[name]

fun extend(envMap: Map<String, String>) {
env.putAll(envMap)
}

fun reset() {
env.clear()
env.putAll(baseEnv)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package no.nav.tms.token.support.azure.exchange.config

import no.nav.tms.token.support.azure.exchange.AzureEnvironment

internal class Environment (
val azureClientId: String = getAzureEnvVar("AZURE_APP_CLIENT_ID"),
val azureTenantId: String = getAzureEnvVar("AZURE_APP_TENANT_ID"),
Expand All @@ -8,7 +10,5 @@ internal class Environment (
val azureOpenidTokenEndpoint: String = getAzureEnvVar("AZURE_OPENID_CONFIG_TOKEN_ENDPOINT")
)

private fun getAzureEnvVar(varName: String): String {
return System.getenv(varName)
?: throw IllegalArgumentException("Fant ikke $varName for azure. Påse at nais.yaml er konfigurert riktig.")
}
private fun getAzureEnvVar(varName: String) = AzureEnvironment.get(varName)
?: throw IllegalArgumentException("Fant ikke $varName for azure. Påse at nais.yaml er konfigurert riktig.")
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package no.nav.tms.token.support.azure.exchange.config

import com.fasterxml.jackson.databind.DeserializationFeature
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import io.ktor.serialization.jackson.*
import org.apache.http.impl.conn.SystemDefaultRoutePlanner
import java.net.ProxySelector

internal object HttpClientBuilder {
internal fun buildHttpClient(enableDefaultProxy: Boolean): HttpClient {
return HttpClient(Apache) {
install(ContentNegotiation) {
json(kotlinxSerializer())
jackson {
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}

install(HttpTimeout)
Expand All @@ -24,12 +26,6 @@ internal object HttpClientBuilder {
}
}

private fun kotlinxSerializer() =
Json {
ignoreUnknownKeys = true
}


private fun HttpClientConfig<ApacheEngineConfig>.enableSystemDefaultProxy() {
engine {
customizeClient { setRoutePlanner(SystemDefaultRoutePlanner(ProxySelector.getDefault())) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package no.nav.tms.token.support.azure.exchange.consumer

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import com.fasterxml.jackson.annotation.JsonAlias

@Serializable
internal data class AzureTokenResponse(
@SerialName("access_token") val accessToken: String,
@SerialName("token_type") val tokenType: String,
@SerialName("expires_in") val expiresIn: Int,
@SerialName("ext_expires_in") val extExpiresIn: Int
@JsonAlias("access_token") val accessToken: String,
@JsonAlias("token_type") val tokenType: String,
@JsonAlias("expires_in") val expiresIn: Int,
@JsonAlias("ext_expires_in") val extExpiresIn: Int
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package no.nav.tms.token.support.azure.exchange

import com.nimbusds.jwt.SignedJWT
import io.kotest.matchers.collections.shouldContain
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.mockk.*
import kotlinx.coroutines.runBlocking
import no.nav.tms.token.support.azure.exchange.consumer.AzureConsumer
import no.nav.tms.token.support.azure.exchange.service.CachingAzureService
import no.nav.tms.token.support.azure.exchange.service.NonCachingAzureService
import org.amshove.kluent.`should be equal to`
import org.amshove.kluent.`should contain`
import org.amshove.kluent.`should not be equal to`
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test

Expand Down Expand Up @@ -41,14 +41,14 @@ internal class AzureServiceTest {
nonCachingazureService.getAccessToken(target)
}

result `should be equal to` exchangedToken
result shouldBe exchangedToken

val signedJwt = assertion.captured.let { SignedJWT.parse(it) }
val claims = signedJwt.jwtClaimsSet

claims.audience `should contain` jwtAudience
claims.issuer `should be equal to` clientId
claims.subject `should be equal to` clientId
claims.audience shouldContain jwtAudience
claims.issuer shouldBe clientId
claims.subject shouldBe clientId
}

@Test
Expand All @@ -65,14 +65,14 @@ internal class AzureServiceTest {
cachingAzureService.getAccessToken(target)
}

result `should be equal to` exchangedToken
result shouldBe exchangedToken

val signedJwt = assertion.captured.let { SignedJWT.parse(it) }
val claims = signedJwt.jwtClaimsSet

claims.audience `should contain` jwtAudience
claims.issuer `should be equal to` clientId
claims.subject `should be equal to` clientId
claims.audience shouldContain jwtAudience
claims.issuer shouldBe clientId
claims.subject shouldBe clientId
}

@Test
Expand Down Expand Up @@ -137,9 +137,9 @@ internal class AzureServiceTest {
coVerify(exactly = 1) {azureConsumer.fetchToken(any(), target1) }
coVerify(exactly = 1) {azureConsumer.fetchToken(any(), target2) }

result1 `should be equal to` result3
result2 `should be equal to` result4
result1 `should not be equal to` result2
result3 `should not be equal to` result4
result1 shouldBe result3
result2 shouldBe result4
result1 shouldNotBe result2
result3 shouldNotBe result4
}
}
6 changes: 2 additions & 4 deletions azure-validation-mock/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
`maven-publish`
`java-library`
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
Expand All @@ -15,7 +14,6 @@ dependencies {
implementation(Ktor.clientJson)
implementation(Nimbusds.joseJwt)
testImplementation(kotlin("test-junit5"))
testImplementation(Kluent.kluent)
testImplementation(Mockk.mockk)
testImplementation(Ktor.clientMock)
testImplementation(Ktor.serverTestHost)
Expand Down Expand Up @@ -60,8 +58,8 @@ publishing {
}
}

java {
toolchain {
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.tms.token.support.azure.validation.mock

import io.kotest.matchers.shouldBe
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
Expand All @@ -10,7 +11,6 @@ import io.ktor.server.routing.*
import io.ktor.server.testing.*
import no.nav.tms.token.support.azure.validation.AzureAuthenticator
import no.nav.tms.token.support.azure.validation.AzurePrincipal
import org.amshove.kluent.`should be equal to`
import org.junit.jupiter.api.Test

internal class AzureAuthTest {
Expand All @@ -34,7 +34,7 @@ internal class AzureAuthTest {

val response = client.get("/test")

response.status `should be equal to` HttpStatusCode.Unauthorized
response.status shouldBe HttpStatusCode.Unauthorized
}

@Test
Expand All @@ -53,8 +53,8 @@ internal class AzureAuthTest {

val response = client.get("/test")

response.status `should be equal to` HttpStatusCode.OK
response.body<String>() `should be equal to` jwtOverrideString
response.status shouldBe HttpStatusCode.OK
response.body<String>() shouldBe jwtOverrideString
}

@Test
Expand All @@ -73,8 +73,8 @@ internal class AzureAuthTest {

val response = client.get("/test")

response.status `should be equal to` HttpStatusCode.OK
response.body<String>().isNotBlank() `should be equal to` true
response.status shouldBe HttpStatusCode.OK
response.body<String>().isNotBlank() shouldBe true
}

@Test
Expand All @@ -94,7 +94,7 @@ internal class AzureAuthTest {

val response = client.get("/test")

response.status `should be equal to` HttpStatusCode.Unauthorized
response.status shouldBe HttpStatusCode.Unauthorized
}

private fun Application.testApi(authConfig: Application.() -> Unit) {
Expand Down
12 changes: 7 additions & 5 deletions azure-validation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import org.gradle.internal.impldep.com.amazonaws.util.json.Jackson

plugins {
`maven-publish`
`java-library`
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
api(kotlin("stdlib-jdk8"))
implementation(Logback.classic)
implementation(JacksonDatatype.moduleKotlin)
implementation(JacksonDatatype.datatypeJsr310)
implementation(KotlinLogging.logging)
implementation(Ktor.serverAuth)
implementation(Ktor.serverAuthJwt)
implementation(Ktor.clientApache)
implementation(Ktor.clientJson)
implementation(Ktor.serialization)
implementation(Ktor.clientContentNegotiation)
implementation(Ktor.serializationKotlinxJson)
implementation(Ktor.jackson)
implementation(Nimbusds.joseJwt)
testImplementation(kotlin("test-junit5"))
testImplementation(Kluent.kluent)
testImplementation(Mockk.mockk)
testImplementation(Ktor.clientMock)
testImplementation(Ktor.serverTestHost)
Expand Down Expand Up @@ -63,8 +65,8 @@ publishing {
}
}

java {
toolchain {
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package no.nav.tms.token.support.azure.validation

// Proxy for System environment which allows for mocking or overwriting default env
object AzureEnvironment {
private val baseEnv = System.getenv()

private val env = mutableMapOf<String, String>()

init {
env.putAll(baseEnv)
}

fun get(name: String) = env[name]

fun extend(envMap: Map<String, String>) {
env.putAll(envMap)
}

fun reset() {
env.clear()
env.putAll(baseEnv)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package no.nav.tms.token.support.azure.validation.install

import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import io.ktor.serialization.jackson.*
import org.apache.http.impl.conn.SystemDefaultRoutePlanner
import java.net.ProxySelector

internal object HttpClientBuilder {
internal fun build(enableDefaultProxy: Boolean): HttpClient {
return HttpClient(Apache) {
install(ContentNegotiation) {
json(kotlinxSerializer())
jackson {
configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
}
}
install(HttpTimeout)

Expand All @@ -23,11 +25,6 @@ internal object HttpClientBuilder {
}
}

private fun kotlinxSerializer() =
Json {
ignoreUnknownKeys = true
}

private fun HttpClientConfig<ApacheEngineConfig>.enableSystemDefaultProxy() {
engine {
customizeClient { setRoutePlanner(SystemDefaultRoutePlanner(ProxySelector.getDefault())) }
Expand Down
Loading

0 comments on commit f04e623

Please sign in to comment.