Search in sources :

Example 1 with AdmissionResponse

use of io.stackgres.operatorframework.admissionwebhook.AdmissionResponse in project stackgres by ongres.

the class AbstractGenericExceptionMapper method toResponse.

@Override
public Response toResponse(T throwable) {
    int statusCode = Status.INTERNAL_SERVER_ERROR.getStatusCode();
    Throwable cause = Throwables.getRootCause(throwable);
    if (cause instanceof WebApplicationException) {
        statusCode = WebApplicationException.class.cast(cause).getResponse().getStatus();
    }
    if (cause instanceof UnauthorizedException) {
        return new UnauthorizedExceptionMapper().toResponse(UnauthorizedException.class.cast(cause));
    }
    if (statusCode == Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
        LOGGER.error("An error occurred in the REST API", throwable);
    }
    String message = cause.getMessage();
    if (uriInfo != null && (uriInfo.getPath().startsWith(ValidationUtil.VALIDATION_PATH + "/") || uriInfo.getPath().startsWith(MutationUtil.MUTATION_PATH + "/"))) {
        AdmissionResponse admissionResponse = new AdmissionResponse();
        admissionResponse.setAllowed(false);
        admissionResponse.setStatus(new StatusBuilder().withMessage(message).withCode(statusCode).build());
        AdmissionReviewResponse admissionReviewResponse = new AdmissionReviewResponse();
        admissionReviewResponse.setResponse(admissionResponse);
        return Response.ok().type(MediaType.APPLICATION_JSON).entity(admissionReviewResponse).build();
    }
    return Response.status(statusCode).type(MediaType.APPLICATION_JSON).entity(message).build();
}
Also used : AdmissionReviewResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse) WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedExceptionMapper(io.quarkus.resteasy.runtime.UnauthorizedExceptionMapper) UnauthorizedException(io.quarkus.security.UnauthorizedException) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) AdmissionResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)

Example 2 with AdmissionResponse

use of io.stackgres.operatorframework.admissionwebhook.AdmissionResponse in project stackgres by ongres.

the class MutationResource method mutate.

default AdmissionReviewResponse mutate(T admissionReview, JsonPatchMutationPipeline<T> pipeline) {
    AdmissionRequest<?> request = admissionReview.getRequest();
    UUID requestUid = request.getUid();
    logger.info("Mutating admission review " + requestUid.toString() + " of kind " + request.getKind().toString());
    AdmissionResponse response = new AdmissionResponse();
    response.setUid(requestUid);
    AdmissionReviewResponse reviewResponse = new AdmissionReviewResponse();
    reviewResponse.setResponse(response);
    reviewResponse.setGroup(admissionReview.getGroup());
    reviewResponse.setKind(admissionReview.getKind());
    reviewResponse.setVersion(admissionReview.getVersion());
    try {
        pipeline.mutate(admissionReview).ifPresent(path -> {
            response.setPatchType("JSONPatch");
            String base64Path = Base64.getEncoder().encodeToString(path.getBytes(StandardCharsets.UTF_8));
            response.setPatch(base64Path);
        });
        response.setAllowed(true);
    } catch (Exception ex) {
        Status status = new StatusBuilder().withMessage(Optional.ofNullable(ex.getMessage()).orElse("null")).withCode(500).build();
        logger.error("cannot proceed with request " + requestUid.toString() + " cause: " + status.getMessage(), ex);
        response.setAllowed(false);
        response.setStatus(status);
    }
    return reviewResponse;
}
Also used : AdmissionReviewResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse) Status(io.fabric8.kubernetes.api.model.Status) StatusBuilder(io.fabric8.kubernetes.api.model.StatusBuilder) UUID(java.util.UUID) AdmissionResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)

Example 3 with AdmissionResponse

use of io.stackgres.operatorframework.admissionwebhook.AdmissionResponse in project stackgres by ongres.

the class ValidationResource method validate.

/**
 * Validate a review using a {@code ValidationPipeline}.
 */
default AdmissionReviewResponse validate(T admissionReview, ValidationPipeline<T> pipeline) {
    AdmissionRequest<?> request = admissionReview.getRequest();
    UUID requestUid = request.getUid();
    logger.info("Validating admission review " + requestUid.toString() + " of kind " + request.getKind().toString());
    AdmissionResponse response = new AdmissionResponse();
    response.setUid(requestUid);
    AdmissionReviewResponse reviewResponse = new AdmissionReviewResponse();
    reviewResponse.setResponse(response);
    reviewResponse.setGroup(admissionReview.getGroup());
    reviewResponse.setKind(admissionReview.getKind());
    reviewResponse.setVersion(admissionReview.getVersion());
    try {
        pipeline.validate(admissionReview);
        response.setAllowed(true);
    } catch (ValidationFailed validationFailed) {
        Status result = validationFailed.getResult();
        logger.error("cannot proceed with request " + requestUid.toString() + " cause: " + result.getMessage());
        response.setAllowed(false);
        response.setStatus(result);
    }
    return reviewResponse;
}
Also used : AdmissionReviewResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse) Status(io.fabric8.kubernetes.api.model.Status) UUID(java.util.UUID) AdmissionResponse(io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)

Aggregations

AdmissionResponse (io.stackgres.operatorframework.admissionwebhook.AdmissionResponse)3 AdmissionReviewResponse (io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse)3 Status (io.fabric8.kubernetes.api.model.Status)2 StatusBuilder (io.fabric8.kubernetes.api.model.StatusBuilder)2 UUID (java.util.UUID)2 UnauthorizedExceptionMapper (io.quarkus.resteasy.runtime.UnauthorizedExceptionMapper)1 UnauthorizedException (io.quarkus.security.UnauthorizedException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1