Search in sources :

Example 1 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder in project cdap by caskdata.

the class SecureStoreTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    cConf.set(Constants.Security.Store.PROVIDER, "file");
    SConfiguration sConf = SConfiguration.create();
    sConf.set(Constants.Security.Store.FILE_PASSWORD, "secret");
    Injector injector = Guice.createInjector(new ConfigModule(cConf, new Configuration(), sConf), new SecureStoreModules().getInMemoryModules(), new AuthorizationTestModule(), new AuthenticationContextModules().getNoOpModule(), new AbstractModule() {

        @Override
        protected void configure() {
            bind(AuthorizationEnforcer.class).to(NoOpAuthorizer.class);
            bind(NamespaceAdmin.class).to(InMemoryNamespaceClient.class).in(Scopes.SINGLETON);
            bind(NamespaceQueryAdmin.class).to(NamespaceAdmin.class);
        }
    });
    injector.getInstance(NamespaceAdmin.class).create(NamespaceMeta.DEFAULT);
    httpServer = new CommonNettyHttpServiceBuilder(injector.getInstance(CConfiguration.class), "SecureStore").setHttpHandlers(Collections.singleton(injector.getInstance(SecureStoreHandler.class))).build();
    httpServer.start();
}
Also used : CConfiguration(co.cask.cdap.common.conf.CConfiguration) SConfiguration(co.cask.cdap.common.conf.SConfiguration) Configuration(org.apache.hadoop.conf.Configuration) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) ConfigModule(co.cask.cdap.common.guice.ConfigModule) AuthenticationContextModules(co.cask.cdap.security.auth.context.AuthenticationContextModules) SecureStoreModules(co.cask.cdap.security.guice.SecureStoreModules) NamespaceAdmin(co.cask.cdap.common.namespace.NamespaceAdmin) NoOpAuthorizer(co.cask.cdap.security.spi.authorization.NoOpAuthorizer) CConfiguration(co.cask.cdap.common.conf.CConfiguration) AuthorizationTestModule(co.cask.cdap.security.authorization.AuthorizationTestModule) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) SConfiguration(co.cask.cdap.common.conf.SConfiguration) BeforeClass(org.junit.BeforeClass)

Example 2 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder in project cdap by caskdata.

the class WebappProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    try {
        ProgramType processorType = program.getType();
        Preconditions.checkNotNull(processorType, "Missing processor type");
        Preconditions.checkArgument(processorType == ProgramType.WEBAPP, "Only WEBAPP process type is supported");
        LOG.info("Initializing Webapp for app {} with jar {}", program.getApplicationId(), program.getJarLocation().getName());
        String serviceName = getServiceName(program.getId());
        Preconditions.checkNotNull(serviceName, "Cannot determine service name for program %s", program.getName());
        LOG.info("Got service name {}", serviceName);
        // Start netty server
        // TODO: add metrics reporting
        JarHttpHandler jarHttpHandler = webappHttpHandlerFactory.createHandler(program.getJarLocation());
        NettyHttpService.Builder builder = new CommonNettyHttpServiceBuilder(cConf, program.getId().toString());
        builder.setHttpHandlers(jarHttpHandler);
        builder.setUrlRewriter(new WebappURLRewriter(jarHttpHandler));
        builder.setHost(hostname.getCanonicalHostName());
        NettyHttpService httpService = builder.build();
        httpService.start();
        final InetSocketAddress address = httpService.getBindAddress();
        RunId runId = ProgramRunners.getRunId(options);
        // Register service, and the serving host names.
        final List<Cancellable> cancellables = Lists.newArrayList();
        LOG.info("Webapp {} running on address {} registering as {}", program.getApplicationId(), address, serviceName);
        cancellables.add(serviceAnnouncer.announce(serviceName, address.getPort()));
        for (String hname : getServingHostNames(Locations.newInputSupplier(program.getJarLocation()))) {
            final String sname = ProgramType.WEBAPP.name().toLowerCase() + "/" + hname;
            LOG.info("Webapp {} running on address {} registering as {}", program.getApplicationId(), address, sname);
            cancellables.add(discoveryService.register(ResolvingDiscoverable.of(new Discoverable(sname, address))));
        }
        return new WebappProgramController(program.getId().run(runId), httpService, new Cancellable() {

            @Override
            public void cancel() {
                for (Cancellable cancellable : cancellables) {
                    cancellable.cancel();
                }
            }
        });
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.twill.common.Cancellable) NettyHttpService(co.cask.http.NettyHttpService) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId)

Example 3 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder in project cdap by caskdata.

