Search in sources :

Example 1 with ScopesList

use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.

the class StreamMetaDataTests method testListScopes.

/**
 * Test for listScopes REST API.
 *
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test
public void testListScopes() throws ExecutionException, InterruptedException {
    final String resourceURI = getURI() + "v1/scopes";
    // Test to list scopes.
    List<String> scopesList = Arrays.asList("scope1", "scope2");
    when(mockControllerService.listScopes()).thenReturn(CompletableFuture.completedFuture(scopesList));
    Response response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
    assertEquals("List Scopes response code", 200, response.getStatus());
    assertTrue(response.bufferEntity());
    final ScopesList scopesList1 = response.readEntity(ScopesList.class);
    assertEquals("List count", scopesList1.getScopes().size(), 2);
    assertEquals("List element", scopesList1.getScopes().get(0).getScopeName(), "scope1");
    assertEquals("List element", scopesList1.getScopes().get(1).getScopeName(), "scope2");
    response.close();
    // Test for list scopes failure.
    final CompletableFuture<List<String>> completableFuture = new CompletableFuture<>();
    completableFuture.completeExceptionally(new Exception());
    when(mockControllerService.listScopes()).thenReturn(completableFuture);
    response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
    assertEquals("List Scopes response code", 500, response.getStatus());
    response.close();
}
Also used : Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) List(java.util.List) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) ArrayList(java.util.ArrayList) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) StoreException(io.pravega.controller.store.stream.StoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with ScopesList

use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.

the class StreamMetadataResourceImpl method listScopes.

/**
 * Implementation of listScopes REST API.
 *
 * @param securityContext     The security for API access.
 * @param asyncResponse       AsyncResponse provides means for asynchronous server side response processing.
 */
@Override
public void listScopes(final SecurityContext securityContext, final AsyncResponse asyncResponse) {
    long traceId = LoggerHelpers.traceEnter(log, "listScopes");
    long requestId = requestIdGenerator.nextLong();
    final Principal principal;
    final List<String> authHeader = getAuthorizationHeader();
    try {
        principal = restAuthHelper.authenticate(authHeader);
        restAuthHelper.authorize(authHeader, authorizationResource.ofScopes(), principal, READ);
    } catch (AuthException e) {
        log.warn(requestId, "Get scopes failed due to authentication failure.", e);
        asyncResponse.resume(Response.status(Status.fromStatusCode(e.getResponseCode())).build());
        LoggerHelpers.traceLeave(log, "listScopes", traceId);
        return;
    }
    controllerService.listScopes(requestId).thenApply(scopesList -> {
        ScopesList scopes = new ScopesList();
        scopesList.forEach(scope -> {
            try {
                if (restAuthHelper.isAuthorized(authHeader, authorizationResource.ofScope(scope), principal, READ)) {
                    scopes.addScopesItem(new ScopeProperty().scopeName(scope));
                }
            } catch (AuthException e) {
                log.warn(requestId, e.getMessage(), e);
            // Ignore. This exception occurs under abnormal circumstances and not to determine
            // whether the user is authorized. In case it does occur, we assume that the user
            // is unauthorized.
            }
        });
        return Response.status(Status.OK).entity(scopes).build();
    }).exceptionally(exception -> {
        log.warn(requestId, "listScopes failed with exception: ", exception);
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    }).thenApply(response -> {
        asyncResponse.resume(response);
        LoggerHelpers.traceLeave(log, "listScopes", traceId);
        return response;
    });
}
Also used : ApiV1(io.pravega.controller.server.rest.v1.ApiV1) READ(io.pravega.auth.AuthHandler.Permissions.READ) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) SecurityContext(javax.ws.rs.core.SecurityContext) LoggerFactory(org.slf4j.LoggerFactory) ReaderGroupManagerImpl(io.pravega.client.admin.impl.ReaderGroupManagerImpl) Random(java.util.Random) ReaderGroup(io.pravega.client.stream.ReaderGroup) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) ReaderGroupNotFoundException(io.pravega.client.stream.ReaderGroupNotFoundException) TagLogger(io.pravega.common.tracing.TagLogger) RESTAuthHelper(io.pravega.shared.rest.security.RESTAuthHelper) 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) Stream(io.pravega.client.stream.Stream) 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) AuthorizationResource(io.pravega.shared.security.auth.AuthorizationResource) 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) Principal(java.security.Principal) 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) Futures(io.pravega.common.concurrent.Futures) AuthException(io.pravega.auth.AuthException) CreateScopeStatus(io.pravega.controller.stream.api.grpc.v1.Controller.CreateScopeStatus) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) 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) Status(javax.ws.rs.core.Response.Status) AuthorizationResourceImpl(io.pravega.shared.security.auth.AuthorizationResourceImpl) 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) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ModelHelper(io.pravega.controller.server.rest.ModelHelper) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) AuthHandlerManager(io.pravega.shared.rest.security.AuthHandlerManager) ReaderGroupsListReaderGroups(io.pravega.controller.server.rest.generated.model.ReaderGroupsListReaderGroups) UpdateStreamStatus(io.pravega.controller.stream.api.grpc.v1.Controller.UpdateStreamStatus) ClientConfig(io.pravega.client.ClientConfig) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) ScopeProperty(io.pravega.controller.server.rest.generated.model.ScopeProperty) AuthException(io.pravega.auth.AuthException) Principal(java.security.Principal)

