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");
}
}
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));
}
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));
}
}
}
}
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();
}
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));
}
Aggregations