|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.broker.admin; |
| 20 | + |
| 21 | +import io.jsonwebtoken.Jwts; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.UUID; |
| 24 | +import lombok.Cleanup; |
| 25 | +import lombok.SneakyThrows; |
| 26 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 27 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 28 | +import org.apache.pulsar.client.impl.auth.AuthenticationToken; |
| 29 | +import org.apache.pulsar.common.policies.data.AuthAction; |
| 30 | +import org.apache.pulsar.common.policies.data.RetentionPolicies; |
| 31 | +import org.apache.pulsar.common.policies.data.TenantInfo; |
| 32 | +import org.apache.pulsar.security.MockedPulsarStandalone; |
| 33 | +import org.testng.Assert; |
| 34 | +import org.testng.annotations.AfterClass; |
| 35 | +import org.testng.annotations.BeforeClass; |
| 36 | +import org.testng.annotations.Test; |
| 37 | + |
| 38 | +import static org.awaitility.Awaitility.await; |
| 39 | + |
| 40 | + |
| 41 | +public final class TopicPoliciesAuthZTest extends MockedPulsarStandalone { |
| 42 | + |
| 43 | + private PulsarAdmin superUserAdmin; |
| 44 | + |
| 45 | + private PulsarAdmin tenantManagerAdmin; |
| 46 | + |
| 47 | + private static final String TENANT_ADMIN_SUBJECT = UUID.randomUUID().toString(); |
| 48 | + private static final String TENANT_ADMIN_TOKEN = Jwts.builder() |
| 49 | + .claim("sub", TENANT_ADMIN_SUBJECT).signWith(SECRET_KEY).compact(); |
| 50 | + |
| 51 | + @SneakyThrows |
| 52 | + @BeforeClass |
| 53 | + public void before() { |
| 54 | + configureTokenAuthentication(); |
| 55 | + configureDefaultAuthorization(); |
| 56 | + start(); |
| 57 | + this.superUserAdmin =PulsarAdmin.builder() |
| 58 | + .serviceHttpUrl(getPulsarService().getWebServiceAddress()) |
| 59 | + .authentication(new AuthenticationToken(SUPER_USER_TOKEN)) |
| 60 | + .build(); |
| 61 | + final TenantInfo tenantInfo = superUserAdmin.tenants().getTenantInfo("public"); |
| 62 | + tenantInfo.getAdminRoles().add(TENANT_ADMIN_SUBJECT); |
| 63 | + superUserAdmin.tenants().updateTenant("public", tenantInfo); |
| 64 | + this.tenantManagerAdmin = PulsarAdmin.builder() |
| 65 | + .serviceHttpUrl(getPulsarService().getWebServiceAddress()) |
| 66 | + .authentication(new AuthenticationToken(TENANT_ADMIN_TOKEN)) |
| 67 | + .build(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @SneakyThrows |
| 72 | + @AfterClass |
| 73 | + public void after() { |
| 74 | + close(); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @SneakyThrows |
| 79 | + @Test |
| 80 | + public void testRetention() { |
| 81 | + final String random = UUID.randomUUID().toString(); |
| 82 | + final String topic = "persistent://public/default/" + random; |
| 83 | + final String subject = UUID.randomUUID().toString(); |
| 84 | + final String token = Jwts.builder() |
| 85 | + .claim("sub", subject).signWith(SECRET_KEY).compact(); |
| 86 | + superUserAdmin.topics().createNonPartitionedTopic(topic); |
| 87 | + |
| 88 | + @Cleanup |
| 89 | + final PulsarAdmin subAdmin = PulsarAdmin.builder() |
| 90 | + .serviceHttpUrl(getPulsarService().getWebServiceAddress()) |
| 91 | + .authentication(new AuthenticationToken(token)) |
| 92 | + .build(); |
| 93 | + final RetentionPolicies definedRetentionPolicy = new RetentionPolicies(1, 1); |
| 94 | + // test superuser |
| 95 | + superUserAdmin.topicPolicies().setRetention(topic, definedRetentionPolicy); |
| 96 | + |
| 97 | + // because the topic policies is eventual consistency, we should wait here |
| 98 | + await().untilAsserted(() -> { |
| 99 | + final RetentionPolicies receivedRetentionPolicy = superUserAdmin.topicPolicies().getRetention(topic); |
| 100 | + Assert.assertEquals(receivedRetentionPolicy, definedRetentionPolicy); |
| 101 | + }); |
| 102 | + superUserAdmin.topicPolicies().removeRetention(topic); |
| 103 | + |
| 104 | + await().untilAsserted(() -> { |
| 105 | + final RetentionPolicies retention = superUserAdmin.topicPolicies().getRetention(topic); |
| 106 | + Assert.assertNull(retention); |
| 107 | + }); |
| 108 | + |
| 109 | + // test tenant manager |
| 110 | + |
| 111 | + tenantManagerAdmin.topicPolicies().setRetention(topic, definedRetentionPolicy); |
| 112 | + await().untilAsserted(() -> { |
| 113 | + final RetentionPolicies receivedRetentionPolicy = tenantManagerAdmin.topicPolicies().getRetention(topic); |
| 114 | + Assert.assertEquals(receivedRetentionPolicy, definedRetentionPolicy); |
| 115 | + }); |
| 116 | + tenantManagerAdmin.topicPolicies().removeRetention(topic); |
| 117 | + await().untilAsserted(() -> { |
| 118 | + final RetentionPolicies retention = tenantManagerAdmin.topicPolicies().getRetention(topic); |
| 119 | + Assert.assertNull(retention); |
| 120 | + }); |
| 121 | + |
| 122 | + // test nobody |
| 123 | + |
| 124 | + try { |
| 125 | + subAdmin.topicPolicies().getRetention(topic); |
| 126 | + Assert.fail("unexpected behaviour"); |
| 127 | + } catch (PulsarAdminException ex) { |
| 128 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 129 | + } |
| 130 | + |
| 131 | + try { |
| 132 | + |
| 133 | + subAdmin.topicPolicies().setRetention(topic, definedRetentionPolicy); |
| 134 | + Assert.fail("unexpected behaviour"); |
| 135 | + } catch (PulsarAdminException ex) { |
| 136 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 137 | + } |
| 138 | + |
| 139 | + try { |
| 140 | + subAdmin.topicPolicies().removeRetention(topic); |
| 141 | + Assert.fail("unexpected behaviour"); |
| 142 | + } catch (PulsarAdminException ex) { |
| 143 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 144 | + } |
| 145 | + |
| 146 | + // test sub user with permissions |
| 147 | + for (AuthAction action : AuthAction.values()) { |
| 148 | + superUserAdmin.namespaces().grantPermissionOnNamespace("public/default", |
| 149 | + subject, Set.of(action)); |
| 150 | + try { |
| 151 | + subAdmin.topicPolicies().getRetention(topic); |
| 152 | + Assert.fail("unexpected behaviour"); |
| 153 | + } catch (PulsarAdminException ex) { |
| 154 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 155 | + } |
| 156 | + |
| 157 | + try { |
| 158 | + |
| 159 | + subAdmin.topicPolicies().setRetention(topic, definedRetentionPolicy); |
| 160 | + Assert.fail("unexpected behaviour"); |
| 161 | + } catch (PulsarAdminException ex) { |
| 162 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 163 | + } |
| 164 | + |
| 165 | + try { |
| 166 | + subAdmin.topicPolicies().removeRetention(topic); |
| 167 | + Assert.fail("unexpected behaviour"); |
| 168 | + } catch (PulsarAdminException ex) { |
| 169 | + Assert.assertTrue(ex instanceof PulsarAdminException.NotAuthorizedException); |
| 170 | + } |
| 171 | + superUserAdmin.namespaces().revokePermissionsOnNamespace("public/default", subject); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments