Search in sources :

Example 21 with ClientSslConfigBuilder

use of io.servicetalk.transport.api.ClientSslConfigBuilder in project servicetalk by apple.

the class SniTest method noSniClientDefaultServerFallbackSuccess.

@ParameterizedTest(name = "protocols={0}, alpn={1}")
@MethodSource("protocolsAndAlpn")
void noSniClientDefaultServerFallbackSuccess(List<HttpProtocol> protocols, boolean useALPN) throws Exception {
    try (ServerContext serverContext = HttpServers.forAddress(localAddress(0)).protocols(protocolConfigs(protocols)).sslConfig(trustedServerConfig(alpnIds(protocols, useALPN)), singletonMap("no_match" + SNI_HOSTNAME, untrustedServerConfig())).listenBlockingAndAwait(newSslVerifyService());
        BlockingHttpClient client = HttpClients.forSingleAddress(getLoopbackAddress().getHostName(), serverHostAndPort(serverContext).port()).protocols(protocolConfigs(protocols)).sslConfig(configureAlpn(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()), protocols, useALPN).build()).inferSniHostname(false).buildBlocking()) {
        HttpRequest request = client.get("/");
        ReservedBlockingHttpConnection reserved = client.reserveConnection(request);
        assertNotNull(reserved.connectionContext().sslSession());
        assertEquals(HttpResponseStatus.OK, reserved.request(request).status());
        reserved.release();
    }
}
Also used : HttpRequest(io.servicetalk.http.api.HttpRequest) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ReservedBlockingHttpConnection(io.servicetalk.http.api.ReservedBlockingHttpConnection) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with ClientSslConfigBuilder

use of io.servicetalk.transport.api.ClientSslConfigBuilder in project servicetalk by apple.

the class SslProvidersTest method setUp.

private void setUp(SslProvider serverSslProvider, SslProvider clientSslProvider, int payloadLength) throws Exception {
    payloadBody = randomString(payloadLength);
    serverContext = HttpServers.forAddress(localAddress(0)).sslConfig(new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).provider(serverSslProvider).build()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        assertThat(ctx.sslSession(), is(notNullValue()));
        assertThat(request.path(), is("/path"));
        assertThat(request.headers().get(CONTENT_TYPE), is(TEXT_PLAIN_UTF_8));
        assertThat(request.payloadBody(textSerializerUtf8()), is("request-payload-body-" + payloadBody));
        return responseFactory.ok().payloadBody("response-payload-body-" + payloadBody, textSerializerUtf8());
    });
    client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(NettyIoExecutors.createIoExecutor("client-io")).sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).peerHost(serverPemHostname()).provider(clientSslProvider).build()).buildBlocking();
}
Also used : ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) OPENSSL(io.servicetalk.transport.api.SslProvider.OPENSSL) SslProvider(io.servicetalk.transport.api.SslProvider) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) JDK(io.servicetalk.transport.api.SslProvider.JDK) MethodSource(org.junit.jupiter.params.provider.MethodSource) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) NettyIoExecutors(io.servicetalk.transport.netty.NettyIoExecutors) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) HttpResponse(io.servicetalk.http.api.HttpResponse) Arguments(org.junit.jupiter.params.provider.Arguments) OK(io.servicetalk.http.api.HttpResponseStatus.OK) DefaultTestCerts.serverPemHostname(io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) TEXT_PLAIN_UTF_8(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) Matchers.is(org.hamcrest.Matchers.is) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder)

Example 23 with ClientSslConfigBuilder

use of io.servicetalk.transport.api.ClientSslConfigBuilder in project servicetalk by apple.

the class TcpFastOpenTest method newClient.

private static BlockingHttpClient newClient(final ServerContext serverContext, final Collection<HttpProtocol> protocols, final boolean secure, @SuppressWarnings("rawtypes") final Map<SocketOption, Object> clientOptions) {
    SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(toConfigs(protocols));
    if (secure) {
        builder.sslConfig(new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).sniHostname(serverPemHostname()).build());
    }
    for (@SuppressWarnings("rawtypes") Entry<SocketOption, Object> entry : clientOptions.entrySet()) {
        @SuppressWarnings("unchecked") SocketOption<Object> option = entry.getKey();
        builder.socketOption(option, entry.getValue());
    }
    return builder.buildBlocking();
}
Also used : AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) SocketOption(java.net.SocketOption) InetSocketAddress(java.net.InetSocketAddress) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder)

Example 24 with ClientSslConfigBuilder

use of io.servicetalk.transport.api.ClientSslConfigBuilder in project servicetalk by apple.

the class Tls13Test method requiredCipher.

