@VisibleForTesting KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) { checkNotNull(region, "region"); checkNotNull(group, "group"); checkNotNull(publicKeyMaterial, "publicKeyMaterial"); logger.debug(">> importing keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; // loop for eventual consistency or race condition. // as this command is idempotent, it should be ok while (keyPair == null) try { keyPair = ec2Api.getKeyPairApi().get().importKeyPairInRegion(region, "jclouds#" + group, publicKeyMaterial); keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< imported keyPair(%s)", keyPair); } catch (IllegalStateException e) { keyPair = Iterables.getFirst(ec2Api.getKeyPairApi().get().describeKeyPairsInRegion(region, "jclouds#" + group), null); if (keyPair != null) { keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< retrieved existing keyPair(%s)", keyPair); } } return keyPair; }
@VisibleForTesting KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) { checkNotNull(region, "region"); checkNotNull(group, "group"); checkNotNull(publicKeyMaterial, "publicKeyMaterial"); logger.debug(">> importing keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; // loop for eventual consistency or race condition. // as this command is idempotent, it should be ok while (keyPair == null) try { keyPair = ec2Api.getKeyPairApi().get().importKeyPairInRegion(region, "jclouds#" + group, publicKeyMaterial); keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< imported keyPair(%s)", keyPair); } catch (IllegalStateException e) { keyPair = Iterables.getFirst(ec2Api.getKeyPairApi().get().describeKeyPairsInRegion(region, "jclouds#" + group), null); if (keyPair != null) { keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< retrieved existing keyPair(%s)", keyPair); } } return keyPair; }
@VisibleForTesting KeyPair importOrReturnExistingKeypair(String region, String group, String publicKeyMaterial) { checkNotNull(region, "region"); checkNotNull(group, "group"); checkNotNull(publicKeyMaterial, "publicKeyMaterial"); logger.debug(">> importing keyPair region(%s) group(%s)", region, group); KeyPair keyPair = null; // loop for eventual consistency or race condition. // as this command is idempotent, it should be ok while (keyPair == null) try { keyPair = ec2Api.getKeyPairApi().get().importKeyPairInRegion(region, "jclouds#" + group, publicKeyMaterial); keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< imported keyPair(%s)", keyPair); } catch (IllegalStateException e) { keyPair = Iterables.getFirst(ec2Api.getKeyPairApi().get().describeKeyPairsInRegion(region, "jclouds#" + group), null); if (keyPair != null) { keyPair = addFingerprintToKeyPair(publicKeyMaterial, keyPair); logger.debug("<< retrieved existing keyPair(%s)", keyPair); } } return keyPair; }
@Test public void testApply() { AWSEC2Api client = createMock(AWSEC2Api.class); AWSKeyPairApi keyApi = createMock(AWSKeyPairApi.class); expect(client.getKeyPairApi()).andReturn((Optional) Optional.of(keyApi)).atLeastOnce(); expect(keyApi.importKeyPairInRegion("region", "jclouds#group", PUBLIC_KEY)).andReturn(pair); replay(client); replay(keyApi); ImportOrReturnExistingKeypair parser = new ImportOrReturnExistingKeypair(client); assertEquals(parser.importOrReturnExistingKeypair("region", "group", PUBLIC_KEY), pairWithFingerprint); verify(client); verify(keyApi); }
@Test public void testApplyWithIllegalStateExceptionRetriesWhenExistingKeyNotFound() { AWSEC2Api client = createMock(AWSEC2Api.class); AWSKeyPairApi keyApi = createMock(AWSKeyPairApi.class); expect(client.getKeyPairApi()).andReturn((Optional) Optional.of(keyApi)).atLeastOnce(); expect(keyApi.importKeyPairInRegion("region", "jclouds#group", PUBLIC_KEY)).andThrow( new IllegalStateException()); expect(keyApi.describeKeyPairsInRegion("region", "jclouds#group")).andReturn(ImmutableSet.<KeyPair> of()); expect(keyApi.importKeyPairInRegion("region", "jclouds#group", PUBLIC_KEY)).andThrow( new IllegalStateException()); expect(keyApi.describeKeyPairsInRegion("region", "jclouds#group")).andReturn(ImmutableSet.<KeyPair> of(pair)); replay(client); replay(keyApi); ImportOrReturnExistingKeypair parser = new ImportOrReturnExistingKeypair(client); assertEquals(parser.importOrReturnExistingKeypair("region", "group", PUBLIC_KEY), pairWithFingerprint); verify(client); verify(keyApi); } }
@Test public void testApplyWithIllegalStateExceptionReturnsExistingKey() { AWSEC2Api client = createMock(AWSEC2Api.class); AWSKeyPairApi keyApi = createMock(AWSKeyPairApi.class); expect(client.getKeyPairApi()).andReturn((Optional) Optional.of(keyApi)).atLeastOnce(); expect(keyApi.importKeyPairInRegion("region", "jclouds#group", PUBLIC_KEY)).andThrow( new IllegalStateException()); expect(keyApi.describeKeyPairsInRegion("region", "jclouds#group")).andReturn(ImmutableSet.of(pair)); replay(client); replay(keyApi); ImportOrReturnExistingKeypair parser = new ImportOrReturnExistingKeypair(client); // enriching to include the ssh fingerprint so that ssh logs are easier to correlate assertEquals(parser.importOrReturnExistingKeypair("region", "group", PUBLIC_KEY), pairWithFingerprint); verify(client); verify(keyApi); }
@Test void testImportKeyPair() throws FileNotFoundException, IOException { String keyName = PREFIX + "2"; cleanupKeyPair(keyName); Map<String, String> myKey = ComputeTestUtils.setupKeyPair(); try { KeyPair keyPair = client.importKeyPairInRegion(null, keyName, myKey.get("public")); checkKeyPair(keyName, keyPair); // TODO generate correct fingerprint and check // assertEquals(keyPair.getKeyFingerprint(), // CryptoStreams.hex(CryptoStreams.md5(myKey.get("public").getBytes()))); // try again to see if there's an error try { client.importKeyPairInRegion(null, keyName, myKey.get("public")); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { } } finally { cleanupKeyPair(keyName); } }