Search in sources :

Example 21 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestHttpServerProvider method testHttps.

@Test
public void testHttps() throws Exception {
    config.setHttpEnabled(false).setHttpsEnabled(true);
    httpsConfig.setKeystorePath(getResource("test.keystore.with.two.passwords").getPath()).setKeystorePassword("airlift").setKeyManagerPassword("airliftkey").setAutomaticHttpsSharedSecret("shared-secret");
    createAndStartServer();
    HttpClientConfig http1ClientConfig = new HttpClientConfig().setHttp2Enabled(false).setTrustStorePath(getResource("test.truststore").getPath()).setTrustStorePassword("airlift").setAutomaticHttpsSharedSecret("shared-secret");
    try (JettyHttpClient httpClient = createJettyClient(http1ClientConfig)) {
        verifyHttps(httpClient, "localhost");
        verifyHttps(httpClient, "127-0-0-1.ip");
        verifyHttps(httpClient, "x--1.ip");
    }
}
Also used : HttpClientConfig(io.airlift.http.client.HttpClientConfig) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Test(org.testng.annotations.Test)

Example 22 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestHttpServerProvider method assertForward.

private void assertForward(ForwardedServlet servlet, Optional<String> proto, Optional<String> host, Optional<String> remoteHost) {
    servlet.reset();
    HttpUriBuilder uriBuilder = HttpUriBuilder.uriBuilderFrom(httpServerInfo.getHttpUri()).replacePath("/some/path");
    try (HttpClient client = new JettyHttpClient()) {
        Builder builder = prepareGet().setUri(uriBuilder.build());
        proto.ifPresent(value -> builder.addHeader(X_FORWARDED_PROTO, value));
        host.ifPresent(value -> builder.addHeader(X_FORWARDED_HOST, value));
        remoteHost.ifPresent(value -> builder.addHeader(X_FORWARDED_FOR, value));
        StringResponse response = client.execute(builder.build(), createStringResponseHandler());
        assertEquals(response.getStatusCode(), 200);
    }
    proto.ifPresent(uriBuilder::scheme);
    host.map(HostAndPort::fromString).ifPresent(uriBuilder::hostAndPort);
    URI forwardedUri = uriBuilder.build();
    assertEquals(servlet.getRequestUrl(), forwardedUri.toString());
    assertEquals(servlet.getScheme(), forwardedUri.getScheme());
    assertEquals(servlet.getIsSecure(), (Boolean) forwardedUri.getScheme().equals("https"));
    remoteHost.ifPresent(value -> assertEquals(servlet.getRemoteAddress(), value));
}
Also used : JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) HttpClient(io.airlift.http.client.HttpClient) HttpUriBuilder(io.airlift.http.client.HttpUriBuilder) Builder(io.airlift.http.client.Request.Builder) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) StringResponse(io.airlift.http.client.StringResponseHandler.StringResponse) URI(java.net.URI)

Example 23 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestJettyMultipleCerts method test.

@Test
public void test() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setStatus(200);
            response.getOutputStream().write((request.getServerName() + " OK").getBytes(UTF_8));
        }
    };
    HttpServerConfig config = new HttpServerConfig().setLogEnabled(false).setHttpEnabled(false).setHttpPort(0).setHttpsEnabled(true);
    HttpsConfig httpsConfig = new HttpsConfig().setHttpsPort(0).setKeystorePath(getResource("multiple-certs/server.p12").getPath()).setKeystorePassword("airlift");
    HashLoginServiceProvider loginServiceProvider = new HashLoginServiceProvider(config);
    NodeInfo nodeInfo = new NodeInfo(new NodeConfig().setEnvironment("test").setNodeInternalAddress("localhost"));
    HttpServerInfo httpServerInfo = new HttpServerInfo(config, Optional.of(httpsConfig), nodeInfo);
    HttpServerProvider serverProvider = new HttpServerProvider(httpServerInfo, nodeInfo, config, Optional.of(httpsConfig), servlet, ImmutableSet.of(new DummyFilter()), ImmutableSet.of(), ImmutableSet.of(), ClientCertificate.NONE, new RequestStats(), new NullEventClient(), Optional.empty());
    serverProvider.setTheAdminServlet(new DummyServlet());
    serverProvider.setLoginService(loginServiceProvider.get());
    serverProvider.setTokenManager(new TraceTokenManager());
    try (Closer closer = Closer.create()) {
        HttpServer server = serverProvider.get();
        closer.register(() -> {
            try {
                server.stop();
            } catch (Exception ignore) {
            }
        });
        server.start();
        JettyHttpClient client = new JettyHttpClient(new HttpClientConfig().setTrustStorePath(getResource("multiple-certs/server.p12").getPath()).setTrustStorePassword("airlift"));
        closer.register(client);
        int httpsPort = httpServerInfo.getHttpsUri().getPort();
        tryHost(client, HostAndPort.fromParts("localhost", httpsPort));
        for (String name : List.of("127.0.0.1", "::1")) {
            assertThatThrownBy(() -> tryHost(client, HostAndPort.fromParts(name, httpsPort))).hasMessageStartingWith(name + " Failed communicating with server").hasRootCauseMessage("No subject alternative names present");
        }
        // 
        for (String name : List.of("single1", "single2", "single3", "single4")) {
            if (doesDomainResolveToLocalhost(name)) {
                tryHost(client, HostAndPort.fromParts(name, httpsPort));
            }
        }
    }
}
Also used : Closer(com.google.common.io.Closer) HttpClientConfig(io.airlift.http.client.HttpClientConfig) NullEventClient(io.airlift.event.client.NullEventClient) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeInfo(io.airlift.node.NodeInfo) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) NodeConfig(io.airlift.node.NodeConfig) Test(org.testng.annotations.Test)

