Java Code Examples for org.apache.pulsar.client.api.ClientBuilder#authentication()

The following examples show how to use org.apache.pulsar.client.api.ClientBuilder#authentication() . 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: PulsarSecurityConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public ClientBuilder configurePulsarBuilder(ClientBuilder builder) throws StageException {
  builder.enableTls(tlsEnabled);
  if (tlsEnabled) {
    builder.tlsTrustCertsFilePath(caCertFileFullPath);

    if (tlsAuthEnabled) {
      Map<String, String> authParams = new HashMap<>();
      authParams.put("tlsCertFile", clientCertFileFullPath);
      authParams.put("tlsKeyFile", clientKeyFileFullPath);

      try {
        builder.authentication(AuthenticationFactory.create(AuthenticationTls.class.getName(), authParams));
      } catch (PulsarClientException.UnsupportedAuthenticationException e) {
        throw new StageException(PulsarErrors.PULSAR_17, e.toString(), e);
      }
    }
  }

  return builder;
}
 
Example 2
Source File: TlsProducerConsumerBase.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
        clientBuilder.authentication(AuthenticationTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example 3
Source File: TlsProducerConsumerBase.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH).enableTls(true).allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
        authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
        clientBuilder.authentication(AuthenticationTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example 4
Source File: ProxyKeyStoreTlsTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected PulsarClient internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    ClientBuilder clientBuilder = PulsarClient.builder()
            .serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    return clientBuilder.build();
}
 
Example 5
Source File: KeyStoreTlsProducerConsumerTestWithAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    Set<String> tlsProtocols = Sets.newConcurrentHashSet();
    tlsProtocols.add("TLSv1.2");

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .tlsProtocols(tlsProtocols)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example 6
Source File: KeyStoreTlsProducerConsumerTestWithoutAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected void internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    if (pulsarClient != null) {
        pulsarClient.close();
    }

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    pulsarClient = clientBuilder.build();
}
 
Example 7
Source File: PulsarBlockChainEventBroadcaster.java    From eventeum with Apache License 2.0 6 votes vote down vote up
public PulsarBlockChainEventBroadcaster(PulsarSettings settings, ObjectMapper mapper) throws PulsarClientException {
	this.mapper = mapper;

	ClientBuilder builder = PulsarClient.builder();

	if (settings.getConfig() != null) {
		builder.loadConf(settings.getConfig());
	}

	Authentication authSettings = settings.getAuthentication();
	if (authSettings != null) {
		builder.authentication(
				authSettings.getPluginClassName(),
				authSettings.getParams());
	}

	client = builder.build();

	blockEventProducer = createProducer(settings.getTopic().getBlockEvents());
	contractEventProducer = createProducer(settings.getTopic().getContractEvents());
       transactionEventProducer = createProducer(settings.getTopic().getTransactionEvents());
}
 
Example 8
Source File: ProxyKeyStoreTlsTestWithAuth.java    From pulsar with Apache License 2.0 6 votes vote down vote up
protected PulsarClient internalSetUpForClient(boolean addCertificates, String lookupUrl) throws Exception {
    ClientBuilder clientBuilder = PulsarClient.builder()
            .serviceUrl(lookupUrl)
            .enableTls(true)
            .useKeyStoreTls(true)
            .tlsTrustStorePath(BROKER_TRUSTSTORE_FILE_PATH)
            .tlsTrustStorePassword(BROKER_TRUSTSTORE_PW)
            .allowTlsInsecureConnection(false)
            .operationTimeout(1000, TimeUnit.MILLISECONDS);
    if (addCertificates) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_TYPE, KEYSTORE_TYPE);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PATH, CLIENT_KEYSTORE_FILE_PATH);
        authParams.put(AuthenticationKeyStoreTls.KEYSTORE_PW, CLIENT_KEYSTORE_PW);
        clientBuilder.authentication(AuthenticationKeyStoreTls.class.getName(), authParams);
    }
    return clientBuilder.build();
}
 
