Search in sources :

Example 16 with ApplicationException

use of uk.gov.ida.exceptions.ApplicationException in project verify-hub by alphagov.

the class PolicyApplicationExceptionMapperTest method toResponse_shouldReturnAnUnauditedErrorStatusIfExceptionIsNotAudited.

@Test
public void toResponse_shouldReturnAnUnauditedErrorStatusIfExceptionIsNotAudited() {
    ApplicationException exception = createUnauditedExceptionThatShouldNotBeAudited();
    final Response response = mapper.toResponse(exception);
    final ErrorStatusDto errorStatus = (ErrorStatusDto) response.getEntity();
    assertThat(errorStatus.isAudited()).isEqualTo(false);
}
Also used : Response(javax.ws.rs.core.Response) ApplicationException(uk.gov.ida.exceptions.ApplicationException) ErrorStatusDto(uk.gov.ida.common.ErrorStatusDto) Test(org.junit.jupiter.api.Test)

Example 17 with ApplicationException

use of uk.gov.ida.exceptions.ApplicationException in project verify-hub by alphagov.

the class PolicyApplicationExceptionMapperTest method toResponse_shouldReturnAnAuditedErrorStatusIfExceptionIsAudited.

@Test
public void toResponse_shouldReturnAnAuditedErrorStatusIfExceptionIsAudited() {
    ApplicationException exception = createAuditedException(ExceptionType.IDP_DISABLED, UUID.randomUUID());
    final Response response = mapper.toResponse(exception);
    final ErrorStatusDto errorStatus = (ErrorStatusDto) response.getEntity();
    assertThat(errorStatus.isAudited()).isEqualTo(true);
}
Also used : Response(javax.ws.rs.core.Response) ApplicationException(uk.gov.ida.exceptions.ApplicationException) ErrorStatusDto(uk.gov.ida.common.ErrorStatusDto) Test(org.junit.jupiter.api.Test)

Example 18 with ApplicationException

use of uk.gov.ida.exceptions.ApplicationException in project verify-hub by alphagov.

the class SamlEngineExceptionMapper method toResponse.

