Search in sources :

Example 1 with PropertyAdmin

use of com.yahoo.pulsar.common.policies.data.PropertyAdmin in project pulsar by yahoo.

the class ProxyAuthorizationTest method test.

@Test
public void test() throws Exception {
    AuthorizationManager auth = service.getAuthorizationManager();
    assertEquals(auth.canLookup(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), false);
    admin.clusters().createCluster("c1", new ClusterData());
    admin.properties().createProperty("p1", new PropertyAdmin(Lists.newArrayList("role1"), Sets.newHashSet("c1")));
    waitForChange();
    admin.namespaces().createNamespace("p1/c1/ns1");
    waitForChange();
    assertEquals(auth.canLookup(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), false);
    admin.namespaces().grantPermissionOnNamespace("p1/c1/ns1", "my-role", EnumSet.of(AuthAction.produce));
    waitForChange();
    assertEquals(auth.canLookup(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), true);
    assertEquals(auth.canProduce(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), true);
    admin.persistentTopics().grantPermission("persistent://p1/c1/ns1/ds2", "other-role", EnumSet.of(AuthAction.consume));
    waitForChange();
    assertEquals(auth.canLookup(DestinationName.get("persistent://p1/c1/ns1/ds2"), "other-role"), true);
    assertEquals(auth.canProduce(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), true);
    assertEquals(auth.canProduce(DestinationName.get("persistent://p1/c1/ns1/ds2"), "other-role"), false);
    assertEquals(auth.canConsume(DestinationName.get("persistent://p1/c1/ns1/ds2"), "other-role"), true);
    assertEquals(auth.canConsume(DestinationName.get("persistent://p1/c1/ns1/ds2"), "no-access-role"), false);
    assertEquals(auth.canLookup(DestinationName.get("persistent://p1/c1/ns1/ds1"), "no-access-role"), false);
    admin.namespaces().grantPermissionOnNamespace("p1/c1/ns1", "my-role", EnumSet.allOf(AuthAction.class));
    waitForChange();
    assertEquals(auth.canProduce(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), true);
    assertEquals(auth.canConsume(DestinationName.get("persistent://p1/c1/ns1/ds1"), "my-role"), true);
    admin.namespaces().deleteNamespace("p1/c1/ns1");
    admin.properties().deleteProperty("p1");
    admin.clusters().deleteCluster("c1");
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) AuthorizationManager(com.yahoo.pulsar.broker.authorization.AuthorizationManager) AuthAction(com.yahoo.pulsar.common.policies.data.AuthAction) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest) Test(org.testng.annotations.Test)

Example 2 with PropertyAdmin

use of com.yahoo.pulsar.common.policies.data.PropertyAdmin in project pulsar by yahoo.

the class AdminTest method resourceQuotas.

@Test
void resourceQuotas() throws Exception {
    // get Default Resource Quota
    ResourceQuota quota = resourceQuotas.getDefaultResourceQuota();
    assertNotNull(quota);
    assertTrue(quota.getBandwidthIn() > 0);
    // set Default Resource Quota
    double defaultBandwidth = 1000;
    quota.setBandwidthIn(defaultBandwidth);
    quota.setBandwidthOut(defaultBandwidth);
    resourceQuotas.setDefaultResourceQuota(quota);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthIn() == defaultBandwidth);
    assertTrue(resourceQuotas.getDefaultResourceQuota().getBandwidthOut() == defaultBandwidth);
    String property = "prop-xyz";
    String cluster = "use";
    String namespace = "ns";
    String bundleRange = "0x00000000_0xffffffff";
    Policies policies = new Policies();
    doReturn(policies).when(resourceQuotas).getNamespacePolicies(property, cluster, namespace);
    doReturn("client-id").when(resourceQuotas).clientAppId();
    try {
        resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    try {
        resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
        fail();
    } catch (Exception e) {
    // OK : should fail without creating policies
    }
    // create policies
    PropertyAdmin admin = new PropertyAdmin();
    admin.getAllowedClusters().add(cluster);
    mockZookKeeper.create(PulsarWebResource.path("policies", property), ObjectMapperFactory.getThreadLocal().writeValueAsBytes(admin), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // customized bandwidth for this namespace
    double customizeBandwidth = 3000;
    quota.setBandwidthIn(customizeBandwidth);
    quota.setBandwidthOut(customizeBandwidth);
    // set and get Resource Quota
    resourceQuotas.setNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange, quota);
    ResourceQuota bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertEquals(quota, bundleQuota);
    // remove quota which sets to default quota
    resourceQuotas.removeNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    bundleQuota = resourceQuotas.getNamespaceBundleResourceQuota(property, cluster, namespace, bundleRange);
    assertTrue(bundleQuota.getBandwidthIn() == defaultBandwidth);
    assertTrue(bundleQuota.getBandwidthOut() == defaultBandwidth);
}
Also used : Policies(com.yahoo.pulsar.common.policies.data.Policies) ResourceQuota(com.yahoo.pulsar.common.policies.data.ResourceQuota) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with PropertyAdmin

