Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add readTimeout to http connections #63

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<version>3.2.5</version>
<relativePath/>
</parent>

Expand All @@ -29,7 +29,7 @@
<maven.exec.skip>false</maven.exec.skip>
<!--end standard properties-->

<kotlin.version>1.9.22</kotlin.version>
<kotlin.version>1.9.23</kotlin.version>

</properties>

Expand Down Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.7.3</version>
<version>1.8.0</version>
</dependency>

<!-- TEST -->
Expand All @@ -91,7 +91,7 @@
<dependency>
<groupId>org.mockito.kotlin</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>5.2.1</version>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -103,7 +103,7 @@
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.3.1</version>
<version>3.5.3</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -168,7 +168,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version>
<version>0.8.12</version>
<executions>
<execution>
<id>before-unit-test-execution</id>
Expand Down Expand Up @@ -237,7 +237,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<version>3.2.5</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<argLine>${surefire.jacoco.args}</argLine>
Expand All @@ -251,7 +251,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.3</version>
<version>3.2.5</version>
<configuration>
<argLine>${failsafe.jacoco.args}</argLine>
<groups>contract</groups>
Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/no/fdk/userapi/adapter/AltinnAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.net.URI

private val logger = LoggerFactory.getLogger(AltinnAdapter::class.java)

@Service
class AltinnAdapter(private val hostProperties: HostProperties) {
private val sixtySeconds = 60000
private val twentySeconds = 20000

private fun altinnStream(url: URL): InputStream =
with(url.openConnection() as HttpURLConnection) {
connectTimeout = sixtySeconds
private fun altinnStream(uri: URI): InputStream =
with(uri.toURL().openConnection() as HttpURLConnection) {
connectTimeout = twentySeconds
readTimeout = twentySeconds
connect()
if (HttpStatus.resolve(responseCode)?.is2xxSuccessful == true) {
inputStream
Expand All @@ -31,9 +32,9 @@ class AltinnAdapter(private val hostProperties: HostProperties) {
}

private fun getReportees(socialSecurityNumber: String, serviceCode: String): List<AltinnSubject?> {
val url = URL("${hostProperties.altinnProxyHost}/api/serviceowner/reportees?ForceEIAuthentication&subject=$socialSecurityNumber&servicecode=$serviceCode&serviceedition=1&\$top=1000")
val uri = URI("${hostProperties.altinnProxyHost}/api/serviceowner/reportees?ForceEIAuthentication&subject=$socialSecurityNumber&servicecode=$serviceCode&serviceedition=1&\$top=1000")
return try {
jacksonObjectMapper().readValue(altinnStream(url))
jacksonObjectMapper().readValue(altinnStream(uri))
} catch (ex: Exception) {
logger.error("Unable to get reportees from Altinn", ex)
emptyList()
Expand All @@ -50,8 +51,8 @@ class AltinnAdapter(private val hostProperties: HostProperties) {

fun getRights(socialSecurityNumber: String, orgNumber: String): AltinnRightsResponse? =
try {
val url = URL("${hostProperties.altinnProxyHost}/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=${socialSecurityNumber}&reportee=${orgNumber}&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27")
jacksonObjectMapper().readValue(altinnStream(url))
val uri = URI("${hostProperties.altinnProxyHost}/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=${socialSecurityNumber}&reportee=${orgNumber}&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27")
jacksonObjectMapper().readValue(altinnStream(uri))
} catch (ex: Exception) {
logger.error("Unable to get rights from Altinn", ex)
null
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/no/fdk/userapi/adapter/TermsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.net.HttpURLConnection
import java.net.URL
import java.net.URI

private val logger = LoggerFactory.getLogger(TermsAdapter::class.java)

Expand All @@ -18,11 +18,12 @@ class TermsAdapter(
private val fiveSeconds = 5000

fun orgAcceptedTermsVersion(organization: String): String {
val url = URL("${hostProperties.termsHost}/terms/org/$organization/version")
val uri = URI("${hostProperties.termsHost}/terms/org/$organization/version")
try {
with(url.openConnection() as HttpURLConnection) {
with(uri.toURL().openConnection() as HttpURLConnection) {
setRequestProperty("X-API-KEY", securityProperties.userApiKey)
connectTimeout = fiveSeconds
readTimeout = fiveSeconds
connect()
if (HttpStatus.resolve(responseCode)?.is2xxSuccessful == true) {
inputStream.bufferedReader().use {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ spring:
config.activate.on-profile: contract-test
application:
hosts:
altinnProxyHost: http://localhost:5000/altinn
termsHost: http://localhost:5000/terms
altinnProxyHost: http://localhost:5555/altinn
termsHost: http://localhost:5555/terms
whitelists:
adminList: ${ADMIN_LIST:10987654321}
orgNrWhitelist: ${ORGNR_WHITELIST:920210023,910258028,123456789}
Expand Down
38 changes: 20 additions & 18 deletions src/test/kotlin/no/fdk/userapi/utils/ContractMock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package no.fdk.userapi.utils
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.ok
import com.github.tomakehurst.wiremock.client.WireMock.okJson
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import org.slf4j.LoggerFactory
import java.io.IOException
Expand All @@ -15,7 +17,7 @@ fun startMockServer() {
if(!mockserver.isRunning) {
mockserver.stubFor(
get(urlEqualTo("/ping"))
.willReturn(aResponse().withStatus(200)))
.willReturn(ok()))

mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=12345678901&servicecode=5755&serviceedition=1&\$top=1000"))
Expand All @@ -25,42 +27,42 @@ fun startMockServer() {
.willReturn(aResponse().withStatus(404)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5755&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5756&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5977&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&servicecode=5755&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&servicecode=5756&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))

mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=10987654321&reportee=920210023&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_0)))
.willReturn(okJson(ALTINN_RIGHTS_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=11223344556&reportee=910258028&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_1)))
.willReturn(okJson(ALTINN_RIGHTS_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=11223344556&reportee=123456789&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_2)))
.willReturn(okJson(ALTINN_RIGHTS_2)))

mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/920210023/version"))
.willReturn(aResponse().withStatus(200).withBody("1.2.3")))
.willReturn(ok("1.2.3")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/910258028/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.0")))
.willReturn(ok("1.0.0")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/123456789/version"))
.willReturn(aResponse().withStatus(404)))
Expand All @@ -70,16 +72,16 @@ fun startMockServer() {
.willReturn(aResponse().withStatus(404)))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/987592567/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.1")))
.willReturn(ok("1.0.1")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/923954791/version"))
.willReturn(aResponse().withStatus(200).withBody("12.16.11")))
.willReturn(ok("12.16.11")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/974760673/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.1")))
.willReturn(ok("1.0.1")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/974761076/version"))
.willReturn(aResponse().withStatus(200).withBody("1.1.1")))
.willReturn(ok("1.1.1")))

mockserver.start()
}
Expand All @@ -101,7 +103,7 @@ abstract class WiremockContext {
startMockServer()

try {
val con = URL("http://localhost:5000/ping").openConnection() as HttpURLConnection
val con = URL("http://localhost:5555/ping").openConnection() as HttpURLConnection
con.connect()
if (con.responseCode != 200) {
logger.debug("Ping to mock server failed")
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/no/fdk/userapi/utils/TestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package no.fdk.userapi.utils
import no.fdk.userapi.model.AltinnOrganization
import no.fdk.userapi.model.AltinnReporteeType

const val LOCAL_SERVER_PORT = 5000
const val LOCAL_SERVER_PORT = 5555
const val API_TEST_PORT = 5050

const val EDITOR_ROLE: String = "editor"
Expand Down
Loading