use of uk.gov.gchq.gaffer.commonutil.exception.UnauthorisedException in project Gaffer by gchq.
the class OperationChainLimiter method preExecute.
/**
* Checks the {@link OperationChain}
* is allowed to be executed by the user.
* This is done by checking the user's auths against the auth scores getting the users maximum score limit value.
* Then checking the operation score of all operations in the chain and comparing the total score value of the chain against a users maximum score limit.
* If an operation cannot be executed then an {@link IllegalAccessError} is thrown.
*
* @param user the user to authorise.
* @param opChain the operation chain.
*/
@Override
public void preExecute(final OperationChain<?> opChain, final User user) {
if (null != opChain) {
Integer chainScore = 0;
Integer maxAuthScore = getMaxUserAuthScore(user.getOpAuths());
for (final Operation operation : opChain.getOperations()) {
chainScore += authorise(operation);
if (chainScore > maxAuthScore) {
throw new UnauthorisedException("The maximum score limit for this user is " + maxAuthScore + ".\n" + "The requested operation chain exceeded this score limit.");
}
}
}
}
use of uk.gov.gchq.gaffer.commonutil.exception.UnauthorisedException in project Gaffer by gchq.
the class OperationServiceV2 method executeChunkedChain.
@SuppressFBWarnings
@Override
public Response executeChunkedChain(final OperationChain opChain) {
// Create chunked output instance
final Throwable[] threadException = new Throwable[1];
final ChunkedOutput<String> output = new ChunkedOutput<>(String.class, "\r\n");
final Context context = userFactory.createContext();
// create thread to write chunks to the chunked output object
Thread thread = new Thread(() -> {
try {
final Object result = _execute(opChain, context).getFirst();
chunkResult(result, output);
} catch (final Exception e) {
throw new RuntimeException(e);
} finally {
CloseableUtil.close(output);
CloseableUtil.close(opChain);
}
});
// By default threads throw nothing, so set the ExceptionHandler
thread.setUncaughtExceptionHandler((thread1, exception) -> threadException[0] = exception.getCause());
thread.start();
// Sleep to check exception will be caught
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
return Response.status(INTERNAL_SERVER_ERROR).entity(new Error.ErrorBuilder().status(Status.INTERNAL_SERVER_ERROR).statusCode(500).simpleMessage(e.getMessage()).build()).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
// If there was an UnauthorisedException thrown return 403, else return a 500
if (null != threadException[0]) {
if (threadException.getClass().equals(UnauthorisedException.class)) {
return Response.status(INTERNAL_SERVER_ERROR).entity(new Error.ErrorBuilder().status(Status.FORBIDDEN).statusCode(403).simpleMessage(threadException[0].getMessage()).build()).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
} else {
return Response.status(INTERNAL_SERVER_ERROR).entity(new Error.ErrorBuilder().status(Status.INTERNAL_SERVER_ERROR).statusCode(500).simpleMessage(threadException[0].getMessage()).build()).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
}
// Return ok output
return Response.ok(output).header(GAFFER_MEDIA_TYPE_HEADER, GAFFER_MEDIA_TYPE).build();
}
use of uk.gov.gchq.gaffer.commonutil.exception.UnauthorisedException in project Gaffer by gchq.
the class GafferExceptionMapperTest method shouldPropagateForbiddenError.
@Test
public void shouldPropagateForbiddenError() {
// When
GafferExceptionMapper gafferExceptionMapper = new GafferExceptionMapper();
ResponseEntity<?> response = gafferExceptionMapper.handleUnauthorisedException(null, new UnauthorisedException("nah"));
// Then
assertEquals(FORBIDDEN.getStatusCode(), response.getStatusCode().value());
assertEquals("nah", ((Error) response.getBody()).getSimpleMessage());
}
Aggregations