Skip to content

Commit

Permalink
Spring boot 3 (#661)
Browse files Browse the repository at this point in the history
* Oppgraderer Spring Boot-versjon

* Tar i bruk ny statuskode

* Setter jvmtarget til 17

* Fikser sånn at webClient kompilerer, vet ikke om den virker ennå

* Det er innebyggd retry på service unavailable i apache httpclient v5 med 1 retry

* Fikset kafkatester
Litt uklart hvorfor de begynte å feile

* Bumper til v2

* Oppdaterer pom og fjerner bygg for spring3-branch

* Tar i bruk mockk-jvm

* kotlin module jackson

---------

Co-authored-by: Johan Blomgren <[email protected]>
  • Loading branch information
madsop-nav and blommish authored May 8, 2023
1 parent 6a7ba52 commit 6b28bd8
Show file tree
Hide file tree
Showing 28 changed files with 167 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
run: |
export GIT_COMMIT_HASH=$(git log -n 1 --pretty=format:'%h')
export GIT_COMMIT_DATE=$(git log -1 --pretty='%ad' --date=format:'%Y%m%d%H%M%S')
export VERSION=1.${GIT_COMMIT_DATE}_${GIT_COMMIT_HASH}
export VERSION=2.${GIT_COMMIT_DATE}_${GIT_COMMIT_HASH}
echo "Setting version $VERSION"
mvn --settings .m2/maven-settings.xml versions:set -DnewVersion="$VERSION"
mvn --settings .m2/maven-settings.xml versions:commit
Expand Down
60 changes: 0 additions & 60 deletions .github/workflows/build-spring3.yml

This file was deleted.

9 changes: 5 additions & 4 deletions http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -76,7 +76,7 @@
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<artifactId>mockk-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -86,7 +86,7 @@
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<artifactId>wiremock-jre8-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -132,6 +132,7 @@
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
<jvmTarget>17</jvmTarget>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class RetryOAuth2HttpClient(
private val retryExceptions = setOf(
SocketException::class,
SocketTimeoutException::class,
HttpServerErrorException.ServiceUnavailable::class,
HttpServerErrorException.GatewayTimeout::class,
HttpServerErrorException.BadGateway::class
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package no.nav.familie.http.config

import org.apache.http.HttpHost
import org.apache.http.HttpRequest
import org.apache.http.client.HttpClient
import org.apache.http.client.config.RequestConfig
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.impl.conn.DefaultProxyRoutePlanner
import org.apache.http.protocol.HttpContext
import org.apache.hc.client5.http.classic.HttpClient
import org.apache.hc.client5.http.config.RequestConfig
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner
import org.apache.hc.core5.http.HttpHost
import org.apache.hc.core5.http.io.SocketConfig
import org.apache.hc.core5.http.protocol.HttpContext
import org.apache.hc.core5.util.Timeout
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.web.client.RestTemplateCustomizer
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory
Expand All @@ -17,30 +19,37 @@ interface INaisProxyCustomizer : RestTemplateCustomizer

@Component
class NaisProxyCustomizer(
@Value("\${familie.nais.proxy.connectTimeout:15000}") val connectTimeout: Int,
@Value("\${familie.nais.proxy.socketTimeout:15000}") val socketTimeout: Int,
@Value("\${familie.nais.proxy.requestTimeout:15000}") val requestTimeout: Int
@Value("\${familie.nais.proxy.connectTimeout:15000}") val connectTimeout: Long,
@Value("\${familie.nais.proxy.socketTimeout:15000}") val socketTimeout: Long,
@Value("\${familie.nais.proxy.requestTimeout:15000}") val requestTimeout: Long
) : INaisProxyCustomizer {

override fun customize(restTemplate: RestTemplate) {
val proxy = HttpHost("webproxy-nais.nav.no", 8088)
val client: HttpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.setConnectionRequestTimeout(requestTimeout)
.setConnectTimeout(Timeout.ofSeconds(connectTimeout))
.setConnectionRequestTimeout(Timeout.ofSeconds(requestTimeout))
.build()
)
.setConnectionManager(
PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultSocketConfig(
SocketConfig.custom()
.setSoTimeout(Timeout.ofMilliseconds(socketTimeout))
.build()
)
.build()
)
.setRoutePlanner(object : DefaultProxyRoutePlanner(proxy) {

public override fun determineProxy(
target: HttpHost,
request: HttpRequest,
context: HttpContext
): HttpHost? {
return if (target.hostName.contains("microsoft")) {
super.determineProxy(target, request, context)
super.determineProxy(target, context)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package no.nav.familie.http.interceptor

import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import no.nav.familie.log.auditlogger.AuditLogger
import no.nav.familie.sikkerhet.OIDCUtil
import org.slf4j.LoggerFactory
import org.springframework.context.annotation.Import
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@Component
@Import(OIDCUtil::class)
class InternLoggerInterceptor(private val oidcUtil: OIDCUtil) : HandlerInterceptorAdapter() {
class InternLoggerInterceptor(private val oidcUtil: OIDCUtil) : HandlerInterceptor {

override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
val ansvarligSaksbehandler: String = hentSaksbehandler(oidcUtil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package no.nav.familie.http.interceptor

import org.slf4j.LoggerFactory
import org.springframework.http.HttpRequest
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatus.Series.CLIENT_ERROR
import org.springframework.http.HttpStatus.Series.SERVER_ERROR
import org.springframework.http.HttpStatusCode
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse
Expand All @@ -24,16 +22,16 @@ class TimingAndLoggingClientHttpRequestInterceptor : ClientHttpRequestIntercepto
companion object {
private val LOG = LoggerFactory.getLogger(TimingAndLoggingClientHttpRequestInterceptor::class.java)

private fun log(request: HttpRequest, code: HttpStatus, timer: StopWatch) {
private fun log(request: HttpRequest, code: HttpStatusCode, timer: StopWatch) {
if (hasError(code)) {
LOG.warn("{} - {} - ({}). Dette tok {}ms", request.methodValue, request.uri, code, timer.totalTimeMillis)
} else {
LOG.info("{} - {} - ({}). Dette tok {}ms", request.methodValue, request.uri, code, timer.totalTimeMillis)
}
}

private fun hasError(code: HttpStatus): Boolean {
return code.series() == CLIENT_ERROR || code.series() == SERVER_ERROR
private fun hasError(code: HttpStatusCode): Boolean {
return code.is4xxClientError || code.is5xxServerError
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class RetryOAuth2HttpClientTest {
internal fun `503 - skal prøve på nytt`() {
stub(WireMock.aResponse().withStatus(503))
post()
wireMockServer.verify(3, RequestPatternBuilder.allRequests())
wireMockServer.verify(2, RequestPatternBuilder.allRequests())
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<artifactId>mockk-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import io.mockk.clearAllMocks
import io.mockk.impl.annotations.MockK
import org.apache.kafka.clients.consumer.Consumer
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.catchThrowable
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
Expand Down Expand Up @@ -34,25 +35,30 @@ class KafkaErrorHandlerTest {

@Test
fun `skal stoppe container hvis man mottar feil med en tom liste med records`() {
assertThatThrownBy { errorHandler.handleRemaining(RuntimeException("Feil i test"), emptyList(), consumer, container) }
.hasMessageNotContaining("Feil i test")
.hasMessageContaining("Sjekk securelogs for mer info")
val throwable = catchThrowable {
errorHandler.handleRemaining(RuntimeException("Feil i test"), emptyList(), consumer, container)
}

assertThat(throwable)
.hasStackTraceContaining("Sjekk securelogs for mer info")
.hasCauseExactlyInstanceOf(Exception::class.java)
assertThat(throwable.stackTraceToString()).doesNotContain("Feil i test")
}

@Test
fun `skal stoppe container hvis man mottar feil med en liste med records`() {
val consumerRecord = ConsumerRecord("topic", 1, 1, 1, "record")
assertThatThrownBy {
val throwable = catchThrowable {
errorHandler.handleRemaining(
RuntimeException("Feil i test"),
listOf(consumerRecord),
consumer,
container
)
}
.hasMessageNotContaining("Feil i test")
.hasMessageContaining("Sjekk securelogs for mer info")
assertThat(throwable)
.hasStackTraceContaining("Sjekk securelogs for mer info")
.hasCauseExactlyInstanceOf(Exception::class.java)
assertThat(throwable.stackTraceToString()).doesNotContain("Feil i test")
}
}
4 changes: 2 additions & 2 deletions leader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<artifactId>wiremock-jre8-standalone</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -43,7 +43,7 @@
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<artifactId>mockk-jvm</artifactId>
<scope>test</scope>
</dependency>

Expand Down
6 changes: 3 additions & 3 deletions log/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -62,7 +62,7 @@
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<artifactId>mockk-jvm</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package no.nav.familie.log.auditlogger

import jakarta.servlet.http.HttpServletRequest
import org.slf4j.LoggerFactory
import org.springframework.http.HttpMethod
import org.springframework.web.reactive.function.client.ClientRequest
import javax.servlet.http.HttpServletRequest

object AuditLogger {

Expand Down
14 changes: 7 additions & 7 deletions log/src/main/java/no/nav/familie/log/filter/LogFilter.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package no.nav.familie.log.filter

import jakarta.servlet.FilterChain
import jakarta.servlet.FilterConfig
import jakarta.servlet.ServletException
import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpFilter
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import no.nav.familie.log.IdUtils
import no.nav.familie.log.NavHttpHeaders
import no.nav.familie.log.mdc.MDCConstants.MDC_CALL_ID
Expand All @@ -11,13 +18,6 @@ import org.slf4j.MDC
import java.io.EOFException
import java.io.IOException
import java.util.function.Supplier
import javax.servlet.FilterChain
import javax.servlet.FilterConfig
import javax.servlet.ServletException
import javax.servlet.http.Cookie
import javax.servlet.http.HttpFilter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class LogFilter(
/**
Expand Down
Loading

0 comments on commit 6b28bd8

Please sign in to comment.