This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize authorization by caching authorization results
### Motivation To follow Kafka's behavior, KoP also performs authorization for each PRODUCE or FETCH request. If the custom authorization provider is slow to authorize produce or consume permissions, the performance will be impacted. ### Modifications Introduce caches for authorization: - PRODUCE: (topic, role) -> result - FETCH: (topic, role, group) -> result; Add `SlowAuthorizationTest` to verify the producer and consumer won't be affected significantly by slow authorization.
- Loading branch information
1 parent
5193592
commit 45db3ed
Showing
4 changed files
with
294 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...ava/io/streamnative/pulsar/handlers/kop/security/auth/KafkaAuthorizationMockTestBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.streamnative.pulsar.handlers.kop.security.auth; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import com.google.common.collect.Sets; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.streamnative.pulsar.handlers.kop.KopProtocolHandlerTestBase; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import javax.crypto.SecretKey; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.common.PartitionInfo; | ||
import org.apache.pulsar.broker.authentication.AuthenticationProviderToken; | ||
import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils; | ||
import org.apache.pulsar.client.admin.PulsarAdmin; | ||
import org.apache.pulsar.client.admin.PulsarAdminException; | ||
import org.apache.pulsar.client.impl.auth.AuthenticationToken; | ||
|
||
/** | ||
* Unit test for Authorization with `entryFormat=pulsar`. | ||
*/ | ||
public class KafkaAuthorizationMockTestBase extends KopProtocolHandlerTestBase { | ||
|
||
protected static final String TENANT = "KafkaAuthorizationTest"; | ||
protected static final String NAMESPACE = "ns1"; | ||
protected static final SecretKey SECRET_KEY = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256); | ||
|
||
protected static final String ADMIN_USER = "pass.pass"; | ||
protected String authorizationProviderClassName = KafkaMockAuthorizationProvider.class.getName(); | ||
|
||
@Override | ||
protected void setup() throws Exception { | ||
Properties properties = new Properties(); | ||
properties.setProperty("tokenSecretKey", AuthTokenUtils.encodeKeyBase64(SECRET_KEY)); | ||
|
||
String adminToken = AuthTokenUtils.createToken(SECRET_KEY, ADMIN_USER, Optional.empty()); | ||
|
||
conf.setSaslAllowedMechanisms(Sets.newHashSet("PLAIN")); | ||
conf.setKafkaMetadataTenant("internal"); | ||
conf.setKafkaMetadataNamespace("__kafka"); | ||
conf.setKafkaTenant(TENANT); | ||
conf.setKafkaNamespace(NAMESPACE); | ||
|
||
conf.setClusterName(super.configClusterName); | ||
conf.setAuthorizationEnabled(true); | ||
conf.setAuthenticationEnabled(true); | ||
conf.setAuthorizationAllowWildcardsMatching(true); | ||
conf.setAuthorizationProvider(authorizationProviderClassName); | ||
conf.setAuthenticationProviders( | ||
Sets.newHashSet(AuthenticationProviderToken.class.getName())); | ||
conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName()); | ||
conf.setBrokerClientAuthenticationParameters("token:" + adminToken); | ||
conf.setProperties(properties); | ||
|
||
super.internalSetup(); | ||
} | ||
|
||
@Override | ||
protected void cleanup() throws Exception { | ||
super.admin = spy(PulsarAdmin.builder().serviceHttpUrl(brokerUrl.toString()) | ||
.authentication(this.conf.getBrokerClientAuthenticationPlugin(), | ||
this.conf.getBrokerClientAuthenticationParameters()).build()); | ||
} | ||
|
||
@Override | ||
protected void createAdmin() throws Exception { | ||
super.admin = spy(PulsarAdmin.builder().serviceHttpUrl(brokerUrl.toString()) | ||
.authentication(this.conf.getBrokerClientAuthenticationPlugin(), | ||
this.conf.getBrokerClientAuthenticationParameters()).build()); | ||
} | ||
|
||
public void testSuperUserProduceAndConsume() throws PulsarAdminException { | ||
String superUserToken = AuthTokenUtils.createToken(SECRET_KEY, "pass.pass", Optional.empty()); | ||
String topic = "testSuperUserProduceAndConsumeTopic"; | ||
String fullNewTopicName = "persistent://" + TENANT + "/" + NAMESPACE + "/" + topic; | ||
KProducer kProducer = new KProducer(topic, false, "localhost", getKafkaBrokerPort(), | ||
TENANT + "/" + NAMESPACE, "token:" + superUserToken); | ||
int totalMsgs = 10; | ||
String messageStrPrefix = topic + "_message_"; | ||
|
||
for (int i = 0; i < totalMsgs; i++) { | ||
String messageStr = messageStrPrefix + i; | ||
kProducer.getProducer().send(new ProducerRecord<>(topic, i, messageStr)); | ||
} | ||
KConsumer kConsumer = new KConsumer(topic, "localhost", getKafkaBrokerPort(), false, | ||
TENANT + "/" + NAMESPACE, "token:" + superUserToken, "DemoKafkaOnPulsarConsumer"); | ||
kConsumer.getConsumer().subscribe(Collections.singleton(topic)); | ||
|
||
int i = 0; | ||
while (i < totalMsgs) { | ||
ConsumerRecords<Integer, String> records = kConsumer.getConsumer().poll(Duration.ofSeconds(1)); | ||
for (ConsumerRecord<Integer, String> record : records) { | ||
Integer key = record.key(); | ||
assertEquals(messageStrPrefix + key.toString(), record.value()); | ||
i++; | ||
} | ||
} | ||
assertEquals(i, totalMsgs); | ||
|
||
// no more records | ||
ConsumerRecords<Integer, String> records = kConsumer.getConsumer().poll(Duration.ofMillis(200)); | ||
assertTrue(records.isEmpty()); | ||
|
||
// ensure that we can list the topic | ||
Map<String, List<PartitionInfo>> result = kConsumer.getConsumer().listTopics(Duration.ofSeconds(1)); | ||
assertEquals(result.size(), 1); | ||
assertTrue(result.containsKey(topic), | ||
"list of topics " + result.keySet() + " does not contains " + topic); | ||
|
||
// Cleanup | ||
kProducer.close(); | ||
kConsumer.close(); | ||
admin.topics().deletePartitionedTopic(fullNewTopicName); | ||
} | ||
} |
Oops, something went wrong.