@Test public void testAccessToken_UsesAgentIdentities() throws Exception { final AgentProxy proxy = mock(AgentProxy.class); final Identity identity = mockIdentity(); final AuthenticatingHttpConnector authConnector = createAuthenticatingConnectorWithAccessToken( Optional.of(proxy), ImmutableList.of(identity)); final String path = "/foo/bar"; final HttpsURLConnection connection = mock(HttpsURLConnection.class); when(connector.connect(argThat(matchesAnyEndpoint(path)), eq(method), eq(entity), argThat(hasKeys(Collections.singletonList("Authorization")))) ).thenReturn(connection); when(connection.getResponseCode()).thenReturn(200); final URI uri = new URI("https: final HttpURLConnection returnedConnection = authConnector.connect(uri, method, entity, headers); assertSame(returnedConnection, connection); verify(connector).setExtraHttpsHandler(isA(HttpsHandler.class)); }
@Override public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException { final Endpoint endpoint = endpointIterator.next(); final URI ipUri; try { ipUri = toIpUri(endpoint, uri); } catch (URISyntaxException e) { throw new HeliosException(e); } try { log.debug("connecting to {}", ipUri); final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get(); if (accessTokenOpt.isPresent()) { final String token = accessTokenOpt.get().getTokenValue(); headers.put("Authorization", singletonList("Bearer " + token)); log.debug("Add Authorization header with bearer token"); } if (clientCertificatePath.isPresent()) { return connectWithCertificateFile(ipUri, method, entity, headers); } else if (agentProxy.isPresent() && !identities.isEmpty()) { return connectWithIdentities(identities, ipUri, method, entity, headers); } else { return doConnect(ipUri, method, entity, headers); } } catch (ConnectException | SocketTimeoutException | UnknownHostException e) { log.debug(e.toString()); throw new HeliosException("Unable to connect to master: " + ipUri, e); } catch (IOException e) { throw new HeliosException("Unexpected error connecting to " + ipUri, e); } }
AuthenticatingHttpConnector implements HttpConnector { @Override public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException { final Endpoint endpoint = endpointIterator.next(); final URI ipUri; try { ipUri = toIpUri(endpoint, uri); } catch (URISyntaxException e) { throw new HeliosException(e); } try { log.debug("connecting to {}", ipUri); final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get(); if (accessTokenOpt.isPresent()) { final String token = accessTokenOpt.get().getTokenValue(); headers.put("Authorization", singletonList("Bearer " + token)); log.debug("Add Authorization header with bearer token"); } if (clientCertificatePath.isPresent()) { return connectWithCertificateFile(ipUri, method, entity, headers); } else if (agentProxy.isPresent() && !identities.isEmpty()) { return connectWithIdentities(identities, ipUri, method, entity, headers); } else { return doConnect(ipUri, method, entity, headers); } } catch (ConnectException | SocketTimeoutException | UnknownHostException e) { log.debug(e.toString()); throw new HeliosException("Unable to connect to master: " + ipUri, e); } catch (IOException e) { throw new HeliosException("Unexpected error connecting to " + ipUri, e); } } }
AuthenticatingHttpConnector implements HttpConnector { @Override public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException { final Endpoint endpoint = endpointIterator.next(); final URI ipUri; try { ipUri = toIpUri(endpoint, uri); } catch (URISyntaxException e) { throw new HeliosException(e); } try { log.debug("connecting to {}", ipUri); final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get(); if (accessTokenOpt.isPresent()) { final String token = accessTokenOpt.get().getTokenValue(); headers.put("Authorization", singletonList("Bearer " + token)); log.debug("Add Authorization header with bearer token"); } if (clientCertificatePath.isPresent()) { return connectWithCertificateFile(ipUri, method, entity, headers); } else if (agentProxy.isPresent() && !identities.isEmpty()) { return connectWithIdentities(identities, ipUri, method, entity, headers); } else { return doConnect(ipUri, method, entity, headers); } } catch (ConnectException | SocketTimeoutException | UnknownHostException e) { log.debug(e.toString()); throw new HeliosException("Unable to connect to master: " + ipUri, e); } catch (IOException e) { throw new HeliosException("Unexpected error connecting to " + ipUri, e); } } AuthenticatingHttpConnector(final String user,
                                     final Supplier<Optional<AccessToken>> accessTokenSupplier,
                                     final Optional<AgentProxy> agentProxyOpt,
                                     final Optional<CertKeyPaths> clientCertificatePath,
                                     final EndpointIterator endpointIterator,
                                     final DefaultHttpConnector delegate); @VisibleForTesting  AuthenticatingHttpConnector(final String user,
                              final Supplier<Optional<AccessToken>> accessTokenSupplier,
                              final Optional<AgentProxy> agentProxyOpt,
                              final Optional<CertKeyPaths> clientCertificatePath,
                              final EndpointIterator endpointIterator,
                              final DefaultHttpConnector delegate,
                              final List<Identity> identities); }
AuthenticatingHttpConnector implements HttpConnector { @Override public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException { final Endpoint endpoint = endpointIterator.next(); final URI ipUri; try { ipUri = toIpUri(endpoint, uri); } catch (URISyntaxException e) { throw new HeliosException(e); } try { log.debug("connecting to {}", ipUri); final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get(); if (accessTokenOpt.isPresent()) { final String token = accessTokenOpt.get().getTokenValue(); headers.put("Authorization", singletonList("Bearer " + token)); log.debug("Add Authorization header with bearer token"); } if (clientCertificatePath.isPresent()) { return connectWithCertificateFile(ipUri, method, entity, headers); } else if (agentProxy.isPresent() && !identities.isEmpty()) { return connectWithIdentities(identities, ipUri, method, entity, headers); } else { return doConnect(ipUri, method, entity, headers); } } catch (ConnectException | SocketTimeoutException | UnknownHostException e) { log.debug(e.toString()); throw new HeliosException("Unable to connect to master: " + ipUri, e); } catch (IOException e) { throw new HeliosException("Unexpected error connecting to " + ipUri, e); } } AuthenticatingHttpConnector(final String user,
                                     final Supplier<Optional<AccessToken>> accessTokenSupplier,
                                     final Optional<AgentProxy> agentProxyOpt,
                                     final Optional<CertKeyPaths> clientCertificatePath,
                                     final EndpointIterator endpointIterator,
                                     final DefaultHttpConnector delegate); @VisibleForTesting  AuthenticatingHttpConnector(final String user,
                              final Supplier<Optional<AccessToken>> accessTokenSupplier,
                              final Optional<AgentProxy> agentProxyOpt,
                              final Optional<CertKeyPaths> clientCertificatePath,
                              final EndpointIterator endpointIterator,
                              final DefaultHttpConnector delegate,
                              final List<Identity> identities); @Override HttpURLConnection connect(final URI uri, final String method, final byte[] entity,
                                   final Map<String, List<String>> headers); @Override void close(); }
AuthenticatingHttpConnector implements HttpConnector { @Override public HttpURLConnection connect(final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws HeliosException { final Endpoint endpoint = endpointIterator.next(); final URI ipUri; try { ipUri = toIpUri(endpoint, uri); } catch (URISyntaxException e) { throw new HeliosException(e); } try { log.debug("connecting to {}", ipUri); final Optional<AccessToken> accessTokenOpt = accessTokenSupplier.get(); if (accessTokenOpt.isPresent()) { final String token = accessTokenOpt.get().getTokenValue(); headers.put("Authorization", singletonList("Bearer " + token)); log.debug("Add Authorization header with bearer token"); } if (clientCertificatePath.isPresent()) { return connectWithCertificateFile(ipUri, method, entity, headers); } else if (agentProxy.isPresent() && !identities.isEmpty()) { return connectWithIdentities(identities, ipUri, method, entity, headers); } else { return doConnect(ipUri, method, entity, headers); } } catch (ConnectException | SocketTimeoutException | UnknownHostException e) { log.debug(e.toString()); throw new HeliosException("Unable to connect to master: " + ipUri, e); } catch (IOException e) { throw new HeliosException("Unexpected error connecting to " + ipUri, e); } } AuthenticatingHttpConnector(final String user,
                                     final Supplier<Optional<AccessToken>> accessTokenSupplier,
                                     final Optional<AgentProxy> agentProxyOpt,
                                     final Optional<CertKeyPaths> clientCertificatePath,
                                     final EndpointIterator endpointIterator,
                                     final DefaultHttpConnector delegate); @VisibleForTesting  AuthenticatingHttpConnector(final String user,
                              final Supplier<Optional<AccessToken>> accessTokenSupplier,
                              final Optional<AgentProxy> agentProxyOpt,
                              final Optional<CertKeyPaths> clientCertificatePath,
                              final EndpointIterator endpointIterator,
                              final DefaultHttpConnector delegate,
                              final List<Identity> identities); @Override HttpURLConnection connect(final URI uri, final String method, final byte[] entity,
                                   final Map<String, List<String>> headers); @Override void close();  }