use of io.cdap.cdap.common.security.HttpsEnabler in project cdap by cdapio.
the class RuntimeServiceRoutingHandler method openConnection.
/**
* Opens a {@link HttpURLConnection} to the given service for the given program run.
*
* @throws BadRequestException if the request for service routing is not valid
*/
private HttpURLConnection openConnection(HttpRequest request, String namespace, String app, String version, String programType, String program, String run, String service) throws BadRequestException {
ApplicationId appId = new NamespaceId(namespace).app(app, version);
ProgramRunId programRunId = new ProgramRunId(appId, ProgramType.valueOfCategoryName(programType, BadRequestException::new), program, run);
requestValidator.getProgramRunStatus(programRunId, request);
Discoverable discoverable = endpointStrategyLoadingCache.getUnchecked(service).pick(2, TimeUnit.SECONDS);
if (discoverable == null) {
throw new ServiceUnavailableException(service);
}
String prefix = String.format("%s/runtime/namespaces/%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s", Constants.Gateway.INTERNAL_API_VERSION_3, namespace, app, version, programType, program, run, service);
URI uri = URIScheme.createURI(discoverable, request.uri().substring(prefix.length()));
try {
URL url = uri.toURL();
HttpURLConnection urlConn;
try {
urlConn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// If fail to open the connection, treat it as service unavailable so that the client can retry
throw new ServiceUnavailableException(service);
}
if (urlConn instanceof HttpsURLConnection) {
new HttpsEnabler().setTrustAll(true).enable((HttpsURLConnection) urlConn);
}
for (Map.Entry<String, String> header : request.headers().entries()) {
urlConn.setRequestProperty(header.getKey(), header.getValue());
}
urlConn.setRequestMethod(request.method().name());
urlConn.setDoInput(true);
return urlConn;
} catch (MalformedURLException | ProtocolException e) {
// This can only happen if the incoming request is bad
throw new BadRequestException("Invalid request due to " + e.getMessage(), e);
}
}
use of io.cdap.cdap.common.security.HttpsEnabler in project cdap by cdapio.
the class HealthCheckService method initiate.
public void initiate(String host, int port, String serviceName) {
this.serviceName = serviceName;
NettyHttpService.Builder builder = new CommonNettyHttpServiceBuilder(cConf, serviceName).setHttpHandlers(handlers).setHost(host).setPort(port);
if (cConf.getBoolean(Constants.Security.SSL.INTERNAL_ENABLED)) {
new HttpsEnabler().configureKeyStore(cConf, sConf).enable(builder);
}
httpService = builder.build();
}
use of io.cdap.cdap.common.security.HttpsEnabler in project cdap by cdapio.
the class ExternalMTLSAuthenticationServerTest method openConnection.
private HttpsURLConnection openConnection(URL url, String keyStoreResource) throws Exception {
HttpsURLConnection urlConn = (HttpsURLConnection) super.openConnection(url);
URL clientKeystoreURL = ExternalMTLSAuthenticationServerTest.class.getClassLoader().getResource(keyStoreResource);
Assert.assertNotNull(clientKeystoreURL);
KeyStore ks = KeyStore.getInstance("JKS");
try (InputStream is = clientKeystoreURL.openConnection().getInputStream()) {
ks.load(is, "secret".toCharArray());
}
return new HttpsEnabler().setKeyStore(ks, () -> configuration.get("security.auth.server.ssl.keystore.password", "secret").toCharArray()).setTrustAll(true).enable(urlConn);
}
use of io.cdap.cdap.common.security.HttpsEnabler in project cdap by cdapio.
the class AppFabricServer method startUp.
/**
* Configures the AppFabricService pre-start.
*/
@Override
protected void startUp() throws Exception {
LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.APP_FABRIC_HTTP));
Futures.allAsList(ImmutableList.of(provisioningService.start(), applicationLifecycleService.start(), bootstrapService.start(), programRuntimeService.start(), programNotificationSubscriberService.start(), runRecordCorrectorService.start(), coreSchedulerService.start(), eventPublishManager.start(), runRecordCounterService.start())).get();
// Create handler hooks
List<MetricsReporterHook> handlerHooks = handlerHookNames.stream().map(name -> new MetricsReporterHook(metricsCollectionService, name)).collect(Collectors.toList());
// Run http service on random port
NettyHttpService.Builder httpServiceBuilder = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.APP_FABRIC_HTTP).setHost(hostname.getCanonicalHostName()).setHandlerHooks(handlerHooks).setHttpHandlers(handlers).setConnectionBacklog(cConf.getInt(Constants.AppFabric.BACKLOG_CONNECTIONS, Constants.AppFabric.DEFAULT_BACKLOG)).setExecThreadPoolSize(cConf.getInt(Constants.AppFabric.EXEC_THREADS, Constants.AppFabric.DEFAULT_EXEC_THREADS)).setBossThreadPoolSize(cConf.getInt(Constants.AppFabric.BOSS_THREADS, Constants.AppFabric.DEFAULT_BOSS_THREADS)).setWorkerThreadPoolSize(cConf.getInt(Constants.AppFabric.WORKER_THREADS, Constants.AppFabric.DEFAULT_WORKER_THREADS)).setPort(cConf.getInt(Constants.AppFabric.SERVER_PORT));
if (sslEnabled) {
new HttpsEnabler().configureKeyStore(cConf, sConf).enable(httpServiceBuilder);
}
cancelHttpService = startHttpService(httpServiceBuilder.build());
long applicationCount = TransactionRunners.run(transactionRunner, (TxCallable<Long>) context -> AppMetadataStore.create(context).getApplicationCount());
long namespaceCount = new DefaultNamespaceStore(transactionRunner).getNamespaceCount();
metricsCollectionService.getContext(Collections.emptyMap()).gauge(Constants.Metrics.Program.APPLICATION_COUNT, applicationCount);
metricsCollectionService.getContext(Collections.emptyMap()).gauge(Constants.Metrics.Program.NAMESPACE_COUNT, namespaceCount);
}
use of io.cdap.cdap.common.security.HttpsEnabler in project cdap by caskdata.
the class AppFabricServer method startUp.
/**
* Configures the AppFabricService pre-start.
*/
@Override
protected void startUp() throws Exception {
LoggingContextAccessor.setLoggingContext(new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(), Constants.Logging.COMPONENT_NAME, Constants.Service.APP_FABRIC_HTTP));
Futures.allAsList(ImmutableList.of(provisioningService.start(), applicationLifecycleService.start(), bootstrapService.start(), programRuntimeService.start(), programNotificationSubscriberService.start(), runRecordCorrectorService.start(), coreSchedulerService.start(), eventPublishManager.start(), runRecordCounterService.start())).get();
// Create handler hooks
List<MetricsReporterHook> handlerHooks = handlerHookNames.stream().map(name -> new MetricsReporterHook(metricsCollectionService, name)).collect(Collectors.toList());
// Run http service on random port
NettyHttpService.Builder httpServiceBuilder = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.APP_FABRIC_HTTP).setHost(hostname.getCanonicalHostName()).setHandlerHooks(handlerHooks).setHttpHandlers(handlers).setConnectionBacklog(cConf.getInt(Constants.AppFabric.BACKLOG_CONNECTIONS, Constants.AppFabric.DEFAULT_BACKLOG)).setExecThreadPoolSize(cConf.getInt(Constants.AppFabric.EXEC_THREADS, Constants.AppFabric.DEFAULT_EXEC_THREADS)).setBossThreadPoolSize(cConf.getInt(Constants.AppFabric.BOSS_THREADS, Constants.AppFabric.DEFAULT_BOSS_THREADS)).setWorkerThreadPoolSize(cConf.getInt(Constants.AppFabric.WORKER_THREADS, Constants.AppFabric.DEFAULT_WORKER_THREADS)).setPort(cConf.getInt(Constants.AppFabric.SERVER_PORT));
if (sslEnabled) {
new HttpsEnabler().configureKeyStore(cConf, sConf).enable(httpServiceBuilder);
}
cancelHttpService = startHttpService(httpServiceBuilder.build());
long applicationCount = TransactionRunners.run(transactionRunner, (TxCallable<Long>) context -> AppMetadataStore.create(context).getApplicationCount());
long namespaceCount = new DefaultNamespaceStore(transactionRunner).getNamespaceCount();
metricsCollectionService.getContext(Collections.emptyMap()).gauge(Constants.Metrics.Program.APPLICATION_COUNT, applicationCount);
metricsCollectionService.getContext(Collections.emptyMap()).gauge(Constants.Metrics.Program.NAMESPACE_COUNT, namespaceCount);
}
Aggregations