Search in sources :

Example 1 with TenantInfo

use of org.apache.pulsar.common.policies.data.TenantInfo in project SBK by kmgowda.

the class PulsarTopicHandler method createTopic.

public void createTopic(boolean recreate) throws IOException {
    if (config.tenant != null && config.nameSpace != null) {
        final String fullNameSpace = config.tenant + "/" + config.nameSpace;
        if (config.cluster != null) {
            try {
                ClusterData clusterData = new ClusterData(config.adminUri, null, config.brokerUri, null);
                adminClient.clusters().createCluster(config.cluster, clusterData);
                if (!adminClient.tenants().getTenants().contains(config.tenant)) {
                    adminClient.tenants().createTenant(config.tenant, new TenantInfo(Collections.emptySet(), Sets.newHashSet(config.cluster)));
                }
            } catch (ConflictException ex) {
                ex.printStackTrace();
            } catch (PulsarAdminException ex) {
                throw new IOException(ex);
            }
        }
        try {
            adminClient.namespaces().createNamespace(fullNameSpace);
        } catch (ConflictException ex) {
        /* ex.printStackTrace(); */
        } catch (PulsarAdminException ex) {
            throw new IOException(ex);
        }
        try {
            adminClient.namespaces().setPersistence(fullNameSpace, new PersistencePolicies(config.ensembleSize, config.writeQuorum, config.ackQuorum, 1.0));
            adminClient.namespaces().setBacklogQuota(fullNameSpace, new BacklogQuota(Long.MAX_VALUE, RetentionPolicy.producer_exception));
            adminClient.namespaces().setDeduplicationStatus(fullNameSpace, config.deduplicationEnabled);
        } catch (PulsarAdminException ex) {
            throw new IOException(ex);
        }
    }
    if (recreate) {
        try {
            adminClient.topics().deletePartitionedTopic(config.topicName);
        } catch (NotFoundException ex) {
        /* already deleted or not existing */
        } catch (PulsarAdminException ex) {
            throw new IOException(ex);
        }
    }
    try {
        adminClient.topics().createPartitionedTopic(config.topicName, config.partitions);
    } catch (ConflictException ex) {
    /* ex.printStackTrace(); */
    } catch (PulsarAdminException ex) {
        throw new IOException(ex);
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) ConflictException(org.apache.pulsar.client.admin.PulsarAdminException.ConflictException) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota)

Example 2 with TenantInfo

use of org.apache.pulsar.common.policies.data.TenantInfo in project starlight-for-kafka by datastax.

the class SaslPlainEndToEndPulsarProxyTest method setup.

@BeforeClass
@Override
protected void setup() throws Exception {
    super.setup();
    // when using PulsarAdmin you need to be Tenant Admin in order to
    // list topic partitions
    TenantInfo tenantInfo = admin.tenants().getTenantInfo(TENANT);
    tenantInfo.getAdminRoles().add(SIMPLE_USER);
    admin.tenants().updateTenant(TENANT, tenantInfo);
    startProxy();
}
Also used : TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) BeforeClass(org.testng.annotations.BeforeClass)

Example 3 with TenantInfo

use of org.apache.pulsar.common.policies.data.TenantInfo in project starlight-for-kafka by datastax.

the class MetadataUtils method createTenantIfMissing.

private static void createTenantIfMissing(String tenant, KafkaServiceConfiguration conf, String cluster, Tenants tenants) throws PulsarAdminException {
    if (!tenants.getTenants().contains(tenant)) {
        log.info("Tenant: {} does not exist, creating it ...", tenant);
        tenants.createTenant(tenant, TenantInfo.builder().adminRoles(conf.getSuperUserRoles()).allowedClusters(Collections.singleton(cluster)).build());
    } else {
        TenantInfo kafkaMetadataTenantInfo = tenants.getTenantInfo(tenant);
        Set<String> allowedClusters = kafkaMetadataTenantInfo.getAllowedClusters();
        if (!allowedClusters.contains(cluster)) {
            log.info("Tenant: {} exists but cluster: {} is not in the allowedClusters list, updating it ...", tenant, cluster);
            allowedClusters.add(cluster);
            tenants.updateTenant(tenant, kafkaMetadataTenantInfo);
        }
    }
}
Also used : TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo)