Example 9
Source File: PulsarClientTool.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void updateConfig() throws UnsupportedAuthenticationException {
    ClientBuilder clientBuilder = PulsarClient.builder();
    Authentication authentication = null;
    if (isNotBlank(this.authPluginClassName)) {
        authentication = AuthenticationFactory.create(authPluginClassName, authParams);
        clientBuilder.authentication(authentication);
    }
    clientBuilder.allowTlsInsecureConnection(this.tlsAllowInsecureConnection);
    clientBuilder.tlsTrustCertsFilePath(this.tlsTrustCertsFilePath);
    clientBuilder.serviceUrl(serviceURL);

    clientBuilder.useKeyStoreTls(useKeyStoreTls)
            .tlsTrustStoreType(tlsTrustStoreType)
            .tlsTrustStorePath(tlsTrustStorePath)
            .tlsTrustStorePassword(tlsTrustStorePassword);

    if (StringUtils.isNotBlank(proxyServiceURL)) {
        if (proxyProtocol == null) {
            System.out.println("proxy-protocol must be provided with proxy-url");
            System.exit(-1);
        }
        clientBuilder.proxyServiceUrl(proxyServiceURL, proxyProtocol);
    }
    this.produceCommand.updateConfig(clientBuilder, authentication, this.serviceURL);
    this.consumeCommand.updateConfig(clientBuilder, authentication, this.serviceURL);
}
 
Example 10
Source File: ThreadRuntimeFactory.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static PulsarClient createPulsarClient(String pulsarServiceUrl, AuthenticationConfig authConfig)
        throws PulsarClientException {
    ClientBuilder clientBuilder = null;
    if (isNotBlank(pulsarServiceUrl)) {
        clientBuilder = PulsarClient.builder().serviceUrl(pulsarServiceUrl);
        if (authConfig != null) {
            if (isNotBlank(authConfig.getClientAuthenticationPlugin())
                    && isNotBlank(authConfig.getClientAuthenticationParameters())) {
                clientBuilder.authentication(authConfig.getClientAuthenticationPlugin(),
                        authConfig.getClientAuthenticationParameters());
            }
            clientBuilder.enableTls(authConfig.isUseTls());
            clientBuilder.allowTlsInsecureConnection(authConfig.isTlsAllowInsecureConnection());
            clientBuilder.enableTlsHostnameVerification(authConfig.isTlsHostnameVerificationEnable());
            clientBuilder.tlsTrustCertsFilePath(authConfig.getTlsTrustCertsFilePath());
        }
        return clientBuilder.build();
    }
    return null;
}
 
Example 11
Source File: WebSocketService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private PulsarClient createClientInstance(ClusterData clusterData) throws IOException {
     ClientBuilder clientBuilder = PulsarClient.builder() //
             .statsInterval(0, TimeUnit.SECONDS) //
             .enableTls(config.isTlsEnabled()) //
             .allowTlsInsecureConnection(config.isTlsAllowInsecureConnection()) //
             .tlsTrustCertsFilePath(config.getBrokerClientTrustCertsFilePath()) //
             .ioThreads(config.getWebSocketNumIoThreads()) //
             .connectionsPerBroker(config.getWebSocketConnectionsPerBroker());

     if (isNotBlank(config.getBrokerClientAuthenticationPlugin())
             && isNotBlank(config.getBrokerClientAuthenticationParameters())) {
         clientBuilder.authentication(config.getBrokerClientAuthenticationPlugin(),
                 config.getBrokerClientAuthenticationParameters());
     }

     if (config.isBrokerClientTlsEnabled()) {
if (isNotBlank(clusterData.getBrokerServiceUrlTls())) {
		clientBuilder.serviceUrl(clusterData.getBrokerServiceUrlTls());
} else if (isNotBlank(clusterData.getServiceUrlTls())) {
		clientBuilder.serviceUrl(clusterData.getServiceUrlTls());
}
     } else if (isNotBlank(clusterData.getBrokerServiceUrl())) {
         clientBuilder.serviceUrl(clusterData.getBrokerServiceUrl());
     } else {
         clientBuilder.serviceUrl(clusterData.getServiceUrl());
     }

     return clientBuilder.build();
 }
 