@Override
public Response toResponse(Exception exception) {
    final UUID errorId = UUID.randomUUID();
    Response.ResponseBuilder response = Response.status(Response.Status.BAD_REQUEST);
    if (exception instanceof ApplicationException) {
        ApplicationException applicationException = (ApplicationException) exception;
        response.entity(logAndGetErrorStatusDto(applicationException.getExceptionType().getLevel(), applicationException.getExceptionType(), applicationException, applicationException.getErrorId(), applicationException.isAudited()));
    } else if (exception instanceof SamlContextException) {
        SamlContextException contextException = (SamlContextException) exception;
        response.entity(logAndGetErrorStatusDto(contextException.getLogLevel(), contextException.getExceptionType(), exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlFailedToDecryptException) {
        response.entity(logAndGetErrorStatusDto(((SamlFailedToDecryptException) exception).getLogLevel(), ExceptionType.INVALID_SAML_FAILED_TO_DECRYPT, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlDuplicateRequestIdException) {
        response.entity(logAndGetErrorStatusDto(((SamlDuplicateRequestIdException) exception).getLogLevel(), ExceptionType.INVALID_SAML_DUPLICATE_REQUEST_ID, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlRequestTooOldException) {
        response.entity(logAndGetErrorStatusDto(((SamlTransformationErrorException) exception).getLogLevel(), ExceptionType.INVALID_SAML_REQUEST_TOO_OLD, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlTransformationErrorException) {
        response.entity(logAndGetErrorStatusDto(((SamlTransformationErrorException) exception).getLogLevel(), ExceptionType.INVALID_SAML, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof UnableToGenerateSamlException) {
        response.entity(logAndGetErrorStatusDto(((UnableToGenerateSamlException) exception).getLogLevel(), ExceptionType.INVALID_INPUT, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof NoKeyConfiguredForEntityException) {
        response.entity(logAndGetErrorStatusDto(ERROR, ExceptionType.NO_KEY_CONFIGURED_FOR_ENTITY, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else {
        levelLogger.log(WARN, exception, errorId);
    }
    return response.build();
}
Also used : Response(javax.ws.rs.core.Response) ApplicationException(uk.gov.ida.exceptions.ApplicationException) SamlTransformationErrorException(uk.gov.ida.saml.core.validation.SamlTransformationErrorException) NoKeyConfiguredForEntityException(uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException) UUID(java.util.UUID) SamlDuplicateRequestIdException(uk.gov.ida.saml.hub.exception.SamlDuplicateRequestIdException) SamlRequestTooOldException(uk.gov.ida.saml.hub.exception.SamlRequestTooOldException) SamlFailedToDecryptException(uk.gov.ida.saml.security.exception.SamlFailedToDecryptException)

Example 19 with ApplicationException

use of uk.gov.ida.exceptions.ApplicationException in project verify-hub by alphagov.

the class MatchingServiceHealthChecker method isHealthyResponse.

private boolean isHealthyResponse(final URI matchingServiceUri, Optional<String> response) {
    if (response.isEmpty()) {
        return false;
    }
    String exceptionMessage = format("Matching service health check failed for URI {0}", matchingServiceUri);
    try {
        // Saml-engine expects the saml to be base64 encoded
        String base64Response = Base64.getEncoder().encodeToString(response.get().getBytes(Charset.defaultCharset()));
        final SamlMessageDto samlMessageDto = new SamlMessageDto(base64Response);
        final MatchingServiceHealthCheckerResponseDto responseFromMatchingService = samlEngineProxy.translateHealthcheckMatchingServiceResponse(samlMessageDto);
        if (responseFromMatchingService.getStatus() != MatchingServiceIdaStatus.Healthy) {
            return false;
        }
    } catch (ApplicationException e) {
        eventLogger.logException(e, exceptionMessage);
        return false;
    } catch (RuntimeException e) {
        LOG.warn(format("Matching service health check failed for URI {0}", matchingServiceUri), e);
        return false;
    }
    return true;
}
Also used : SamlMessageDto(uk.gov.ida.hub.samlsoapproxy.contract.SamlMessageDto) ApplicationException(uk.gov.ida.exceptions.ApplicationException) MatchingServiceHealthCheckerResponseDto(uk.gov.ida.hub.samlsoapproxy.contract.MatchingServiceHealthCheckerResponseDto)

Example 20 with ApplicationException

use of uk.gov.ida.exceptions.ApplicationException in project verify-hub by alphagov.

the class MatchingServiceHealthCheckClient method sendHealthCheckRequest.

public MatchingServiceHealthCheckResponseDto sendHealthCheckRequest(final Element matchingServiceHealthCheckRequest, final URI matchingServiceUri) {
    // Use a custom timer so that we get separate metrics for each matching service
    final String scope = matchingServiceUri.toString().replace(':', '_').replace('/', '_');
    final Timer timer = metricsRegistry.timer(MetricRegistry.name(MatchingServiceHealthCheckClient.class, "sendHealthCheckRequest", scope));
    final Timer.Context context = timer.time();
    HealthCheckResponse healthCheckResponse;
    try {
        healthCheckResponse = client.makeSoapRequestForHealthCheck(matchingServiceHealthCheckRequest, matchingServiceUri);
    } catch (ApplicationException ex) {
        final String errorMessage = MessageFormat.format("Failed to complete matching service health check to {0}.", matchingServiceUri);
        LOG.warn(errorMessage, ex);
        return new MatchingServiceHealthCheckResponseDto(Optional.empty());
    } finally {
        context.stop();
    }
    return new MatchingServiceHealthCheckResponseDto(Optional.of(XmlUtils.writeToString(healthCheckResponse.getResponseElement())));
}
Also used : ApplicationException(uk.gov.ida.exceptions.ApplicationException) Timer(com.codahale.metrics.Timer) HealthCheckResponse(uk.gov.ida.hub.samlsoapproxy.rest.HealthCheckResponse) MatchingServiceHealthCheckResponseDto(uk.gov.ida.hub.samlsoapproxy.domain.MatchingServiceHealthCheckResponseDto)

Aggregations

ApplicationException (uk.gov.ida.exceptions.ApplicationException)27 Test (org.junit.jupiter.api.Test)20 UUID (java.util.UUID)8 Response (javax.ws.rs.core.Response)7 ErrorStatusDto (uk.gov.ida.common.ErrorStatusDto)6 URI (java.net.URI)4 SamlMessageDto (uk.gov.ida.hub.samlsoapproxy.contract.SamlMessageDto)4 EventDetails (uk.gov.ida.hub.shared.eventsink.EventDetails)4 MatchingServiceHealthCheckResponseDto (uk.gov.ida.hub.samlsoapproxy.domain.MatchingServiceHealthCheckResponseDto)3 SessionId (uk.gov.ida.common.SessionId)2 ApplicationException.createAuditedException (uk.gov.ida.exceptions.ApplicationException.createAuditedException)2 ApplicationException.createUnauditedException (uk.gov.ida.exceptions.ApplicationException.createUnauditedException)2 MatchingServiceHealthCheckerResponseDto (uk.gov.ida.hub.samlsoapproxy.contract.MatchingServiceHealthCheckerResponseDto)2 Timer (com.codahale.metrics.Timer)1 Timed (com.codahale.metrics.annotation.Timed)1 IOException (java.io.IOException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 DateTime (org.joda.time.DateTime)1 Element (org.w3c.dom.Element)1 SAXException (org.xml.sax.SAXException)1