Example 4 with TenantInfo

use of org.apache.pulsar.common.policies.data.TenantInfo in project starlight-for-kafka by datastax.

the class SimpleAclAuthorizer method isSuperUserOrTenantAdmin.

/**
 * Check if specified role is an admin of the tenant or superuser.
 *
 * @param tenant the tenant to check
 * @param role the role to check
 * @return a CompletableFuture containing a boolean in which true means the role is an admin user
 * and false if it is not
 */
private CompletableFuture<Boolean> isSuperUserOrTenantAdmin(String tenant, String role, KafkaPrincipal currentUser) {
    CompletableFuture<Boolean> future = new CompletableFuture<>();
    isSuperUser(role).whenComplete((isSuperUser, ex) -> {
        if (ex != null || !isSuperUser) {
            pulsarService.getTenantInfoAsync(tenant).thenAccept(tenantInfo -> {
                if (!tenantInfo.isPresent()) {
                    future.complete(false);
                    return;
                }
                TenantInfo info = tenantInfo.get();
                future.complete(role != null && info.getAdminRoles() != null && info.getAdminRoles().contains(role));
            });
            return;
        }
        future.complete(true);
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo)

Example 5 with TenantInfo

use of org.apache.pulsar.common.policies.data.TenantInfo in project starlight-for-kafka by datastax.

the class KafkaAuthorizationTestBase method testAuthorizationFailed.

@Test(timeOut = 20000)
public void testAuthorizationFailed() throws PulsarAdminException {
    String newTenant = "newTenantAuthorizationFailed";
    String testTopic = "persistent://" + newTenant + "/" + NAMESPACE + "/topic1";
    try {
        admin.tenants().createTenant(newTenant, TenantInfo.builder().adminRoles(Collections.singleton(ADMIN_USER)).allowedClusters(Collections.singleton(configClusterName)).build());
        TenantInfo tenantInfo = admin.tenants().getTenantInfo(TENANT);
        log.info("tenantInfo for {} {} in test", TENANT, tenantInfo);
        assertNotNull(tenantInfo);
        admin.namespaces().createNamespace(newTenant + "/" + NAMESPACE);
        admin.topics().createPartitionedTopic(testTopic, 1);
        @Cleanup KProducer kProducer = new KProducer(testTopic, false, "localhost", getClientPort(), newTenant + "/" + NAMESPACE, "token:" + userToken);
        kProducer.getProducer().send(new ProducerRecord<>(testTopic, 0, "")).get();
        fail("should have failed");
    } catch (Exception e) {
        log.info("the error", e);
        assertTrue(e.getMessage().contains("TopicAuthorizationException"));
    } finally {
        // Cleanup
        admin.topics().deletePartitionedTopic(testTopic);
    }
}
Also used : ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) TenantInfo(org.apache.pulsar.common.policies.data.TenantInfo) Cleanup(lombok.Cleanup) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.testng.annotations.Test)

Aggregations

TenantInfo (org.apache.pulsar.common.policies.data.TenantInfo)127 Test (org.testng.annotations.Test)67 Set (java.util.Set)42 BeforeMethod (org.testng.annotations.BeforeMethod)42 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)34 HashSet (java.util.HashSet)33 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)32 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)32 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)30 Map (java.util.Map)28 PulsarService (org.apache.pulsar.broker.PulsarService)27 LocalBookkeeperEnsemble (org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble)27 Lists (com.google.common.collect.Lists)24 Sets (com.google.common.collect.Sets)24 Collections (java.util.Collections)24 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)24 AfterMethod (org.testng.annotations.AfterMethod)24 Optional (java.util.Optional)21 SimpleLoadManagerImpl (org.apache.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl)21 FutureUtil (org.apache.pulsar.common.util.FutureUtil)21