Java Code Examples for org.apache.pulsar.broker.ServiceConfiguration#setBrokerServicePortTls()

The following examples show how to use org.apache.pulsar.broker.ServiceConfiguration#setBrokerServicePortTls() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultipleListenerValidatorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testListenerWithTLSPort() {
    ServiceConfiguration config = new ServiceConfiguration();
    config.setBrokerServicePortTls(Optional.of(6651));
    config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651");
    config.setInternalListenerName("internal");
    MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}
 
Example 2
Source File: MultipleListenerValidatorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void testListenerWithoutNonTLSAddress() {
    ServiceConfiguration config = new ServiceConfiguration();
    config.setBrokerServicePortTls(Optional.of(6651));
    config.setAdvertisedListeners(" internal:pulsar+ssl://127.0.0.1:6651");
    config.setInternalListenerName("internal");
    MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}
 
Example 3
Source File: MultipleListenerValidatorTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void testWithoutListenerNameInAdvertisedListeners() {
    ServiceConfiguration config = new ServiceConfiguration();
    config.setBrokerServicePortTls(Optional.of(6651));
    config.setAdvertisedListeners(" internal:pulsar://127.0.0.1:6660, internal:pulsar+ssl://127.0.0.1:6651");
    config.setInternalListenerName("external");
    MultipleListenerValidator.validateAndAnalysisAdvertisedListener(config);
}
 
Example 4
Source File: BrokerServiceLookupTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * 1. Start broker1 and broker2 with tls enable 2. Hit HTTPS lookup url at broker2 which redirects to HTTPS broker1
 *
 * @throws Exception
 */
@Test
public void testWebserviceServiceTls() throws Exception {
    log.info("-- Starting {} test --", methodName);
    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";

    /**** start broker-2 ****/
    ServiceConfiguration conf2 = new ServiceConfiguration();
    conf2.setAdvertisedAddress("localhost");
    conf2.setBrokerServicePort(Optional.of(0));
    conf2.setBrokerServicePortTls(Optional.of(0));
    conf2.setWebServicePort(Optional.of(0));
    conf2.setWebServicePortTls(Optional.of(0));
    conf2.setAdvertisedAddress("localhost");
    conf2.setTlsAllowInsecureConnection(true);
    conf2.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf2.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    conf2.setClusterName(conf.getClusterName());
    conf2.setZookeeperServers("localhost:2181");

    @Cleanup
    PulsarService pulsar2 = startBroker(conf2);

    // restart broker1 with tls enabled
    conf.setBrokerServicePortTls(Optional.of(0));
    conf.setWebServicePortTls(Optional.of(0));
    conf.setTlsAllowInsecureConnection(true);
    conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    stopBroker();
    startBroker();
    pulsar.getLoadManager().get().writeLoadReportOnZookeeper();
    pulsar2.getLoadManager().get().writeLoadReportOnZookeeper();

    LoadManager loadManager1 = spy(pulsar.getLoadManager().get());
    LoadManager loadManager2 = spy(pulsar2.getLoadManager().get());
    Field loadManagerField = NamespaceService.class.getDeclaredField("loadManager");
    loadManagerField.setAccessible(true);

    // mock: redirect request to leader [2]
    doReturn(true).when(loadManager2).isCentralized();
    loadManagerField.set(pulsar2.getNamespaceService(), new AtomicReference<>(loadManager2));
    loadManagerField.set(pulsar.getNamespaceService(), new AtomicReference<>(loadManager1));

    // mock: return Broker2 as a Least-loaded broker when leader receives
    // request [3]
    doReturn(true).when(loadManager1).isCentralized();
    doReturn(true).when(loadManager2).isCentralized();
    SimpleResourceUnit resourceUnit = new SimpleResourceUnit(pulsar.getWebServiceAddress(), null);
    doReturn(Optional.of(resourceUnit)).when(loadManager2).getLeastLoaded(any(ServiceUnitId.class));
    doReturn(Optional.of(resourceUnit)).when(loadManager1).getLeastLoaded(any(ServiceUnitId.class));


    /**** started broker-2 ****/

    URI brokerServiceUrl = new URI("pulsar://localhost:" + conf2.getBrokerServicePort().get());
    @Cleanup
    PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(brokerServiceUrl.toString()).build();

    final String lookupResourceUrl = "/lookup/v2/topic/persistent/my-property/my-ns/my-topic1";

    // set client cert_key file
    KeyManager[] keyManagers = null;
    Certificate[] tlsCert = SecurityUtility.loadCertificatesFromPemFile(TLS_CLIENT_CERT_FILE_PATH);
    PrivateKey tlsKey = SecurityUtility.loadPrivateKeyFromPemFile(TLS_CLIENT_KEY_FILE_PATH);
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    ks.setKeyEntry("private", tlsKey, "".toCharArray(), tlsCert);
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks, "".toCharArray());
    keyManagers = kmf.getKeyManagers();
    TrustManager[] trustManagers = InsecureTrustManagerFactory.INSTANCE.getTrustManagers();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagers, trustManagers, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());

    // hit broker2 url
    URLConnection con = new URL(pulsar2.getWebServiceAddressTls() + lookupResourceUrl).openConnection();
    log.info("orignal url: {}", con.getURL());
    con.connect();
    log.info("connected url: {} ", con.getURL());
    // assert connect-url: broker2-https
    assertEquals(new Integer(con.getURL().getPort()), conf2.getWebServicePortTls().get());
    InputStream is = con.getInputStream();
    // assert redirect-url: broker1-https only
    log.info("redirected url: {}", con.getURL());
    assertEquals(new Integer(con.getURL().getPort()), conf.getWebServicePortTls().get());
    is.close();

    loadManager1 = null;
    loadManager2 = null;
}
 
