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 "";
}
}
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));
}
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();
}
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();
}
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();
}
Aggregations