Skip to content

Commit 950d043

Browse files
nodecesrinath-ctds
authored andcommitted
[fix][broker] Fix tenant creation and update with null value (apache#24209)
Signed-off-by: Zixuan Liu <nodeces@gmail.com> (cherry picked from commit c59412e) (cherry picked from commit b4ba347)
1 parent 1a6b37c commit 950d043

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/TenantsBase.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,25 @@ protected CompletableFuture<Void> internalDeleteTenantAsyncForcefully(String ten
273273
}
274274

275275
private CompletableFuture<Void> validateClustersAsync(TenantInfo info) {
276-
// empty cluster shouldn't be allowed
277-
if (info == null || info.getAllowedClusters().stream().filter(c -> !StringUtils.isBlank(c))
278-
.collect(Collectors.toSet()).isEmpty()
279-
|| info.getAllowedClusters().stream().anyMatch(ac -> StringUtils.isBlank(ac))) {
280-
log.warn("[{}] Failed to validate due to clusters are empty", clientAppId());
281-
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED, "Clusters can not be empty"));
276+
if (info == null) {
277+
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED, "TenantInfo is null"));
282278
}
279+
280+
Set<String> allowedClusters = info.getAllowedClusters();
281+
if (allowedClusters == null) {
282+
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED, "Clusters cannot be null"));
283+
}
284+
285+
Set<String> cleanedClusters = allowedClusters.stream()
286+
.filter(c -> !StringUtils.isBlank(c))
287+
.collect(Collectors.toSet());
288+
if (cleanedClusters.isEmpty() || allowedClusters.stream().anyMatch(StringUtils::isBlank)) {
289+
log.warn("[{}] Validation failed: allowed clusters are empty or contain blanks", clientAppId());
290+
return FutureUtil.failedFuture(
291+
new RestException(Status.PRECONDITION_FAILED, "Clusters cannot be empty or blank"));
292+
}
293+
283294
return clusterResources().listAsync().thenAccept(availableClusters -> {
284-
Set<String> allowedClusters = info.getAllowedClusters();
285295
List<String> nonexistentClusters = allowedClusters.stream()
286296
.filter(cluster -> !(availableClusters.contains(cluster) || GLOBAL_CLUSTER.equals(cluster)))
287297
.collect(Collectors.toList());

pulsar-broker/src/test/java/org/apache/pulsar/broker/admin/AdminApiTenantTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.pulsar.client.admin.PulsarAdminException;
3030
import org.apache.pulsar.common.policies.data.ClusterData;
3131
import org.apache.pulsar.common.policies.data.TenantInfo;
32+
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
3233
import org.testng.annotations.AfterClass;
3334
import org.testng.annotations.BeforeClass;
3435
import org.testng.annotations.Test;
@@ -73,4 +74,49 @@ public void testDeleteNonExistTenant() {
7374
String tenant = "test-non-exist-tenant-" + UUID.randomUUID();
7475
assertThrows(PulsarAdminException.NotFoundException.class, () -> admin.tenants().deleteTenant(tenant));
7576
}
77+
78+
@Test
79+
public void testCreateTenantWithNull() {
80+
String tenant = "test-create-tenant-with-null-value-" + UUID.randomUUID();
81+
// Put doesn't allow null value
82+
assertThrows(PulsarAdminException.class,
83+
() -> admin.tenants().createTenant(tenant, null));
84+
}
85+
86+
@Test
87+
public void testCreateTenantWithInvalidCluster() {
88+
String tenant = "test-create-tenant-with-invalid-cluster-" + UUID.randomUUID();
89+
// clusters is empty
90+
assertThrows(PulsarAdminException.PreconditionFailedException.class,
91+
() -> admin.tenants().createTenant(tenant, TenantInfo.builder().build()));
92+
93+
// clusters is null
94+
assertThrows(PulsarAdminException.PreconditionFailedException.class,
95+
() -> {
96+
TenantInfoImpl tenantInfo = new TenantInfoImpl();
97+
tenantInfo.setAdminRoles(null);
98+
tenantInfo.setAllowedClusters(null);
99+
admin.tenants().createTenant(tenant, tenantInfo);
100+
});
101+
}
102+
103+
@Test
104+
public void testUpdateTenantWithInvalidCluster() throws PulsarAdminException {
105+
String tenant = "test-update-tenant-with-invalid-cluster-" + UUID.randomUUID();
106+
admin.tenants().createTenant(tenant,
107+
TenantInfo.builder().allowedClusters(Collections.singleton(CLUSTER)).build());
108+
109+
// clusters is empty
110+
assertThrows(PulsarAdminException.PreconditionFailedException.class,
111+
() -> admin.tenants().updateTenant(tenant, TenantInfo.builder().build()));
112+
113+
// clusters is null
114+
assertThrows(PulsarAdminException.PreconditionFailedException.class,
115+
() -> {
116+
TenantInfoImpl tenantInfo = new TenantInfoImpl();
117+
tenantInfo.setAdminRoles(null);
118+
tenantInfo.setAllowedClusters(null);
119+
admin.tenants().updateTenant(tenant, tenantInfo);
120+
});
121+
}
76122
}

0 commit comments

Comments
 (0)