Search in sources :

Example 21 with AuthenticationException

use of io.pravega.common.auth.AuthenticationException in project pravega by pravega.

the class StreamMetadataResourceImpl method listStreams.

/**
 * Implementation of listStreams REST API.
 *
 * @param scopeName           The scope name of stream.
 * @param securityContext     The security for API access.
 * @param asyncResponse       AsyncResponse provides means for asynchronous server side response processing.
 */
@Override
public void listStreams(final String scopeName, final String showInternalStreams, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "listStreams");
    try {
        authenticate(scopeName, READ);
    } catch (AuthenticationException e) {
        log.warn("List streams for {} failed due to authentication failure.", scopeName);
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "listStreams", traceId);
        return;
    }
    boolean showOnlyInternalStreams = showInternalStreams != null && showInternalStreams.equals("true");
    controllerService.listStreamsInScope(scopeName).thenApply(streamsList -> {
        StreamsList streams = new StreamsList();
        streamsList.forEach(stream -> {
            // otherwise display the regular user created streams.
            if (!showOnlyInternalStreams ^ stream.getStreamName().startsWith(INTERNAL_NAME_PREFIX)) {
                streams.addStreamsItem(ModelHelper.encodeStreamResponse(stream));
            }
        });
        log.info("Successfully fetched streams for scope: {}", scopeName);
        return Response.status(Status.OK).entity(streams).build();
    }).exceptionally(exception -> {
        if (exception.getCause() instanceof StoreException.DataNotFoundException || exception instanceof StoreException.DataNotFoundException) {
            log.warn("Scope name: {} not found", scopeName);
            return Response.status(Status.NOT_FOUND).build();
        } else {
            log.warn("listStreams for {} failed with exception: {}", scopeName, exception);
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }).thenApply(asyncResponse::resume).thenAccept(x -> LoggerHelpers.traceLeave(log, "listStreams", traceId));
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) Arrays(java.util.Arrays) ApiV1(io.pravega.controller.server.rest.v1.ApiV1) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) AuthHandler(io.pravega.auth.AuthHandler) SecurityContext(javax.ws.rs.core.SecurityContext) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) LocalController(io.pravega.controller.server.eventProcessor.LocalController) StoreException(io.pravega.controller.store.stream.StoreException) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Map(java.util.Map) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) INTERNAL_NAME_PREFIX(io.pravega.shared.NameUtils.INTERNAL_NAME_PREFIX) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) CreateScopeRequest(io.pravega.controller.server.rest.generated.model.CreateScopeRequest) Collectors(java.util.stream.Collectors) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) READER_GROUP_STREAM_PREFIX(io.pravega.shared.NameUtils.READER_GROUP_STREAM_PREFIX) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) HttpHeaders(javax.ws.rs.core.HttpHeaders) StreamState(io.pravega.controller.server.rest.generated.model.StreamState) Response(javax.ws.rs.core.Response) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) CompletableFuture(java.util.concurrent.CompletableFuture) UpdateStreamRequest(io.pravega.controller.server.rest.generated.model.UpdateStreamRequest) ArrayList(java.util.ArrayList) READ_UPDATE(io.pravega.auth.AuthHandler.Permissions.READ_UPDATE) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Status(javax.ws.rs.core.Response.Status) LoggerHelpers(io.pravega.common.LoggerHelpers) ControllerService(io.pravega.controller.server.ControllerService) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) ScopeProperty(io.pravega.controller.server.rest.generated.model.ScopeProperty) ModelHelper(io.pravega.controller.server.rest.ModelHelper) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) PravegaAuthManager(io.pravega.controller.server.rpc.auth.PravegaAuthManager) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) AuthenticationException(io.pravega.common.auth.AuthenticationException) StoreException(io.pravega.controller.store.stream.StoreException)

Example 22 with AuthenticationException

use of io.pravega.common.auth.AuthenticationException in project pravega by pravega.