Example 3 with ScopesList

use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.

the class StreamMetaDataAuthFocusedTests method listScopes.

private ScopesList listScopes(List<String> scopeNames, String username, String password) {
    final String resourceURI = getURI() + "v1/scopes";
    when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(scopeNames));
    Invocation requestInvocation = this.invocationBuilder(resourceURI, username, password).buildGet();
    Response response = requestInvocation.invoke();
    ScopesList scopes = response.readEntity(ScopesList.class);
    response.close();
    return scopes;
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) AuthFileUtils.credentialsAndAclAsString(io.pravega.auth.AuthFileUtils.credentialsAndAclAsString)

Example 4 with ScopesList

use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.

the class StreamMetaDataAuthFocusedTests method testListScopesReturnsAllScopesForUserWithPermissionOnRootAndChildren.

// endregion
// region Scope listing tests
@Test
public void testListScopesReturnsAllScopesForUserWithPermissionOnRootAndChildren() {
    // Arrange
    final String resourceURI = getURI() + "v1/scopes";
    when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(Arrays.asList("scopea", "scopeb", "scopec")));
    Invocation requestInvocation = this.invocationBuilder(resourceURI, USER_SCOPE_LISTER, DEFAULT_PASSWORD).buildGet();
    // Act
    Response response = requestInvocation.invoke();
    ScopesList scopes = response.readEntity(ScopesList.class);
    // Assert
    assertEquals(3, scopes.getScopes().size());
    response.close();
}
Also used : Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) AuthFileUtils.credentialsAndAclAsString(io.pravega.auth.AuthFileUtils.credentialsAndAclAsString) Test(org.junit.Test)

Example 5 with ScopesList

use of io.pravega.controller.server.rest.generated.model.ScopesList in project pravega by pravega.

the class StreamMetaDataTests method testlistScopes.

/**
 * Test for listScopes REST API.
 *
 * @throws ExecutionException
 * @throws InterruptedException
 */
@Test(timeout = 30000)
public void testlistScopes() throws ExecutionException, InterruptedException {
    final String resourceURI = getURI() + "v1/scopes";
    // Test to list scopes.
    List<String> scopesList = Arrays.asList("scope1", "scope2", "scope3");
    when(mockControllerService.listScopes(anyLong())).thenReturn(CompletableFuture.completedFuture(scopesList));
    Response response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
    assertEquals("List Scopes response code", 200, response.getStatus());
    assertTrue(response.bufferEntity());
    verifyScopes(response.readEntity(ScopesList.class));
    response.close();
    // Test for list scopes failure.
    final CompletableFuture<List<String>> completableFuture = new CompletableFuture<>();
    completableFuture.completeExceptionally(new Exception());
    when(mockControllerService.listScopes(anyLong())).thenReturn(completableFuture);
    response = addAuthHeaders(client.target(resourceURI).request()).buildGet().invoke();
    assertEquals("List Scopes response code", 500, response.getStatus());
    response.close();
}
Also used : Response(javax.ws.rs.core.Response) CompletableFuture(java.util.concurrent.CompletableFuture) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) StreamsList(io.pravega.controller.server.rest.generated.model.StreamsList) List(java.util.List) ScopesList(io.pravega.controller.server.rest.generated.model.ScopesList) ArrayList(java.util.ArrayList) ReaderGroupsList(io.pravega.controller.server.rest.generated.model.ReaderGroupsList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StoreException(io.pravega.controller.store.stream.StoreException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

ScopesList (io.pravega.controller.server.rest.generated.model.ScopesList)7 Response (javax.ws.rs.core.Response)6 Test (org.junit.Test)5 AuthFileUtils.credentialsAndAclAsString (io.pravega.auth.AuthFileUtils.credentialsAndAclAsString)4 ReaderGroupsList (io.pravega.controller.server.rest.generated.model.ReaderGroupsList)3 StreamsList (io.pravega.controller.server.rest.generated.model.StreamsList)3 StoreException (io.pravega.controller.store.stream.StoreException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Invocation (javax.ws.rs.client.Invocation)3 AuthException (io.pravega.auth.AuthException)1 READ (io.pravega.auth.AuthHandler.Permissions.READ)1 READ_UPDATE (io.pravega.auth.AuthHandler.Permissions.READ_UPDATE)1 ClientConfig (io.pravega.client.ClientConfig)1 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)1 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)1 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)1 ReaderGroup (io.pravega.client.stream.ReaderGroup)1 ReaderGroupNotFoundException (io.pravega.client.stream.ReaderGroupNotFoundException)1