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

The following examples show how to use org.apache.pulsar.client.api.ClientBuilder#tlsTrustCertsFilePath() . 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: 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 2
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 3
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 4
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 5
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 6
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;
}
 
Example 7
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 8
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;
}