Java Code Examples for org.apache.pulsar.client.admin.PulsarAdmin#builder()

The following examples show how to use org.apache.pulsar.client.admin.PulsarAdmin#builder() . 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: PulsarAdminServiceImpl.java    From pulsar-manager with Apache License 2.0 6 votes vote down vote up
private PulsarAdmin createPulsarAdmin(String url) {
    try {
        log.info("Create Pulsar Admin instance. url={}, authPlugin={}, authParams={}, tlsAllowInsecureConnection={}, tlsTrustCertsFilePath={}, tlsEnableHostnameVerification={}",
                url, authPlugin, authParams, tlsAllowInsecureConnection, tlsTrustCertsFilePath, tlsEnableHostnameVerification);
        PulsarAdminBuilder pulsarAdminBuilder = PulsarAdmin.builder();
        pulsarAdminBuilder.serviceHttpUrl(url);
        if (StringUtils.isNotBlank(pulsarJwtToken)) {
            pulsarAdminBuilder.authentication(AuthenticationFactory.token(pulsarJwtToken));
        } else {
            pulsarAdminBuilder.authentication(authPlugin, authParams);
        }
        pulsarAdminBuilder.allowTlsInsecureConnection(tlsAllowInsecureConnection);
        pulsarAdminBuilder.tlsTrustCertsFilePath(tlsTrustCertsFilePath);
        pulsarAdminBuilder.enableTlsHostnameVerification(tlsEnableHostnameVerification);
        return pulsarAdminBuilder.build();
    } catch (PulsarClientException e) {
        PulsarAdminOperationException pulsarAdminOperationException = new PulsarAdminOperationException("Failed to create Pulsar Admin instance.");
        log.error(pulsarAdminOperationException.getMessage(), e);
        throw pulsarAdminOperationException;
    }
}
 
Example 2
Source File: PulsarConnectorConfig.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@NotNull
public PulsarAdmin getPulsarAdmin() throws PulsarClientException {
    if (this.pulsarAdmin == null) {
        PulsarAdminBuilder builder = PulsarAdmin.builder();

        if (getAuthPlugin() != null) {
            builder.authentication(getAuthPlugin(), getAuthParams());
        }

        if (isTlsAllowInsecureConnection() != null) {
            builder.allowTlsInsecureConnection(isTlsAllowInsecureConnection());
        }

        if (isTlsHostnameVerificationEnable() != null) {
            builder.enableTlsHostnameVerification(isTlsHostnameVerificationEnable());
        }

        if (getTlsTrustCertsFilePath() != null) {
            builder.tlsTrustCertsFilePath(getTlsTrustCertsFilePath());
        }

        this.pulsarAdmin = builder.serviceHttpUrl(getBrokerServiceUrl()).build();
    }
    return this.pulsarAdmin;
}
 
Example 3
Source File: PulsarClientKafkaConfig.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static PulsarAdminBuilder getAdminBuilder(String serviceUrl, Properties properties) {
    PulsarAdminBuilder adminBuilder = PulsarAdmin.builder();
    if (properties == null) {
        return adminBuilder;
    }

    adminBuilder.serviceHttpUrl(properties.getProperty(ADMIN_SERVICE_URL, serviceUrl));

    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);
                adminBuilder.authentication(className, authParamsString);
            } else if (properties.containsKey(AUTHENTICATION_PARAMS_MAP)) {
                Map<String, String> authParams = (Map<String, String>) properties.get(AUTHENTICATION_PARAMS_MAP);
                adminBuilder.authentication(className, authParams);
            } else {
                @SuppressWarnings("unchecked")
                Class<Authentication> clazz = (Class<Authentication>) Class.forName(className);
                Authentication auth = clazz.newInstance();
                adminBuilder.authentication(auth);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    adminBuilder.allowTlsInsecureConnection(
            Boolean.parseBoolean(properties.getProperty(TLS_ALLOW_INSECURE_CONNECTION, "false")));
    adminBuilder.enableTlsHostnameVerification(
            Boolean.parseBoolean(properties.getProperty(TLS_HOSTNAME_VERIFICATION, "false")));

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

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

    return adminBuilder;
}
 
Example 4
Source File: WebServiceTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowUnversionedClients,
        boolean enableTls, boolean enableAuth, boolean allowInsecure) throws Exception {
    Set<String> providers = new HashSet<>();
    providers.add("org.apache.pulsar.broker.authentication.AuthenticationProviderTls");

    Set<String> roles = new HashSet<>();
    roles.add("client");

    ServiceConfiguration config = new ServiceConfiguration();
    config.setAdvertisedAddress("localhost");
    config.setBrokerServicePort(Optional.of(0));
    config.setWebServicePort(Optional.of(0));
    if (enableTls) {
        config.setWebServicePortTls(Optional.of(0));
    }
    config.setClientLibraryVersionCheckEnabled(enableFilter);
    config.setAuthenticationEnabled(enableAuth);
    config.setAuthenticationProviders(providers);
    config.setAuthorizationEnabled(false);
    config.setSuperUserRoles(roles);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    config.setTlsAllowInsecureConnection(allowInsecure);
    config.setTlsTrustCertsFilePath(allowInsecure ? "" : TLS_CLIENT_CERT_FILE_PATH);
    config.setClusterName("local");
    config.setAdvertisedAddress("localhost"); // TLS certificate expects localhost
    config.setZookeeperServers("localhost:2181");
    config.setHttpMaxRequestSize(10 * 1024);
    pulsar = spy(new PulsarService(config));
    doReturn(zkFactory).when(pulsar).getZooKeeperClientFactory();
    doReturn(new MockedBookKeeperClientFactory()).when(pulsar).newBookKeeperClientFactory();
    pulsar.start();

    try {
        pulsar.getZkClient().delete("/minApiVersion", -1);
    } catch (Exception ex) {
    }
    pulsar.getZkClient().create("/minApiVersion", minApiVersion.getBytes(), null, CreateMode.PERSISTENT);

    String BROKER_URL_BASE = "http://localhost:" + pulsar.getListenPortHTTP().get();
    String BROKER_URL_BASE_TLS = "https://localhost:" + pulsar.getListenPortHTTPS().orElse(-1);
    String serviceUrl = BROKER_URL_BASE;

    PulsarAdminBuilder adminBuilder = PulsarAdmin.builder();
    if (enableTls && enableAuth) {
        serviceUrl = BROKER_URL_BASE_TLS;

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

        adminBuilder.authentication(AuthenticationTls.class.getName(), authParams).allowTlsInsecureConnection(true);
    }

    BROKER_LOOKUP_URL = BROKER_URL_BASE
            + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";
    BROKER_LOOKUP_URL_TLS = BROKER_URL_BASE_TLS
            + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";

    PulsarAdmin pulsarAdmin = adminBuilder.serviceHttpUrl(serviceUrl).build();

    try {
        pulsarAdmin.clusters().createCluster(config.getClusterName(),
                new ClusterData(pulsar.getSafeWebServiceAddress()));
    } catch (ConflictException ce) {
        // This is OK.
    } finally {
        pulsarAdmin.close();
    }
}