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);
}
}
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();
}
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);
}
}
}
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;
}
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);
}
}
Aggregations