Search in sources :

Example 1 with SshAgentHttpsHandler

use of com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler in project helios by spotify.

the class HttpsHandlersTest method testSshAgent.

@Test
public void testSshAgent() throws Exception {
    final byte[] random = new byte[255];
    new Random().nextBytes(random);
    final AgentProxy proxy = mock(AgentProxy.class);
    final Identity identity = mock(Identity.class);
    when(identity.getKeyBlob()).thenReturn(random);
    when(proxy.sign(any(Identity.class), any(byte[].class))).thenAnswer(new Answer<byte[]>() {

        @Override
        public byte[] answer(InvocationOnMock invocation) throws Throwable {
            final byte[] bytesToSign = (byte[]) invocation.getArguments()[1];
            return sha1digest(bytesToSign);
        }
    });
    final SshAgentHttpsHandler h = new SshAgentHttpsHandler("foo", true, proxy, identity);
    final CertificateAndPrivateKey pair = h.createCertificateAndPrivateKey();
    assertNotNull(pair);
    assertNotNull(pair.getCertificate());
    assertNotNull(pair.getPrivateKey());
}
Also used : SshAgentHttpsHandler(com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler) Random(java.util.Random) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AgentProxy(com.spotify.sshagentproxy.AgentProxy) Identity(com.spotify.sshagentproxy.Identity) CertificateAndPrivateKey(com.spotify.helios.client.tls.CertificateAndPrivateKey) Test(org.junit.Test)

Example 2 with SshAgentHttpsHandler

use of com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler in project helios by spotify.

the class AuthenticatingHttpConnector method connectWithIdentities.

private HttpURLConnection connectWithIdentities(final List<Identity> identities, final URI uri, final String method, final byte[] entity, final Map<String, List<String>> headers) throws IOException, HeliosException {
    if (identities.isEmpty()) {
        throw new IllegalArgumentException("identities cannot be empty");
    }
    final Queue<Identity> queue = new LinkedList<>(identities);
    HttpURLConnection connection = null;
    while (!queue.isEmpty()) {
        final Identity identity = queue.poll();
        delegate.setExtraHttpsHandler(new SshAgentHttpsHandler(user, false, agentProxy.get(), identity));
        connection = doConnect(uri, method, entity, headers);
        // check the status and retry the request if necessary
        final int responseCode = connection.getResponseCode();
        final boolean retryResponse = responseCode == HTTP_FORBIDDEN || responseCode == HTTP_UNAUTHORIZED;
        if (retryResponse && !queue.isEmpty()) {
            // there was some sort of security error. if we have any more SSH identities to try,
            // retry with the next available identity
            log.debug("retrying with next SSH identity since {} failed", identity == null ? "the previous one" : identity.getComment());
            continue;
        }
        break;
    }
    return connection;
}
Also used : SshAgentHttpsHandler(com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler) HttpURLConnection(java.net.HttpURLConnection) Identity(com.spotify.sshagentproxy.Identity) LinkedList(java.util.LinkedList)

Aggregations

SshAgentHttpsHandler (com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler)2 Identity (com.spotify.sshagentproxy.Identity)2 CertificateAndPrivateKey (com.spotify.helios.client.tls.CertificateAndPrivateKey)1 AgentProxy (com.spotify.sshagentproxy.AgentProxy)1 HttpURLConnection (java.net.HttpURLConnection)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1