use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class LoadBalancerTest method createNamespace.
private void createNamespace(PulsarService pulsar, String namespace, int numBundles) throws Exception {
Policies policies = new Policies();
policies.bundles = getBundles(numBundles);
ObjectMapper jsonMapper = ObjectMapperFactory.create();
ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
String zpath = AdminResource.path(POLICIES, namespace);
ZkUtils.createFullPathOptimistic(globalZk, zpath, jsonMapper.writeValueAsBytes(policies), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class HttpTopicLookupv2Test method testValidateReplicationSettingsOnNamespace.
@Test
public void testValidateReplicationSettingsOnNamespace() throws Exception {
final String property = "my-prop";
final String cluster = "global";
final String ns1 = "ns1";
final String ns2 = "ns2";
Policies policies1 = new Policies();
doReturn(Optional.of(policies1)).when(policiesCache).get(AdminResource.path(POLICIES, property, cluster, ns1));
Policies policies2 = new Policies();
policies2.replication_clusters = Lists.newArrayList("invalid-localCluster");
doReturn(Optional.of(policies2)).when(policiesCache).get(AdminResource.path(POLICIES, property, cluster, ns2));
TopicLookup destLookup = spy(new TopicLookup());
doReturn(false).when(destLookup).isRequestHttps();
destLookup.setPulsar(pulsar);
doReturn("null").when(destLookup).clientAppId();
Field uriField = PulsarWebResource.class.getDeclaredField("uri");
uriField.setAccessible(true);
UriInfo uriInfo = mock(UriInfo.class);
uriField.set(destLookup, uriInfo);
doReturn(false).when(config).isAuthorizationEnabled();
AsyncResponse asyncResponse = mock(AsyncResponse.class);
destLookup.lookupTopicAsync(TopicDomain.persistent.value(), property, cluster, ns1, "empty-cluster", false, asyncResponse);
ArgumentCaptor<Throwable> arg = ArgumentCaptor.forClass(Throwable.class);
verify(asyncResponse).resume(arg.capture());
assertEquals(arg.getValue().getClass(), RestException.class);
AsyncResponse asyncResponse2 = mock(AsyncResponse.class);
destLookup.lookupTopicAsync(TopicDomain.persistent.value(), property, cluster, ns2, "invalid-localCluster", false, asyncResponse2);
ArgumentCaptor<Throwable> arg2 = ArgumentCaptor.forClass(Throwable.class);
verify(asyncResponse2).resume(arg2.capture());
// Should have raised exception for invalid cluster
assertEquals(arg2.getValue().getClass(), RestException.class);
}
use of org.apache.pulsar.common.policies.data.Policies in project incubator-pulsar by apache.
the class NamespacesTest method testGrantAndRevokePermissions.
@Test(enabled = false)
public void testGrantAndRevokePermissions() throws Exception {
Policies expectedPolicies = new Policies();
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role", EnumSet.of(AuthAction.produce));
expectedPolicies.auth_policies.namespace_auth.put("my-role", EnumSet.of(AuthAction.produce));
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
expectedPolicies.auth_policies.namespace_auth.put("other-role", EnumSet.of(AuthAction.consume));
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName(), "my-role");
expectedPolicies.auth_policies.namespace_auth.remove("my-role");
assertEquals(namespaces.getPolicies(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies);
assertEquals(namespaces.getPermissions(this.testProperty, this.testLocalCluster, this.testLocalNamespaces.get(0).getLocalName()), expectedPolicies.auth_policies.namespace_auth);
// Non-existing namespaces
try {
namespaces.getPolicies(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.getPermissions(this.testProperty, this.testLocalCluster, "non-existing-namespace-1");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.grantPermissionOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role", EnumSet.of(AuthAction.produce));
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
try {
namespaces.revokePermissionsOnNamespace(this.testProperty, this.testLocalCluster, "non-existing-namespace-1", "my-role");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
}
NamespaceName testNs = this.testLocalNamespaces.get(1);
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.getPolicies(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.getPermissions(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName());
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
fail("should have failed");
} catch (RestException e) {
// Ok
}
mockZookKeeper.failNow(Code.BADVERSION);
try {
namespaces.grantPermissionOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role", EnumSet.of(AuthAction.consume));
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
}
mockZookKeeper.failNow(Code.BADVERSION);
try {
namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
fail("should have failed");
} catch (RestException e) {
assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
}
mockZookKeeper.failNow(Code.SESSIONEXPIRED);
try {
namespaces.revokePermissionsOnNamespace(testNs.getProperty(), testNs.getCluster(), testNs.getLocalName(), "other-role");
fail("should have failed");
} catch (RestException e) {
// Ok
}
}
Aggregations