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();
}
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;
}
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;
}
Aggregations