private static void createKey(String keyName, AmazonEC2 ec2) { try { List<KeyPairInfo> keyPairList = ec2.describeKeyPairs().getKeyPairs(); for (KeyPairInfo keyPair : keyPairList) { if (keyName.equalsIgnoreCase(keyPair.getKeyName())) { System.out.println("Using key " + keyName); return; } } System.out.println("Creating key " + keyName + "in local directory"); CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest(); newKeyRequest.setKeyName(keyName); CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest); KeyPair keyPair = new KeyPair(); keyPair = keyresult.getKeyPair(); String privateKey = keyPair.getKeyMaterial(); writeKeytoFile(keyName, privateKey); } catch (AmazonServiceException ase) { System.out.println("Caught Exception: " + ase.getMessage()); System.out.println("Reponse Status Code: " + ase.getStatusCode()); System.out.println("Error Code: " + ase.getErrorCode()); System.out.println("Request ID: " + ase.getRequestId()); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof CreateKeyPairRequest == false) return false; CreateKeyPairRequest other = (CreateKeyPairRequest) obj; if (other.getKeyName() == null ^ this.getKeyName() == null) return false; if (other.getKeyName() != null && other.getKeyName().equals(this.getKeyName()) == false) return false; return true; }
@Action( value = "/manage/submitEC2Key", results = { @Result(name = "input", location = "/manage/view_ec2_keys.jsp"), @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect") }) public String submitEC2Key() { String retVal = SUCCESS; try { // get AWS credentials from DB AWSCred awsCred = AWSCredDB.getAWSCred(); // set AWS credentials for service BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey()); // create service AmazonEC2 service = new AmazonEC2Client(awsCredentials); service.setEndpoint(ec2Key.getEc2Region()); // create key pair request CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(); createKeyPairRequest.withKeyName(ec2Key.getKeyNm()); // call service CreateKeyPairResult createKeyPairResult = service.createKeyPair(createKeyPairRequest); // get key pair result KeyPair keyPair = createKeyPairResult.getKeyPair(); // set private key String privateKey = keyPair.getKeyMaterial(); ec2Key.setPrivateKey(privateKey); // add to db Long keyId = EC2KeyDB.saveEC2Key(ec2Key); // store private key SSHUtil.storePrivateKey(keyId.toString(), ec2Key.getPrivateKey().trim()); } catch (AmazonServiceException ex) { addActionError(ex.getMessage()); retVal = INPUT; } return retVal; }
public Request<CreateKeyPairRequest> marshall(CreateKeyPairRequest createKeyPairRequest) { if (createKeyPairRequest == null) { throw new AmazonClientException("Invalid argument passed to marshall(...)"); } Request<CreateKeyPairRequest> request = new DefaultRequest<CreateKeyPairRequest>(createKeyPairRequest, "AmazonEC2"); request.addParameter("Action", "CreateKeyPair"); request.addParameter("Version", "2012-10-01"); if (createKeyPairRequest.getKeyName() != null) { request.addParameter("KeyName", StringUtils.fromString(createKeyPairRequest.getKeyName())); } return request; }