use of org.apache.pulsar.client.admin.PulsarAdminBuilder in project pulsar by apache.
the class PulsarStandalone method start.
public void start() throws Exception {
if (config == null) {
log.error("Failed to load configuration");
System.exit(1);
}
log.debug("--- setup PulsarStandaloneStarter ---");
if (!this.isOnlyBroker()) {
ServerConfiguration bkServerConf = new ServerConfiguration();
bkServerConf.loadConf(new File(configFile).toURI().toURL());
// Start LocalBookKeeper
bkEnsemble = new LocalBookkeeperEnsemble(this.getNumOfBk(), this.getZkPort(), this.getBkPort(), this.getStreamStoragePort(), this.getZkDir(), this.getBkDir(), this.isWipeData(), "127.0.0.1");
bkEnsemble.startStandalone(bkServerConf, !this.isNoStreamStorage());
}
if (this.isNoBroker()) {
return;
}
// initialize the functions worker
if (!this.isNoFunctionsWorker()) {
workerConfig = PulsarService.initializeWorkerConfigFromBrokerConfig(config, this.getFnWorkerConfigFile());
// worker talks to local broker
if (this.isNoStreamStorage()) {
// only set the state storage service url when state is enabled.
workerConfig.setStateStorageServiceUrl(null);
} else if (workerConfig.getStateStorageServiceUrl() == null) {
workerConfig.setStateStorageServiceUrl("bk://127.0.0.1:" + this.getStreamStoragePort());
}
fnWorkerService = WorkerServiceLoader.load(workerConfig);
} else {
workerConfig = new WorkerConfig();
}
// Start Broker
broker = new PulsarService(config, workerConfig, Optional.ofNullable(fnWorkerService), (exitCode) -> {
log.info("Halting standalone process with code {}", exitCode);
LogManager.shutdown();
Runtime.getRuntime().halt(exitCode);
});
broker.start();
final String cluster = config.getClusterName();
final AdvertisedListener internalListener = ServiceConfigurationUtils.getInternalListener(config);
if (!config.isTlsEnabled()) {
checkArgument(config.getWebServicePort().isPresent(), "webServicePort must be present");
checkArgument(internalListener.getBrokerServiceUrl() != null, "plaintext must be configured on internal listener");
URL webServiceUrl = new URL(String.format("http://%s:%d", ServiceConfigurationUtils.getWebServiceAddress(config), config.getWebServicePort().get()));
admin = PulsarAdmin.builder().serviceHttpUrl(webServiceUrl.toString()).authentication(config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters()).build();
ClusterData clusterData = ClusterData.builder().serviceUrl(webServiceUrl.toString()).brokerServiceUrl(internalListener.getBrokerServiceUrl().toString()).build();
createSampleNameSpace(clusterData, cluster);
} else {
checkArgument(config.getWebServicePortTls().isPresent(), "webServicePortTls must be present");
checkArgument(internalListener.getBrokerServiceUrlTls() != null, "TLS must be configured on internal listener");
URL webServiceUrlTls = new URL(String.format("https://%s:%d", ServiceConfigurationUtils.getWebServiceAddress(config), config.getWebServicePortTls().get()));
PulsarAdminBuilder builder = PulsarAdmin.builder().serviceHttpUrl(webServiceUrlTls.toString()).authentication(config.getBrokerClientAuthenticationPlugin(), config.getBrokerClientAuthenticationParameters());
// set trust store if needed.
if (config.isBrokerClientTlsEnabled()) {
if (config.isBrokerClientTlsEnabledWithKeyStore()) {
builder.useKeyStoreTls(true).tlsTrustStoreType(config.getBrokerClientTlsTrustStoreType()).tlsTrustStorePath(config.getBrokerClientTlsTrustStore()).tlsTrustStorePassword(config.getBrokerClientTlsTrustStorePassword());
} else {
builder.tlsTrustCertsFilePath(config.getBrokerClientTrustCertsFilePath());
}
builder.allowTlsInsecureConnection(config.isTlsAllowInsecureConnection());
}
admin = builder.build();
ClusterData clusterData = ClusterData.builder().serviceUrlTls(webServiceUrlTls.toString()).brokerServiceUrlTls(internalListener.getBrokerServiceUrlTls().toString()).build();
createSampleNameSpace(clusterData, cluster);
}
// create default namespace
createNameSpace(cluster, TopicName.PUBLIC_TENANT, TopicName.PUBLIC_TENANT + "/" + TopicName.DEFAULT_NAMESPACE);
// create pulsar system namespace
createNameSpace(cluster, SYSTEM_NAMESPACE.getTenant(), SYSTEM_NAMESPACE.toString());
if (config.isTransactionCoordinatorEnabled() && !admin.namespaces().getTopics(SYSTEM_NAMESPACE.toString()).contains(TRANSACTION_COORDINATOR_ASSIGN.getPartition(0).toString())) {
admin.topics().createPartitionedTopic(TRANSACTION_COORDINATOR_ASSIGN.toString(), 1);
}
log.debug("--- setup completed ---");
}
use of org.apache.pulsar.client.admin.PulsarAdminBuilder in project pulsar by apache.
the class BrokerService method getClusterPulsarAdmin.
public PulsarAdmin getClusterPulsarAdmin(String cluster, Optional<ClusterData> clusterDataOp) {
PulsarAdmin admin = clusterAdmins.get(cluster);
if (admin != null) {
return admin;
}
return clusterAdmins.computeIfAbsent(cluster, key -> {
try {
ClusterData data = clusterDataOp.orElseThrow(() -> new MetadataStoreException.NotFoundException(cluster));
ServiceConfiguration conf = pulsar.getConfig();
boolean isTlsUrl = conf.isBrokerClientTlsEnabled() && isNotBlank(data.getServiceUrlTls());
String adminApiUrl = isTlsUrl ? data.getServiceUrlTls() : data.getServiceUrl();
PulsarAdminBuilder builder = PulsarAdmin.builder().serviceHttpUrl(adminApiUrl).authentication(conf.getBrokerClientAuthenticationPlugin(), conf.getBrokerClientAuthenticationParameters());
if (isTlsUrl) {
builder.allowTlsInsecureConnection(conf.isTlsAllowInsecureConnection());
if (conf.isBrokerClientTlsEnabledWithKeyStore()) {
builder.useKeyStoreTls(true).tlsTrustStoreType(conf.getBrokerClientTlsTrustStoreType()).tlsTrustStorePath(conf.getBrokerClientTlsTrustStore()).tlsTrustStorePassword(conf.getBrokerClientTlsTrustStorePassword());
} else {
builder.tlsTrustCertsFilePath(conf.getBrokerClientTrustCertsFilePath());
}
}
// most of the admin request requires to make zk-call so, keep the max read-timeout based on
// zk-operation timeout
builder.readTimeout(conf.getMetadataStoreOperationTimeoutSeconds(), TimeUnit.SECONDS);
PulsarAdmin adminClient = builder.build();
log.info("created admin with url {} ", adminApiUrl);
return adminClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
use of org.apache.pulsar.client.admin.PulsarAdminBuilder in project pulsar by apache.
the class WebServiceTest method setupEnv.
private void setupEnv(boolean enableFilter, String minApiVersion, boolean allowUnversionedClients, boolean enableTls, boolean enableAuth, boolean allowInsecure, double rateLimit, boolean disableTrace) throws Exception {
if (pulsar != null) {
throw new Exception("broker already started");
}
Set<String> providers = new HashSet<>();
providers.add("org.apache.pulsar.broker.authentication.AuthenticationProviderTls");
Set<String> roles = new HashSet<>();
roles.add("client");
ServiceConfiguration config = new ServiceConfiguration();
config.setAdvertisedAddress("localhost");
config.setBrokerShutdownTimeoutMs(0L);
config.setBrokerServicePort(Optional.of(0));
config.setWebServicePort(Optional.of(0));
if (enableTls) {
config.setWebServicePortTls(Optional.of(0));
}
config.setClientLibraryVersionCheckEnabled(enableFilter);
config.setAuthenticationEnabled(enableAuth);
config.setAuthenticationProviders(providers);
config.setAuthorizationEnabled(false);
config.setSuperUserRoles(roles);
config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
config.setTlsAllowInsecureConnection(allowInsecure);
config.setTlsTrustCertsFilePath(allowInsecure ? "" : TLS_CLIENT_CERT_FILE_PATH);
config.setClusterName("local");
// TLS certificate expects localhost
config.setAdvertisedAddress("localhost");
config.setMetadataStoreUrl("zk:localhost:2181");
config.setHttpMaxRequestSize(10 * 1024);
config.setDisableHttpDebugMethods(disableTrace);
if (rateLimit > 0) {
config.setHttpRequestsLimitEnabled(true);
config.setHttpRequestsMaxPerSecond(rateLimit);
}
pulsar = spyWithClassAndConstructorArgs(PulsarService.class, config);
// mock zk
MockZooKeeper mockZooKeeper = MockedPulsarServiceBaseTest.createMockZooKeeper();
doReturn(new ZKMetadataStore(mockZooKeeper)).when(pulsar).createConfigurationMetadataStore();
doReturn(new ZKMetadataStore(mockZooKeeper)).when(pulsar).createLocalMetadataStore();
doReturn(new MockedBookKeeperClientFactory()).when(pulsar).newBookKeeperClientFactory();
pulsar.start();
try {
pulsar.getLocalMetadataStore().delete("/minApiVersion", Optional.empty()).join();
} catch (Exception ex) {
}
pulsar.getLocalMetadataStore().put("/minApiVersion", minApiVersion.getBytes(), Optional.of(-1L)).join();
String BROKER_URL_BASE = "http://localhost:" + pulsar.getListenPortHTTP().get();
String BROKER_URL_BASE_TLS = "https://localhost:" + pulsar.getListenPortHTTPS().orElse(-1);
String serviceUrl = BROKER_URL_BASE;
PulsarAdminBuilder adminBuilder = PulsarAdmin.builder();
if (enableTls && enableAuth) {
serviceUrl = BROKER_URL_BASE_TLS;
Map<String, String> authParams = new HashMap<>();
authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
adminBuilder.authentication(AuthenticationTls.class.getName(), authParams).allowTlsInsecureConnection(true);
}
BROKER_LOOKUP_URL = BROKER_URL_BASE + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";
BROKER_LOOKUP_URL_TLS = BROKER_URL_BASE_TLS + "/lookup/v2/destination/persistent/my-property/local/my-namespace/my-topic";
PulsarAdmin pulsarAdmin = adminBuilder.serviceHttpUrl(serviceUrl).build();
try {
pulsarAdmin.clusters().createCluster(config.getClusterName(), ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress()).build());
} catch (ConflictException ce) {
// This is OK.
} finally {
pulsarAdmin.close();
}
}
use of org.apache.pulsar.client.admin.PulsarAdminBuilder in project pulsar by apache.
the class PulsarAdminTool method main.
public static void main(String[] args) throws Exception {
lastExitCode = 0;
if (args.length == 0) {
System.out.println("Usage: pulsar-admin CONF_FILE_PATH [options] [command] [command options]");
exit(0);
return;
}
String configFile = args[0];
Properties properties = new Properties();
if (configFile != null) {
try (FileInputStream fis = new FileInputStream(configFile)) {
properties.load(fis);
}
}
PulsarAdminTool tool = new PulsarAdminTool(properties);
int cmdPos;
args = Arrays.copyOfRange(args, 1, args.length);
for (cmdPos = 0; cmdPos < args.length; cmdPos++) {
if (tool.commandMap.containsKey(args[cmdPos])) {
break;
}
}
++cmdPos;
boolean isLocalRun = cmdPos < args.length && "localrun".equalsIgnoreCase(args[cmdPos]);
Function<PulsarAdminBuilder, ? extends PulsarAdmin> adminFactory;
if (isLocalRun) {
// bypass constructing admin client
adminFactory = (adminBuilder) -> null;
} else {
adminFactory = (adminBuilder) -> {
try {
return adminBuilder.build();
} catch (Exception ex) {
System.err.println(ex.getClass() + ": " + ex.getMessage());
exit(1);
return null;
}
};
}
if (tool.run(args, adminFactory)) {
exit(0);
} else {
exit(1);
}
}
use of org.apache.pulsar.client.admin.PulsarAdminBuilder in project pulsar by apache.
the class TopicsAuthTest method setup.
@Override
@BeforeMethod
protected void setup() throws Exception {
// enable auth&auth and use JWT at broker
conf.setAuthenticationEnabled(true);
conf.setAuthorizationEnabled(true);
conf.getProperties().setProperty("tokenSecretKey", "data:;base64," + Base64.getEncoder().encodeToString(SECRET_KEY.getEncoded()));
Set<String> superUserRoles = new HashSet<>();
superUserRoles.add("admin");
conf.setSuperUserRoles(superUserRoles);
Set<String> providers = new HashSet<>();
providers.add(AuthenticationProviderToken.class.getName());
conf.setAuthenticationProviders(providers);
super.internalSetup();
PulsarAdminBuilder pulsarAdminBuilder = PulsarAdmin.builder().serviceHttpUrl(brokerUrl != null ? brokerUrl.toString() : brokerUrlTls.toString()).authentication(AuthenticationToken.class.getName(), ADMIN_TOKEN);
admin = Mockito.spy(pulsarAdminBuilder.build());
admin.clusters().createCluster(testLocalCluster, new ClusterDataImpl());
admin.tenants().createTenant(testTenant, new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet(testLocalCluster)));
admin.namespaces().createNamespace(testTenant + "/" + testNamespace, Sets.newHashSet(testLocalCluster));
admin.namespaces().grantPermissionOnNamespace(testTenant + "/" + testNamespace, "producer", EnumSet.of(AuthAction.produce));
admin.namespaces().grantPermissionOnNamespace(testTenant + "/" + testNamespace, "consumer", EnumSet.of(AuthAction.consume));
}
Aggregations