the class StreamMetadataResourceImpl method getReaderGroup.

@Override
public void getReaderGroup(final String scopeName, final String readerGroupName, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "getReaderGroup");
    try {
        authenticate(scopeName + "/" + readerGroupName, READ);
    } catch (AuthenticationException e) {
        log.warn("Get reader group for {} failed due to authentication failure.", scopeName + "/" + readerGroupName);
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "getReaderGroup", traceId);
        return;
    }
    ReaderGroupManager readerGroupManager = new ReaderGroupManagerImpl(scopeName, this.localController, new ClientFactoryImpl(scopeName, this.localController), this.connectionFactory);
    ReaderGroupProperty readerGroupProperty = new ReaderGroupProperty();
    readerGroupProperty.setScopeName(scopeName);
    readerGroupProperty.setReaderGroupName(readerGroupName);
    CompletableFuture.supplyAsync(() -> {
        ReaderGroup readerGroup = readerGroupManager.getReaderGroup(readerGroupName);
        readerGroupProperty.setOnlineReaderIds(new ArrayList<>(readerGroup.getOnlineReaders()));
        readerGroupProperty.setStreamList(new ArrayList<>(readerGroup.getStreamNames()));
        return Response.status(Status.OK).entity(readerGroupProperty).build();
    }, controllerService.getExecutor()).exceptionally(exception -> {
        log.warn("getReaderGroup for {} failed with exception: ", readerGroupName, exception);
        if (exception.getCause() instanceof InvalidStreamException) {
            return Response.status(Status.NOT_FOUND).build();
        } else {
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }).thenAccept(response -> {
        asyncResponse.resume(response);
        readerGroupManager.close();
        LoggerHelpers.traceLeave(log, "getReaderGroup", traceId);
    });
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) Arrays(java.util.Arrays) ApiV1(io.pravega.controller.server.rest.v1.ApiV1) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) AuthHandler(io.pravega.auth.AuthHandler) SecurityContext(javax.ws.rs.core.SecurityContext) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) LocalController(io.pravega.controller.server.eventProcessor.LocalController) StoreException(io.pravega.controller.store.stream.StoreException) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Map(java.util.Map) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) INTERNAL_NAME_PREFIX(io.pravega.shared.NameUtils.INTERNAL_NAME_PREFIX) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) CreateScopeRequest(io.pravega.controller.server.rest.generated.model.CreateScopeRequest) Collectors(java.util.stream.Collectors) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) READER_GROUP_STREAM_PREFIX(io.pravega.shared.NameUtils.READER_GROUP_STREAM_PREFIX) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) HttpHeaders(javax.ws.rs.core.HttpHeaders) StreamState(io.pravega.controller.server.rest.generated.model.StreamState) Response(javax.ws.rs.core.Response) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) CompletableFuture(java.util.concurrent.CompletableFuture) UpdateStreamRequest(io.pravega.controller.server.rest.generated.model.UpdateStreamRequest) ArrayList(java.util.ArrayList) READ_UPDATE(io.pravega.auth.AuthHandler.Permissions.READ_UPDATE) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Status(javax.ws.rs.core.Response.Status) LoggerHelpers(io.pravega.common.LoggerHelpers) ControllerService(io.pravega.controller.server.ControllerService) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) ScopeProperty(io.pravega.controller.server.rest.generated.model.ScopeProperty) ModelHelper(io.pravega.controller.server.rest.ModelHelper) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) PravegaAuthManager(io.pravega.controller.server.rpc.auth.PravegaAuthManager) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) AuthenticationException(io.pravega.common.auth.AuthenticationException) ReaderGroup(io.pravega.client.stream.ReaderGroup) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) InvalidStreamException(io.pravega.client.stream.InvalidStreamException)

Example 23 with AuthenticationException

use of io.pravega.common.auth.AuthenticationException in project pravega by pravega.

the class StreamMetadataResourceImpl method listReaderGroups.

