Search in sources :

Example 71 with SslConnectionFactory

use of org.eclipse.jetty.server.SslConnectionFactory in project nifi by apache.

the class TestHttpClient method setup.

@BeforeClass
public static void setup() throws Exception {
    // Create embedded Jetty server
    // Use less threads to mitigate Gateway Timeout (504) with proxy test
    // Minimum thread pool size = (acceptors=2 + selectors=8 + request=1), defaults to max=200
    final QueuedThreadPool threadPool = new QueuedThreadPool(50);
    server = new Server(threadPool);
    final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
    final ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setContextPath("/nifi-api");
    final ServletContextHandler wrongPathContextHandler = new ServletContextHandler();
    wrongPathContextHandler.setContextPath("/wrong/nifi-api");
    handlerCollection.setHandlers(new Handler[] { contextHandler, wrongPathContextHandler });
    server.setHandler(handlerCollection);
    final ServletHandler servletHandler = new ServletHandler();
    contextHandler.insertHandler(servletHandler);
    final ServletHandler wrongPathServletHandler = new ServletHandler();
    wrongPathContextHandler.insertHandler(wrongPathServletHandler);
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/certs/localhost-ks.jks");
    sslContextFactory.setKeyStorePassword("localtest");
    sslContextFactory.setKeyStoreType("JKS");
    httpConnector = new ServerConnector(server);
    final HttpConfiguration https = new HttpConfiguration();
    https.addCustomizer(new SecureRequestCustomizer());
    sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, "http/1.1"), new HttpConnectionFactory(https));
    server.setConnectors(new Connector[] { httpConnector, sslConnector });
    wrongPathServletHandler.addServletWithMapping(WrongSiteInfoServlet.class, "/site-to-site");
    servletHandler.addServletWithMapping(SiteInfoServlet.class, "/site-to-site");
    servletHandler.addServletWithMapping(PeersServlet.class, "/site-to-site/peers");
    servletHandler.addServletWithMapping(PortTransactionsAccessDeniedServlet.class, "/data-transfer/input-ports/input-access-denied-id/transactions");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-running-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-running-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesServlet.class, "/data-transfer/input-ports/input-running-id/transactions/transaction-id/flow-files");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutServlet.class, "/data-transfer/input-ports/input-timeout-id/transactions/transaction-id/flow-files");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions");
    servletHandler.addServletWithMapping(InputPortTransactionServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutAfterDataExchangeServlet.class, "/data-transfer/input-ports/input-timeout-data-ex-id/transactions/transaction-id/flow-files");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-running-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-running-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesServlet.class, "/data-transfer/output-ports/output-running-id/transactions/transaction-id/flow-files");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutServlet.class, "/data-transfer/output-ports/output-timeout-id/transactions/transaction-id/flow-files");
    servletHandler.addServletWithMapping(PortTransactionsServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions");
    servletHandler.addServletWithMapping(OutputPortTransactionServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions/transaction-id");
    servletHandler.addServletWithMapping(FlowFilesTimeoutAfterDataExchangeServlet.class, "/data-transfer/output-ports/output-timeout-data-ex-id/transactions/transaction-id/flow-files");
    server.start();
    logger.info("Starting server on port {} for HTTP, and {} for HTTPS", httpConnector.getLocalPort(), sslConnector.getLocalPort());
    startProxyServer();
    startProxyServerWithAuth();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) DefaultHttpProxyServer(org.littleshoot.proxy.impl.DefaultHttpProxyServer) Server(org.eclipse.jetty.server.Server) HttpProxyServer(org.littleshoot.proxy.HttpProxyServer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) BeforeClass(org.junit.BeforeClass)

Example 72 with SslConnectionFactory

use of org.eclipse.jetty.server.SslConnectionFactory in project athenz by yahoo.

the class InstanceProviderContainer method run.

public void run() {
    try {
        QueuedThreadPool threadPool = new QueuedThreadPool();
        threadPool.setMaxThreads(16);
        Server server = new Server(threadPool);
        ServletContextHandler handler = new ServletContextHandler();
        handler.setContextPath("");
        ResourceConfig config = new ResourceConfig(InstanceProviderResources.class).register(new Binder());
        handler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
        server.setHandler(handler);
        // SSL Context Factory
        SslContextFactory sslContextFactory = createSSLContextObject();
        // SSL HTTP Configuration
        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(10043);
        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
        httpsConfig.addCustomizer(new SecureRequestCustomizer());
        // SSL Connector
        ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
        sslConnector.setPort(10043);
        server.addConnector(sslConnector);
        server.start();
        server.join();
    } catch (Exception e) {
        System.err.println("*** " + e);
    }
}
Also used : SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ServerConnector(org.eclipse.jetty.server.ServerConnector) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 73 with SslConnectionFactory

use of org.eclipse.jetty.server.SslConnectionFactory in project felix by apache.

the class JettyService method initializeHttps.

private boolean initializeHttps() {
    HttpConnectionFactory connFactory = new HttpConnectionFactory();
    configureHttpConnectionFactory(connFactory);
    SslContextFactory sslContextFactory = new SslContextFactory();
    configureSslContextFactory(sslContextFactory);
    ServerConnector connector = new ServerConnector(server, config.getAcceptors(), config.getSelectors(), new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()), connFactory);
    HttpConfiguration httpConfiguration = connFactory.getHttpConfiguration();
    httpConfiguration.addCustomizer(new SecureRequestCustomizer());
    if (this.config.isProxyLoadBalancerConnection()) {
        httpConfiguration.addCustomizer(customizerWrapper);
    }
    configureConnector(connector, this.config.getHttpsPort());
    return startConnector(connector);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 74 with SslConnectionFactory

use of org.eclipse.jetty.server.SslConnectionFactory in project cxf by apache.

the class JettyHTTPServerEngine method createConnectorJetty.

AbstractConnector createConnectorJetty(SslContextFactory sslcf, String hosto, int porto, int major, int minor) {
    AbstractConnector result = null;
    try {
        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSendServerVersion(getSendServerVersion());
        HttpConnectionFactory httpFactory = new HttpConnectionFactory(httpConfig);
        Collection<ConnectionFactory> connectionFactories = new ArrayList<>();
        result = new org.eclipse.jetty.server.ServerConnector(server);
        if (tlsServerParameters != null) {
            httpConfig.addCustomizer(new org.eclipse.jetty.server.SecureRequestCustomizer());
            SslConnectionFactory scf = new SslConnectionFactory(sslcf, "HTTP/1.1");
            connectionFactories.add(scf);
            String proto = (major > 9 || (major == 9 && minor >= 3)) ? "SSL" : "SSL-HTTP/1.1";
            result.setDefaultProtocol(proto);
        }
        connectionFactories.add(httpFactory);
        result.setConnectionFactories(connectionFactories);
        if (getMaxIdleTime() > 0) {
            result.setIdleTimeout(Long.valueOf(getMaxIdleTime()));
        }
    } catch (RuntimeException rex) {
        throw rex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return result;
}
Also used : HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ConnectionFactory(org.eclipse.jetty.server.ConnectionFactory) ServerConnector(org.eclipse.jetty.server.ServerConnector) AbstractConnector(org.eclipse.jetty.server.AbstractConnector)

Example 75 with SslConnectionFactory

use of org.eclipse.jetty.server.SslConnectionFactory in project drill by axbaretto.

the class WebServer method createHttpsConnector.

/**
 * Create an HTTPS connector for given jetty server instance. If the admin has specified keystore/truststore settings
 * they will be used else a self-signed certificate is generated and used.
 *
 * @return Initialized {@link ServerConnector} for HTTPS connections.
 * @throws Exception
 */
private ServerConnector createHttpsConnector(int port, int acceptors, int selectors) throws Exception {
    logger.info("Setting up HTTPS connector for web server");
    final SslContextFactory sslContextFactory = new SslContextFactory();
    SSLConfig ssl = new SSLConfigBuilder().config(config).mode(SSLConfig.Mode.SERVER).initializeSSLContext(false).validateKeyStore(true).build();
    if (ssl.isSslValid()) {
        logger.info("Using configured SSL settings for web server");
        sslContextFactory.setKeyStorePath(ssl.getKeyStorePath());
        sslContextFactory.setKeyStorePassword(ssl.getKeyStorePassword());
        sslContextFactory.setKeyManagerPassword(ssl.getKeyPassword());
        if (ssl.hasTrustStorePath()) {
            sslContextFactory.setTrustStorePath(ssl.getTrustStorePath());
            if (ssl.hasTrustStorePassword()) {
                sslContextFactory.setTrustStorePassword(ssl.getTrustStorePassword());
            }
        }
    } else {
        logger.info("Using generated self-signed SSL settings for web server");
        final SecureRandom random = new SecureRandom();
        // Generate a private-public key pair
        final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024, random);
        final KeyPair keyPair = keyPairGenerator.generateKeyPair();
        final DateTime now = DateTime.now();
        // Create builder for certificate attributes
        final X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE).addRDN(BCStyle.OU, "Apache Drill (auth-generated)").addRDN(BCStyle.O, "Apache Software Foundation (auto-generated)").addRDN(BCStyle.CN, workManager.getContext().getEndpoint().getAddress());
        final Date notBefore = now.minusMinutes(1).toDate();
        final Date notAfter = now.plusYears(5).toDate();
        final BigInteger serialNumber = new BigInteger(128, random);
        // Create a certificate valid for 5years from now.
        final X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(// attributes
        nameBuilder.build(), serialNumber, notBefore, notAfter, nameBuilder.build(), keyPair.getPublic());
        // Sign the certificate using the private key
        final ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(keyPair.getPrivate());
        final X509Certificate certificate = new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner));
        // Check the validity
        certificate.checkValidity(now.toDate());
        // Make sure the certificate is self-signed.
        certificate.verify(certificate.getPublicKey());
        // Generate a random password for keystore protection
        final String keyStorePasswd = RandomStringUtils.random(20);
        final KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(null, null);
        keyStore.setKeyEntry("DrillAutoGeneratedCert", keyPair.getPrivate(), keyStorePasswd.toCharArray(), new java.security.cert.Certificate[] { certificate });
        sslContextFactory.setKeyStore(keyStore);
        sslContextFactory.setKeyStorePassword(keyStorePasswd);
    }
    final HttpConfiguration httpsConfig = new HttpConfiguration();
    httpsConfig.addCustomizer(new SecureRequestCustomizer());
    // SSL Connector
    final ServerConnector sslConnector = new ServerConnector(embeddedJetty, null, null, null, acceptors, selectors, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(httpsConfig));
    sslConnector.setPort(port);
    return sslConnector;
}
Also used : X500NameBuilder(org.bouncycastle.asn1.x500.X500NameBuilder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) DateTime(org.joda.time.DateTime) ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) SSLConfig(org.apache.drill.exec.ssl.SSLConfig) KeyPair(java.security.KeyPair) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ContentSigner(org.bouncycastle.operator.ContentSigner) SecureRandom(java.security.SecureRandom) SSLConfigBuilder(org.apache.drill.exec.ssl.SSLConfigBuilder) KeyPairGenerator(java.security.KeyPairGenerator) KeyStore(java.security.KeyStore) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) X509v3CertificateBuilder(org.bouncycastle.cert.X509v3CertificateBuilder) BigInteger(java.math.BigInteger)

Aggregations

SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)106 ServerConnector (org.eclipse.jetty.server.ServerConnector)101 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)96 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)90 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)87 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)82 Server (org.eclipse.jetty.server.Server)56 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)19 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)17 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)16 IOException (java.io.IOException)15 File (java.io.File)14 ConnectionFactory (org.eclipse.jetty.server.ConnectionFactory)11 ServletException (javax.servlet.ServletException)10 HTTP2ServerConnectionFactory (org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory)10 MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)9 Connector (org.eclipse.jetty.server.Connector)9 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)9 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)9 ArrayList (java.util.ArrayList)8