Search in sources :

Example 1 with KeyStoreFactory

use of org.mockserver.socket.tls.KeyStoreFactory in project signers by ConsenSys.

the class HashicorpIntegrationTest method hashicorpVaultReturnsEncryptionKeyOverTls.

@Test
void hashicorpVaultReturnsEncryptionKeyOverTls() throws IOException {
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().getSocketFactory());
    final ClientAndServer clientAndServer = new ClientAndServer(0);
    clientAndServer.when(request().withPath(".*")).respond(response().withStatusCode(200).withBody("{\"data\":{\"data\":{\"value\":\"" + EXPECTED_KEY_STRING + "\"}}}"));
    final Path configFile = createConfigFile(DEFAULT_HOST, clientAndServer.getLocalPort(), ROOT_TOKEN, KEY_PATH, null, TIMEOUT_MILLISECONDS, true, "JKS", keyStoreFactory.keyStoreFileName, KeyStoreFactory.KEY_STORE_PASSWORD);
    final HashicorpKeyConfig keyConfig = TomlConfigLoader.fromToml(configFile, null);
    final HashicorpConnection connection = factory.create(keyConfig.getConnectionParams());
    final String keyFetched = connection.fetchKey(keyConfig.getKeyDefinition());
    assertThat(keyFetched).isEqualTo(EXPECTED_KEY_STRING);
}
Also used : Path(java.nio.file.Path) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) HashicorpKeyConfig(tech.pegasys.signers.hashicorp.config.HashicorpKeyConfig) ClientAndServer(org.mockserver.integration.ClientAndServer) Test(org.junit.jupiter.api.Test)

Example 2 with KeyStoreFactory

use of org.mockserver.socket.tls.KeyStoreFactory in project mockserver by mock-server.

the class AbstractClientAuthenticationMockingIntegrationTest method shouldFailToAuthenticateInHttpApacheClient.

@Test
public void shouldFailToAuthenticateInHttpApacheClient() throws Exception {
    // given
    mockServerClient.when(request().withMethod("POST").withPath(calculatePath("some_path"))).respond(response().withStatusCode(200).withBody("some_body_response"));
    // when
    HttpClient httpClient = HttpClients.custom().setSSLContext(new KeyStoreFactory(configuration(), new MockServerLogger()).sslContext()).build();
    HttpResponse response = httpClient.execute(new HttpPost(new URIBuilder().setScheme("http").setHost("localhost").setPort(getServerPort()).setPath(calculatePath("some_path")).build()));
    String responseBody = new String(EntityUtils.toByteArray(response.getEntity()), StandardCharsets.UTF_8);
    // then
    assertThat(response.getStatusLine().getStatusCode(), is(426));
    assertThat(response.containsHeader("Upgrade"), is(true));
    assertThat(responseBody, is(""));
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) URIBuilder(org.apache.http.client.utils.URIBuilder) Test(org.junit.Test)

Example 3 with KeyStoreFactory

use of org.mockserver.socket.tls.KeyStoreFactory in project mockserver by mock-server.

the class ClientAuthenticationAdditionalCertificateChainMockingIntegrationTest method getSslContext.

private SSLContext getSslContext() {
    KeyAndCertificateFactory keyAndCertificateFactory = KeyAndCertificateFactoryFactory.createKeyAndCertificateFactory(configuration(), new MockServerLogger());
    assertThat(keyAndCertificateFactory, notNullValue());
    keyAndCertificateFactory.buildAndSavePrivateKeyAndX509Certificate();
    return new KeyStoreFactory(configuration(), new MockServerLogger()).sslContext(privateKeyFromPEMFile("org/mockserver/netty/integration/tls/leaf-key-pkcs8.pem"), x509FromPEMFile("org/mockserver/netty/integration/tls/leaf-cert.pem"), x509FromPEMFile("org/mockserver/netty/integration/tls/ca.pem"), new X509Certificate[] { x509FromPEMFile("org/mockserver/netty/integration/tls/ca.pem"), keyAndCertificateFactory.certificateAuthorityX509Certificate() });
}
Also used : KeyAndCertificateFactory(org.mockserver.socket.tls.KeyAndCertificateFactory) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger)

Example 4 with KeyStoreFactory

use of org.mockserver.socket.tls.KeyStoreFactory in project mockserver by mock-server.

the class ServletProxyIntegrationTest method startServer.

