Skip to content

Commit

Permalink
[fix][admin] Fix namespace admin api exception response (apache#22587)
Browse files Browse the repository at this point in the history
(cherry picked from commit f25776d)
(cherry picked from commit 5ffec8a)
  • Loading branch information
coderzc authored and srinath-ctds committed May 16, 2024
1 parent 09679d3 commit 484f611
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,7 @@ protected void internalSetMaxUnackedMessagesPerConsumer(Integer maxUnackedMessag
}

protected void internalSetMaxSubscriptionsPerTopic(Integer maxSubscriptionsPerTopic){
validateNamespacePolicyOperationAsync(namespaceName, PolicyName.MAX_SUBSCRIPTIONS, PolicyOperation.WRITE);
validateNamespacePolicyOperation(namespaceName, PolicyName.MAX_SUBSCRIPTIONS, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();
if (maxSubscriptionsPerTopic != null && maxSubscriptionsPerTopic < 0) {
throw new RestException(Status.PRECONDITION_FAILED,
Expand Down Expand Up @@ -2136,9 +2136,10 @@ protected CompletableFuture<Void> internalSetOffloadThresholdInSecondsAsync(long
f.complete(null);
})
.exceptionally(t -> {
Throwable cause = FutureUtil.unwrapCompletionException(t);
log.error("[{}] Failed to update offloadThresholdInSeconds configuration for namespace {}",
clientAppId(), namespaceName, t);
f.completeExceptionally(new RestException(t));
f.completeExceptionally(new RestException(cause));
return null;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
package org.apache.pulsar.broker.admin;

import io.jsonwebtoken.Jwts;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import lombok.Cleanup;
import lombok.SneakyThrows;
import org.apache.pulsar.client.admin.PulsarAdmin;
Expand All @@ -32,10 +36,6 @@
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Test(groups = "broker-admin")
public class NamespaceAuthZTest extends MockedPulsarStandalone {
Expand Down Expand Up @@ -160,4 +160,43 @@ public void testProperties() {
}
superUserAdmin.topics().delete(topic, true);
}

@Test
@SneakyThrows
public void testOffloadThresholdInSeconds() {
final String namespace = "public/default";
final String subject = UUID.randomUUID().toString();
final String token = Jwts.builder()
.claim("sub", subject).signWith(SECRET_KEY).compact();
@Cleanup final PulsarAdmin subAdmin = PulsarAdmin.builder()
.serviceHttpUrl(getPulsarService().getWebServiceAddress())
.authentication(new AuthenticationToken(token))
.build();
Assert.assertThrows(PulsarAdminException.NotAuthorizedException.class,
() -> subAdmin.namespaces().getOffloadThresholdInSeconds(namespace));

Assert.assertThrows(PulsarAdminException.NotAuthorizedException.class,
() -> subAdmin.namespaces().setOffloadThresholdInSeconds(namespace, 10000));
}

@Test
@SneakyThrows
public void testMaxSubscriptionsPerTopic() {
final String namespace = "public/default";
final String subject = UUID.randomUUID().toString();
final String token = Jwts.builder()
.claim("sub", subject).signWith(SECRET_KEY).compact();
@Cleanup final PulsarAdmin subAdmin = PulsarAdmin.builder()
.serviceHttpUrl(getPulsarService().getWebServiceAddress())
.authentication(new AuthenticationToken(token))
.build();
Assert.assertThrows(PulsarAdminException.NotAuthorizedException.class,
() -> subAdmin.namespaces().getMaxSubscriptionsPerTopic(namespace));

Assert.assertThrows(PulsarAdminException.NotAuthorizedException.class,
() -> subAdmin.namespaces().setMaxSubscriptionsPerTopic(namespace, 100));

Assert.assertThrows(PulsarAdminException.NotAuthorizedException.class,
() -> subAdmin.namespaces().removeMaxSubscriptionsPerTopic(namespace));
}
}

0 comments on commit 484f611

Please sign in to comment.