Example 5
Source File: BrokerAdminClientTlsAuthTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * Test case => Use Multiple Brokers
 *           => Create a namespace with bundles distributed among these brokers.
 *           => Use Tls as authPlugin for everything.
 *           => Run list topics command
 * @throws Exception
 */
@Test
public void testPersistentList() throws Exception {
    log.info("-- Starting {} test --", methodName);

    /***** Start Broker 2 ******/
    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setBrokerServicePort(Optional.of(0));
    conf.setBrokerServicePortTls(Optional.of(0));
    conf.setWebServicePort(Optional.of(0));
    conf.setWebServicePortTls(Optional.of(0));
    conf.setAdvertisedAddress("localhost");
    conf.setClusterName(this.conf.getClusterName());
    conf.setZookeeperServers("localhost:2181");
    buildConf(conf);

    @Cleanup
    PulsarService pulsar2 = startBroker(conf);

    /***** Broker 2 Started *****/
    try (PulsarAdmin admin = buildAdminClient("superproxy")) {
        admin.clusters().createCluster("test", new ClusterData(brokerUrl.toString()));
        admin.tenants().createTenant("tenant",
                                     new TenantInfo(ImmutableSet.of("admin"),
                                                    ImmutableSet.of("test")));
    }
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        Policies policies = new Policies();
        policies.bundles = new BundlesData(4);
        policies.auth_policies.namespace_auth.put("admin", ImmutableSet.of(AuthAction.produce, AuthAction.consume));
        policies.replication_clusters = ImmutableSet.of("test");
        admin.namespaces().createNamespace("tenant/ns", policies);
        try {
            admin.topics().getList("tenant/ns");
        } catch (PulsarAdminException ex) {
            ex.printStackTrace();
            fail("Should not have thrown an exception");
        }
    }

}
 
Example 6
Source File: ModularLoadManagerImplTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
void setup() throws Exception {
    executor = new ThreadPoolExecutor(1, 20, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>());

    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, 0, () -> 0);
    bkEnsemble.start();

    // Start broker 1
    ServiceConfiguration config1 = new ServiceConfiguration();
    config1.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config1.setClusterName("use");
    config1.setWebServicePort(Optional.of(0));
    config1.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());

    config1.setAdvertisedAddress("localhost");
    config1.setBrokerServicePort(Optional.of(0));
    config1.setBrokerServicePortTls(Optional.of(0));
    config1.setWebServicePortTls(Optional.of(0));
    pulsar1 = new PulsarService(config1);
    pulsar1.start();

    primaryHost = String.format("%s:%d", "localhost", pulsar1.getListenPortHTTP().get());
    url1 = new URL(pulsar1.getWebServiceAddress());
    admin1 = PulsarAdmin.builder().serviceHttpUrl(url1.toString()).build();

    // Start broker 2
    ServiceConfiguration config2 = new ServiceConfiguration();
    config2.setLoadManagerClassName(ModularLoadManagerImpl.class.getName());
    config2.setClusterName("use");
    config2.setWebServicePort(Optional.of(0));
    config2.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config2.setAdvertisedAddress("localhost");
    config2.setBrokerServicePort(Optional.of(0));
    config2.setBrokerServicePortTls(Optional.of(0));
    config2.setWebServicePortTls(Optional.of(0));
    pulsar2 = new PulsarService(config2);
    pulsar2.start();

    secondaryHost = String.format("%s:%d", "localhost", pulsar2.getListenPortHTTP().get());
    url2 = new URL(pulsar2.getWebServiceAddress());
    admin2 = PulsarAdmin.builder().serviceHttpUrl(url2.toString()).build();

    primaryLoadManager = (ModularLoadManagerImpl) getField(pulsar1.getLoadManager().get(), "loadManager");
    secondaryLoadManager = (ModularLoadManagerImpl) getField(pulsar2.getLoadManager().get(), "loadManager");
    nsFactory = new NamespaceBundleFactory(pulsar1, Hashing.crc32());
    Thread.sleep(100);
}
 
