/** * Makes a call to the operation specified by the waiter by taking the corresponding request and returns the * corresponding result * * @param describeKeyPairsRequest * Corresponding request for the operation * @return Corresponding result of the operation */ @Override public DescribeKeyPairsResult apply(DescribeKeyPairsRequest describeKeyPairsRequest) { return client.describeKeyPairs(describeKeyPairsRequest); } }
public static void main(String[] args) { final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); DescribeKeyPairsResult response = ec2.describeKeyPairs(); for(KeyPairInfo key_pair : response.getKeyPairs()) { System.out.printf( "Found key pair with name %s " + "and fingerprint %s", key_pair.getKeyName(), key_pair.getKeyFingerprint()); } } }
/** * Makes a call to the operation specified by the waiter by taking the corresponding request and returns the * corresponding result * * @param describeKeyPairsRequest * Corresponding request for the operation * @return Corresponding result of the operation */ @Override public DescribeKeyPairsResult apply(DescribeKeyPairsRequest describeKeyPairsRequest) { return client.describeKeyPairs(describeKeyPairsRequest); } }
/** * Load keypairs * * @return list of keypairs */ public static List<String> loadKeypairs() { List<String> resultList = new ArrayList<String>(); DescribeKeyPairsResult results = getEC2Client().describeKeyPairs(); for (KeyPairInfo key : results.getKeyPairs()) { resultList.add(key.getKeyName()); } return resultList; }
/** * Load keypairs * * @return list of keypairs */ public static List<String> loadKeypairs() { List<String> resultList = new ArrayList<String>(); DescribeKeyPairsResult results = getEC2Client().describeKeyPairs(); for (KeyPairInfo key : results.getKeyPairs()) { resultList.add(key.getKeyName()); } return resultList; }
private boolean exists(AmazonEC2 ec2) { // to enable conventionMappings feature String keyName = getKeyName(); try { DescribeKeyPairsResult describeKeyPairsResult = ec2.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyName)); return describeKeyPairsResult.getKeyPairs().isEmpty() == false; } catch (AmazonClientException e) { return false; } }
public FormValidation doGenerateKey(StaplerResponse rsp, URL ec2EndpointUrl, boolean useInstanceProfileForCredentials, String accessId, String secretKey) throws IOException, ServletException { try { AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials, accessId, secretKey); AmazonEC2 ec2 = connect(credentialsProvider, ec2EndpointUrl); List<KeyPairInfo> existingKeys = ec2.describeKeyPairs().getKeyPairs(); int n = 0; while(true) { boolean found = false; for (KeyPairInfo k : existingKeys) { if(k.getKeyName().equals("hudson-"+n)) found=true; } if(!found) break; n++; } CreateKeyPairRequest request = new CreateKeyPairRequest("hudson-" + n); KeyPair key = ec2.createKeyPair(request).getKeyPair(); rsp.addHeader("script","findPreviousFormItem(button,'privateKey').value='"+key.getKeyMaterial().replace("\n","\\n")+"'"); return FormValidation.ok(Messages.EC2Cloud_Success()); } catch (AmazonClientException e) { LOGGER.log(Level.WARNING, "Failed to check EC2 credential",e); return FormValidation.error(e.getMessage()); } } }
public FormValidation doGenerateKey(StaplerResponse rsp, URL ec2EndpointUrl, boolean useInstanceProfileForCredentials, String credentialsId, String roleArn, String roleSessionName, String region) throws IOException, ServletException { try { AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials, credentialsId, roleArn, roleSessionName, region); AmazonEC2 ec2 = connect(credentialsProvider, ec2EndpointUrl); List<KeyPairInfo> existingKeys = ec2.describeKeyPairs().getKeyPairs(); int n = 0; while (true) { boolean found = false; for (KeyPairInfo k : existingKeys) { if (k.getKeyName().equals("hudson-" + n)) found = true; } if (!found) break; n++; } CreateKeyPairRequest request = new CreateKeyPairRequest("hudson-" + n); KeyPair key = ec2.createKeyPair(request).getKeyPair(); rsp.addHeader("script", "findPreviousFormItem(button,'privateKey').value='" + key.getKeyMaterial().replace("\n", "\\n") + "'"); return FormValidation.ok(Messages.EC2Cloud_Success()); } catch (AmazonClientException e) { LOGGER.log(Level.WARNING, "Failed to check EC2 credential", e); return FormValidation.error(e.getMessage()); } }
/** * Finds the {@link KeyPairInfo} that corresponds to this key in EC2. */ public com.amazonaws.services.ec2.model.KeyPair find(AmazonEC2 ec2) throws IOException, AmazonClientException { String fp = getFingerprint(); String pfp = getPublicFingerprint(); for (KeyPairInfo kp : ec2.describeKeyPairs().getKeyPairs()) { if (kp.getKeyFingerprint().equalsIgnoreCase(fp)) { com.amazonaws.services.ec2.model.KeyPair keyPair = new com.amazonaws.services.ec2.model.KeyPair(); keyPair.setKeyName(kp.getKeyName()); keyPair.setKeyFingerprint(fp); keyPair.setKeyMaterial(Secret.toString(privateKey)); return keyPair; } if (kp.getKeyFingerprint().equalsIgnoreCase(pfp)) { com.amazonaws.services.ec2.model.KeyPair keyPair = new com.amazonaws.services.ec2.model.KeyPair(); keyPair.setKeyName(kp.getKeyName()); keyPair.setKeyFingerprint(pfp); keyPair.setKeyMaterial(Secret.toString(privateKey)); return keyPair; } } return null; }
/** * Finds the {@link KeyPairInfo} that corresponds to this key in EC2. */ public com.amazonaws.services.ec2.model.KeyPair find(AmazonEC2 ec2) throws IOException, AmazonClientException { String fp = getFingerprint(); String pfp = getPublicFingerprint(); for(KeyPairInfo kp : ec2.describeKeyPairs().getKeyPairs()) { if(kp.getKeyFingerprint().equalsIgnoreCase(fp)) { com.amazonaws.services.ec2.model.KeyPair keyPair = new com.amazonaws.services.ec2.model.KeyPair(); keyPair.setKeyName(kp.getKeyName()); keyPair.setKeyFingerprint(fp); keyPair.setKeyMaterial(Secret.toString(privateKey)); return keyPair; } if(kp.getKeyFingerprint().equalsIgnoreCase(pfp)) { com.amazonaws.services.ec2.model.KeyPair keyPair = new com.amazonaws.services.ec2.model.KeyPair(); keyPair.setKeyName(kp.getKeyName()); keyPair.setKeyFingerprint(pfp); keyPair.setKeyMaterial(Secret.toString(privateKey)); return keyPair; } } return null; }