use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class AuthHandlerManager method authenticateAndAuthorize.
/**
* API to authenticate and authorize access to a given resource.
* @param resource The resource identifier for which the access needs to be controlled.
* @param credentials Credentials used for authentication.
* @param level Expected level of access.
* @return Returns true if the entity represented by the custom auth headers had given level of access to the resource.
* Returns false if the entity does not have access.
* @throws AuthenticationException if an authentication failure occurred.
*/
public boolean authenticateAndAuthorize(String resource, String credentials, AuthHandler.Permissions level) throws AuthenticationException {
Preconditions.checkNotNull(credentials, "credentials");
boolean retVal = false;
try {
String[] parts = extractMethodAndToken(credentials);
String method = parts[0];
String token = parts[1];
AuthHandler handler = getHandler(method);
Preconditions.checkNotNull(handler, "Can not find handler.");
Principal principal;
if ((principal = handler.authenticate(token)) == null) {
throw new AuthenticationException("Authentication failure");
}
retVal = handler.authorize(resource, principal).ordinal() >= level.ordinal();
} catch (AuthException e) {
throw new AuthenticationException("Authentication failure");
}
return retVal;
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class LargeEventWriter method writeLargeEvent.
/**
* Write the provided list of events (atomically) to the provided segment.
*
* @param segment The segment to write to
* @param events The events to append
* @param tokenProvider A token provider
* @param config Used for retry configuration parameters
* @throws NoSuchSegmentException If the provided segment does not exit.
* @throws SegmentSealedException If the segment is sealed.
* @throws AuthenticationException If the token can't be used for this segment.
* @throws UnsupportedOperationException If the server does not support large events.
*/
public void writeLargeEvent(Segment segment, List<ByteBuffer> events, DelegationTokenProvider tokenProvider, EventWriterConfig config) throws NoSuchSegmentException, AuthenticationException, SegmentSealedException {
List<ByteBuf> payloads = createBufs(events);
int attempts = 1 + Math.max(0, config.getRetryAttempts());
Retry.withExpBackoff(config.getInitialBackoffMillis(), config.getBackoffMultiple(), attempts, config.getMaxBackoffMillis()).retryWhen(t -> {
Throwable ex = Exceptions.unwrap(t);
if (ex instanceof ConnectionFailedException) {
log.info("Connection failure while sending large event: {}. Retrying", ex.getMessage());
return true;
} else if (ex instanceof TokenExpiredException) {
tokenProvider.signalTokenExpired();
log.info("Authentication token expired while writing large event to segment {}. Retrying", segment);
return true;
} else {
return false;
}
}).run(() -> {
@Cleanup RawClient client = new RawClient(controller, connectionPool, segment);
write(segment, payloads, client, tokenProvider);
return null;
});
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class EventStreamWriterImpl method writeLargeEvent.
@GuardedBy("writeFlushLock")
private void writeLargeEvent(String routingKey, List<ByteBuffer> events, CompletableFuture<Void> ackFuture) {
flush();
boolean success = false;
LargeEventWriter writer = new LargeEventWriter(UUID.randomUUID(), controller, connectionPool);
while (!success) {
Segment segment = selector.getSegmentForEvent(routingKey);
try {
writer.writeLargeEvent(segment, events, tokenProvider, config);
success = true;
ackFuture.complete(null);
} catch (SegmentSealedException | NoSuchSegmentException e) {
log.warn("Write large event on segment {} failed due to {}, it will be retried.", segment, e.getMessage());
handleLogSealed(segment);
tryWaitForSuccessors();
// Make sure that the successors are not sealed themselves.
if (selector.isStreamSealed()) {
ackFuture.completeExceptionally(new SegmentSealedException(segment.toString()));
break;
}
handleMissingLog();
} catch (AuthenticationException e) {
ackFuture.completeExceptionally(e);
break;
}
}
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class SegmentHelper method handleExpectedReplies.
/**
* This method handles the reply returned from RawClient.sendRequest given the expected success and failure cases.
*
* @param callerRequestId request id issues by the client
* @param reply actual reply received
* @param client RawClient for sending request
* @param qualifiedStreamSegmentName StreamSegmentName
* @param requestType request which reply need to be transformed
* @param type WireCommand for this request
* @param expectedSuccessReplies the expected replies for a successful case
* @param expectedFailureReplies the expected replies for a failing case
* @throws ConnectionFailedException in case the reply is unexpected
*/
protected void handleExpectedReplies(long callerRequestId, Reply reply, RawClient client, String qualifiedStreamSegmentName, Class<? extends Request> requestType, WireCommandType type, Map<Class<? extends Request>, Set<Class<? extends Reply>>> expectedSuccessReplies, Map<Class<? extends Request>, Set<Class<? extends Reply>>> expectedFailureReplies) throws ConnectionFailedException {
closeConnection(reply, client, callerRequestId);
Set<Class<? extends Reply>> expectedReplies = expectedSuccessReplies.get(requestType);
Set<Class<? extends Reply>> expectedFailingReplies = expectedFailureReplies.get(requestType);
if (expectedReplies != null && expectedReplies.contains(reply.getClass())) {
log.debug(callerRequestId, "{} {} {} {}.", requestType.getSimpleName(), qualifiedStreamSegmentName, reply.getClass().getSimpleName(), reply.getRequestId());
} else if (expectedFailingReplies != null && expectedFailingReplies.contains(reply.getClass())) {
log.debug(callerRequestId, "{} {} {} {}.", requestType.getSimpleName(), qualifiedStreamSegmentName, reply.getClass().getSimpleName(), reply.getRequestId());
if (reply instanceof WireCommands.NoSuchSegment) {
throw new WireCommandFailedException(type, WireCommandFailedException.Reason.SegmentDoesNotExist);
} else if (reply instanceof WireCommands.TableSegmentNotEmpty) {
throw new WireCommandFailedException(type, WireCommandFailedException.Reason.TableSegmentNotEmpty);
} else if (reply instanceof WireCommands.TableKeyDoesNotExist) {
throw new WireCommandFailedException(type, WireCommandFailedException.Reason.TableKeyDoesNotExist);
} else if (reply instanceof WireCommands.TableKeyBadVersion) {
throw new WireCommandFailedException(type, WireCommandFailedException.Reason.TableKeyBadVersion);
}
} else if (reply instanceof WireCommands.AuthTokenCheckFailed) {
log.warn(callerRequestId, "Auth Check Failed {} {} {} {} with error code {}.", requestType.getSimpleName(), qualifiedStreamSegmentName, reply.getClass().getSimpleName(), reply.getRequestId(), ((WireCommands.AuthTokenCheckFailed) reply).getErrorCode());
throw new WireCommandFailedException(new AuthenticationException(reply.toString()), type, WireCommandFailedException.Reason.AuthFailed);
} else if (reply instanceof WireCommands.WrongHost) {
log.warn(callerRequestId, "Wrong Host {} {} {} {}.", requestType.getSimpleName(), qualifiedStreamSegmentName, reply.getClass().getSimpleName(), reply.getRequestId());
throw new WireCommandFailedException(type, WireCommandFailedException.Reason.UnknownHost);
} else {
log.error(callerRequestId, "Unexpected reply {} {} {} {}.", requestType.getSimpleName(), qualifiedStreamSegmentName, reply.getClass().getSimpleName(), reply.getRequestId());
throw new ConnectionFailedException("Unexpected reply of " + reply + " when expecting one of " + expectedReplies.stream().map(Object::toString).collect(Collectors.joining(", ")));
}
}
use of io.pravega.auth.AuthenticationException in project pravega by pravega.
the class SegmentHelper method processAndRethrowException.
@VisibleForTesting
<T extends Request & WireCommand> void processAndRethrowException(long callerRequestId, T request, Throwable e) {
Throwable unwrap = Exceptions.unwrap(e);
WireCommandFailedException ex = null;
if (unwrap instanceof ConnectionFailedException || unwrap instanceof ConnectionClosedException) {
log.warn(callerRequestId, "Connection dropped {}", request.getRequestId());
throw new WireCommandFailedException(request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
} else if (unwrap instanceof AuthenticationException) {
log.warn(callerRequestId, "Authentication Exception {}", request.getRequestId());
throw new WireCommandFailedException(request.getType(), WireCommandFailedException.Reason.AuthFailed);
} else if (unwrap instanceof TokenExpiredException) {
log.warn(callerRequestId, "Token expired {}", request.getRequestId());
throw new WireCommandFailedException(request.getType(), WireCommandFailedException.Reason.AuthFailed);
} else if (unwrap instanceof TimeoutException) {
log.warn(callerRequestId, "Request timed out. {}", request.getRequestId());
throw new WireCommandFailedException(request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
} else {
log.error(callerRequestId, "Request failed {}", request.getRequestId(), e);
throw new CompletionException(e);
}
}
Aggregations