generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PI-2397 Add allow list for domain event detail URLs
- Loading branch information
1 parent
da5bcca
commit 467e7b1
Showing
110 changed files
with
388 additions
and
576 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...messaging/src/main/kotlin/uk/gov/justice/digital/hmpps/detail/DomainEventDetailService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package uk.gov.justice.digital.hmpps.detail | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.core.ParameterizedTypeReference | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.client.RestClient | ||
import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent | ||
import java.net.URI | ||
|
||
@Service | ||
class DomainEventDetailService( | ||
@Qualifier("oauth2Client") val restClient: RestClient?, | ||
@Value("\${messaging.consumer.detail.urls:#{null}}") val allowedUrls: List<String>? | ||
) { | ||
fun validate(detailUrl: String?): URI { | ||
val uri = URI.create(requireNotNull(detailUrl) { "Detail URL must not be null" }) | ||
val baseUrl = "${uri.scheme}://${uri.authority}" | ||
require(allowedUrls == null || allowedUrls!!.contains(baseUrl)) { "Unexpected detail URL: $baseUrl" } | ||
return uri | ||
} | ||
|
||
final inline fun <reified T> getDetail(event: HmppsDomainEvent): T = | ||
getDetail(validate(event.detailUrl), object : ParameterizedTypeReference<T>() {}) | ||
|
||
final inline fun <reified T> getDetail(detailUrl: String?): T = | ||
getDetail(validate(detailUrl), object : ParameterizedTypeReference<T>() {}) | ||
|
||
fun <T> getDetail(uri: URI, type: ParameterizedTypeReference<T>): T = | ||
requireNotNull(restClient).get().uri(uri).retrieve().body<T>(type)!! | ||
|
||
final inline fun <reified T> getDetailResponse(event: HmppsDomainEvent): ResponseEntity<T> = | ||
getDetailResponse(validate(event.detailUrl), object : ParameterizedTypeReference<T>() {}) | ||
|
||
fun <T> getDetailResponse(uri: URI, type: ParameterizedTypeReference<T>): ResponseEntity<T> = | ||
requireNotNull(restClient).get().uri(uri).retrieve().toEntity<T>(type) | ||
} |
84 changes: 84 additions & 0 deletions
84
...aging/src/test/kotlin/uk/gov/justice/digital/hmpps/detail/DomainEventDetailServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package uk.gov.justice.digital.hmpps.detail | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.Answers | ||
import org.mockito.Mock | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.whenever | ||
import org.springframework.core.ParameterizedTypeReference | ||
import org.springframework.web.client.RestClient | ||
import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent | ||
import java.net.URI | ||
|
||
@ExtendWith(MockitoExtension::class) | ||
internal class DomainEventDetailServiceTest { | ||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
lateinit var client: RestClient | ||
|
||
lateinit var service: DomainEventDetailService | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
service = DomainEventDetailService(client, listOf("http://localhost:8080")) | ||
} | ||
|
||
@Test | ||
fun `missing URL is rejected`() { | ||
val exception = assertThrows<IllegalArgumentException> { | ||
service.getDetail<Any>( | ||
HmppsDomainEvent( | ||
eventType = "test", | ||
version = 1 | ||
) | ||
) | ||
} | ||
assertThat(exception.message, equalTo("Detail URL must not be null")) | ||
} | ||
|
||
@Test | ||
fun `invalid URL is rejected`() { | ||
assertThrows<IllegalArgumentException> { | ||
service.getDetail<Any>( | ||
HmppsDomainEvent( | ||
eventType = "test", | ||
version = 1, | ||
detailUrl = "invalid url" | ||
) | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `URL must be in allowed list`() { | ||
val exception = assertThrows<IllegalArgumentException> { | ||
service.getDetail<Any>( | ||
HmppsDomainEvent( | ||
eventType = "test", | ||
version = 1, | ||
detailUrl = "https://example.com" | ||
) | ||
) | ||
} | ||
assertThat(exception.message, equalTo("Unexpected detail URL: https://example.com")) | ||
} | ||
|
||
@Test | ||
fun `API is called if URL is valid`() { | ||
whenever(client.get().uri(any<URI>()).retrieve().body(any<ParameterizedTypeReference<String>>())) | ||
.thenReturn("API Response") | ||
val response = service.getDetail<String>( | ||
HmppsDomainEvent( | ||
eventType = "test", | ||
version = 1, | ||
detailUrl = "http://localhost:8080" | ||
) | ||
) | ||
assertThat(response, equalTo("API Response")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
...emises-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/config/RestClientConfig.kt
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
...n/uk/gov/justice/digital/hmpps/integrations/approvedpremises/ApprovedPremisesApiClient.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.