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