@VisibleForTesting public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { String keyPairName = null; boolean shouldAutomaticallyCreateKeyPair = true; if (options instanceof EC2TemplateOptions) { keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); if (keyPairName == null) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) .shouldAutomaticallyCreateKeyPair(); } if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { keyPairName = createOrImportKeyPair(region, group, options); } else if (keyPairName != null) { if (options.getLoginPrivateKey() != null) { String pem = options.getLoginPrivateKey(); KeyPair keyPair = KeyPair.builder().region(region).keyName(keyPairName).fingerprint( fingerprintPrivateKey(pem)).sha1OfPrivateKey(sha1PrivateKey(pem)).keyMaterial(pem).build(); RegionAndName key = new RegionAndName(region, keyPairName); credentialsMap.put(key, keyPair); } } if (options.getRunScript() != null) { RegionAndName regionAndName = new RegionAndName(region, keyPairName); checkArgument( credentialsMap.containsKey(regionAndName), "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)", regionAndName); } return keyPairName; }
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" + "MIIEowIBAAKCAQEA0CbFlhSdbMdad2ux2BVqk6Ut5fLKb0CdbqubGcEBfwsSz9Rp4Ile76P90MpV\n" + "3UdbzJmtjMW9CR6l9s11mV6PcZP4qnODp3nd6a+lPeL3wVYQ47DsTJ/Bx5dI17zA5mU57n6mV0a3\n" + "DbSoPKSdaKTQdo2THnVE9P9sPKZWueAcsE4Yw/qcTjoxrtUnAH/AXN250v0tkKIOvMhu\n" + "-----END RSA PRIVATE KEY-----").build();
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" + "MIIEowIBAAKCAQEA0CbFlhSdbMdad2ux2BVqk6Ut5fLKb0CdbqubGcEBfwsSz9Rp4Ile76P90MpV\n" + "3UdbzJmtjMW9CR6l9s11mV6PcZP4qnODp3nd6a+lPeL3wVYQ47DsTJ/Bx5dI17zA5mU57n6mV0a3\n" + "DbSoPKSdaKTQdo2THnVE9P9sPKZWueAcsE4Yw/qcTjoxrtUnAH/AXN250v0tkKIOvMhu\n" + "-----END RSA PRIVATE KEY-----").build();
@VisibleForTesting public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { String keyPairName = null; boolean shouldAutomaticallyCreateKeyPair = true; if (options instanceof EC2TemplateOptions) { keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); if (keyPairName == null) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) .shouldAutomaticallyCreateKeyPair(); } if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { keyPairName = createOrImportKeyPair(region, group, options); } else if (keyPairName != null) { if (options.getLoginPrivateKey() != null) { String pem = options.getLoginPrivateKey(); KeyPair keyPair = KeyPair.builder().region(region).keyName(keyPairName).fingerprint( fingerprintPrivateKey(pem)).sha1OfPrivateKey(sha1PrivateKey(pem)).keyMaterial(pem).build(); RegionAndName key = new RegionAndName(region, keyPairName); credentialsMap.put(key, keyPair); } } if (options.getRunScript() != null) { RegionAndName regionAndName = new RegionAndName(region, keyPairName); checkArgument( credentialsMap.containsKey(regionAndName), "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)", regionAndName); } return keyPairName; }
@VisibleForTesting public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { String keyPairName = null; boolean shouldAutomaticallyCreateKeyPair = true; if (options instanceof EC2TemplateOptions) { keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); if (keyPairName == null) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) .shouldAutomaticallyCreateKeyPair(); } if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { keyPairName = createOrImportKeyPair(region, group, options); } else if (keyPairName != null) { if (options.getLoginPrivateKey() != null) { String pem = options.getLoginPrivateKey(); KeyPair keyPair = KeyPair.builder().region(region).keyName(keyPairName).fingerprint( fingerprintPrivateKey(pem)).sha1OfPrivateKey(sha1PrivateKey(pem)).keyMaterial(pem).build(); RegionAndName key = new RegionAndName(region, keyPairName); credentialsMap.put(key, keyPair); } } if (options.getRunScript() != null) { RegionAndName regionAndName = new RegionAndName(region, keyPairName); checkArgument( credentialsMap.containsKey(regionAndName), "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)", regionAndName); } return keyPairName; }
@VisibleForTesting public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { String keyPairName = null; boolean shouldAutomaticallyCreateKeyPair = true; if (options instanceof EC2TemplateOptions) { keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); if (keyPairName == null) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) .shouldAutomaticallyCreateKeyPair(); } if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { keyPairName = createOrImportKeyPair(region, group, options); } else if (keyPairName != null) { if (options.getLoginPrivateKey() != null) { String pem = options.getLoginPrivateKey(); KeyPair keyPair = KeyPair.builder().region(region).keyName(keyPairName).fingerprint( fingerprintPrivateKey(pem)).sha1OfPrivateKey(sha1PrivateKey(pem)).keyMaterial(pem).build(); RegionAndName key = new RegionAndName(region, keyPairName); credentialsMap.put(key, keyPair); } } if (options.getRunScript() != null) { RegionAndName regionAndName = new RegionAndName(region, keyPairName); checkArgument( credentialsMap.containsKey(regionAndName), "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)", regionAndName); } return keyPairName; }
@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); }
@VisibleForTesting public String createNewKeyPairUnlessUserSpecifiedOtherwise(String region, String group, TemplateOptions options) { String keyPairName = null; boolean shouldAutomaticallyCreateKeyPair = true; if (options instanceof EC2TemplateOptions) { keyPairName = EC2TemplateOptions.class.cast(options).getKeyPair(); if (keyPairName == null) shouldAutomaticallyCreateKeyPair = EC2TemplateOptions.class.cast(options) .shouldAutomaticallyCreateKeyPair(); } if (keyPairName == null && shouldAutomaticallyCreateKeyPair) { keyPairName = createOrImportKeyPair(region, group, options); } else if (keyPairName != null) { if (options.getLoginPrivateKey() != null) { String pem = options.getLoginPrivateKey(); KeyPair keyPair = KeyPair.builder().region(region).keyName(keyPairName).fingerprint( fingerprintPrivateKey(pem)).sha1OfPrivateKey(sha1PrivateKey(pem)).keyMaterial(pem).build(); RegionAndName key = new RegionAndName(region, keyPairName); credentialsMap.put(key, keyPair); } } if (options.getRunScript() != null) { RegionAndName regionAndName = new RegionAndName(region, keyPairName); checkArgument( credentialsMap.containsKey(regionAndName), "no private key configured for: %s; please use options.overrideLoginCredentialWith(rsa_private_text)", regionAndName); } return keyPairName; }
@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(); }
@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(); }
@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(); }
public void endElement(String uri, String name, String qName) { if (qName.equals("keyFingerprint")) { builder.sha1OfPrivateKey(currentOrNull(currentText)); } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region != null) builder.region(region); try { keyPairs.add(builder.build()); } finally { builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); } currentText.setLength(0); }
public void endElement(String uri, String name, String qName) { if (qName.equals("keyFingerprint")) { builder.sha1OfPrivateKey(currentOrNull(currentText)); } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region != null) builder.region(region); try { keyPairs.add(builder.build()); } finally { builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); } currentText = new StringBuilder(); }
public void endElement(String uri, String name, String qName) { if (qName.equals("keyFingerprint")) { builder.sha1OfPrivateKey(currentOrNull(currentText)); } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region != null) builder.region(region); try { keyPairs.add(builder.build()); } finally { builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); } currentText.setLength(0); }
public void endElement(String uri, String name, String qName) { if (qName.equals("keyFingerprint")) { builder.sha1OfPrivateKey(currentOrNull(currentText)); } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region != null) builder.region(region); try { keyPairs.add(builder.build()); } finally { builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); } currentText.setLength(0); }
public static Builder builder() { return new Builder(); }
public void endElement(String uri, String name, String qName) { if (qName.equals("keyFingerprint")) { builder.sha1OfPrivateKey(currentOrNull(currentText)); } else if (qName.equals("item")) { String region = AWSUtils.findRegionInArgsOrNull(getRequest()); if (region != null) builder.region(region); try { keyPairs.add(builder.build()); } finally { builder = KeyPair.builder().region(defaultRegion.get()); } } else if (qName.equals("keyName")) { builder.keyName(currentOrNull(currentText)); } currentText = new StringBuilder(); }
public static Builder builder() { return new Builder(); }
public static Builder builder() { return new Builder(); }
public static Builder builder() { return new Builder(); }