Example 12
Source File: PulsarService.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public synchronized PulsarClient getClient() throws PulsarServerException {
    if (this.client == null) {
        try {
            ClientBuilder builder = PulsarClient.builder()
                .serviceUrl(this.getConfiguration().isTlsEnabled()
                            ? this.brokerServiceUrlTls : this.brokerServiceUrl)
                .enableTls(this.getConfiguration().isTlsEnabled())
                .allowTlsInsecureConnection(this.getConfiguration().isTlsAllowInsecureConnection())
                .tlsTrustCertsFilePath(this.getConfiguration().getTlsCertificateFilePath());

            if (this.getConfiguration().isBrokerClientTlsEnabled()) {
                if (this.getConfiguration().isBrokerClientTlsEnabledWithKeyStore()) {
                    builder.useKeyStoreTls(true)
                            .tlsTrustStoreType(this.getConfiguration().getBrokerClientTlsTrustStoreType())
                            .tlsTrustStorePath(this.getConfiguration().getBrokerClientTlsTrustStore())
                            .tlsTrustStorePassword(this.getConfiguration().getBrokerClientTlsTrustStorePassword());
                } else {
                    builder.tlsTrustCertsFilePath(
                            isNotBlank(this.getConfiguration().getBrokerClientTrustCertsFilePath())
                                    ? this.getConfiguration().getBrokerClientTrustCertsFilePath()
                                    : this.getConfiguration().getTlsCertificateFilePath());
                }
            }

            if (isNotBlank(this.getConfiguration().getBrokerClientAuthenticationPlugin())) {
                builder.authentication(this.getConfiguration().getBrokerClientAuthenticationPlugin(),
                                       this.getConfiguration().getBrokerClientAuthenticationParameters());
            }
            this.client = builder.build();
        } catch (Exception e) {
            throw new PulsarServerException(e);
        }
    }
    return this.client;
}
 
