use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthorizationFailure.
@Test
public void testDiscoveryLookupAuthorizationFailure() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// set Authentication provider which returns "invalid" appid so, authorization fails
Set<String> providersClassNames = Sets.newHashSet(MockAuthorizationProviderFail.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
// set authentication data
Authentication auth = new Authentication() {
private static final long serialVersionUID = 1L;
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
private static final long serialVersionUID = 1L;
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
};
@Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
try {
pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
fail("should have failed due to authentication");
} catch (PulsarClientException e) {
// Ok: expected
assertTrue(e instanceof PulsarClientException.LookupException);
}
}
use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthenticationFailure.
@Test
public void testDiscoveryLookupAuthenticationFailure() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// set Authentication provider which fails authentication
Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProviderFail.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
// set authentication data
Authentication auth = new Authentication() {
private static final long serialVersionUID = 1L;
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
private static final long serialVersionUID = 1L;
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
};
@Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
try {
pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
fail("should have failed due to authentication");
} catch (PulsarClientException e) {
// Ok: expected
}
}
use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.
the class BrokerServiceLookupTest method testDiscoveryLookupAuthAndAuthSuccess.
@Test
public void testDiscoveryLookupAuthAndAuthSuccess() throws Exception {
// (1) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setBindOnLocalhost(true);
// add Authentication Provider
Set<String> providersClassNames = Sets.newHashSet(MockAuthenticationProvider.class.getName());
config.setAuthenticationProviders(providersClassNames);
// enable authentication and authorization
config.setAuthenticationEnabled(true);
config.setAuthorizationEnabled(true);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (2) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrl();
// set authentication data
Authentication auth = new Authentication() {
private static final long serialVersionUID = 1L;
@Override
public void close() throws IOException {
}
@Override
public String getAuthMethodName() {
return "auth";
}
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
return new AuthenticationDataProvider() {
private static final long serialVersionUID = 1L;
};
}
@Override
public void configure(Map<String, String> authParams) {
}
@Override
public void start() throws PulsarClientException {
}
};
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).build();
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic("persistent://my-property/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://my-property/use2/my-ns/my-topic1").create();
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message<byte[]> 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();
producer.close();
}
use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.
the class BrokerServiceLookupTest method testDiscoveryLookupTls.
/**
* Verify discovery-service binary-proto lookup using tls
*
* @throws Exception
*/
@SuppressWarnings("deprecation")
@Test
public void testDiscoveryLookupTls() throws Exception {
final String TLS_SERVER_CERT_FILE_PATH = "./src/test/resources/certificate/server.crt";
final String TLS_SERVER_KEY_FILE_PATH = "./src/test/resources/certificate/server.key";
final String TLS_CLIENT_CERT_FILE_PATH = "./src/test/resources/certificate/client.crt";
final String TLS_CLIENT_KEY_FILE_PATH = "./src/test/resources/certificate/client.key";
// (1) restart broker1 with tls enabled
conf.setTlsAllowInsecureConnection(true);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
stopBroker();
startBroker();
// (2) start discovery service
ServiceConfig config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setServicePortTls(nextFreePort());
config.setTlsEnabled(true);
config.setBindOnLocalhost(true);
config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
DiscoveryService discoveryService = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
discoveryService.start();
// (3) lookup using discovery service
final String discoverySvcUrl = discoveryService.getServiceUrlTls();
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
Authentication auth = new AuthenticationTls();
auth.configure(authParams);
PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(discoverySvcUrl).authentication(auth).enableTls(true).allowTlsInsecureConnection(true).build();
Consumer<byte[]> consumer = pulsarClient2.newConsumer().topic("persistent://my-property2/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
Producer<byte[]> producer = pulsarClient2.newProducer().topic("persistent://my-property2/use2/my-ns/my-topic1").create();
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message<byte[]> 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();
producer.close();
}
use of org.apache.pulsar.discovery.service.DiscoveryService in project incubator-pulsar by apache.
the class BaseDiscoveryTestSetup method setup.
protected void setup() throws Exception {
config = new ServiceConfig();
config.setServicePort(nextFreePort());
config.setServicePortTls(nextFreePort());
config.setBindOnLocalhost(true);
config.setTlsEnabled(true);
config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
mockZookKeeper = createMockZooKeeper();
service = spy(new DiscoveryService(config));
doReturn(mockZooKeeperClientFactory).when(service).getZooKeeperClientFactory();
service.start();
}
Aggregations