Search in sources :

Example 1 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class DiscoveryServiceWebTest method testRedirectUrlWithServerStarted.

/**
 * 1. Start : Broker and Discovery service. 2. Provide started broker server as active broker to Discovery service
 * 3. Call GET, PUT, POST request to discovery service that redirects to Broker service and receives response
 *
 * @throws Exception
 */
@Test
public void testRedirectUrlWithServerStarted() throws Exception {
    // 1. start server
    int port = PortManager.nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    String serviceUrl = server.getServiceUri().toString();
    String putRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    String postRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1/replication";
    String getRequestUrl = serviceUrl + "admin/namespaces/p1";
    /**
     * verify : every time when vip receives a request: it redirects to above brokers sequentially and broker
     * returns appropriate response which must not be null.
     */
    assertEquals(hitBrokerService(HttpMethod.POST, postRequestUrl, Lists.newArrayList("use")), "Cannot set replication on a non-global namespace");
    assertEquals(hitBrokerService(HttpMethod.PUT, putRequestUrl, new BundlesData(1)), "Property does not exist");
    assertEquals(hitBrokerService(HttpMethod.GET, getRequestUrl, null), "Property does not exist");
    server.stop();
}
Also used : ServerManager(org.apache.pulsar.discovery.service.server.ServerManager) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test)

Example 2 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookupAuthorizationFailure.

@Test
public void testDiscoveryLookupAuthorizationFailure() throws Exception {
    // (1) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setBindOnLocalhost(true);
    // set Authentication provider which returns "invalid" appid so, authorization fails
    Set<String> providersClassNames = Sets.newHashSet(MockAuthorizationProviderFail.class.getName());
    config.setAuthenticationProviders(providersClassNames);
    // enable authentication
    config.setAuthenticationEnabled(true);
    config.setAuthorizationEnabled(true);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (2) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrl();
    // set authentication data
    Authentication auth = new Authentication() {

        private static final long serialVersionUID = 1L;

        @Override
        public void close() throws IOException {
        }

        @Override
        public String getAuthMethodName() {
            return "auth";
        }

        @Override
        public AuthenticationDataProvider getAuthData() throws PulsarClientException {
            return new AuthenticationDataProvider() {

                private static final long serialVersionUID = 1L;
            };
        }

        @Override
        public void configure(Map<String, String> authParams) {
        }

        @Override
        public void start() throws PulsarClientException {
        }
    };
    @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
    try {
        pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
        fail("should have failed due to authentication");
    } catch (PulsarClientException e) {
        // Ok: expected
        assertTrue(e instanceof PulsarClientException.LookupException);
    }
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Map(java.util.Map) HashMap(java.util.HashMap) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 3 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookupAuthenticationFailure.

@Test
public void testDiscoveryLookupAuthenticationFailure() throws Exception {
    // (1) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setBindOnLocalhost(true);
    // set Authentication provider which fails authentication
    Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProviderFail.class.getName());
    config.setAuthenticationProviders(providersClassNames);
    // enable authentication
    config.setAuthenticationEnabled(true);
    config.setAuthorizationEnabled(true);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (2) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrl();
    // set authentication data
    Authentication auth = new Authentication() {

        private static final long serialVersionUID = 1L;

        @Override
        public void close() throws IOException {
        }

        @Override
        public String getAuthMethodName() {
            return "auth";
        }

        @Override
        public AuthenticationDataProvider getAuthData() throws PulsarClientException {
            return new AuthenticationDataProvider() {

                private static final long serialVersionUID = 1L;
            };
        }

        @Override
        public void configure(Map<String, String> authParams) {
        }

        @Override
        public void start() throws PulsarClientException {
        }
    };
    @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
    try {
        pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
        fail("should have failed due to authentication");
    } catch (PulsarClientException e) {
    // Ok: expected
    }
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Map(java.util.Map) HashMap(java.util.HashMap) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test)

Example 4 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookupAuthAndAuthSuccess.

@Test
public void testDiscoveryLookupAuthAndAuthSuccess() throws Exception {
    // (1) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setBindOnLocalhost(true);
    // add Authentication Provider
    Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProvider.class.getName());
    config.setAuthenticationProviders(providersClassNames);
    // enable authentication and authorization
    config.setAuthenticationEnabled(true);
    config.setAuthorizationEnabled(true);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (2) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrl();
    // set authentication data
    Authentication auth = new Authentication() {

        private static final long serialVersionUID = 1L;

        @Override
        public void close() throws IOException {
        }

        @Override
        public String getAuthMethodName() {
            return "auth";
        }

        @Override
        public AuthenticationDataProvider getAuthData() throws PulsarClientException {
            return new AuthenticationDataProvider() {

                private static final long serialVersionUID = 1L;
            };
        }

        @Override
        public void configure(Map<String, String> authParams) {
        }

        @Override
        public void start() throws PulsarClientException {
        }
    };
    PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use2/my-ns/my-topic1").create();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    producer.close();
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 5 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookupTls.

/**
 * Verify discovery-service binary-proto lookup using tls
 *
 * @throws Exception
 */
@SuppressWarnings("deprecation")
@Test
public void testDiscoveryLookupTls() throws Exception {
    final String TLS_SERVER_CERT_FILE_PATH = "./src/test/resources/certificate/server.crt";
    final String TLS_SERVER_KEY_FILE_PATH = "./src/test/resources/certificate/server.key";
    final String TLS_CLIENT_CERT_FILE_PATH = "./src/test/resources/certificate/client.crt";
    final String TLS_CLIENT_KEY_FILE_PATH = "./src/test/resources/certificate/client.key";
    // (1) restart broker1 with tls enabled
    conf.setTlsAllowInsecureConnection(true);
    conf.setTlsEnabled(true);
    conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    stopBroker();
    startBroker();
    // (2) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setServicePortTls(nextFreePort());
    config.setTlsEnabled(true);
    config.setBindOnLocalhost(true);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (3) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrlTls();
    Map<String, String> authParams = new HashMap<>();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication auth = new AuthenticationTls();
    auth.configure(authParams);
    PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).enableTls(true).allowTlsInsecureConnection(true).build();
    Consumer<byte[]> consumer = pulsarClient2.newConsumer().topic("persistent://my-property2/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient2.newProducer().topic("persistent://my-property2/use2/my-ns/my-topic1").create();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    producer.close();
}
Also used : HashMap(java.util.HashMap) AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Test(org.testng.annotations.Test)

Aggregations

ServiceConfig (org.apache.pulsar.discovery.service.server.ServiceConfig)10 Test (org.testng.annotations.Test)9 DiscoveryService (org.apache.pulsar.discovery.service.DiscoveryService)6 HashMap (java.util.HashMap)4 ServerManager (org.apache.pulsar.discovery.service.server.ServerManager)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Cleanup (lombok.Cleanup)2 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)2 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)2 KeeperException (org.apache.zookeeper.KeeperException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 SecureRandom (java.security.SecureRandom)1