Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][broker] fix can not revoke permission after update topic partition #17393

Merged
merged 2 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ protected void internalGrantPermissionsOnTopic(final AsyncResponse asyncResponse
});
}

private CompletableFuture<Void> revokePermissionsAsync(String topicUri, String role) {
private CompletableFuture<Void> revokePermissionsAsync(String topicUri, String role, boolean force) {
return namespaceResources().getPoliciesAsync(namespaceName).thenCompose(
policiesOptional -> {
Policies policies = policiesOptional.orElseThrow(() ->
Expand All @@ -354,8 +354,12 @@ private CompletableFuture<Void> revokePermissionsAsync(String topicUri, String r
|| !policies.auth_policies.getTopicAuthentication().get(topicUri).containsKey(role)) {
log.warn("[{}] Failed to revoke permission from role {} on topic: Not set at topic level {}",
clientAppId(), role, topicUri);
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED,
"Permissions are not set at the topic level"));
if (force) {
nodece marked this conversation as resolved.
Show resolved Hide resolved
return CompletableFuture.completedFuture(null);
} else {
return FutureUtil.failedFuture(new RestException(Status.PRECONDITION_FAILED,
"Permissions are not set at the topic level"));
}
}
// Write the new policies to metadata store
return namespaceResources().setPoliciesAsync(namespaceName, p -> {
Expand All @@ -381,10 +385,10 @@ protected void internalRevokePermissionsOnTopic(AsyncResponse asyncResponse, Str
for (int i = 0; i < numPartitions; i++) {
TopicName topicNamePartition = topicName.getPartition(i);
future = future.thenComposeAsync(unused ->
revokePermissionsAsync(topicNamePartition.toString(), role));
revokePermissionsAsync(topicNamePartition.toString(), role, true));
}
}
return future.thenComposeAsync(unused -> revokePermissionsAsync(topicName.toString(), role))
return future.thenComposeAsync(unused -> revokePermissionsAsync(topicName.toString(), role, false))
.thenAccept(unused -> asyncResponse.resume(Response.noContent().build()));
}))
).exceptionally(ex -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public void testProxyAuthorization() throws Exception {
* 2. Update the topic partition number to 4.
* 3. Use new producer/consumer with client role to process the topic.
* 4. Broker should authorize producer/consumer normally.
* 5. revoke produce/consumer permission of topic
* 6. new producer/consumer should not be authorized
* </pre>
*/
@Test
Expand Down Expand Up @@ -297,6 +299,26 @@ public void testUpdatePartitionNumAndReconnect() throws Exception {
Assert.assertEquals(messageSet, receivedMessageSet);
consumer.close();
producer.close();

// revoke produce/consume permission
admin.topics().revokePermissions(topicName, CLIENT_ROLE);
nodece marked this conversation as resolved.
Show resolved Hide resolved

// produce/consume the topic should fail
try {
consumer = proxyClient.newConsumer()
.topic(topicName)
.subscriptionName(subscriptionName).subscribe();
Assert.fail("Should not pass");
} catch (PulsarClientException.AuthorizationException ex) {
// ok
}
try {
producer = proxyClient.newProducer(Schema.BYTES)
.topic(topicName).create();
Assert.fail("Should not pass");
} catch (PulsarClientException.AuthorizationException ex) {
// ok
}
log.info("-- Exiting {} test --", methodName);
}

Expand Down