lhotari commented on code in PR #23372:
URL: https://github.com/apache/pulsar/pull/23372#discussion_r1793336356


##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/PulsarAuthorizationProvider.java:
##########
@@ -251,6 +254,68 @@ public CompletableFuture<Void> 
grantPermissionAsync(TopicName topicName, Set<Aut
         });
     }
 
+    public CompletableFuture<Void> 
grantPermissionAsync(List<GrantTopicPermissionOptions> options) {
+        return getPoliciesReadOnlyAsync().thenCompose(readonly -> {
+            if (readonly) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Policies are read-only. Broker cannot do 
read-write operations");
+                }
+                throw new IllegalStateException("policies are in readonly 
mode");
+            }
+            TopicName topicName = TopicName.get(options.get(0).getTopic());
+            return pulsarResources.getNamespaceResources()
+                    .setPoliciesAsync(topicName.getNamespaceObject(), policies 
-> {

Review Comment:
   This code seems to assume that all topics are in the same namespace. That 
seems like a problematic assumption. Is there a goal to support 
`GrantTopicPermissionOptions` that apply to topics that could be in different 
namespaces?
   
   Although I can see that there's a check in the different layer. The 
validation should happen here again unless support for multiple namespaces is 
added. The reason for this is that the caller shouldn't need to know 
implementation details. If it needs to know the implementation details, it's a 
leaky or broken abstraction.



##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/authorization/PulsarAuthorizationProvider.java:
##########
@@ -251,6 +254,68 @@ public CompletableFuture<Void> 
grantPermissionAsync(TopicName topicName, Set<Aut
         });
     }
 
+    public CompletableFuture<Void> 
grantPermissionAsync(List<GrantTopicPermissionOptions> options) {
+        return getPoliciesReadOnlyAsync().thenCompose(readonly -> {
+            if (readonly) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Policies are read-only. Broker cannot do 
read-write operations");
+                }
+                throw new IllegalStateException("policies are in readonly 
mode");
+            }
+            TopicName topicName = TopicName.get(options.get(0).getTopic());
+            return pulsarResources.getNamespaceResources()
+                    .setPoliciesAsync(topicName.getNamespaceObject(), policies 
-> {
+                        options.stream().forEach(o -> {
+                            final String topicUri = 
TopicName.get(o.getTopic()).toString();
+                            policies.auth_policies.getTopicAuthentication()
+                                    .computeIfAbsent(topicUri, __ -> new 
HashMap<>())
+                                    .put(o.getRole(), o.getActions());
+                        });
+                        return policies;
+                    }).whenComplete((__, ex) -> {
+                        if (ex != null) {
+                            log.error("Failed to grant permissions for {}", 
options);
+                        } else {
+                            log.info("Successfully granted access for {}", 
options);
+                        }
+                    });
+        });
+    }
+
+    @Override
+    public CompletableFuture<Void> 
revokePermissionAsync(List<RevokeTopicPermissionOptions> options) {
+        return getPoliciesReadOnlyAsync().thenCompose(readonly -> {
+            if (readonly) {
+                if (log.isDebugEnabled()) {
+                    log.debug("Policies are read-only. Broker cannot do 
read-write operations");
+                }
+                throw new IllegalStateException("policies are in readonly 
mode");
+            }
+            TopicName topicName = TopicName.get(options.get(0).getTopic());
+            return pulsarResources.getNamespaceResources()
+                    .setPoliciesAsync(topicName.getNamespaceObject(), policies 
-> {

Review Comment:
   This code seems to assume that all topics are in the same namespace. Similar 
comment as in the previous case.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to