use of com.yahoo.pulsar.client.impl.auth.AuthenticationTls in project pulsar by yahoo.
the class AuthenticatedProducerConsumerTest method testTlsSyncProducerAndConsumer.
@Test(dataProvider = "batch")
public void testTlsSyncProducerAndConsumer(int batchMessageDelayMs) throws Exception {
log.info("-- Starting {} test --", methodName);
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
Authentication authTls = new AuthenticationTls();
authTls.configure(authParams);
internalSetup(authTls);
admin.clusters().createCluster("use", new ClusterData(brokerUrl.toString(), brokerUrlTls.toString(), "pulsar://localhost:" + BROKER_PORT, "pulsar+ssl://localhost:" + BROKER_PORT_TLS));
admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
admin.namespaces().createNamespace("my-property/use/my-ns");
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setSubscriptionType(SubscriptionType.Exclusive);
Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic1", "my-subscriber-name", conf);
ProducerConfiguration producerConf = new ProducerConfiguration();
if (batchMessageDelayMs != 0) {
producerConf.setBatchingEnabled(true);
producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(5);
}
Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic1", producerConf);
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < 10; i++) {
msg = consumer.receive(5, TimeUnit.SECONDS);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
// Acknowledge the consumption of all messages at once
consumer.acknowledgeCumulative(msg);
consumer.close();
log.info("-- Exiting {} test --", methodName);
}
use of com.yahoo.pulsar.client.impl.auth.AuthenticationTls in project pulsar by yahoo.
the class AuthenticatedProducerConsumerTest method testInternalServerExceptionOnLookup.
/**
* verifies that topicLookup/PartitionMetadataLookup gives InternalServerError(500) instead 401(auth_failed) on
* unknown-exception failure
*
* @throws Exception
*/
@Test
public void testInternalServerExceptionOnLookup() throws Exception {
log.info("-- Starting {} test --", methodName);
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
Authentication authTls = new AuthenticationTls();
authTls.configure(authParams);
internalSetup(authTls);
admin.clusters().createCluster("use", new ClusterData(brokerUrl.toString(), brokerUrlTls.toString(), "pulsar://localhost:" + BROKER_PORT, "pulsar+ssl://localhost:" + BROKER_PORT_TLS));
admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
String namespace = "my-property/use/my-ns";
admin.namespaces().createNamespace(namespace);
String destination = "persistent://" + namespace + "1/topic1";
// this will cause NPE and it should throw 500
mockZookKeeper.shutdown();
pulsar.getConfiguration().setSuperUserRoles(Sets.newHashSet());
try {
admin.persistentTopics().getPartitionedTopicMetadata(destination);
} catch (PulsarAdminException e) {
Assert.assertTrue(e.getCause() instanceof InternalServerErrorException);
}
try {
admin.lookups().lookupDestination(destination);
} catch (PulsarAdminException e) {
Assert.assertTrue(e.getCause() instanceof InternalServerErrorException);
}
}
use of com.yahoo.pulsar.client.impl.auth.AuthenticationTls in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthDisallowInsecure.
@Test
public void testTlsAuthDisallowInsecure() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(false);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.impl.auth.AuthenticationTls in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthAllowInsecure.
@Test
public void testTlsAuthAllowInsecure() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(true);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
use of com.yahoo.pulsar.client.impl.auth.AuthenticationTls in project pulsar by yahoo.
the class BrokerServiceTest method testTlsAuthUseTrustCert.
@Test
public void testTlsAuthUseTrustCert() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
final String subName = "newSub";
ClientConfiguration clientConfig;
ConsumerConfiguration consumerConfig;
Consumer consumer;
Authentication auth;
Set<String> providers = new HashSet<>();
providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
conf.setAuthenticationEnabled(true);
conf.setAuthenticationProviders(providers);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
conf.setTlsAllowInsecureConnection(false);
conf.setTlsTrustCertsFilePath(TLS_CLIENT_CERT_FILE_PATH);
restartBroker();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
PulsarClient pulsarClient = null;
// Case 1: Access without client certificate
try {
clientConfig = new ClientConfiguration();
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("Authentication required"));
} finally {
pulsarClient.close();
}
// Case 2: Access with client certificate
try {
auth = new AuthenticationTls();
auth.configure(authParams);
clientConfig = new ClientConfiguration();
clientConfig.setAuthentication(auth);
clientConfig.setUseTls(true);
clientConfig.setTlsAllowInsecureConnection(true);
clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
consumerConfig = new ConsumerConfiguration();
consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
consumer.close();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
Aggregations