Search in sources :

Example 1 with DefaultCredentials

use of io.pravega.client.stream.impl.DefaultCredentials in project pravega by pravega.

the class PravegaAuthManagerTest method registerInterceptors.

@Test
public void registerInterceptors() throws Exception {
    // Test the registration method.
    GRPCServerConfig config = GRPCServerConfigImpl.builder().authorizationEnabled(true).userPasswordFile(file.getAbsolutePath()).port(1000).build();
    PravegaAuthManager manager = new PravegaAuthManager(config);
    int port = TestUtils.getAvailableListenPort();
    ServerBuilder<?> server = ServerBuilder.forPort(port).useTransportSecurity(new File("../config/cert.pem"), new File("../config/key.pem"));
    server.addService(serviceImpl);
    manager.registerInterceptors(server);
    server.build().start();
    InlineExecutor executor = new InlineExecutor();
    Credentials creds = new DefaultCredentials("1111_aaaa", "admin");
    final ControllerImpl controllerClient = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(URI.create("tcp://localhost:" + port)).build()).retryAttempts(1).build(), executor);
    MultivaluedMap<String, String> map = new MultivaluedHashMap();
    // Without specifying a valid handler.
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Non existent interceptor method.
    map.add("method", "invalid");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method but no parameters for default interceptor.
    map.putSingle("method", "Pravega-Default");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method but no password for default interceptor.
    map.putSingle("username", "dummy3");
    assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
    // Specify a valid method and parameters but invalid resource for default interceptor.
    map.putSingle("password", "password");
    assertFalse("Not existent resource should return false", manager.authenticate("invalid", map, AuthHandler.Permissions.READ));
    // Valid parameters for default interceptor
    map.putSingle("username", "dummy3");
    map.putSingle("password", "password");
    assertTrue("Read access for read resource should return true", manager.authenticate("readresource", map, AuthHandler.Permissions.READ));
    // Stream/scope access should be extended to segment.
    assertTrue("Read access for read resource should return true", manager.authenticate("readresource/segment", map, AuthHandler.Permissions.READ));
    // Levels of access
    assertFalse("Write access for read resource should return false", manager.authenticate("readresource", map, AuthHandler.Permissions.READ_UPDATE));
    assertTrue("Read access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ));
    assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
    // Check the wildcard access
    map.putSingle("username", "dummy4");
    assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
    map.putSingle("method", "testHandler");
    assertTrue("Test handler should be called", manager.authenticate("any", map, AuthHandler.Permissions.READ));
    assertThrows(RetriesExhaustedException.class, () -> controllerClient.createScope("hi").join());
}
Also used : DefaultCredentials(io.pravega.client.stream.impl.DefaultCredentials) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) InlineExecutor(io.pravega.test.common.InlineExecutor) ControllerImpl(io.pravega.client.stream.impl.ControllerImpl) GRPCServerConfig(io.pravega.controller.server.rpc.grpc.GRPCServerConfig) File(java.io.File) Credentials(io.pravega.client.stream.impl.Credentials) DefaultCredentials(io.pravega.client.stream.impl.DefaultCredentials) Test(org.junit.Test)

Example 2 with DefaultCredentials

use of io.pravega.client.stream.impl.DefaultCredentials in project pravega by pravega.

the class InProcPravegaClusterTest method createTestStream.

/**
 * Create the test stream.
 *
 * @throws Exception on any errors.
 */
@Test
public void createTestStream() throws Exception {
    Assert.assertNotNull("Pravega not initialized", localPravega);
    String scope = "Scope";
    String streamName = "Stream";
    int numSegments = 10;
    ClientConfig clientConfig = ClientConfig.builder().controllerURI(URI.create(localPravega.getInProcPravegaCluster().getControllerURI())).credentials(new DefaultCredentials("1111_aaaa", "admin")).trustStore("../config/cert.pem").validateHostName(false).build();
    @Cleanup StreamManager streamManager = StreamManager.create(clientConfig);
    streamManager.createScope(scope);
    Assert.assertTrue("Stream creation is not successful ", streamManager.createStream(scope, streamName, StreamConfiguration.builder().scope(scope).streamName(streamName).scalingPolicy(ScalingPolicy.fixed(numSegments)).build()));
    log.info("Created stream: " + streamName);
    ClientFactory clientFactory = ClientFactory.withScope(scope, clientConfig);
    EventStreamWriter<String> writer = clientFactory.createEventWriter(streamName, new JavaSerializer<String>(), EventWriterConfig.builder().build());
    log.info("Created writer for stream: " + streamName);
    writer.writeEvent("hello").get();
    log.info("Wrote data to the stream");
}
Also used : DefaultCredentials(io.pravega.client.stream.impl.DefaultCredentials) StreamManager(io.pravega.client.admin.StreamManager) ClientFactory(io.pravega.client.ClientFactory) ClientConfig(io.pravega.client.ClientConfig) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

DefaultCredentials (io.pravega.client.stream.impl.DefaultCredentials)2 Test (org.junit.Test)2 ClientConfig (io.pravega.client.ClientConfig)1 ClientFactory (io.pravega.client.ClientFactory)1 StreamManager (io.pravega.client.admin.StreamManager)1 ControllerImpl (io.pravega.client.stream.impl.ControllerImpl)1 Credentials (io.pravega.client.stream.impl.Credentials)1 GRPCServerConfig (io.pravega.controller.server.rpc.grpc.GRPCServerConfig)1 InlineExecutor (io.pravega.test.common.InlineExecutor)1 File (java.io.File)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 Cleanup (lombok.Cleanup)1