Search in sources :

Example 16 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class PersistentTopicsTest method testGetMessageById.

@Test
public void testGetMessageById() throws Exception {
    TenantInfoImpl tenantInfo = new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test"));
    admin.tenants().createTenant("tenant-xyz", tenantInfo);
    admin.namespaces().createNamespace("tenant-xyz/ns-abc", Sets.newHashSet("test"));
    final String topicName1 = "persistent://tenant-xyz/ns-abc/testGetMessageById1";
    final String topicName2 = "persistent://tenant-xyz/ns-abc/testGetMessageById2";
    admin.topics().createNonPartitionedTopic(topicName1);
    admin.topics().createNonPartitionedTopic(topicName2);
    @Cleanup ProducerBase<byte[]> producer1 = (ProducerBase<byte[]>) pulsarClient.newProducer().topic(topicName1).enableBatching(false).create();
    String data1 = "test1";
    MessageIdImpl id1 = (MessageIdImpl) producer1.send(data1.getBytes());
    @Cleanup ProducerBase<byte[]> producer2 = (ProducerBase<byte[]>) pulsarClient.newProducer().topic(topicName2).enableBatching(false).create();
    String data2 = "test2";
    MessageIdImpl id2 = (MessageIdImpl) producer2.send(data2.getBytes());
    Message<byte[]> message1 = admin.topics().getMessageById(topicName1, id1.getLedgerId(), id1.getEntryId());
    Assert.assertEquals(message1.getData(), data1.getBytes());
    Message<byte[]> message2 = admin.topics().getMessageById(topicName2, id2.getLedgerId(), id2.getEntryId());
    Assert.assertEquals(message2.getData(), data2.getBytes());
    Message<byte[]> message3 = null;
    try {
        message3 = admin.topics().getMessageById(topicName2, id1.getLedgerId(), id1.getEntryId());
        Assert.fail();
    } catch (Exception e) {
        Assert.assertNull(message3);
    }
    Message<byte[]> message4 = null;
    try {
        message4 = admin.topics().getMessageById(topicName1, id2.getLedgerId(), id2.getEntryId());
        Assert.fail();
    } catch (Exception e) {
        Assert.assertNull(message4);
    }
}
Also used : ProducerBase(org.apache.pulsar.client.impl.ProducerBase) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) BatchMessageIdImpl(org.apache.pulsar.client.impl.BatchMessageIdImpl) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) RestException(org.apache.pulsar.broker.web.RestException) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 17 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class AdminApiTlsAuthTest method testUnauthorizedUserAsOriginalPrincipal.

@Test
public void testUnauthorizedUserAsOriginalPrincipal() throws Exception {
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        admin.tenants().createTenant("tenant1", new TenantInfoImpl(ImmutableSet.of("proxy", "user1"), ImmutableSet.of("test")));
        admin.namespaces().createNamespace("tenant1/ns1");
    }
    WebTarget root = buildWebClient("proxy");
    try {
        root.path("/admin/v2/namespaces").path("tenant1").request(MediaType.APPLICATION_JSON).header("X-Original-Principal", "user2").get(new GenericType<List<String>>() {
        });
        Assert.fail("user2 should not be authorized");
    } catch (NotAuthorizedException e) {
    // expected
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 18 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class AdminApiTlsAuthTest method testProxyRoleCantListNamespacesEvenWithAccess.

@Test
public void testProxyRoleCantListNamespacesEvenWithAccess() throws Exception {
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        admin.tenants().createTenant("tenant1", new TenantInfoImpl(ImmutableSet.of("proxy"), ImmutableSet.of("test")));
        admin.namespaces().createNamespace("tenant1/ns1");
    }
    try (PulsarAdmin admin = buildAdminClient("proxy")) {
        admin.namespaces().getNamespaces("tenant1");
        Assert.fail("Shouldn't be able to list namespaces");
    } catch (PulsarAdminException.NotAuthorizedException e) {
    // expected
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 19 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class AdminApiTlsAuthTest method testUnauthorizedUserAsOriginalPrincipalProxyIsSuperUser.

@Test
public void testUnauthorizedUserAsOriginalPrincipalProxyIsSuperUser() throws Exception {
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        admin.tenants().createTenant("tenant1", new TenantInfoImpl(ImmutableSet.of("user1"), ImmutableSet.of("test")));
        admin.namespaces().createNamespace("tenant1/ns1");
    }
    WebTarget root = buildWebClient("superproxy");
    try {
        root.path("/admin/v2/namespaces").path("tenant1").request(MediaType.APPLICATION_JSON).header("X-Original-Principal", "user2").get(new GenericType<List<String>>() {
        });
        Assert.fail("user2 should not be authorized");
    } catch (NotAuthorizedException e) {
    // expected
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 20 with TenantInfoImpl

use of org.apache.pulsar.common.policies.data.TenantInfoImpl in project pulsar by apache.

the class AdminApiTlsAuthTest method testProxyUserViaProxy.

@Test
public void testProxyUserViaProxy() throws Exception {
    try (PulsarAdmin admin = buildAdminClient("admin")) {
        admin.tenants().createTenant("tenant1", new TenantInfoImpl(ImmutableSet.of("proxy"), ImmutableSet.of("test")));
        admin.namespaces().createNamespace("tenant1/ns1");
    }
    WebTarget root = buildWebClient("superproxy");
    try {
        root.path("/admin/v2/namespaces").path("tenant1").request(MediaType.APPLICATION_JSON).header("X-Original-Principal", "proxy").get(new GenericType<List<String>>() {
        });
        Assert.fail("proxy should not be authorized");
    } catch (NotAuthorizedException e) {
    // expected
    }
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

TenantInfoImpl (org.apache.pulsar.common.policies.data.TenantInfoImpl)690 Test (org.testng.annotations.Test)458 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)211 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)146 BeforeMethod (org.testng.annotations.BeforeMethod)144 Cleanup (lombok.Cleanup)139 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)136 PulsarClient (org.apache.pulsar.client.api.PulsarClient)91 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)72 HashSet (java.util.HashSet)55 HashMap (java.util.HashMap)49 ServiceConfiguration (org.apache.pulsar.broker.ServiceConfiguration)48 CompletableFuture (java.util.concurrent.CompletableFuture)41 List (java.util.List)38 PulsarService (org.apache.pulsar.broker.PulsarService)37 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)36 ArrayList (java.util.ArrayList)33 WebTarget (javax.ws.rs.client.WebTarget)33 Policies (org.apache.pulsar.common.policies.data.Policies)33 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)31