Example 13
Source File: WorkerUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static PulsarClient getPulsarClient(String pulsarServiceUrl, String authPlugin, String authParams,
                                           Boolean useTls, String tlsTrustCertsFilePath,
                                           Boolean allowTlsInsecureConnection,
                                           Boolean enableTlsHostnameVerificationEnable) {

    try {
        ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(pulsarServiceUrl);

        if (isNotBlank(authPlugin)
                && isNotBlank(authParams)) {
            clientBuilder.authentication(authPlugin, authParams);
        }
        if (useTls != null) {
            clientBuilder.enableTls(useTls);
        }
        if (allowTlsInsecureConnection != null) {
            clientBuilder.allowTlsInsecureConnection(allowTlsInsecureConnection);
        }
        if (isNotBlank(tlsTrustCertsFilePath)) {
            clientBuilder.tlsTrustCertsFilePath(tlsTrustCertsFilePath);
        }
        if (enableTlsHostnameVerificationEnable != null) {
            clientBuilder.enableTlsHostnameVerification(enableTlsHostnameVerificationEnable);
        }

        return clientBuilder.build();
    } catch (PulsarClientException e) {
        log.error("Error creating pulsar client", e);
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: PulsarFunctionPublishTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
void setup(Method method) throws Exception {

    // delete all function temp files
    File dir = new File(System.getProperty("java.io.tmpdir"));
    File[] foundFiles = dir.listFiles((ignoredDir, name) -> name.startsWith("function"));

    for (File file : foundFiles) {
        file.delete();
    }

    log.info("--- Setting up method {} ---", method.getName());

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

    config = spy(new ServiceConfiguration());
    config.setClusterName("use");
    Set<String> superUsers = Sets.newHashSet("superUser");
    config.setSuperUserRoles(superUsers);
    config.setWebServicePort(Optional.of(0));
    config.setWebServicePortTls(Optional.of(0));
    config.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config.setBrokerServicePort(Optional.of(0));
    config.setBrokerServicePortTls(Optional.of(0));
    config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    config.setTlsAllowInsecureConnection(true);
    config.setAdvertisedAddress("localhost");

    Set<String> providers = new HashSet<>();
    providers.add(AuthenticationProviderTls.class.getName());
    config.setAuthenticationEnabled(true);
    config.setAuthenticationProviders(providers);

    config.setAuthorizationEnabled(true);
    config.setAuthorizationProvider(PulsarAuthorizationProvider.class.getName());

    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config.setTlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);

    config.setBrokerClientAuthenticationPlugin(AuthenticationTls.class.getName());
    config.setBrokerClientAuthenticationParameters(
            "tlsCertFile:" + TLS_CLIENT_CERT_FILE_PATH + "," + "tlsKeyFile:" + TLS_CLIENT_KEY_FILE_PATH);
    config.setBrokerClientTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH);
    config.setBrokerClientTlsEnabled(true);
    config.setAllowAutoTopicCreationType("non-partitioned");

    functionsWorkerService = createPulsarFunctionWorker(config);

    Optional<WorkerService> functionWorkerService = Optional.of(functionsWorkerService);
    pulsar = new PulsarService(config, functionWorkerService, (exitCode) -> {});
    pulsar.start();

    String brokerServiceUrl = pulsar.getWebServiceAddressTls();
    urlTls = new URL(brokerServiceUrl);


    Map<String, String> authParams = new HashMap<>();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);

    admin = spy(
            PulsarAdmin.builder().serviceHttpUrl(brokerServiceUrl).tlsTrustCertsFilePath(TLS_TRUST_CERT_FILE_PATH)
                    .allowTlsInsecureConnection(true).authentication(authTls).build());

    brokerStatsClient = admin.brokerStats();
    primaryHost = pulsar.getWebServiceAddress();

    // update cluster metadata
    ClusterData clusterData = new ClusterData(urlTls.toString());
    admin.clusters().updateCluster(config.getClusterName(), clusterData);

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(this.workerConfig.getPulsarServiceUrl());
    if (isNotBlank(workerConfig.getClientAuthenticationPlugin())
            && isNotBlank(workerConfig.getClientAuthenticationParameters())) {
        clientBuilder.enableTls(workerConfig.isUseTls());
        clientBuilder.allowTlsInsecureConnection(workerConfig.isTlsAllowInsecureConnection());
        clientBuilder.authentication(workerConfig.getClientAuthenticationPlugin(),
                workerConfig.getClientAuthenticationParameters());
    }
    pulsarClient = clientBuilder.build();

    TenantInfo propAdmin = new TenantInfo();
    propAdmin.getAdminRoles().add("superUser");
    propAdmin.setAllowedClusters(Sets.newHashSet(Lists.newArrayList("use")));
    admin.tenants().updateTenant(tenant, propAdmin);

    System.setProperty(JAVA_INSTANCE_JAR_PROPERTY,
            FutureUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath());

    functionWorkerService.get().getLeaderService().waitLeaderInit();
}
 