@Override
public void listReaderGroups(final String scopeName, final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "listReaderGroups");
    try {
        authenticate(scopeName, READ);
    } catch (AuthenticationException e) {
        log.warn("Get reader groups for {} failed due to authentication failure.", scopeName);
        asyncResponse.resume(Response.status(Status.UNAUTHORIZED).build());
        LoggerHelpers.traceLeave(log, "listReaderGroups", traceId);
        return;
    }
    // Each reader group is represented by a stream within the mentioned scope.
    controllerService.listStreamsInScope(scopeName).thenApply(streamsList -> {
        ReaderGroupsList readerGroups = new ReaderGroupsList();
        streamsList.forEach(stream -> {
            if (stream.getStreamName().startsWith(READER_GROUP_STREAM_PREFIX)) {
                ReaderGroupsListReaderGroups readerGroup = new ReaderGroupsListReaderGroups();
                readerGroup.setReaderGroupName(stream.getStreamName().substring(READER_GROUP_STREAM_PREFIX.length()));
                readerGroups.addReaderGroupsItem(readerGroup);
            }
        });
        log.info("Successfully fetched readerGroups for scope: {}", scopeName);
        return Response.status(Status.OK).entity(readerGroups).build();
    }).exceptionally(exception -> {
        if (exception.getCause() instanceof StoreException.DataNotFoundException || exception instanceof StoreException.DataNotFoundException) {
            log.warn("Scope name: {} not found", scopeName);
            return Response.status(Status.NOT_FOUND).build();
        } else {
            log.warn("listReaderGroups for {} failed with exception: ", scopeName, exception);
            return Response.status(Status.INTERNAL_SERVER_ERROR).build();
        }
    }).thenApply(asyncResponse::resume).thenAccept(x -> LoggerHelpers.traceLeave(log, "listReaderGroups", traceId));
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) Arrays(java.util.Arrays) ApiV1(io.pravega.controller.server.rest.v1.ApiV1) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) AuthHandler(io.pravega.auth.AuthHandler) SecurityContext(javax.ws.rs.core.SecurityContext) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) LocalController(io.pravega.controller.server.eventProcessor.LocalController) StoreException(io.pravega.controller.store.stream.StoreException) ClientFactoryImpl(io.pravega.client.stream.impl.ClientFactoryImpl) ReaderGroupManager(io.pravega.client.admin.ReaderGroupManager) Map(java.util.Map) ReaderGroupProperty(io.pravega.controller.server.rest.generated.model.ReaderGroupProperty) INTERNAL_NAME_PREFIX(io.pravega.shared.NameUtils.INTERNAL_NAME_PREFIX) DeleteScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteScopeStatus) CreateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateStreamStatus) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) CreateScopeRequest(io.pravega.controller.server.rest.generated.model.CreateScopeRequest) Collectors(java.util.stream.Collectors) CreateStreamRequest(io.pravega.controller.server.rest.generated.model.CreateStreamRequest) READER_GROUP_STREAM_PREFIX(io.pravega.shared.NameUtils.READER_GROUP_STREAM_PREFIX) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) HttpHeaders(javax.ws.rs.core.HttpHeaders) StreamState(io.pravega.controller.server.rest.generated.model.StreamState) Response(javax.ws.rs.core.Response) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) CompletableFuture(java.util.concurrent.CompletableFuture) UpdateStreamRequest(io.pravega.controller.server.rest.generated.model.UpdateStreamRequest) ArrayList(java.util.ArrayList) READ_UPDATE(io.pravega.auth.AuthHandler.Permissions.READ_UPDATE) ScaleMetadata(io.pravega.controller.store.stream.ScaleMetadata) DeleteStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.DeleteStreamStatus) ConnectionFactory(io.pravega.client.netty.impl.ConnectionFactory) Status(javax.ws.rs.core.Response.Status) LoggerHelpers(io.pravega.common.LoggerHelpers) ControllerService(io.pravega.controller.server.ControllerService) NameUtils(io.pravega.shared.NameUtils) Iterator(java.util.Iterator) ScopeProperty(io.pravega.controller.server.rest.generated.model.ScopeProperty) ModelHelper(io.pravega.controller.server.rest.ModelHelper) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) PravegaAuthManager(io.pravega.controller.server.rpc.auth.PravegaAuthManager) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) AuthenticationException(io.pravega.common.auth.AuthenticationException) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) StoreException(io.pravega.controller.store.stream.StoreException)