@BeforeClass
@SuppressWarnings("deprecation")
public static void startServer() throws Exception {
    servletContext = "";
    // start server
    echoServer = new EchoServer(false);
    // wait for server to start up
    TimeUnit.MILLISECONDS.sleep(500);
    // start proxy (in tomcat)
    tomcat = new Tomcat();
    tomcat.setBaseDir(new File(".").getCanonicalPath() + File.separatorChar + "tomcat" + (servletContext.length() > 0 ? "_" + servletContext : ""));
    // add http port
    tomcat.setPort(PROXY_PORT);
    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setRedirectPort(PROXY_HTTPS_PORT);
    // add https connector
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(configuration(), new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    Connector httpsConnector = new Connector();
    httpsConnector.setPort(PROXY_HTTPS_PORT);
    httpsConnector.setSecure(true);
    httpsConnector.setAttribute("keyAlias", KeyStoreFactory.KEY_STORE_CERT_ALIAS);
    httpsConnector.setAttribute("keystorePass", KeyStoreFactory.KEY_STORE_PASSWORD);
    httpsConnector.setAttribute("keystoreFile", new File(keyStoreFactory.keyStoreFileName).getAbsoluteFile());
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("clientAuth", false);
    httpsConnector.setAttribute("SSLEnabled", true);
    Service service = tomcat.getService();
    service.addConnector(httpsConnector);
    // add servlet
    Context ctx = tomcat.addContext("/" + servletContext, new File(".").getAbsolutePath());
    tomcat.addServlet("/" + servletContext, "mockServerServlet", new ProxyServlet());
    ctx.addServletMappingDecoded("/*", "mockServerServlet");
    ctx.addApplicationListener(ProxyServlet.class.getName());
    // start server
    tomcat.start();
    // start client
    mockServerClient = new MockServerClient("localhost", PROXY_PORT, servletContext);
}
Also used : Context(org.apache.catalina.Context) Connector(org.apache.catalina.connector.Connector) Tomcat(org.apache.catalina.startup.Tomcat) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) ProxyServlet(org.mockserver.proxyservlet.ProxyServlet) Service(org.apache.catalina.Service) MockServerClient(org.mockserver.client.MockServerClient) File(java.io.File) EchoServer(org.mockserver.echo.http.EchoServer) BeforeClass(org.junit.BeforeClass)

Example 5 with KeyStoreFactory

use of org.mockserver.socket.tls.KeyStoreFactory in project mockserver by mock-server.

the class ServletProxyWithContextPathIntegrationTest method startServer.

@BeforeClass
@SuppressWarnings("deprecation")
public static void startServer() throws Exception {
    servletContext = "proxy";
    // start server
    echoServer = new EchoServer(false);
    // wait for server to start up
    TimeUnit.MILLISECONDS.sleep(500);
    // start proxy (in tomcat)
    tomcat = new Tomcat();
    tomcat.setBaseDir(new File(".").getCanonicalPath() + File.separatorChar + "tomcat" + (servletContext.length() > 0 ? "_" + servletContext : ""));
    // add http port
    tomcat.setPort(PROXY_PORT);
    Connector defaultConnector = tomcat.getConnector();
    defaultConnector.setRedirectPort(PROXY_HTTPS_PORT);
    // add https connector
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(configuration(), new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    Connector httpsConnector = new Connector();
    httpsConnector.setPort(PROXY_HTTPS_PORT);
    httpsConnector.setSecure(true);
    httpsConnector.setAttribute("keyAlias", KeyStoreFactory.KEY_STORE_CERT_ALIAS);
    httpsConnector.setAttribute("keystorePass", KeyStoreFactory.KEY_STORE_PASSWORD);
    httpsConnector.setAttribute("keystoreFile", new File(keyStoreFactory.keyStoreFileName).getAbsoluteFile());
    httpsConnector.setAttribute("sslProtocol", "TLS");
    httpsConnector.setAttribute("clientAuth", false);
    httpsConnector.setAttribute("SSLEnabled", true);
    Service service = tomcat.getService();
    service.addConnector(httpsConnector);
    // add servlet
    Context ctx = tomcat.addContext("/" + servletContext, new File(".").getAbsolutePath());
    tomcat.addServlet("/" + servletContext, "mockServerServlet", new ProxyServlet());
    ctx.addServletMappingDecoded("/*", "mockServerServlet");
    ctx.addApplicationListener(ProxyServlet.class.getName());
    // start server
    tomcat.start();
    // start client
    mockServerClient = new MockServerClient("localhost", PROXY_PORT, servletContext);
}
Also used : Context(org.apache.catalina.Context) Connector(org.apache.catalina.connector.Connector) Tomcat(org.apache.catalina.startup.Tomcat) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) ProxyServlet(org.mockserver.proxyservlet.ProxyServlet) Service(org.apache.catalina.Service) MockServerClient(org.mockserver.client.MockServerClient) File(java.io.File) EchoServer(org.mockserver.echo.http.EchoServer) BeforeClass(org.junit.BeforeClass)

Aggregations

MockServerLogger (org.mockserver.logging.MockServerLogger)24 KeyStoreFactory (org.mockserver.socket.tls.KeyStoreFactory)24 HttpResponse (org.apache.http.HttpResponse)10 HttpClient (org.apache.http.client.HttpClient)10 HttpPost (org.apache.http.client.methods.HttpPost)8 URIBuilder (org.apache.http.client.utils.URIBuilder)8 Test (org.junit.Test)8 BeforeClass (org.junit.BeforeClass)6 File (java.io.File)5 Context (org.apache.catalina.Context)5 Service (org.apache.catalina.Service)5 Connector (org.apache.catalina.connector.Connector)5 Tomcat (org.apache.catalina.startup.Tomcat)5 HttpHost (org.apache.http.HttpHost)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 MockServerClient (org.mockserver.client.MockServerClient)4 IOException (java.io.IOException)2 java.net (java.net)2 InetSocketAddress (java.net.InetSocketAddress)2 Proxy (java.net.Proxy)2