Example 15
Source File: PulsarFunctionAdminTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
void setup(Method method) throws Exception {

    log.info("--- Setting up method {} ---", method.getName());

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

    config = spy(new ServiceConfiguration());
    config.setClusterName("use");
    Set<String> superUsers = Sets.newHashSet("superUser");
    config.setSuperUserRoles(superUsers);
    config.setWebServicePort(Optional.of(0));
    config.setWebServicePortTls(Optional.of(0));
    config.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config.setBrokerServicePort(Optional.of(0));
    config.setBrokerServicePortTls(Optional.of(0));
    config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());


    Set<String> providers = new HashSet<>();
    providers.add(AuthenticationProviderTls.class.getName());
    config.setAuthenticationEnabled(true);
    config.setAuthenticationProviders(providers);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config.setTlsAllowInsecureConnection(true);


    functionsWorkerService = createPulsarFunctionWorker(config);
    Optional<WorkerService> functionWorkerService = Optional.of(functionsWorkerService);
    pulsar = new PulsarService(config, functionWorkerService, (exitCode) -> {});
    pulsar.start();
    urlTls = new URL(pulsar.getBrokerServiceUrlTls());

    Map<String, String> authParams = new HashMap<>();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);

    admin = spy(
            PulsarAdmin.builder()
                    .serviceHttpUrl(pulsar.getWebServiceAddressTls())
                    .tlsTrustCertsFilePath(TLS_CLIENT_CERT_FILE_PATH)
                    .allowTlsInsecureConnection(true)
                    .authentication(authTls)
                    .build());

    brokerStatsClient = admin.brokerStats();
    primaryHost = pulsar.getWebServiceAddress();

    // update cluster metadata
    ClusterData clusterData = new ClusterData(urlTls.toString());
    admin.clusters().updateCluster(config.getClusterName(), clusterData);

    ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(this.workerConfig.getPulsarServiceUrl());
    if (isNotBlank(workerConfig.getClientAuthenticationPlugin())
            && isNotBlank(workerConfig.getClientAuthenticationParameters())) {
        clientBuilder.enableTls(workerConfig.isUseTls());
        clientBuilder.allowTlsInsecureConnection(workerConfig.isTlsAllowInsecureConnection());
        clientBuilder.authentication(workerConfig.getClientAuthenticationPlugin(),
                workerConfig.getClientAuthenticationParameters());
    }
    pulsarClient = clientBuilder.build();

    TenantInfo propAdmin = new TenantInfo();
    propAdmin.setAllowedClusters(Sets.newHashSet(Lists.newArrayList("use")));
    admin.tenants().updateTenant(tenant, propAdmin);

    Thread.sleep(100);
}
 
Example 16
Source File: CompactorTool.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Arguments arguments = new Arguments();
    JCommander jcommander = new JCommander(arguments);
    jcommander.setProgramName("PulsarTopicCompactor");

    // parse args by JCommander
    jcommander.parse(args);
    if (arguments.help) {
        jcommander.usage();
        System.exit(-1);
    }

    // init broker config
    ServiceConfiguration brokerConfig;
    if (isBlank(arguments.brokerConfigFile)) {
        jcommander.usage();
        throw new IllegalArgumentException("Need to specify a configuration file for broker");
    } else {
        brokerConfig = PulsarConfigurationLoader.create(
                arguments.brokerConfigFile, ServiceConfiguration.class);
    }

    ClientBuilder clientBuilder = PulsarClient.builder();

    if (isNotBlank(brokerConfig.getBrokerClientAuthenticationPlugin())) {
        clientBuilder.authentication(brokerConfig.getBrokerClientAuthenticationPlugin(),
                brokerConfig.getBrokerClientAuthenticationParameters());
    }


    if (brokerConfig.getBrokerServicePortTls().isPresent()) {
        clientBuilder
                .serviceUrl(PulsarService.brokerUrlTls(PulsarService.advertisedAddress(brokerConfig),
                        brokerConfig.getBrokerServicePortTls().get()))
                .allowTlsInsecureConnection(brokerConfig.isTlsAllowInsecureConnection())
                .tlsTrustCertsFilePath(brokerConfig.getTlsCertificateFilePath());

    } else {
        clientBuilder.serviceUrl(PulsarService.brokerUrl(PulsarService.advertisedAddress(brokerConfig),
                brokerConfig.getBrokerServicePort().get()));
    }

    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("compaction-%d").setDaemon(true).build());

    OrderedScheduler executor = OrderedScheduler.newSchedulerBuilder().build();
    ZooKeeperClientFactory zkClientFactory = new ZookeeperBkClientFactoryImpl(executor);

    ZooKeeper zk = zkClientFactory.create(brokerConfig.getZookeeperServers(),
                                          ZooKeeperClientFactory.SessionType.ReadWrite,
                                          (int)brokerConfig.getZooKeeperSessionTimeoutMillis()).get();
    BookKeeperClientFactory bkClientFactory = new BookKeeperClientFactoryImpl();
    BookKeeper bk = bkClientFactory.create(brokerConfig, zk, Optional.empty(), null);
    try (PulsarClient pulsar = clientBuilder.build()) {
        Compactor compactor = new TwoPhaseCompactor(brokerConfig, pulsar, bk, scheduler);
        long ledgerId = compactor.compact(arguments.topic).get();
        log.info("Compaction of topic {} complete. Compacted to ledger {}", arguments.topic, ledgerId);
    } finally {
        bk.close();
        bkClientFactory.close();
        zk.close();
        scheduler.shutdownNow();
        executor.shutdown();
    }
}
 
Example 17
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static ClientBuilder getClientBuilder(Properties properties) {
    ClientBuilder clientBuilder = PulsarClient.builder();

    if (properties.containsKey(AUTHENTICATION_CLASS)) {
        String className = properties.getProperty(AUTHENTICATION_CLASS);
        try {
            if (properties.containsKey(AUTHENTICATION_PARAMS_STRING)) {
                String authParamsString = (String) properties.get(AUTHENTICATION_PARAMS_STRING);
                clientBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                clientBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                clientBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    clientBuilder.enableTls(Boolean.parseBoolean(properties.getProperty(USE_TLS, "false")));
    clientBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    clientBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

    if (properties.containsKey(TLS_TRUST_CERTS_FILE_PATH)) {
        clientBuilder.tlsTrustCertsFilePath(properties.getProperty(TLS_TRUST_CERTS_FILE_PATH));
    }

    if (properties.containsKey(OPERATION_TIMEOUT_MS)) {
        clientBuilder.operationTimeout(Integer.parseInt(properties.getProperty(OPERATION_TIMEOUT_MS)),
                TimeUnit.MILLISECONDS);
    }

    if (properties.containsKey(STATS_INTERVAL_SECONDS)) {
        clientBuilder.statsInterval(Integer.parseInt(properties.getProperty(STATS_INTERVAL_SECONDS)),
                TimeUnit.SECONDS);
    }

    if (properties.containsKey(NUM_IO_THREADS)) {
        clientBuilder.ioThreads(Integer.parseInt(properties.getProperty(NUM_IO_THREADS)));
    }

    if (properties.containsKey(CONNECTIONS_PER_BROKER)) {
        clientBuilder.connectionsPerBroker(Integer.parseInt(properties.getProperty(CONNECTIONS_PER_BROKER)));
    }

    if (properties.containsKey(USE_TCP_NODELAY)) {
        clientBuilder.enableTcpNoDelay(Boolean.parseBoolean(properties.getProperty(USE_TCP_NODELAY)));
    }

    if (properties.containsKey(CONCURRENT_LOOKUP_REQUESTS)) {
        clientBuilder.maxConcurrentLookupRequests(Integer.parseInt(properties.getProperty(CONCURRENT_LOOKUP_REQUESTS)));
    }

    if (properties.containsKey(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)) {
        clientBuilder.maxNumberOfRejectedRequestPerConnection(
                Integer.parseInt(properties.getProperty(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)));
    }

    return clientBuilder;
}
 
Example 18
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static ClientBuilder getClientBuilder(Properties properties) {
    ClientBuilder clientBuilder = PulsarClient.builder();

    if (properties.containsKey(AUTHENTICATION_CLASS)) {
        String className = properties.getProperty(AUTHENTICATION_CLASS);
        try {
            if (properties.containsKey(AUTHENTICATION_PARAMS_STRING)) {
                String authParamsString = (String) properties.get(AUTHENTICATION_PARAMS_STRING);
                clientBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                clientBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                clientBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    clientBuilder.enableTls(Boolean.parseBoolean(properties.getProperty(USE_TLS, "false")));
    clientBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    clientBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

    if (properties.containsKey(TLS_TRUST_CERTS_FILE_PATH)) {
        clientBuilder.tlsTrustCertsFilePath(properties.getProperty(TLS_TRUST_CERTS_FILE_PATH));
    }

    if (properties.containsKey(OPERATION_TIMEOUT_MS)) {
        clientBuilder.operationTimeout(Integer.parseInt(properties.getProperty(OPERATION_TIMEOUT_MS)),
                TimeUnit.MILLISECONDS);
    }

    if (properties.containsKey(STATS_INTERVAL_SECONDS)) {
        clientBuilder.statsInterval(Integer.parseInt(properties.getProperty(STATS_INTERVAL_SECONDS)),
                TimeUnit.SECONDS);
    }

    if (properties.containsKey(NUM_IO_THREADS)) {
        clientBuilder.ioThreads(Integer.parseInt(properties.getProperty(NUM_IO_THREADS)));
    }

    if (properties.containsKey(CONNECTIONS_PER_BROKER)) {
        clientBuilder.connectionsPerBroker(Integer.parseInt(properties.getProperty(CONNECTIONS_PER_BROKER)));
    }

    if (properties.containsKey(USE_TCP_NODELAY)) {
        clientBuilder.enableTcpNoDelay(Boolean.parseBoolean(properties.getProperty(USE_TCP_NODELAY)));
    }

    if (properties.containsKey(CONCURRENT_LOOKUP_REQUESTS)) {
        clientBuilder.maxConcurrentLookupRequests(Integer.parseInt(properties.getProperty(CONCURRENT_LOOKUP_REQUESTS)));
    }

    if (properties.containsKey(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)) {
        clientBuilder.maxNumberOfRejectedRequestPerConnection(
                Integer.parseInt(properties.getProperty(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)));
    }

    if (properties.containsKey(KEEPALIVE_INTERVAL_MS)) {
        clientBuilder.keepAliveInterval(Integer.parseInt(properties.getProperty(KEEPALIVE_INTERVAL_MS)),
                TimeUnit.MILLISECONDS);
    }

    return clientBuilder;
}
 
Example 19
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static ClientBuilder getClientBuilder(Properties properties) {
    ClientBuilder clientBuilder = PulsarClient.builder();
    if (properties == null) {
        return clientBuilder;
    }

    if (properties.containsKey(AUTHENTICATION_CLASS)) {
        String className = properties.getProperty(AUTHENTICATION_CLASS);
        try {
            if (properties.containsKey(AUTHENTICATION_PARAMS_STRING)) {
                String authParamsString = (String) properties.get(AUTHENTICATION_PARAMS_STRING);
                clientBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                clientBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                clientBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    clientBuilder.enableTls(Boolean.parseBoolean(properties.getProperty(USE_TLS, "false")));
    clientBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    clientBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

    if (properties.containsKey(TLS_TRUST_CERTS_FILE_PATH)) {
        clientBuilder.tlsTrustCertsFilePath(properties.getProperty(TLS_TRUST_CERTS_FILE_PATH));
    }

    if (properties.containsKey(OPERATION_TIMEOUT_MS)) {
        clientBuilder.operationTimeout(Integer.parseInt(properties.getProperty(OPERATION_TIMEOUT_MS)),
                TimeUnit.MILLISECONDS);
    }

    if (properties.containsKey(STATS_INTERVAL_SECONDS)) {
        clientBuilder.statsInterval(Integer.parseInt(properties.getProperty(STATS_INTERVAL_SECONDS)),
                TimeUnit.SECONDS);
    }

    if (properties.containsKey(NUM_IO_THREADS)) {
        clientBuilder.ioThreads(Integer.parseInt(properties.getProperty(NUM_IO_THREADS)));
    }

    if (properties.containsKey(CONNECTIONS_PER_BROKER)) {
        clientBuilder.connectionsPerBroker(Integer.parseInt(properties.getProperty(CONNECTIONS_PER_BROKER)));
    }

    if (properties.containsKey(USE_TCP_NODELAY)) {
        clientBuilder.enableTcpNoDelay(Boolean.parseBoolean(properties.getProperty(USE_TCP_NODELAY)));
    }

    if (properties.containsKey(CONCURRENT_LOOKUP_REQUESTS)) {
        clientBuilder
                .maxConcurrentLookupRequests(Integer.parseInt(properties.getProperty(CONCURRENT_LOOKUP_REQUESTS)));
    }

    if (properties.containsKey(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)) {
        clientBuilder.maxNumberOfRejectedRequestPerConnection(
                Integer.parseInt(properties.getProperty(MAX_NUMBER_OF_REJECTED_REQUESTS_PER_CONNECTION)));
    }

    return clientBuilder;
}