@ParameterizedTest
@MethodSource("sslProviders")
void requiredCipher(SslProvider serverSslProvider, SslProvider clientSslProvider, @Nullable String cipher) throws Exception {
    ServerSslConfigBuilder serverSslBuilder = new ServerSslConfigBuilder(DefaultTestCerts::loadServerPem, DefaultTestCerts::loadServerKey).sslProtocols(TLS1_3).provider(serverSslProvider);
    if (cipher != null) {
        serverSslBuilder.ciphers(singletonList(cipher));
    }
    try (ServerContext serverContext = forAddress(localAddress(0)).ioExecutor(SERVER_CTX.ioExecutor()).executor(SERVER_CTX.executor()).executionStrategy(defaultStrategy()).enableWireLogging("servicetalk-tests-wire-logger", TRACE, () -> false).sslConfig(serverSslBuilder.build()).listenBlockingAndAwait((ctx, request, responseFactory) -> {
        assertThat(request.payloadBody(textSerializerUtf8()), equalTo("request-payload-body"));
        SslConfig sslConfig = ctx.sslConfig();
        assertThat(sslConfig, is(notNullValue()));
        assertThat(sslConfig.sslProtocols(), contains(TLS1_3));
        SSLSession sslSession = ctx.sslSession();
        assertThat(sslSession, is(notNullValue()));
        return responseFactory.ok().payloadBody(sslSession.getProtocol(), textSerializerUtf8());
    })) {
        ClientSslConfigBuilder clientSslBuilder = new ClientSslConfigBuilder(DefaultTestCerts::loadServerCAPem).sslProtocols(TLS1_3).peerHost(serverPemHostname()).provider(clientSslProvider);
        if (cipher != null) {
            clientSslBuilder.ciphers(singletonList(cipher));
        }
        try (BlockingHttpClient client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).ioExecutor(CLIENT_CTX.ioExecutor()).executor(CLIENT_CTX.executor()).executionStrategy(defaultStrategy()).enableWireLogging("servicetalk-tests-wire-logger", TRACE, Boolean.FALSE::booleanValue).sslConfig(clientSslBuilder.build()).buildBlocking();
            BlockingHttpConnection connection = client.reserveConnection(client.get("/"))) {
            SslConfig sslConfig = connection.connectionContext().sslConfig();
            assertThat(sslConfig, is(notNullValue()));
            assertThat(sslConfig.sslProtocols(), contains(TLS1_3));
            SSLSession sslSession = connection.connectionContext().sslSession();
            assertThat(sslSession, is(notNullValue()));
            assertThat(sslSession.getProtocol(), equalTo(TLS1_3));
            if (cipher != null) {
                assertThat(sslSession.getCipherSuite(), equalTo(cipher));
            }
            HttpResponse response = client.request(client.post("/").payloadBody("request-payload-body", textSerializerUtf8()));
            assertThat(response.status(), is(OK));
            assertThat(response.headers().get(CONTENT_TYPE), is(TEXT_PLAIN_UTF_8));
            assertThat(response.payloadBody(textSerializerUtf8()), equalTo(TLS1_3));
        }
    }
}
Also used : SslConfig(io.servicetalk.transport.api.SslConfig) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpConnection(io.servicetalk.http.api.BlockingHttpConnection) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) SSLSession(javax.net.ssl.SSLSession) HttpResponse(io.servicetalk.http.api.HttpResponse) DefaultTestCerts(io.servicetalk.test.resources.DefaultTestCerts) ClientSslConfigBuilder(io.servicetalk.transport.api.ClientSslConfigBuilder) ServerSslConfigBuilder(io.servicetalk.transport.api.ServerSslConfigBuilder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ClientSslConfigBuilder (io.servicetalk.transport.api.ClientSslConfigBuilder)24 DefaultTestCerts (io.servicetalk.test.resources.DefaultTestCerts)20 ServerContext (io.servicetalk.transport.api.ServerContext)16 ServerSslConfigBuilder (io.servicetalk.transport.api.ServerSslConfigBuilder)13 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)11 HostAndPort (io.servicetalk.transport.api.HostAndPort)9 AddressUtils.localAddress (io.servicetalk.transport.netty.internal.AddressUtils.localAddress)9 AddressUtils.serverHostAndPort (io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort)9 Test (org.junit.jupiter.api.Test)9 DefaultTestCerts.serverPemHostname (io.servicetalk.test.resources.DefaultTestCerts.serverPemHostname)8 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)8 HttpResponse (io.servicetalk.http.api.HttpResponse)7 Matchers.is (org.hamcrest.Matchers.is)7 Nullable (javax.annotation.Nullable)6 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)6 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 InetSocketAddress (java.net.InetSocketAddress)5 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)5 AfterEach (org.junit.jupiter.api.AfterEach)5