use of io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse 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.AdmissionReviewResponse in project stackgres by ongres.
the class ClusterValidationResourceTest method givenValidAllowedRequest_thenResponseUidShouldMatchRequestUid.
@Test
void givenValidAllowedRequest_thenResponseUidShouldMatchRequestUid() throws IOException {
String requestBody = getFileAsString("cluster_allow_requests/valid_creation.json");
JsonNode admissionRequest = mapper.readTree(requestBody);
UUID requestUid = UUID.fromString(admissionRequest.get("request").get("uid").asText());
Response response = target("/stackgres/validation/sgcluster").request(MediaType.APPLICATION_JSON).post(Entity.json(requestBody));
AdmissionReviewResponse admissionResponse = response.readEntity(AdmissionReviewResponse.class);
assertEquals(requestUid, admissionResponse.getResponse().getUid());
}
use of io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse in project stackgres by ongres.
the class ClusterValidationResourceTest method givenValidAllowedRequest_thenAdmissionShouldBeAllowed.
@Test
void givenValidAllowedRequest_thenAdmissionShouldBeAllowed() {
String requestBody = getFileAsString("cluster_allow_requests/valid_creation.json");
Response response = target("/stackgres/validation/sgcluster").request(MediaType.APPLICATION_JSON).post(Entity.json(requestBody));
AdmissionReviewResponse admissionResponse = response.readEntity(AdmissionReviewResponse.class);
assertTrue(admissionResponse.getResponse().isAllowed());
}
use of io.stackgres.operatorframework.admissionwebhook.AdmissionReviewResponse 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.AdmissionReviewResponse in project stackgres by ongres.
the class ValidationResourceTest method givenAnValidAdmissionReview_itShouldReturnASuccessfulResponse.
@Test
void givenAnValidAdmissionReview_itShouldReturnASuccessfulResponse() throws ValidationFailed {
doNothing().when(pipeline).validate(review);
AdmissionReviewResponse response = resource.validate(review);
assertTrue(response.getResponse().isAllowed());
verify(pipeline).validate(review);
}
Aggregations