public static Builder fromKeyPair(KeyPair in) { return new Builder().region(in.getRegion()).keyName(in.getKeyName()).sha1OfPrivateKey(in.getSha1OfPrivateKey()) .keyMaterial(in.getKeyMaterial()); } }
public KeyPair build() { if (fingerprint == null && keyMaterial != null) fingerprint(SshKeys.fingerprintPrivateKey(keyMaterial)); return new KeyPair(region, keyName, sha1OfPrivateKey, keyMaterial, fingerprint); }
@Override public void startDocument() { builder = KeyPair.builder().region(defaultRegion.get()); }
protected void checkKeyPair(String keyName, KeyPair keyPair) { assertNotNull(keyPair); assertNotNull(keyPair.getSha1OfPrivateKey()); assertEquals(keyPair.getKeyName(), keyName); Set<KeyPair> twoResults = client.describeKeyPairsInRegion(null, keyName); assertNotNull(twoResults); assertEquals(twoResults.size(), 1); KeyPair listPair = twoResults.iterator().next(); assertEquals(listPair.getKeyName(), keyPair.getKeyName()); assertEquals(listPair.getSha1OfPrivateKey(), keyPair.getSha1OfPrivateKey()); }
@Test void testCreateKeyPair() { String keyName = PREFIX + "1"; try { client.deleteKeyPairInRegion(null, keyName); } catch (Exception e) { } client.deleteKeyPairInRegion(null, keyName); KeyPair result = client.createKeyPairInRegion(null, keyName); assertNotNull(result); assertNotNull(result.getKeyMaterial()); assertNotNull(result.getSha1OfPrivateKey()); assertEquals(result.getKeyName(), keyName); Set<KeyPair> twoResults = Sets.newLinkedHashSet(client.describeKeyPairsInRegion(null, keyName)); assertNotNull(twoResults); assertEquals(twoResults.size(), 1); KeyPair listPair = twoResults.iterator().next(); assertEquals(listPair.getKeyName(), result.getKeyName()); assertEquals(listPair.getSha1OfPrivateKey(), result.getSha1OfPrivateKey()); }
/** * {@inheritDoc} */ public int compareTo(KeyPair o) { return (this == o) ? 0 : getKeyName().compareTo(o.getKeyName()); }
@VisibleForTesting String getPrivateKeyOrNull(RunningInstance instance) { KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName())); return keyPair != null ? keyPair.getKeyMaterial() : null; }
KeyPair expected = KeyPair.builder().region(defaultRegion).keyName("jclouds#test#0").sha1OfPrivateKey( "13:36:74:b9:56:bb:07:96:c0:19:ab:00:7f:9f:06:d2:16:a0:45:32").keyMaterial( "-----BEGIN RSA PRIVATE KEY-----\n" assert SshKeys.privateKeyHasSha1(result.getKeyMaterial(), result.getSha1OfPrivateKey()); assert SshKeys.privateKeyHasFingerprint(result.getKeyMaterial(), result.getFingerprint());
@Override public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { RegionAndName key = new RegionAndName(region, group); KeyPair pair; if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) { pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey())); options.dontAuthorizePublicKey(); if (hasLoginCredential.apply(options)) pair = pair.toBuilder().keyMaterial(options.getLoginPrivateKey()).build(); credentialsMap.put(key, pair); } else { if (hasPublicKeyMaterial.apply(options)) { logger.warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)"); } return super.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options); } return pair.getKeyName(); }
client.getKeyPairApi().get().deleteKeyPairInRegion(keyPair.getRegion(), keyPair.getKeyName()); } catch (Exception e) { e.printStackTrace();
@Test public void testCreateNewKeyPairUnlessUserSpecifiedOtherwise_importsKeyPairAndUnsetsTemplateInstructionWhenPublicKeySuppliedAndAddsCredentialToMapWhenOverridingCredsAreSet() { // setup constants String region = Region.AP_SOUTHEAST_1; String group = "group"; // create mocks CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions strategy = setupStrategy(); AWSEC2TemplateOptions options = keyPair(group).authorizePublicKey("ssh-rsa").overrideLoginCredentials( LoginCredentials.builder().user("foo").privateKey(CREDENTIALS.credential).build()); KeyPair keyPair = new KeyPair(region, group, "//TODO", null, null); // setup expectations expect( strategy.importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, CREDENTIALS.credential))).andReturn(keyPair); expect( strategy.credentialsMap.put(new RegionAndName(region, group), keyPair.toBuilder().keyMaterial(CREDENTIALS.credential).build())).andReturn(null); // replay mocks replayStrategy(strategy); // run assertEquals(strategy.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options), group); // verify mocks verifyStrategy(strategy); }
@Test void testImportKeyPair() throws 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); // check the fingerprint of public key (in the sha10OfPrivateKey field) assertEquals(keyPair.getSha1OfPrivateKey(), SshKeys.fingerprintPublicKey(myKey.get("public"))); // try again to see if there's an error try { client.importKeyPairInRegion(null, keyName, myKey.get("public")); fail("Duplicate call importKeyPairInRegion should have failed!"); } catch (IllegalStateException e) { } } finally { cleanupKeyPair(keyName); } }
public KeyPair addFingerprintToKeyPair(String publicKeyMaterial, KeyPair keyPair) { // add in the fingerprint as it makes correlating keys in ssh logs possible keyPair = keyPair.toBuilder().fingerprint(fingerprintPublicKey(publicKeyMaterial)).build(); return keyPair; }
@Test(enabled = false) void testCreateKeyPair() { String keyName = INSTANCE_PREFIX + "1"; try { client.getKeyPairServices().deleteKeyPairInRegion(null, keyName); } catch (Exception e) { } keyPair = client.getKeyPairServices().createKeyPairInRegion(null, keyName); assertNotNull(keyPair); assertNotNull(keyPair.getKeyMaterial()); assertNotNull(keyPair.getSha1OfPrivateKey()); assertEquals(keyPair.getKeyName(), keyName); }
/** * {@inheritDoc} */ public int compareTo(KeyPair o) { return (this == o) ? 0 : getKeyName().compareTo(o.getKeyName()); }
@VisibleForTesting String getPrivateKeyOrNull(RunningInstance instance) { KeyPair keyPair = credentialsMap.get(new RegionAndName(instance.getRegion(), instance.getKeyName())); return keyPair != null ? keyPair.getKeyMaterial() : null; }
KeyPair expected = KeyPair.builder().region(defaultRegion).keyName("jclouds#test#0").sha1OfPrivateKey( "13:36:74:b9:56:bb:07:96:c0:19:ab:00:7f:9f:06:d2:16:a0:45:32").keyMaterial( "-----BEGIN RSA PRIVATE KEY-----\n" assert SshKeys.privateKeyHasSha1(result.getKeyMaterial(), result.getSha1OfPrivateKey()); assert SshKeys.privateKeyHasFingerprint(result.getKeyMaterial(), result.getFingerprint());
protected void checkKeyPair(String keyName, KeyPair keyPair) { assertNotNull(keyPair); assertNotNull(keyPair.getSha1OfPrivateKey()); assertEquals(keyPair.getKeyName(), keyName); Set<KeyPair> twoResults = client.describeKeyPairsInRegion(null, keyName); assertNotNull(twoResults); assertEquals(twoResults.size(), 1); KeyPair listPair = twoResults.iterator().next(); assertEquals(listPair.getKeyName(), keyPair.getKeyName()); assertEquals(listPair.getSha1OfPrivateKey(), keyPair.getSha1OfPrivateKey()); } }
@Override public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { RegionAndName key = new RegionAndName(region, group); KeyPair pair; if (and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential)).apply(options)) { pair = importExistingKeyPair.apply(new RegionNameAndPublicKeyMaterial(region, group, options.getPublicKey())); options.dontAuthorizePublicKey(); if (hasLoginCredential.apply(options)) pair = pair.toBuilder().keyMaterial(options.getLoginPrivateKey()).build(); credentialsMap.put(key, pair); } else { if (hasPublicKeyMaterial.apply(options)) { logger.warn("to avoid creating temporary keys in aws-ec2, use templateOption overrideLoginCredentialWith(id_rsa)"); } return super.createNewKeyPairUnlessUserSpecifiedOtherwise(region, group, options); } return pair.getKeyName(); }
client.getKeyPairServices().deleteKeyPairInRegion(keyPair.getRegion(), keyPair.getKeyName()); } catch (Exception e) { e.printStackTrace();