Skip to content

Commit

Permalink
add encoding for WebTargets parts (#2359)
Browse files Browse the repository at this point in the history
### Motivation

Fixes #2304 

Issue #2304 reported that Pulsar admin commands will fail with some url style parts,
```
root@pulsar-admin:/pulsar# pulsar-admin namespaces grant-permission --actions produce,consume --role 'spiffe://developer/passport' core-platform/testing
HTTP 404 Not Found
Reason: HTTP 404 Not Found
```
This PR try to fix it.

### Modifications

- use URLEncoder to encode webTarget parts.
- add tests to verify

### Result

all UT passed
  • Loading branch information
jiazhai authored and sijie committed Aug 12, 2018
1 parent 20caf5f commit c0a5072
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Expand Up @@ -620,19 +620,25 @@ public void namespaces() throws PulsarAdminException, PulsarServerException, Exc
// Ok, got the non authorized exception since usc cluster is not in the allowed clusters list.
}

// test with url style role.
admin.namespaces().grantPermissionOnNamespace("prop-xyz/ns1",
"spiffe://developer/passport-role", EnumSet.allOf(AuthAction.class));
admin.namespaces().grantPermissionOnNamespace("prop-xyz/ns1", "my-role", EnumSet.allOf(AuthAction.class));

Policies policies = new Policies();
policies.replication_clusters = Sets.newHashSet("test");
policies.bundles = Policies.defaultBundle();
policies.auth_policies.namespace_auth.put("spiffe://developer/passport-role", EnumSet.allOf(AuthAction.class));
policies.auth_policies.namespace_auth.put("my-role", EnumSet.allOf(AuthAction.class));

assertEquals(admin.namespaces().getPolicies("prop-xyz/ns1"), policies);
assertEquals(admin.namespaces().getPermissions("prop-xyz/ns1"), policies.auth_policies.namespace_auth);

assertEquals(admin.namespaces().getTopics("prop-xyz/ns1"), Lists.newArrayList());

admin.namespaces().revokePermissionsOnNamespace("prop-xyz/ns1", "spiffe://developer/passport-role");
admin.namespaces().revokePermissionsOnNamespace("prop-xyz/ns1", "my-role");
policies.auth_policies.namespace_auth.remove("spiffe://developer/passport-role");
policies.auth_policies.namespace_auth.remove("my-role");
assertEquals(admin.namespaces().getPolicies("prop-xyz/ns1"), policies);

Expand Down
Expand Up @@ -18,14 +18,26 @@
*/
package org.apache.pulsar.client.admin.internal;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.client.WebTarget;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class WebTargets {

static WebTarget addParts(WebTarget target, String[] parts) {
if (parts != null && parts.length > 0) {
for (String part : parts) {
target = target.path(part);
String encode;
try {
encode = URLEncoder.encode(part, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
log.error(String.format("%s is Unknown", StandardCharsets.UTF_8.toString()) + "exception - [{}]", e);
encode = part;
}
target = target.path(encode);
}
}
return target;
Expand Down

0 comments on commit c0a5072

Please sign in to comment.