|
| 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.proxy.server; |
| 20 | + |
| 21 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 22 | +import static org.mockito.Mockito.spy; |
| 23 | +import static org.testng.Assert.assertTrue; |
| 24 | +import com.google.common.collect.Sets; |
| 25 | +import io.jsonwebtoken.SignatureAlgorithm; |
| 26 | +import java.util.Calendar; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.Properties; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.concurrent.CompletableFuture; |
| 33 | +import javax.crypto.SecretKey; |
| 34 | +import lombok.Cleanup; |
| 35 | +import lombok.extern.slf4j.Slf4j; |
| 36 | +import org.apache.pulsar.broker.authentication.AuthenticationProviderToken; |
| 37 | +import org.apache.pulsar.broker.authentication.AuthenticationService; |
| 38 | +import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils; |
| 39 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 40 | +import org.apache.pulsar.client.api.Producer; |
| 41 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 42 | +import org.apache.pulsar.client.api.PulsarClient; |
| 43 | +import org.apache.pulsar.client.impl.ClientCnx; |
| 44 | +import org.apache.pulsar.client.impl.PulsarClientImpl; |
| 45 | +import org.apache.pulsar.client.impl.auth.AuthenticationToken; |
| 46 | +import org.apache.pulsar.common.configuration.PulsarConfigurationLoader; |
| 47 | +import org.apache.pulsar.common.policies.data.ClusterData; |
| 48 | +import org.apache.pulsar.common.policies.data.TenantInfoImpl; |
| 49 | +import org.awaitility.Awaitility; |
| 50 | +import org.mockito.Mockito; |
| 51 | +import org.testng.annotations.AfterClass; |
| 52 | +import org.testng.annotations.BeforeClass; |
| 53 | +import org.testng.annotations.DataProvider; |
| 54 | +import org.testng.annotations.Test; |
| 55 | + |
| 56 | +@Slf4j |
| 57 | +public class ProxyRefreshAuthTest extends ProducerConsumerBase { |
| 58 | + private final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256); |
| 59 | + |
| 60 | + private ProxyService proxyService; |
| 61 | + private final ProxyConfiguration proxyConfig = new ProxyConfiguration(); |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void doInitConf() throws Exception { |
| 65 | + super.doInitConf(); |
| 66 | + |
| 67 | + // enable tls and auth&auth at broker |
| 68 | + conf.setAuthenticationEnabled(true); |
| 69 | + conf.setAuthorizationEnabled(false); |
| 70 | + conf.setTopicLevelPoliciesEnabled(false); |
| 71 | + conf.setProxyRoles(Collections.singleton("Proxy")); |
| 72 | + conf.setAdvertisedAddress(null); |
| 73 | + conf.setAuthenticateOriginalAuthData(true); |
| 74 | + conf.setBrokerServicePort(Optional.of(0)); |
| 75 | + conf.setWebServicePort(Optional.of(0)); |
| 76 | + |
| 77 | + Set<String> superUserRoles = new HashSet<>(); |
| 78 | + superUserRoles.add("superUser"); |
| 79 | + conf.setSuperUserRoles(superUserRoles); |
| 80 | + |
| 81 | + conf.setAuthenticationProviders(Set.of(AuthenticationProviderToken.class.getName())); |
| 82 | + Properties properties = new Properties(); |
| 83 | + properties.setProperty("tokenSecretKey", AuthTokenUtils.encodeKeyBase64(SECRET_KEY)); |
| 84 | + conf.setProperties(properties); |
| 85 | + |
| 86 | + conf.setClusterName("proxy-authorization"); |
| 87 | + conf.setNumExecutorThreadPoolSize(5); |
| 88 | + |
| 89 | + conf.setAuthenticationRefreshCheckSeconds(1); |
| 90 | + } |
| 91 | + |
| 92 | + @BeforeClass |
| 93 | + @Override |
| 94 | + protected void setup() throws Exception { |
| 95 | + super.init(); |
| 96 | + |
| 97 | + admin = PulsarAdmin.builder().serviceHttpUrl(pulsar.getWebServiceAddress()) |
| 98 | + .authentication(new AuthenticationToken( |
| 99 | + () -> AuthTokenUtils.createToken(SECRET_KEY, "client", Optional.empty()))).build(); |
| 100 | + String namespaceName = "my-tenant/my-ns"; |
| 101 | + admin.clusters().createCluster("proxy-authorization", |
| 102 | + ClusterData.builder().serviceUrlTls(brokerUrlTls.toString()).build()); |
| 103 | + admin.tenants().createTenant("my-tenant", |
| 104 | + new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), Sets.newHashSet("proxy-authorization"))); |
| 105 | + admin.namespaces().createNamespace(namespaceName); |
| 106 | + |
| 107 | + // start proxy service |
| 108 | + proxyConfig.setAuthenticationEnabled(true); |
| 109 | + proxyConfig.setAuthorizationEnabled(false); |
| 110 | + proxyConfig.setForwardAuthorizationCredentials(true); |
| 111 | + proxyConfig.setBrokerServiceURL(pulsar.getBrokerServiceUrl()); |
| 112 | + proxyConfig.setAdvertisedAddress(null); |
| 113 | + |
| 114 | + proxyConfig.setServicePort(Optional.of(0)); |
| 115 | + proxyConfig.setBrokerProxyAllowedTargetPorts("*"); |
| 116 | + proxyConfig.setWebServicePort(Optional.of(0)); |
| 117 | + |
| 118 | + proxyConfig.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName()); |
| 119 | + proxyConfig.setBrokerClientAuthenticationParameters( |
| 120 | + AuthTokenUtils.createToken(SECRET_KEY, "Proxy", Optional.empty())); |
| 121 | + proxyConfig.setAuthenticationProviders(Set.of(AuthenticationProviderToken.class.getName())); |
| 122 | + Properties properties = new Properties(); |
| 123 | + properties.setProperty("tokenSecretKey", AuthTokenUtils.encodeKeyBase64(SECRET_KEY)); |
| 124 | + proxyConfig.setProperties(properties); |
| 125 | + |
| 126 | + proxyService = Mockito.spy(new ProxyService(proxyConfig, |
| 127 | + new AuthenticationService( |
| 128 | + PulsarConfigurationLoader.convertFrom(proxyConfig)))); |
| 129 | + } |
| 130 | + |
| 131 | + @AfterClass(alwaysRun = true) |
| 132 | + @Override |
| 133 | + protected void cleanup() throws Exception { |
| 134 | + super.internalCleanup(); |
| 135 | + proxyService.close(); |
| 136 | + } |
| 137 | + |
| 138 | + private void startProxy(boolean forwardAuthData) throws Exception { |
| 139 | + pulsar.getConfiguration().setAuthenticateOriginalAuthData(forwardAuthData); |
| 140 | + proxyConfig.setForwardAuthorizationCredentials(forwardAuthData); |
| 141 | + proxyService.start(); |
| 142 | + } |
| 143 | + |
| 144 | + @DataProvider |
| 145 | + Object[] forwardAuthDataProvider() { |
| 146 | + return new Object[]{true, false}; |
| 147 | + } |
| 148 | + |
| 149 | + @Test(dataProvider = "forwardAuthDataProvider") |
| 150 | + public void testAuthDataRefresh(boolean forwardAuthData) throws Exception { |
| 151 | + log.info("-- Starting {} test --", methodName); |
| 152 | + |
| 153 | + startProxy(forwardAuthData); |
| 154 | + |
| 155 | + AuthenticationToken authenticationToken = new AuthenticationToken(() -> { |
| 156 | + Calendar calendar = Calendar.getInstance(); |
| 157 | + calendar.add(Calendar.SECOND, 1); |
| 158 | + return AuthTokenUtils.createToken(SECRET_KEY, "client", Optional.of(calendar.getTime())); |
| 159 | + }); |
| 160 | + |
| 161 | + pulsarClient = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()) |
| 162 | + .authentication(authenticationToken) |
| 163 | + .build(); |
| 164 | + |
| 165 | + String topic = "persistent://my-tenant/my-ns/my-topic1"; |
| 166 | + @Cleanup |
| 167 | + Producer<byte[]> ignored = spy(pulsarClient.newProducer() |
| 168 | + .topic(topic).create()); |
| 169 | + |
| 170 | + PulsarClientImpl pulsarClientImpl = (PulsarClientImpl) pulsarClient; |
| 171 | + Set<CompletableFuture<ClientCnx>> connections = pulsarClientImpl.getCnxPool().getConnections(); |
| 172 | + |
| 173 | + Awaitility.await().during(4, SECONDS).untilAsserted(() -> { |
| 174 | + pulsarClient.getPartitionsForTopic(topic).get(); |
| 175 | + assertTrue(connections.stream().allMatch(n -> { |
| 176 | + try { |
| 177 | + ClientCnx clientCnx = n.get(); |
| 178 | + long timestamp = clientCnx.getLastDisconnectedTimestamp(); |
| 179 | + return timestamp == 0; |
| 180 | + } catch (Exception e) { |
| 181 | + throw new RuntimeException(e); |
| 182 | + } |
| 183 | + })); |
| 184 | + }); |
| 185 | + } |
| 186 | +} |
0 commit comments