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

The following examples show how to use org.apache.pulsar.client.api.ClientBuilder#build() . 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: 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 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
Source File: PulsarWorkerAssignmentTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@BeforeMethod(timeOut = 60000)
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");
    final Set<String> superUsers = Sets.newHashSet("superUser");
    config.setSuperUserRoles(superUsers);
    config.setWebServicePort(Optional.of(0));
    config.setZookeeperServers("127.0.0.1" + ":" + bkEnsemble.getZookeeperPort());
    config.setBrokerServicePort(Optional.of(0));
    config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
    config.setAdvertisedAddress("localhost");

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

    admin = spy(PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddress()).build());

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

    // update cluster metadata
    final ClusterData clusterData = new ClusterData(pulsar.getBrokerServiceUrl());
    admin.clusters().updateCluster(config.getClusterName(), clusterData);

    final ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(this.workerConfig.getPulsarServiceUrl());
    pulsarClient = clientBuilder.build();

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

    Thread.sleep(100);
}
 
Example 13
Source File: BasePulsarConfig.java    From datacollector with Apache License 2.0 4 votes vote down vote up
public List<ConfigIssue> init(Stage.Context context) {
  List<ConfigIssue> issues = new ArrayList<>();

  //Validate BasePulsarConfig configs (currently no validation needed as constraints defined in annotations).

  issues.addAll(extraInit(context));

  issues.addAll(securityConfig.init(context));

  //configure client builder if issues is empty
  if (issues.isEmpty()) {
    ClientBuilder clientBuilder;
    clientBuilder = PulsarClient.builder();
    clientBuilder.serviceUrl(serviceURL)
                 .keepAliveInterval(keepAliveInterval, TimeUnit.MILLISECONDS)
                 .operationTimeout(operationTimeout, TimeUnit.MILLISECONDS);

    // chance to subclass to further configure
    issues = extraBuilderConfiguration(clientBuilder);

    if (issues.isEmpty()) {
      try {
        securityConfig.configurePulsarBuilder(clientBuilder);
      } catch (StageException e) {
        LOG.error(e.toString());
        issues.add(context.createConfigIssue(PulsarGroups.PULSAR.name(), null, e.getErrorCode(), e.getParams()));
      }
      try {
        client = clientBuilder.build();
      } catch (Exception ex) {
        LOG.info(Utils.format(PulsarErrors.PULSAR_00.getMessage(), serviceURL), ex);
        issues.add(context.createConfigIssue(PulsarGroups.PULSAR.name(),
            "pulsarConfig.serviceURL",
            PulsarErrors.PULSAR_00,
            serviceURL,
            ex.toString()
        ));
      }
    }
  }

  return issues;
}
 
Example 14
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 15
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 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: PulsarFunctionsITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
static void start() throws Exception {
  // Start local bookkeeper ensemble
  final LocalBookkeeperEnsemble bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, TestUtil::nextFreePort);
  bkEnsemble.start();

  final String brokerServiceUrl = "http://127.0.0.1:" + brokerWebServicePort;

  final ServiceConfiguration config = new ServiceConfiguration();
  config.setClusterName(CLUSTER_NAME);
  final Set<String> superUsers = Sets.newHashSet("superUser");
  config.setSuperUserRoles(superUsers);

  config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
  config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
  config.setWebServicePort(brokerWebServicePort);
  config.setBrokerServicePort(brokerServicePort);

  config.setAuthenticationEnabled(false);
  config.setTlsEnabled(false);
  config.setTlsAllowInsecureConnection(true);
  config.setAdvertisedAddress("localhost");

  final WorkerService functionsWorkerService = createPulsarFunctionWorker(config);
  final URL urlTls = new URL(brokerServiceUrl);
  final Optional<WorkerService> functionWorkerService = Optional.of(functionsWorkerService);
  try (final PulsarService pulsar = new PulsarService(config, functionWorkerService)) {
    pulsar.start();
    try (final PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(brokerServiceUrl).allowTlsInsecureConnection(true).build()) {
      // update cluster metadata
      final ClusterData clusterData = new ClusterData(urlTls.toString());
      admin.clusters().updateCluster(config.getClusterName(), clusterData);

      final ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(workerConfig.getPulsarServiceUrl());
      try (final PulsarClient pulsarClient = clientBuilder.build()) {
        final TenantInfo propAdmin = new TenantInfo();
        propAdmin.getAdminRoles().add("superUser");
        propAdmin.setAllowedClusters(Sets.newHashSet(CLUSTER_NAME));
        admin.tenants().updateTenant(tenant, propAdmin);

        testPulsarFunction(admin, pulsarClient);
      }
    }
  }
}
 
Example 18
Source File: PulsarFunctionsITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
static void start() throws Exception {
  // Start local bookkeeper ensemble
  final LocalBookkeeperEnsemble bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT,TestUtil::nextFreePort);
  bkEnsemble.start();

  final String brokerServiceUrl = "http://127.0.0.1:" + brokerWebServicePort;

  final ServiceConfiguration config = spy(new ServiceConfiguration());
  config.setClusterName(CLUSTER_NAME);
  final Set<String> superUsers = Sets.newHashSet("superUser");
  config.setSuperUserRoles(superUsers);
  config.setWebServicePort(Optional.of(brokerWebServicePort));
  config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
  config.setBrokerServicePort(Optional.of(brokerServicePort));
  config.setLoadManagerClassName(SimpleLoadManagerImpl.class.getName());
  config.setTlsAllowInsecureConnection(true);
  config.setAdvertisedAddress("localhost");

  config.setAuthenticationEnabled(false);
  config.setAuthorizationEnabled(false);

  config.setBrokerClientTlsEnabled(false);
  config.setAllowAutoTopicCreationType("non-partitioned");

  final WorkerService functionsWorkerService = createPulsarFunctionWorker(config);
  final URL urlTls = new URL(brokerServiceUrl);
  final Optional<WorkerService> functionWorkerService = Optional.of(functionsWorkerService);
  try (final PulsarService pulsar = new PulsarService(config, functionWorkerService)) {
    pulsar.start();
    try (final PulsarAdmin admin = PulsarAdmin.builder().serviceHttpUrl(brokerServiceUrl).allowTlsInsecureConnection(true).build()) {
      // update cluster metadata
      final ClusterData clusterData = new ClusterData(urlTls.toString());
      admin.clusters().updateCluster(config.getClusterName(), clusterData);

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

      final String jarFilePathUrl = Utils.FILE + ":" + ExclamationFunction.class.getProtectionDomain().getCodeSource().getLocation().getPath();

      final ClientBuilder clientBuilder = PulsarClient.builder().serviceUrl(workerConfig.getPulsarServiceUrl());
      try (final PulsarClient pulsarClient = clientBuilder.build()) {
        testE2EPulsarFunction(jarFilePathUrl, admin, pulsarClient);
      }
    }
  }
}