Search in sources :

Example 1 with JsonWebToken

use of io.pravega.shared.security.token.JsonWebToken in project pravega by pravega.

the class GrpcAuthHelper method createDelegationToken.

private String createDelegationToken(String resource, AuthHandler.Permissions expectedLevel, String tokenSigningKey) {
    if (isAuthEnabled) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(resource, String.valueOf(expectedLevel));
        return new JsonWebToken("segmentstoreresource", "segmentstore", tokenSigningKey.getBytes(), claims, this.accessTokenTTLInSeconds).toCompactString();
    } else {
        return "";
    }
}
Also used : HashMap(java.util.HashMap) JsonWebToken(io.pravega.shared.security.token.JsonWebToken)

Example 2 with JsonWebToken

use of io.pravega.shared.security.token.JsonWebToken in project pravega by pravega.

the class GrpcAuthHelperTest method createsNonEmptyDelegationTokenWhenAuthIsEnabled.

@Test
public void createsNonEmptyDelegationTokenWhenAuthIsEnabled() {
    GrpcAuthHelper helper = new GrpcAuthHelper(true, "tokenSigningKey", 600);
    String resource = authResource.ofStreamInScope("testScope", "testStream");
    String token = helper.createDelegationToken(resource, AuthHandler.Permissions.READ);
    assertNotNull(token);
    JsonWebToken jwt = JwtParser.parse(token, "tokenSigningKey".getBytes());
    assertNotNull(jwt);
    assertEquals("READ", jwt.getPermissionsByResource().get(resource));
}
Also used : JsonWebToken(io.pravega.shared.security.token.JsonWebToken) Test(org.junit.Test)

Example 3 with JsonWebToken

use of io.pravega.shared.security.token.JsonWebToken in project pravega by pravega.

the class AppendProcessorTest method testSetupAppendClosesConnectionIfTokenHasExpired.

@Test
public void testSetupAppendClosesConnectionIfTokenHasExpired() {
    String streamSegmentName = "scope/stream/0.#epoch.0";
    UUID clientId = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    ConnectionTracker tracker = mock(ConnectionTracker.class);
    val mockedRecorder = Mockito.mock(SegmentStatsRecorder.class);
    @Cleanup AppendProcessor processor = AppendProcessor.defaultBuilder().store(store).connection(new TrackedConnection(connection, tracker)).statsRecorder(mockedRecorder).tokenVerifier(new TokenVerifierImpl("secret")).build();
    setupGetAttributes(streamSegmentName, clientId, store);
    val ac = interceptAppend(store, streamSegmentName, updateEventNumber(clientId, data.length), CompletableFuture.completedFuture((long) data.length));
    Date expiryDate = Date.from(Instant.now().minusSeconds(100));
    JsonWebToken token = new JsonWebToken("subject", "audience", "secret".getBytes(), expiryDate, null);
    SetupAppend setupAppend = new SetupAppend(1, clientId, streamSegmentName, token.toCompactString());
    processor.setupAppend(setupAppend);
    verify(connection).close();
}
Also used : lombok.val(lombok.val) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) JsonWebToken(io.pravega.shared.security.token.JsonWebToken) Date(java.util.Date) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) TokenVerifierImpl(io.pravega.segmentstore.server.host.delegationtoken.TokenVerifierImpl) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with JsonWebToken

use of io.pravega.shared.security.token.JsonWebToken in project pravega by pravega.

the class TokenVerifierImplTest method prepareJwt.

private String prepareJwt(List<String> acls, Integer ttlInSeconds) {
    Map<String, Object> permissionsByResource = new HashMap<>();
    for (String acl : acls) {
        String[] aclContent = acl.split(",");
        String resource = aclContent[0].trim();
        String permission = aclContent[1].trim();
        permissionsByResource.put(resource, permission);
    }
    JsonWebToken token = new JsonWebToken("segmentstoreresource", "segmentstore", "secret".getBytes(), permissionsByResource, ttlInSeconds);
    return token.toCompactString();
}
Also used : HashMap(java.util.HashMap) JsonWebToken(io.pravega.shared.security.token.JsonWebToken)

Example 5 with JsonWebToken

use of io.pravega.shared.security.token.JsonWebToken in project pravega by pravega.

the class AppendProcessorTest method testSetupTokenExpiryTaskClosesConnectionIfTokenHasExpired.

@Test
public void testSetupTokenExpiryTaskClosesConnectionIfTokenHasExpired() {
    // Arrange
    String streamSegmentName = "scope/stream/0.#epoch.0";
    UUID clientId = UUID.randomUUID();
    StreamSegmentStore mockStore = mock(StreamSegmentStore.class);
    ServerConnection mockConnection = mock(ServerConnection.class);
    @Cleanup("shutdown") ScheduledExecutorService executor = new InlineExecutor();
    @Cleanup AppendProcessor processor = AppendProcessor.defaultBuilder().store(mockStore).connection(new TrackedConnection(mockConnection)).tokenExpiryHandlerExecutor(executor).build();
    // Spy the actual Append Processor, so that we can have some of the methods return stubbed values.
    AppendProcessor mockProcessor = spy(processor);
    doReturn(true).when(mockProcessor).isSetupAppendCompleted(streamSegmentName, clientId);
    JsonWebToken token = new JsonWebToken("subject", "audience", "secret".getBytes(), Date.from(Instant.now().minusSeconds(5)), null);
    SetupAppend setupAppend = new SetupAppend(1, clientId, streamSegmentName, token.toCompactString());
    // Act
    mockProcessor.setupTokenExpiryTask(setupAppend, token).join();
    // Assert
    verify(mockConnection).close();
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) InlineExecutor(io.pravega.test.common.InlineExecutor) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UUID(java.util.UUID) Cleanup(lombok.Cleanup) JsonWebToken(io.pravega.shared.security.token.JsonWebToken) Test(org.junit.Test)

Aggregations

JsonWebToken (io.pravega.shared.security.token.JsonWebToken)9 StreamSegmentStore (io.pravega.segmentstore.contracts.StreamSegmentStore)4 SetupAppend (io.pravega.shared.protocol.netty.WireCommands.SetupAppend)4 UUID (java.util.UUID)4 Test (org.junit.Test)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Cleanup (lombok.Cleanup)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AuthHandler (io.pravega.auth.AuthHandler)2 TokenException (io.pravega.auth.TokenException)2 TokenExpiredException (io.pravega.auth.TokenExpiredException)2 Exceptions (io.pravega.common.Exceptions)2 InlineExecutor (io.pravega.test.common.InlineExecutor)2 HashMap (java.util.HashMap)2 NonNull (lombok.NonNull)2 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Throwables (com.google.common.base.Throwables)1 InvalidClaimException (io.pravega.auth.InvalidClaimException)1