Skip to content

Commit

Permalink
Debugging: logger for å se om url blir satt riktig under kall til kon…
Browse files Browse the repository at this point in the history
…toregister.
  • Loading branch information
d142796 committed Jun 3, 2024
1 parent 74596c8 commit 62d7ef1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package no.nav.arbeidsgiver.tiltakrefusjon

import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.KontoregisterProperties
import no.nav.security.token.support.client.core.ClientProperties
import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenService
import no.nav.security.token.support.client.spring.ClientConfigurationProperties
import no.nav.security.token.support.client.spring.oauth2.ClientConfigurationPropertiesMatcher
import no.nav.security.token.support.client.spring.oauth2.EnableOAuth2Client
import no.nav.security.token.support.client.spring.oauth2.OAuth2ClientRequestInterceptor
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -21,11 +24,14 @@ import java.util.*
@Configuration
@Profile("dev-gcp", "prod-gcp")
class SecurityClientConfiguration(
val restTemplateBuilder: RestTemplateBuilder,
val clientConfigurationProperties: ClientConfigurationProperties,
val oAuth2AccessTokenService: OAuth2AccessTokenService
val properties: KontoregisterProperties,
val restTemplateBuilder: RestTemplateBuilder,
val clientConfigurationProperties: ClientConfigurationProperties,
val oAuth2AccessTokenService: OAuth2AccessTokenService
) {

val log: Logger = LoggerFactory.getLogger(javaClass)

@Bean
fun påVegneAvSaksbehandlerGraphRestTemplate() = restTemplateForRegistration("aad-graph")

Expand All @@ -45,9 +51,31 @@ class SecurityClientConfiguration(
private fun restTemplateForRegistration(registration: String): RestTemplate {
val clientProperties = clientConfigurationProperties.registration[registration]
?: throw RuntimeException("could not find oauth2 client config for $registration")
return restTemplateBuilder
.additionalInterceptors(bearerTokenInterceptor(clientProperties, oAuth2AccessTokenService))
.build()
when(registration ) {
"sokos-kontoregister" -> {
val restTemplate: RestTemplate = restTemplateBuilder
.additionalInterceptors(
bearerTokenInterceptorKontoregister(
clientProperties,
oAuth2AccessTokenService
)
)
.build()
return restTemplate
}
else -> {
val restTemplate: RestTemplate = restTemplateBuilder
.additionalInterceptors(
bearerTokenInterceptor(
clientProperties,
oAuth2AccessTokenService
)
)
.build()
return restTemplate
}

}
}


Expand All @@ -61,4 +89,18 @@ class SecurityClientConfiguration(
execution.execute(request, body)
}
}

private fun bearerTokenInterceptorKontoregister(
clientProperties: ClientProperties,
oAuth2AccessTokenService: OAuth2AccessTokenService
): ClientHttpRequestInterceptor {
return ClientHttpRequestInterceptor { request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution ->
val response = oAuth2AccessTokenService.getAccessToken(clientProperties)
request.headers.setBearerAuth(response.accessToken!!)
request.headers.set("Nav-Consumer-Id",properties.consumerId)
request.headers.set("Nav-Call-Id",UUID.randomUUID().toString())
log.info("#### HEADERS: ${request.headers}");
execution.execute(request, body)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package no.nav.arbeidsgiver.tiltakrefusjon.okonomi
import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.request.KontoregisterRequest
import no.nav.arbeidsgiver.tiltakrefusjon.okonomi.response.KontoregisterResponse
import no.nav.arbeidsgiver.tiltakrefusjon.utils.ConditionalOnPropertyNotEmpty
import no.nav.security.token.support.client.core.ClientProperties
import no.nav.security.token.support.client.core.oauth2.OAuth2AccessTokenService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.HttpRequest
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.stereotype.Service
import org.springframework.web.client.RestClientException
import org.springframework.web.client.RestTemplate
Expand All @@ -22,31 +28,23 @@ import java.util.*
@ConditionalOnPropertyNotEmpty("tiltak-refusjon.kontoregister.uri")
class KontoregisterServiceImpl(
val properties: KontoregisterProperties,
@Qualifier("sokosRestTemplate") val restTemplate: RestTemplate
@Qualifier("sokosRestTemplate") val restTemplate: RestTemplate,
val restTemplateBuilder: RestTemplateBuilder,
) : KontoregisterService {

val log: Logger = LoggerFactory.getLogger(javaClass)

override fun hentBankkontonummer(bedriftNr: String): String? {
val requestEntity = lagRequest()
val url = "${properties.uri}/${bedriftNr}"
try {
log.warn("##### kall url: ${url} ");
log.warn("##### kall url: ${url} ${restTemplate}");
val responseMedKontonummerTilBedrift =
restTemplate.exchange<KontoregisterResponse>(url, HttpMethod.GET, requestEntity)
log.warn("##### kall headers: ${responseMedKontonummerTilBedrift.headers}");
restTemplate.exchange<KontoregisterResponse>(url, HttpMethod.GET)
return responseMedKontonummerTilBedrift?.body?.kontonr
} catch (e: RestClientException) {
log.warn("Kontoregister call feiler", e)
}
return null
}

private fun lagRequest(): HttpEntity<KontoregisterRequest> {
val headers = HttpHeaders()
headers["Nav-Consumer-Id"] = properties.consumerId
headers["Nav-Call-Id"] = UUID.randomUUID().toString()
val body = null
return HttpEntity(body, headers)
}
}

0 comments on commit 62d7ef1

Please sign in to comment.