the class MetadataService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting Metadata Service");
    metadataUpgrader.createOrUpgradeIfNecessary();
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.METADATA_SERVICE).setHttpHandlers(handlers).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.METADATA_SERVICE))).setHost(cConf.get(Constants.Metadata.SERVICE_BIND_ADDRESS)).setPort(cConf.getInt(Constants.Metadata.SERVICE_BIND_PORT)).setWorkerThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.Metadata.SERVICE_EXEC_THREADS)).setConnectionBacklog(20000).build();
    httpService.start();
    InetSocketAddress socketAddress = httpService.getBindAddress();
    LOG.info("Metadata service running at {}", socketAddress);
    cancelDiscovery = discoveryService.register(ResolvingDiscoverable.of(new Discoverable(Constants.Service.METADATA_SERVICE, socketAddress)));
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) ResolvingDiscoverable(co.cask.cdap.common.discovery.ResolvingDiscoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder in project cdap by caskdata.

the class MessagingHttpService method startUp.

@Override
protected void startUp() throws Exception {
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.MESSAGING_SERVICE).setHost(cConf.get(Constants.MessagingSystem.HTTP_SERVER_BIND_ADDRESS)).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.MESSAGING_SERVICE))).setWorkerThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_EXECUTOR_THREADS)).setHttpChunkLimit(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_MAX_REQUEST_SIZE_MB) * 1024 * 1024).setExceptionHandler(new HttpExceptionHandler() {

        @Override
        public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
            // TODO: CDAP-7688. Override the handling to return 400 on IllegalArgumentException
            if (t instanceof IllegalArgumentException) {
                logWithTrace(request, t);
                responder.sendString(HttpResponseStatus.BAD_REQUEST, t.getMessage());
            } else {
                super.handle(t, request, responder);
            }
        }

        private void logWithTrace(HttpRequest request, Throwable t) {
            LOG.trace("Error in handling request={} {} for user={}:", request.method().name(), request.uri(), Objects.firstNonNull(SecurityRequestContext.getUserId(), "<null>"), t);
        }
    }).setHttpHandlers(handlers).build();
    httpService.start();
    cancelDiscovery = discoveryService.register(new Discoverable(Constants.Service.MESSAGING_SERVICE, httpService.getBindAddress()));
    LOG.info("Messaging HTTP server started on {}", httpService.getBindAddress());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpResponder(co.cask.http.HttpResponder) Discoverable(org.apache.twill.discovery.Discoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) HttpExceptionHandler(co.cask.cdap.common.HttpExceptionHandler)

Example 5 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder 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(notificationService.start(), applicationLifecycleService.start(), systemArtifactLoader.start(), programRuntimeService.start(), streamCoordinatorClient.start(), programNotificationSubscriberService.start(), programLifecycleService.start(), runRecordCorrectorService.start(), pluginService.start(), coreSchedulerService.start())).get();
    // Create handler hooks
    ImmutableList.Builder<HandlerHook> builder = ImmutableList.builder();
    for (String hook : handlerHookNames) {
        builder.add(new MetricsReporterHook(metricsCollectionService, hook));
    }
    // Run http service on random port
    NettyHttpService.Builder httpServiceBuilder = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.APP_FABRIC_HTTP).setHost(hostname.getCanonicalHostName()).setHandlerHooks(builder.build()).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));
    if (sslEnabled) {
        httpServiceBuilder.setPort(cConf.getInt(Constants.AppFabric.SERVER_SSL_PORT));
        String password = generateRandomPassword();
        KeyStore ks = KeyStores.generatedCertKeyStore(sConf, password);
        SSLHandlerFactory sslHandlerFactory = new SSLHandlerFactory(ks, password);
        httpServiceBuilder.enableSSL(sslHandlerFactory);
    } else {
        httpServiceBuilder.setPort(cConf.getInt(Constants.AppFabric.SERVER_PORT));
    }
    cancelHttpService = startHttpService(httpServiceBuilder.build());
    defaultNamespaceEnsurer.startAndWait();
    if (appVersionUpgradeService != null) {
        appVersionUpgradeService.startAndWait();
    }
}
Also used : MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) ImmutableList(com.google.common.collect.ImmutableList) NettyHttpService(co.cask.http.NettyHttpService) HandlerHook(co.cask.http.HandlerHook) SSLHandlerFactory(co.cask.cdap.security.tools.SSLHandlerFactory) ServiceLoggingContext(co.cask.cdap.common.logging.ServiceLoggingContext) KeyStore(java.security.KeyStore)

Aggregations

CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)7 MetricsReporterHook (co.cask.cdap.common.metrics.MetricsReporterHook)3 NettyHttpService (co.cask.http.NettyHttpService)3 Discoverable (org.apache.twill.discovery.Discoverable)3 AuthorizationClient (co.cask.cdap.client.AuthorizationClient)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)2 MasterAuthenticationContext (co.cask.cdap.security.auth.context.MasterAuthenticationContext)2 AuthorizerInstantiator (co.cask.cdap.security.authorization.AuthorizerInstantiator)2 InMemoryAuthorizer (co.cask.cdap.security.authorization.InMemoryAuthorizer)2 Authorizer (co.cask.cdap.security.spi.authorization.Authorizer)2 InetSocketAddress (java.net.InetSocketAddress)2 FeatureDisabledException (co.cask.cdap.common.FeatureDisabledException)1 HttpExceptionHandler (co.cask.cdap.common.HttpExceptionHandler)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 SConfiguration (co.cask.cdap.common.conf.SConfiguration)1 ConfigModule (co.cask.cdap.common.guice.ConfigModule)1 AuthenticationChannelHandler (co.cask.cdap.common.http.AuthenticationChannelHandler)1 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)1