Example 24 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestMBeanResource method setup.

@BeforeClass
public void setup() {
    Bootstrap app = new Bootstrap(new TestingNodeModule(), new TestingHttpServerModule(), new JsonModule(), new JaxrsModule(), new JmxHttpModule(), binder -> binder.bind(MBeanServer.class).toInstance(mbeanServer));
    Injector injector = app.quiet().initialize();
    lifeCycleManager = injector.getInstance(LifeCycleManager.class);
    server = injector.getInstance(TestingHttpServer.class);
    client = new JettyHttpClient();
}
Also used : LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) TestingHttpServerModule(io.airlift.http.server.testing.TestingHttpServerModule) TestingNodeModule(io.airlift.node.testing.TestingNodeModule) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) JaxrsModule(io.airlift.jaxrs.JaxrsModule) TestingHttpServer(io.airlift.http.server.testing.TestingHttpServer) Bootstrap(io.airlift.bootstrap.Bootstrap) JsonModule(io.airlift.json.JsonModule) BeforeClass(org.testng.annotations.BeforeClass)

Example 25 with JettyHttpClient

use of io.airlift.http.client.jetty.JettyHttpClient in project airlift by airlift.

the class TestHttpClientBinder method testConfigDefaults.

@Test
public void testConfigDefaults() {
    Injector injector = new Bootstrap(binder -> httpClientBinder(binder).bindHttpClient("foo", FooClient.class).withConfigDefaults(config -> config.setRequestTimeout(new Duration(33, MINUTES))), new TraceTokenModule()).quiet().initialize();
    JettyHttpClient httpClient = (JettyHttpClient) injector.getInstance(Key.get(HttpClient.class, FooClient.class));
    assertEquals(httpClient.getRequestTimeoutMillis(), MINUTES.toMillis(33));
}
Also used : Assert.assertSame(org.testng.Assert.assertSame) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Assertions.assertInstanceOf(io.airlift.testing.Assertions.assertInstanceOf) Key(com.google.inject.Key) LifeCycleManager(io.airlift.bootstrap.LifeCycleManager) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) Target(java.lang.annotation.Target) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Assert.assertNotNull(org.testng.Assert.assertNotNull) ElementType(java.lang.annotation.ElementType) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Duration(io.airlift.units.Duration) Retention(java.lang.annotation.Retention) Injector(com.google.inject.Injector) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) HttpClientBinder.httpClientBinder(io.airlift.http.client.HttpClientBinder.httpClientBinder) ImmutableList(com.google.common.collect.ImmutableList) Assert.assertNotSame(org.testng.Assert.assertNotSame) Bootstrap(io.airlift.bootstrap.Bootstrap) Qualifier(javax.inject.Qualifier) Assert.assertTrue(org.testng.Assert.assertTrue) Assert.assertFalse(org.testng.Assert.assertFalse) Injector(com.google.inject.Injector) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) Bootstrap(io.airlift.bootstrap.Bootstrap) Duration(io.airlift.units.Duration) TraceTokenModule(io.airlift.tracetoken.TraceTokenModule) Test(org.testng.annotations.Test)

Aggregations

JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)29 Test (org.testng.annotations.Test)21 HttpClient (io.airlift.http.client.HttpClient)11 HttpClientConfig (io.airlift.http.client.HttpClientConfig)11 Injector (com.google.inject.Injector)10 Bootstrap (io.airlift.bootstrap.Bootstrap)10 Duration (io.airlift.units.Duration)9 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)8 TestingNodeModule (io.airlift.node.testing.TestingNodeModule)8 StatusResponse (io.airlift.http.client.StatusResponseHandler.StatusResponse)7 StringResponse (io.airlift.http.client.StringResponseHandler.StringResponse)7 URI (java.net.URI)7 ImmutableMap (com.google.common.collect.ImmutableMap)5 NodeInfo (io.airlift.node.NodeInfo)4 InMemoryEventModule (io.airlift.event.client.InMemoryEventModule)3 TheServlet (io.airlift.http.server.TheServlet)3 TraceTokenModule (io.airlift.tracetoken.TraceTokenModule)3 File (java.io.File)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3