use of com.yahoo.pulsar.common.policies.data.PropertyAdmin in project pulsar by yahoo.

the class NamespacesTest method testValidateAdminAccessOnProperty.

@Test
public void testValidateAdminAccessOnProperty() throws Exception {
    try {
        final String property = "prop";
        pulsar.getConfiguration().setAuthenticationEnabled(true);
        pulsar.getConfiguration().setAuthorizationEnabled(true);
        final String path = PulsarWebResource.path("policies", property);
        final String data = ObjectMapperFactory.getThreadLocal().writeValueAsString(new PropertyAdmin(Lists.newArrayList(namespaces.clientAppId()), Sets.newHashSet("use")));
        ZkUtils.createFullPathOptimistic(pulsar.getConfigurationCache().getZooKeeper(), path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        namespaces.validateAdminAccessOnProperty(property);
    } catch (RestException e) {
        fail("validateAdminAccessOnProperty failed");
    } finally {
        pulsar.getConfiguration().setAuthenticationEnabled(false);
        pulsar.getConfiguration().setAuthorizationEnabled(false);
    }
}
Also used : PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) RestException(com.yahoo.pulsar.broker.web.RestException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with PropertyAdmin

use of com.yahoo.pulsar.common.policies.data.PropertyAdmin in project pulsar by yahoo.

the class SLAMonitoringTest method createProperty.

private void createProperty(PulsarAdmin pulsarAdmin) throws PulsarClientException, MalformedURLException, PulsarAdminException {
    ClusterData clusterData = new ClusterData();
    clusterData.setServiceUrl(pulsarAdmin.getServiceUrl().toString());
    pulsarAdmins[0].clusters().createCluster("my-cluster", clusterData);
    Set<String> allowedClusters = new HashSet<>();
    allowedClusters.add("my-cluster");
    PropertyAdmin adminConfig = new PropertyAdmin();
    adminConfig.setAllowedClusters(allowedClusters);
    List<String> adminRoles = new ArrayList<>();
    adminRoles.add("");
    adminConfig.setAdminRoles(adminRoles);
    pulsarAdmin.properties().createProperty("sla-monitor", adminConfig);
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with PropertyAdmin

use of com.yahoo.pulsar.common.policies.data.PropertyAdmin in project pulsar by yahoo.

the class AdminApiTest method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    conf.setLoadBalancerEnabled(true);
    super.internalSetup();
    bundleFactory = new NamespaceBundleFactory(pulsar, Hashing.crc32());
    // create otherbroker to test redirect on calls that need
    // namespace ownership
    ServiceConfiguration otherconfig = new ServiceConfiguration();
    otherconfig.setBrokerServicePort(SECONDARY_BROKER_PORT);
    otherconfig.setWebServicePort(SECONDARY_BROKER_WEBSERVICE_PORT);
    otherconfig.setLoadBalancerEnabled(false);
    otherconfig.setClusterName("test");
    otherPulsar = startBroker(otherconfig);
    otheradmin = new PulsarAdmin(new URL("http://127.0.0.1" + ":" + SECONDARY_BROKER_WEBSERVICE_PORT), (Authentication) null);
    // Setup namespaces
    admin.clusters().createCluster("use", new ClusterData("http://127.0.0.1" + ":" + BROKER_WEBSERVICE_PORT));
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use"));
    admin.properties().createProperty("prop-xyz", propertyAdmin);
    admin.namespaces().createNamespace("prop-xyz/use/ns1");
}
Also used : ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) ServiceConfiguration(com.yahoo.pulsar.broker.ServiceConfiguration) PulsarAdmin(com.yahoo.pulsar.client.admin.PulsarAdmin) PropertyAdmin(com.yahoo.pulsar.common.policies.data.PropertyAdmin) Authentication(com.yahoo.pulsar.client.api.Authentication) NamespaceBundleFactory(com.yahoo.pulsar.common.naming.NamespaceBundleFactory) URL(java.net.URL) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

PropertyAdmin (com.yahoo.pulsar.common.policies.data.PropertyAdmin)26 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)15 Test (org.testng.annotations.Test)14 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)9 RestException (com.yahoo.pulsar.broker.web.RestException)5 PulsarAdmin (com.yahoo.pulsar.client.admin.PulsarAdmin)5 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)5 PulsarService (com.yahoo.pulsar.broker.PulsarService)4 ServiceConfiguration (com.yahoo.pulsar.broker.ServiceConfiguration)4 AuthAction (com.yahoo.pulsar.common.policies.data.AuthAction)4 URL (java.net.URL)4 KeeperException (org.apache.zookeeper.KeeperException)4 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)3 Authentication (com.yahoo.pulsar.client.api.Authentication)3 Policies (com.yahoo.pulsar.common.policies.data.Policies)3 LocalBookkeeperEnsemble (com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble)3 BeforeMethod (org.testng.annotations.BeforeMethod)3 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)2 AuthorizationManager (com.yahoo.pulsar.broker.authorization.AuthorizationManager)2 ConflictException (com.yahoo.pulsar.client.admin.PulsarAdminException.ConflictException)2