Example 24 with AuthenticationException

use of io.pravega.common.auth.AuthenticationException in project pravega by pravega.

the class MockController method abortTxSegment.

private CompletableFuture<Void> abortTxSegment(UUID txId, Segment segment) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    FailingReplyProcessor replyProcessor = new FailingReplyProcessor() {

        @Override
        public void connectionDropped() {
            result.completeExceptionally(new ConnectionClosedException());
        }

        @Override
        public void wrongHost(WrongHost wrongHost) {
            result.completeExceptionally(new UnsupportedOperationException());
        }

        @Override
        public void transactionCommitted(TransactionCommitted transactionCommitted) {
            result.completeExceptionally(new RuntimeException("Transaction already committed."));
        }

        @Override
        public void transactionAborted(TransactionAborted transactionAborted) {
            result.complete(null);
        }

        @Override
        public void processingFailure(Exception error) {
            result.completeExceptionally(error);
        }

        @Override
        public void authTokenCheckFailed(WireCommands.AuthTokenCheckFailed authTokenCheckFailed) {
            result.completeExceptionally(new AuthenticationException(authTokenCheckFailed.toString()));
        }
    };
    sendRequestOverNewConnection(new AbortTransaction(idGenerator.get(), segment.getScopedName(), txId, ""), replyProcessor, result);
    return result;
}
Also used : AuthenticationException(io.pravega.common.auth.AuthenticationException) FailingReplyProcessor(io.pravega.shared.protocol.netty.FailingReplyProcessor) TransactionAborted(io.pravega.shared.protocol.netty.WireCommands.TransactionAborted) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) AuthenticationException(io.pravega.common.auth.AuthenticationException) TxnFailedException(io.pravega.client.stream.TxnFailedException) ConnectionClosedException(io.pravega.client.stream.impl.ConnectionClosedException) TransactionCommitted(io.pravega.shared.protocol.netty.WireCommands.TransactionCommitted) CompletableFuture(java.util.concurrent.CompletableFuture) AbortTransaction(io.pravega.shared.protocol.netty.WireCommands.AbortTransaction)

Aggregations

AuthenticationException (io.pravega.common.auth.AuthenticationException)24 CompletableFuture (java.util.concurrent.CompletableFuture)19 FailingReplyProcessor (io.pravega.shared.protocol.netty.FailingReplyProcessor)14 WireCommands (io.pravega.shared.protocol.netty.WireCommands)10 Controller (io.pravega.controller.stream.api.grpc.v1.Controller)9 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)9 WireCommandType (io.pravega.shared.protocol.netty.WireCommandType)9 AuthHandler (io.pravega.auth.AuthHandler)6 READ (io.pravega.auth.AuthHandler.Permissions.READ)5 READ_UPDATE (io.pravega.auth.AuthHandler.Permissions.READ_UPDATE)5 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)5 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)5 ConnectionFactory (io.pravega.client.netty.impl.ConnectionFactory)5 InvalidStreamException (io.pravega.client.stream.InvalidStreamException)5 ReaderGroup (io.pravega.client.stream.ReaderGroup)5 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)5 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)5 LoggerHelpers (io.pravega.common.LoggerHelpers)5 ControllerService (io.pravega.controller.server.ControllerService)5 LocalController (io.pravega.controller.server.eventProcessor.LocalController)5