-
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.
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
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
42 changes: 42 additions & 0 deletions
42
...ier/shibboleth/admin/ui/jsonschema/MetadataFiltersSchemaValidatingControllerAdvice.groovy
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,42 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.jsonschema | ||
|
|
||
| import edu.internet2.tier.shibboleth.admin.ui.domain.filters.MetadataFilter | ||
| import edu.internet2.tier.shibboleth.admin.ui.domain.resolvers.MetadataResolver | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.core.MethodParameter | ||
| import org.springframework.http.HttpInputMessage | ||
| import org.springframework.http.converter.HttpMessageConverter | ||
| import org.springframework.web.bind.annotation.ControllerAdvice | ||
| import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter | ||
|
|
||
| import java.lang.reflect.Type | ||
|
|
||
| import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.LowLevelJsonSchemaValidator.validateMetadataFilterTypePayloadAgainstSchema | ||
| import static edu.internet2.tier.shibboleth.admin.ui.jsonschema.LowLevelJsonSchemaValidator.validateMetadataResolverTypePayloadAgainstSchema | ||
|
|
||
| /** | ||
| * Controller advice implementation for validating metadata filters payload coming from UI layer | ||
| * against pre-defined JSON schema for their respected types. Choosing of the appropriate schema based on incoming | ||
| * resolver types is delegated to @{LowLevelJsonSchemaValidator}. | ||
| * | ||
| * @author Dmitriy Kopylenko | ||
| */ | ||
| @ControllerAdvice | ||
| class MetadataFiltersSchemaValidatingControllerAdvice extends RequestBodyAdviceAdapter { | ||
|
|
||
| @Autowired | ||
| JsonSchemaResourceLocationRegistry jsonSchemaResourceLocationRegistry | ||
|
|
||
| @Override | ||
| boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
| targetType.typeName == MetadataFilter.typeName | ||
| } | ||
|
|
||
| @Override | ||
| HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, | ||
| Type targetType, Class<? extends HttpMessageConverter<?>> converterType) | ||
| throws IOException { | ||
|
|
||
| validateMetadataFilterTypePayloadAgainstSchema(inputMessage, jsonSchemaResourceLocationRegistry) | ||
| } | ||
| } |
78 changes: 78 additions & 0 deletions
78
...leth/admin/ui/controller/MetadataFiltersControllerSchemaValidationIntegrationTests.groovy
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,78 @@ | ||
| package edu.internet2.tier.shibboleth.admin.ui.controller | ||
|
|
||
| import edu.internet2.tier.shibboleth.admin.ui.domain.resolvers.FileBackedHttpMetadataResolver | ||
| import edu.internet2.tier.shibboleth.admin.ui.repository.MetadataResolverRepository | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.boot.test.context.SpringBootTest | ||
| import org.springframework.boot.test.web.client.TestRestTemplate | ||
| import org.springframework.http.HttpEntity | ||
| import org.springframework.http.HttpHeaders | ||
| import org.springframework.test.context.ActiveProfiles | ||
| import spock.lang.Specification | ||
|
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
| @ActiveProfiles(["no-auth", "dev"]) | ||
| class MetadataFiltersControllerSchemaValidationIntegrationTests extends Specification { | ||
|
|
||
| @Autowired | ||
| private TestRestTemplate restTemplate | ||
|
|
||
| @Autowired | ||
| MetadataResolverRepository metadataResolverRepository | ||
|
|
||
| static RESOURCE_URI = '/api/MetadataResolvers/%s/Filters' | ||
|
|
||
| private HTTP_POST = { body, resourceId -> | ||
| this.restTemplate.postForEntity(resourceUriFor(RESOURCE_URI, resourceId), createRequestHttpEntityFor(body), Map) | ||
| } | ||
|
|
||
| private static checkJsonValidationIsPerformed = { | ||
| assert it.statusCodeValue == 400 | ||
| assert it.body.errorMessage.count('Type mistmatch for null') > 0 | ||
| assert it.body.errorMessage.count('Type mistmatch for "not-a-boolean"') > 0 | ||
| true | ||
| } | ||
|
|
||
| def 'POST for EntityAttributesFilter with invalid payload according to schema validation'() { | ||
| given: | ||
| def resolver = metadataResolverRepository.save(new FileBackedHttpMetadataResolver(name: 'fbmr', backingFile: '/tmp/metadata.xml')) | ||
| def postedJsonBody = """ | ||
| { | ||
| "name" : "EntityAttributes", | ||
| "filterEnabled" : "not-a-boolean", | ||
| "entityAttributesFilterTarget" : { | ||
| "entityAttributesFilterTargetType" : "ENTITY", | ||
| "value" : [ "CedewbJJET" ] | ||
| }, | ||
| "attributeRelease" : [ "9ktPyjjiCn" ], | ||
| "relyingPartyOverrides" : { | ||
| "signAssertion" : false, | ||
| "dontSignResponse" : true, | ||
| "turnOffEncryption" : true, | ||
| "useSha" : false, | ||
| "ignoreAuthenticationMethod" : false, | ||
| "omitNotBefore" : true, | ||
| "responderId" : null, | ||
| "nameIdFormats" : [ ], | ||
| "authenticationMethods" : [ ] | ||
| }, | ||
| "@type" : "EntityAttributes" | ||
| } | ||
| """ | ||
|
|
||
| when: | ||
| def result = HTTP_POST(postedJsonBody, resolver.resourceId) | ||
|
|
||
| then: | ||
| checkJsonValidationIsPerformed(result) | ||
|
|
||
| } | ||
|
|
||
| private static HttpEntity<String> createRequestHttpEntityFor(String jsonBody) { | ||
| new HttpEntity<String>(jsonBody, ['Content-Type': 'application/json'] as HttpHeaders) | ||
| } | ||
|
|
||
| private static resourceUriFor(String uriTemplate, String resourceId) { | ||
| String.format(uriTemplate, resourceId) | ||
| } | ||
| } |