This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
SDIT-1113: 🔒️ Secure automated refresh endpoint and test #511
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
82 changes: 82 additions & 0 deletions
82
src/test/kotlin/uk/gov/justice/digital/hmpps/prisonersearch/resource/ResourceSecurityTest.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,82 @@ | ||
package uk.gov.justice.digital.hmpps.prisonersearch.resource | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.context.ApplicationContext | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo | ||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping | ||
import uk.gov.justice.digital.hmpps.prisonersearch.integration.IntegrationTest | ||
import java.io.File | ||
import kotlin.reflect.full.memberFunctions | ||
|
||
class ResourceSecurityTest : IntegrationTest() { | ||
@Autowired | ||
private lateinit var context: ApplicationContext | ||
|
||
private val unprotectedDefaultMethods = setOf( | ||
"GET /v3/api-docs.yaml", | ||
"GET /swagger-ui.html", | ||
"GET /v3/api-docs", | ||
"GET /v3/api-docs/swagger-config", | ||
" /error", | ||
) | ||
private val testClasses = setOf( | ||
PrisonerSearchByBookingIdsResourceTest::class, | ||
PrisonerSearchByReleaseDateResourceTest::class, | ||
PossibleMatchesSearchResourceTest::class, | ||
PrisonerSearchBySinglePrisonResourceTest::class, | ||
PrisonerMatchResourceTest::class, | ||
RestrictedPatientsSearchResourceTest::class, | ||
PrisonerSearchByPrisonerNumbersResourceTest::class, | ||
PrisonerSearchResourceTest::class, | ||
) | ||
|
||
@Test | ||
fun `Ensure all endpoints protected with PreAuthorize`() { | ||
// need to exclude any that are forbidden in helm configuration | ||
val exclusions = File("helm_deploy").walk().filter { it.name.equals("values.yaml") }.flatMap { file -> | ||
file.readLines().map { line -> | ||
line.takeIf { it.contains("location") }?.substringAfter("location ")?.substringBefore(" {") | ||
} | ||
}.filterNotNull().flatMap { path -> listOf("GET", "POST", "PUT", "DELETE").map { "$it $path" } } | ||
.toMutableSet().also { | ||
it.addAll(unprotectedDefaultMethods) | ||
} | ||
|
||
testClasses.flatMap { clazz -> | ||
clazz.nestedClasses.toMutableList().apply { add(clazz) }.flatMap { testClazz -> | ||
testClazz.memberFunctions | ||
.filter { it.name.contains("access forbidden") } | ||
.map { | ||
it.name | ||
.substringAfter("for endpoint ") | ||
.substringBefore(" when no role") | ||
.replace("#", "/") | ||
} | ||
} | ||
}.also { exclusions.addAll(it) } | ||
|
||
context.getBeansOfType(RequestMappingHandlerMapping::class.java).forEach { (_, mapping) -> | ||
mapping.handlerMethods.forEach { (mappingInfo, method) -> | ||
val classAnnotation = method.beanType.getAnnotation(PreAuthorize::class.java) | ||
val annotation = method.getMethodAnnotation(PreAuthorize::class.java) | ||
println("Found $mappingInfo with class annotation $classAnnotation and annotation $annotation") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was this line a temporary data dump? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes it was, will remove in next PR |
||
if (classAnnotation == null && annotation == null) { | ||
mappingInfo.getMappings().forEach { | ||
assertThat(exclusions.contains(it)).withFailMessage { | ||
"Found $mappingInfo of type $method with no PreAuthorize annotation" | ||
}.isTrue() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun RequestMappingInfo.getMappings() = | ||
methodsCondition.methods.map { it.name }.flatMap { | ||
method -> | ||
pathPatternsCondition.patternValues.map { "$method $it" } | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not keen on this - for all I know the test has the right name but tests something entirely different. Not sure of a better way though 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Ideally though wouldn't be needed at all since the resources will have the annotations. However didn't want to start changing this project since we're in the process of ripping it all out anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would still be relevant for Prison API though