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