Search in sources :

Example 1 with AgentProxy

use of com.spotify.sshagentproxy.AgentProxy 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 AgentProxy

use of com.spotify.sshagentproxy.AgentProxy in project helios by spotify.

the class AuthenticatingHttpConnectorTest method testOneIdentity_ResponseIsUnauthorized.

@Test
public void testOneIdentity_ResponseIsUnauthorized() throws Exception {
    final AgentProxy proxy = mock(AgentProxy.class);
    final Identity identity = mockIdentity();
    final AuthenticatingHttpConnector authConnector = createAuthenticatingConnector(Optional.of(proxy), ImmutableList.of(identity));
    final String path = "/another/one";
    final HttpsURLConnection connection = mock(HttpsURLConnection.class);
    when(connector.connect(argThat(matchesAnyEndpoint(path)), eq(method), eq(entity), eq(headers))).thenReturn(connection);
    when(connection.getResponseCode()).thenReturn(401);
    final URI uri = new URI("https://helios" + path);
    final HttpURLConnection returnedConnection = authConnector.connect(uri, method, entity, headers);
    verify(connector).setExtraHttpsHandler(sshAgentHttpsHandlerWithArgs(USER, proxy, identity));
    assertSame("If there is only one identity do not expect any additional endpoints to " + "be called after the first returns Unauthorized", returnedConnection, connection);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) AgentProxy(com.spotify.sshagentproxy.AgentProxy) Identity(com.spotify.sshagentproxy.Identity) URI(java.net.URI) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 3 with AgentProxy

use of com.spotify.sshagentproxy.AgentProxy in project helios by spotify.

the class AuthenticatingHttpConnectorTest method testTwoIdentities_ResponseIsUnauthorized.

@Test
public void testTwoIdentities_ResponseIsUnauthorized() throws Exception {
    final AgentProxy proxy = mock(AgentProxy.class);
    final Identity id1 = mockIdentity();
    final Identity id2 = mockIdentity();
    final AuthenticatingHttpConnector authConnector = createAuthenticatingConnector(Optional.of(proxy), ImmutableList.of(id1, id2));
    final String path = "/another/one";
    // set up two seperate connect() calls - the first returns 401 and the second 200 OK
    final HttpsURLConnection connection1 = mock(HttpsURLConnection.class);
    when(connection1.getResponseCode()).thenReturn(401);
    final HttpsURLConnection connection2 = mock(HttpsURLConnection.class);
    when(connection2.getResponseCode()).thenReturn(200);
    when(connector.connect(argThat(matchesAnyEndpoint(path)), eq(method), eq(entity), eq(headers))).thenReturn(connection1, connection2);
    final URI uri = new URI("https://helios" + path);
    final HttpURLConnection returnedConnection = authConnector.connect(uri, method, entity, headers);
    verify(connector).setExtraHttpsHandler(sshAgentHttpsHandlerWithArgs(USER, proxy, id1));
    verify(connector).setExtraHttpsHandler(sshAgentHttpsHandlerWithArgs(USER, proxy, id2));
    assertSame("Expect returned connection to be the second one, with successful response code", returnedConnection, connection2);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) AgentProxy(com.spotify.sshagentproxy.AgentProxy) Identity(com.spotify.sshagentproxy.Identity) URI(java.net.URI) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 4 with AgentProxy

use of com.spotify.sshagentproxy.AgentProxy in project helios by spotify.

the class AuthenticatingHttpConnectorTest method testOneIdentity_ResponseIsOk.

@Test
public void testOneIdentity_ResponseIsOk() throws Exception {
    final AgentProxy proxy = mock(AgentProxy.class);
    final Identity identity = mockIdentity();
    final AuthenticatingHttpConnector authConnector = createAuthenticatingConnector(Optional.of(proxy), ImmutableList.of(identity));
    final String path = "/another/one";
    final HttpsURLConnection connection = mock(HttpsURLConnection.class);
    when(connector.connect(argThat(matchesAnyEndpoint(path)), eq(method), eq(entity), eq(headers))).thenReturn(connection);
    when(connection.getResponseCode()).thenReturn(200);
    final URI uri = new URI("https://helios" + path);
    authConnector.connect(uri, method, entity, headers);
    verify(connector).setExtraHttpsHandler(sshAgentHttpsHandlerWithArgs(USER, proxy, identity));
}
Also used : AgentProxy(com.spotify.sshagentproxy.AgentProxy) Identity(com.spotify.sshagentproxy.Identity) URI(java.net.URI) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 5 with AgentProxy

use of com.spotify.sshagentproxy.AgentProxy in project helios by spotify.

the class AuthenticatingHttpConnectorTest method testOneIdentity_ServerReturns502BadGateway.

@Test
public void testOneIdentity_ServerReturns502BadGateway() throws Exception {
    final AgentProxy proxy = mock(AgentProxy.class);
    final Identity identity = mockIdentity();
    final AuthenticatingHttpConnector authConnector = createAuthenticatingConnector(Optional.of(proxy), ImmutableList.of(identity));
    final String path = "/foobar";
    final HttpsURLConnection connection = mock(HttpsURLConnection.class);
    when(connector.connect(argThat(matchesAnyEndpoint(path)), eq(method), eq(entity), eq(headers))).thenReturn(connection);
    when(connection.getResponseCode()).thenReturn(502);
    final URI uri = new URI("https://helios" + path);
    final HttpURLConnection returnedConnection = authConnector.connect(uri, method, entity, headers);
    assertSame("If there is only one identity do not expect any additional endpoints to " + "be called after the first returns Unauthorized", returnedConnection, connection);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) AgentProxy(com.spotify.sshagentproxy.AgentProxy) Identity(com.spotify.sshagentproxy.Identity) URI(java.net.URI) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

AgentProxy (com.spotify.sshagentproxy.AgentProxy)5 Identity (com.spotify.sshagentproxy.Identity)5 Test (org.junit.Test)5 URI (java.net.URI)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 HttpURLConnection (java.net.HttpURLConnection)3 SshAgentHttpsHandler (com.spotify.helios.client.HttpsHandlers.SshAgentHttpsHandler)1 CertificateAndPrivateKey (com.spotify.helios.client.tls.CertificateAndPrivateKey)1 Random (java.util.Random)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1