@Test public void https_rejectUpstreamBadCertificate() throws Exception { System.out.println("https_rejectUpstreamBadCertificate"); System.out.println("expect some big exception stack traces in the logs for this one"); Path temporaryDirectory = temporaryFolder.newFolder().toPath(); File harFile = VhsTests.getHttpsExampleHarFile(temporaryDirectory); EntryMatcherFactory entryMatcherFactory = HeuristicEntryMatcher.factory(new BasicHeuristic(), BasicHeuristic.DEFAULT_THRESHOLD_EXCLUSIVE); int port = VhsTests.findOpenPort(); TestContext context = new TestContext() .put(KEY_TLS_MODE, TlsMode.SUPPORT_REQUIRED) .put(KEY_CLIENT_SUPPLIER, BlindlyTrustingClient.supplier()); BrowsermobVhsConfig config = createServerConfig(port, harFile, entryMatcherFactory, context); URI goodUrl = URI.create("https: List<URI> urls = Arrays.asList(goodUrl, selfSignedUrl); PassthruPredicate passthruPredicate = new PassthruPredicate() { @Override public boolean isForwardable(HttpRequest originalRequest, @Nullable ChannelHandlerContext ctx) { String host = getHost(originalRequest); return urls.stream().map(URI::getHost).anyMatch(host::equals); } }; VirtualHarServer server = new BrowsermobVirtualHarServer(config) { @Override protected PassthruPredicate createPassthruPredicate() { return passthruPredicate; } }; Multimap<URI, ResponseSummary> responses; try (VirtualHarServerControl ctrl = server.start()) { Supplier<ApacheRecordingClient> clientFactory = context.get(KEY_CLIENT_SUPPLIER); ApacheRecordingClient client = clientFactory.get(); responses = client.collectResponses(urls, ctrl.getSocketAddress()); } assertEquals("expect 1 response per URL in " + urls, urls.size(), responses.size()); assertTrue("all URLs represented in response map", responses.keySet().containsAll(urls)); ResponseSummary badSslResponse = responses.get(selfSignedUrl).iterator().next(); assertEquals("status " + badSslResponse.statusLine, HttpStatus.SC_BAD_GATEWAY, badSslResponse.statusLine.getStatusCode()); ResponseSummary goodResponse = responses.get(goodUrl).iterator().next(); assertEquals("status from good URL response", HttpStatus.SC_OK, goodResponse.statusLine.getStatusCode()); }
@Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); @Override VirtualHarServerControl start(); }
BrowsermobVirtualHarServer implements VirtualHarServer { @Override public VirtualHarServerControl start() throws IOException { List<Closeable> closeables = new ArrayList<>(); Scratch scratch = config.scratchDirProvider.createScratchDir(); closeables.add(scratch); CertificateAndKeySource certificateAndKeySource; BrowserUpProxy proxy; Path scratchPath = scratch.getRoot(); try { certificateAndKeySource = config.certificateAndKeySourceFactory.produce(config, scratchPath); TlsEndpoint httpsInterceptionServer = config.tlsEndpointFactory.produce(config, scratchPath); closeables.add(httpsInterceptionServer); TrustSource trustSource = httpsInterceptionServer.getTrustSource(); proxy = startProxy(config.bmpResponseManufacturer.withFreshState(), httpsInterceptionServer.getSocketAddress(), certificateAndKeySource, trustSource); } catch (RuntimeException | IOException e) { closeAll(closeables, true); throw e; } return new BrowsermobVhsControl(proxy, closeables); } BrowsermobVirtualHarServer(BrowsermobVhsConfig config); @Override VirtualHarServerControl start();  }