Example 7
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
void setup() throws Exception {
    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, 0, () -> 0);
    bkEnsemble.start();
    ZkUtils.createFullPathOptimistic(bkEnsemble.getZkClient(),
            SimpleLoadManagerImpl.LOADBALANCER_DYNAMIC_SETTING_STRATEGY_ZPATH,
            "{\"loadBalancerStrategy\":\"leastLoadedServer\"}".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);

    final String localhost = "localhost";
    // start brokers
    for (int i = 0; i < BROKER_COUNT; i++) {


        ServiceConfiguration config = new ServiceConfiguration();
        config.setBrokerServicePort(Optional.ofNullable(brokerNativeBrokerPorts[i]));
        config.setClusterName("use");
        config.setAdvertisedAddress(localhost);
        config.setAdvertisedAddress("localhost");
        config.setWebServicePort(Optional.of(0));
        config.setBrokerServicePortTls(Optional.of(0));
        config.setWebServicePortTls(Optional.of(0));
        config.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
        config.setBrokerServicePort(Optional.of(0));
        config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
        config.setAdvertisedAddress(localhost+i);
        config.setLoadBalancerEnabled(false);

        pulsarServices[i] = new PulsarService(config);
        pulsarServices[i].start();
        brokerWebServicePorts[i] = pulsarServices[i].getListenPortHTTP().get();
        brokerNativeBrokerPorts[i] = pulsarServices[i].getBrokerListenPort().get();

        brokerUrls[i] = new URL("http://127.0.0.1" + ":" + brokerWebServicePorts[i]);
        lookupAddresses[i] = pulsarServices[i].getAdvertisedAddress() + ":" + pulsarServices[i].getListenPortHTTP().get();
        pulsarAdmins[i] = PulsarAdmin.builder().serviceHttpUrl(brokerUrls[i].toString()).build();
    }

    createNamespacePolicies(pulsarServices[0]);

    Thread.sleep(100);
}
 
Example 8
Source File: SimpleLoadManagerImplTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
void setup() throws Exception {

    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, 0, () -> 0);
    bkEnsemble.start();

    // Start broker 1
    ServiceConfiguration config1 = spy(new ServiceConfiguration());
    config1.setClusterName("use");
    config1.setWebServicePort(Optional.of(0));
    config1.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config1.setBrokerServicePort(Optional.of(0));
    config1.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    config1.setBrokerServicePortTls(Optional.of(0));
    config1.setWebServicePortTls(Optional.of(0));
    config1.setAdvertisedAddress("localhost");
    pulsar1 = new PulsarService(config1);
    pulsar1.start();

    url1 = new URL(pulsar1.getWebServiceAddress());
    admin1 = PulsarAdmin.builder().serviceHttpUrl(url1.toString()).build();
    brokerStatsClient1 = admin1.brokerStats();
    primaryHost = pulsar1.getWebServiceAddress();

    // Start broker 2
    ServiceConfiguration config2 = new ServiceConfiguration();
    config2.setClusterName("use");
    config2.setWebServicePort(Optional.of(0));
    config2.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config2.setBrokerServicePort(Optional.of(0));
    config2.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    config2.setBrokerServicePortTls(Optional.of(0));
    config2.setWebServicePortTls(Optional.of(0));
    config2.setAdvertisedAddress("localhost");
    pulsar2 = new PulsarService(config2);
    pulsar2.start();

    url2 = new URL(pulsar2.getWebServiceAddress());
    admin2 = PulsarAdmin.builder().serviceHttpUrl(url2.toString()).build();
    brokerStatsClient2 = admin2.brokerStats();
    secondaryHost = pulsar2.getWebServiceAddress();
    Thread.sleep(100);
}