Java Code Examples for org.apache.pulsar.client.api.ClientBuilder#enableTls()
The following examples show how to use
org.apache.pulsar.client.api.ClientBuilder#enableTls() .
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: ThreadRuntimeFactory.java From pulsar with Apache License 2.0 | 6 votes |
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 2
Source File: PulsarSecurityConfig.java From datacollector with Apache License 2.0 | 6 votes |
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 3
Source File: WorkerUtils.java From pulsar with Apache License 2.0 | 5 votes |
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 4
Source File: PulsarClientKafkaConfig.java From pulsar with Apache License 2.0 | 4 votes |
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 5
Source File: PulsarClientKafkaConfig.java From pulsar with Apache License 2.0 | 4 votes |
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 6
Source File: PulsarClientKafkaConfig.java From pulsar with Apache License 2.0 | 4 votes |
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 7
Source File: PulsarFunctionPublishTest.java From pulsar with Apache License 2.0 | 4 votes |
@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 8
Source File: PulsarFunctionAdminTest.java From pulsar with Apache License 2.0 | 4 votes |
@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); }