Search in sources :

Example 16 with PulsarAdmin

use of org.apache.pulsar.client.admin.PulsarAdmin in project incubator-pulsar by apache.

the class AuthorizationProducerConsumerTest method testProducerAndConsumerAuthorization.

/**
 * It verifies plugable authorization service
 *
 * <pre>
 * 1. Client passes correct authorization plugin-name + correct auth role: SUCCESS
 * 2. Client passes correct authorization plugin-name + incorrect auth-role: FAIL
 * 3. Client passes incorrect authorization plugin-name + correct auth-role: FAIL
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testProducerAndConsumerAuthorization() throws Exception {
    log.info("-- Starting {} test --", methodName);
    conf.setAuthorizationProvider(TestAuthorizationProvider.class.getName());
    setup();
    ClientConfiguration adminConf = new ClientConfiguration();
    Authentication adminAuthentication = new ClientAuthentication("superUser");
    adminConf.setAuthentication(adminAuthentication);
    admin = spy(new PulsarAdmin(brokerUrl, adminConf));
    String lookupUrl;
    lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
    Authentication authentication = new ClientAuthentication(clientRole);
    Authentication authenticationInvalidRole = new ClientAuthentication("test-role");
    pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl).authentication(authentication).build();
    PulsarClient pulsarClientInvalidRole = PulsarClient.builder().serviceUrl(lookupUrl).authentication(authenticationInvalidRole).build();
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
    admin.namespaces().createNamespace("my-property/use/my-ns");
    // (1) Valid Producer and consumer creation
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use/my-ns/my-topic").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use/my-ns/my-topic").create();
    consumer.close();
    producer.close();
    // (2) InValid user auth-role will be rejected by authorization service
    try {
        consumer = pulsarClientInvalidRole.newConsumer().topic("persistent://my-property/use/my-ns/my-topic").subscriptionName("my-subscriber-name").subscribe();
        Assert.fail("should have failed with authorization error");
    } catch (PulsarClientException.AuthorizationException pa) {
    // Ok
    }
    try {
        producer = pulsarClientInvalidRole.newProducer().topic("persistent://my-property/use/my-ns/my-topic").create();
        Assert.fail("should have failed with authorization error");
    } catch (PulsarClientException.AuthorizationException pa) {
    // Ok
    }
    log.info("-- Exiting {} test --", methodName);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) URI(java.net.URI) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Test(org.testng.annotations.Test)

Example 17 with PulsarAdmin

use of org.apache.pulsar.client.admin.PulsarAdmin in project incubator-pulsar by apache.

the class BrokerBkEnsemblesTests method setup.

@BeforeMethod
void setup() throws Exception {
    try {
        // start local bookie and zookeeper
        bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, 5001);
        bkEnsemble.start();
        // start pulsar service
        config = new ServiceConfiguration();
        config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
        config.setAdvertisedAddress("localhost");
        config.setWebServicePort(BROKER_WEBSERVICE_PORT);
        config.setClusterName("usc");
        config.setBrokerServicePort(BROKER_SERVICE_PORT);
        config.setAuthorizationEnabled(false);
        config.setAuthenticationEnabled(false);
        config.setManagedLedgerMaxEntriesPerLedger(5);
        config.setManagedLedgerMinLedgerRolloverTimeMinutes(0);
        config.setAdvertisedAddress("127.0.0.1");
        pulsar = new PulsarService(config);
        pulsar.start();
        adminUrl = new URL("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT);
        admin = new PulsarAdmin(adminUrl, (Authentication) null);
        admin.clusters().createCluster("usc", new ClusterData(adminUrl.toString()));
        admin.properties().createProperty("prop", new PropertyAdmin(Lists.newArrayList("appid1"), Sets.newHashSet("usc")));
    } catch (Throwable t) {
        LOG.error("Error setting up broker test", t);
        Assert.fail("Broker test setup failed");
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Authentication(org.apache.pulsar.client.api.Authentication) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 18 with PulsarAdmin

use of org.apache.pulsar.client.admin.PulsarAdmin in project incubator-pulsar by apache.

the class WebServiceTest method setupEnv.

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.setWebServicePort(BROKER_WEBSERVICE_PORT);
    config.setWebServicePortTls(BROKER_WEBSERVICE_PORT_TLS);
    config.setClientLibraryVersionCheckEnabled(enableFilter);
    config.setAuthenticationEnabled(enableAuth);
    config.setAuthenticationProviders(providers);
    config.setAuthorizationEnabled(false);
    config.setSuperUserRoles(roles);
    config.setTlsEnabled(enableTls);
    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");
    // TLS certificate expects localhost
    config.setAdvertisedAddress("localhost");
    config.setZookeeperServers("localhost:2181");
    pulsar = spy(new PulsarService(config));
    doReturn(new MockedZooKeeperClientFactoryImpl()).when(pulsar).getZooKeeperClientFactory();
    doReturn(new MockedBookKeeperClientFactory()).when(pulsar).getBookKeeperClientFactory();
    pulsar.start();
    try {
        pulsar.getZkClient().delete("/minApiVersion", -1);
    } catch (Exception ex) {
    }
    pulsar.getZkClient().create("/minApiVersion", minApiVersion.getBytes(), null, CreateMode.PERSISTENT);
    String serviceUrl = BROKER_URL_BASE;
    ClientConfiguration clientConfig = new ClientConfiguration();
    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);
        Authentication auth = new AuthenticationTls();
        auth.configure(authParams);
        clientConfig.setAuthentication(auth);
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(true);
    }
    PulsarAdmin pulsarAdmin = new PulsarAdmin(new URL(serviceUrl), clientConfig);
    try {
        pulsarAdmin.clusters().createCluster(config.getClusterName(), new ClusterData(pulsar.getWebServiceAddress()));
    } catch (ConflictException ce) {
    // This is OK.
    } finally {
        pulsarAdmin.close();
    }
}
Also used : MockedBookKeeperClientFactory(org.apache.pulsar.broker.MockedBookKeeperClientFactory) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) HashMap(java.util.HashMap) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) URL(java.net.URL) MockedZooKeeperClientFactoryImpl(org.apache.pulsar.zookeeper.MockedZooKeeperClientFactoryImpl) AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) Authentication(org.apache.pulsar.client.api.Authentication) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) HashSet(java.util.HashSet)

Example 19 with PulsarAdmin

use of org.apache.pulsar.client.admin.PulsarAdmin in project incubator-pulsar by apache.

the class SLAMonitoringTest method setup.

@BeforeClass
void setup() throws Exception {
    log.info("---- Initializing SLAMonitoringTest -----");
    // Start local bookkeeper ensemble
    bkEnsemble = new LocalBookkeeperEnsemble(3, ZOOKEEPER_PORT, PortManager.nextFreePort());
    bkEnsemble.start();
    // start brokers
    for (int i = 0; i < BROKER_COUNT; i++) {
        brokerWebServicePorts[i] = PortManager.nextFreePort();
        brokerNativeBrokerPorts[i] = PortManager.nextFreePort();
        ServiceConfiguration config = new ServiceConfiguration();
        config.setBrokerServicePort(brokerNativeBrokerPorts[i]);
        config.setClusterName("my-cluster");
        config.setAdvertisedAddress("localhost");
        config.setWebServicePort(brokerWebServicePorts[i]);
        config.setZookeeperServers("127.0.0.1" + ":" + ZOOKEEPER_PORT);
        config.setBrokerServicePort(brokerNativeBrokerPorts[i]);
        config.setDefaultNumberOfNamespaceBundles(1);
        config.setLoadBalancerEnabled(false);
        configurations[i] = config;
        pulsarServices[i] = new PulsarService(config);
        pulsarServices[i].start();
        brokerUrls[i] = new URL("http://127.0.0.1" + ":" + brokerWebServicePorts[i]);
        pulsarAdmins[i] = new PulsarAdmin(brokerUrls[i], (Authentication) null);
    }
    Thread.sleep(100);
    createProperty(pulsarAdmins[BROKER_COUNT - 1]);
    for (int i = 0; i < BROKER_COUNT; i++) {
        String topic = String.format("%s/%s/%s:%s", NamespaceService.SLA_NAMESPACE_PROPERTY, "my-cluster", pulsarServices[i].getAdvertisedAddress(), brokerWebServicePorts[i]);
        pulsarAdmins[0].namespaces().createNamespace(topic);
    }
}
Also used : ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) PulsarService(org.apache.pulsar.broker.PulsarService) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) Authentication(org.apache.pulsar.client.api.Authentication) LocalBookkeeperEnsemble(org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble) URL(java.net.URL) BeforeClass(org.testng.annotations.BeforeClass)

Example 20 with PulsarAdmin

use of org.apache.pulsar.client.admin.PulsarAdmin in project incubator-pulsar by apache.

the class AdminApiTest method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    conf.setLoadBalancerEnabled(true);
    conf.setTlsEnabled(true);
    conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    super.internalSetup();
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setUseTls(true);
    clientConf.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
    adminTls = spy(new PulsarAdmin(brokerUrlTls, clientConf));
    // create otherbroker to test redirect on calls that need
    // namespace ownership
    mockPulsarSetup = new MockedPulsarService(this.conf);
    mockPulsarSetup.setup();
    otherPulsar = mockPulsarSetup.getPulsar();
    otheradmin = mockPulsarSetup.getAdmin();
    // Setup namespaces
    admin.clusters().createCluster("use", new ClusterData("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT));
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use"));
    admin.properties().createProperty("prop-xyz", propertyAdmin);
    admin.namespaces().createNamespace("prop-xyz/use/ns1");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) NamespaceBundleFactory(org.apache.pulsar.common.naming.NamespaceBundleFactory) ClientConfiguration(org.apache.pulsar.client.api.ClientConfiguration) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)39 URL (java.net.URL)15 Test (org.testng.annotations.Test)14 PulsarService (org.apache.pulsar.broker.PulsarService)12 Authentication (org.apache.pulsar.client.api.Authentication)11 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)11 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)10 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)9 LocalBookkeeperEnsemble (org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble)9 BeforeMethod (org.testng.annotations.BeforeMethod)7 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)6 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 URI (java.net.URI)3 ModularLoadManagerImpl (org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl)3 SimpleLoadManagerImpl (org.apache.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl)3 MalformedURLException (java.net.MalformedURLException)2 Brokers (org.apache.pulsar.client.admin.Brokers)2 ClientConfiguration (org.apache.pulsar.client.api.ClientConfiguration)2