List of usage examples for com.amazonaws.services.ec2.model DescribeKeyPairsRequest DescribeKeyPairsRequest
DescribeKeyPairsRequest
From source file:com.cloudera.director.aws.ec2.EC2InstanceTemplateConfigurationValidator.java
License:Apache License
/** * Validates the EC2 key name./*from www .j a va 2 s .co m*/ * * @param client the EC2 client * @param accumulator the exception condition accumulator * @param localizationContext the localization context */ @VisibleForTesting void checkKeyName(AmazonEC2Client client, Configured configuration, PluginExceptionConditionAccumulator accumulator, LocalizationContext localizationContext) { String keyName = configuration.getConfigurationValue(KEY_NAME, localizationContext); if (keyName != null) { LOG.info(">> Describing key pair"); try { DescribeKeyPairsResult result = client .describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyName)); // TODO Should this be REDACTED instead of NotDisplayed? checkCount(accumulator, KEY_NAME, localizationContext, "NotDisplayed", result.getKeyPairs()); } catch (AmazonServiceException e) { if (e.getErrorCode().startsWith(INVALID_KEY_PAIR)) { addError(accumulator, KEY_NAME, localizationContext, null, INVALID_KEY_NAME_MSG, keyName); } else { throw Throwables.propagate(e); } } } }
From source file:com.dowdandassociates.gentoo.bootstrap.DefaultKeyPairInformation.java
License:Apache License
@PostConstruct private void setup() { boolean nameSet = (null != name); boolean filenameSet = (null != filename); boolean keyExists = false; if (nameSet) { log.info("Checking if key pair \"" + name + "\" exists"); keyExists = !(ec2Client//ww w.ja v a 2 s . c om .describeKeyPairs(new DescribeKeyPairsRequest() .withFilters(new Filter().withName("key-name").withValues(name))) .getKeyPairs().isEmpty()); } if (keyExists && !filenameSet) { log.warn("Key pair \"" + name + "\" exists, but private key location is not specified"); keyExists = false; } if (!keyExists) { if (!nameSet) { name = "gentoo-bootstrap-" + DateFormatUtils.formatUTC(System.currentTimeMillis(), "yyyyMMdd'T'HHmmssSSS'Z'"); } if (!filenameSet) { try { filename = Files .createTempFile(name, ".pem", PosixFilePermissions .asFileAttribute(PosixFilePermissions.fromString("rw-------"))) .toString(); } catch (IOException ioe) { log.warn("Cannot create temp file", ioe); filename = name + ".pem"; } } log.info("Creating key pair \"" + name + "\""); CreateKeyPairResult createResult = ec2Client .createKeyPair(new CreateKeyPairRequest().withKeyName(name)); try { log.info("Saving pem file to \"" + filename + "\""); BufferedWriter outfile = new BufferedWriter(new FileWriter(filename)); try { outfile.write(createResult.getKeyPair().getKeyMaterial()); } catch (IOException ioe) { String message = "Error writing to file \"" + filename + "\""; log.error(message, ioe); throw new RuntimeException(message, ioe); } finally { outfile.close(); } } catch (IOException ioe) { String message = "Error opening file \"" + filename + "\""; log.error(message, ioe); throw new RuntimeException(message, ioe); } builtKeyPair = true; log.info("Key pair \"" + name + "\" built"); } else { builtKeyPair = false; log.info("Key pair \"" + name + "\" exists"); } if (filename.startsWith("~" + File.separator)) { filename = System.getProperty("user.home") + filename.substring(1); } }
From source file:com.ec2box.manage.action.EC2KeyAction.java
License:Apache License
/** * returns keypairs as a json string/*from www .j a v a2 s.c o m*/ */ @Action(value = "/manage/getKeyPairJSON") public String getKeyPairJSON() { AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId()); //set AWS credentials for service BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey()); AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig()); service.setEndpoint(ec2Key.getEc2Region()); DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest(); DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest); List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs(); String json = new Gson().toJson(keyPairInfoList); try { servletResponse.getOutputStream().write(json.getBytes()); } catch (Exception ex) { log.error(ex.toString(), ex); } return null; }
From source file:com.ec2box.manage.action.EC2KeyAction.java
License:Apache License
@Action(value = "/manage/importEC2Key", results = { @Result(name = "input", location = "/manage/view_ec2_keys.jsp"), @Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect") }) public String importEC2Key() { String retVal = SUCCESS;// w w w .j a v a2 s. c o m try { //get AWS credentials from DB AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId()); //set AWS credentials for service BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey()); //create service AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig()); service.setEndpoint(ec2Key.getEc2Region()); //describe key pair request DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest(); describeKeyPairsRequest.setKeyNames(Arrays.asList(ec2Key.getKeyNm())); //call service DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest); if (describeKeyPairsResult != null && describeKeyPairsResult.getKeyPairs().size() > 0) { //add to db EC2KeyDB.saveEC2Key(ec2Key); } else { addActionError("Imported key does not exist on AWS"); retVal = INPUT; } } catch (AmazonServiceException ex) { addActionError(ex.getMessage()); retVal = INPUT; } return retVal; }
From source file:com.github.trask.sandbox.ec2.Ec2Service.java
License:Apache License
public void getOrCreateKeyPair(String keyName, String privateKeyPath) throws FileNotFoundException, JSchException, IOException { DescribeKeyPairsRequest request = new DescribeKeyPairsRequest().withKeyNames(keyName); try {/* ww w .j a va2 s .co m*/ ec2.describeKeyPairs(request); } catch (AmazonServiceException e) { createKeyPair(keyName, privateKeyPath); } }
From source file:com.github.trask.sandbox.ec2.Ec2Service.java
License:Apache License
public void getOrCreateKeyPairGenerateRemotely(String keyName, String privateKeyPath) throws IOException { DescribeKeyPairsRequest request = new DescribeKeyPairsRequest().withKeyNames(keyName); try {//ww w . j a va 2 s .c om ec2.describeKeyPairs(request); } catch (AmazonServiceException e) { createKeyPairGenerateRemotely(keyName, privateKeyPath); } }
From source file:com.keybox.manage.action.ApplicationKeysAction.java
License:Apache License
/** * returns keypairs as a json string//w w w. ja va2 s .c o m */ @Action(value = "/manage/getKeyPairJSON") public String getKeyPairJSON() { AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredentials().getId()); //set AWS credentials for service BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey()); AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig()); service.setEndpoint(ec2Key.getEc2Region()); DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest(); DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest); List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs(); String json = new Gson().toJson(keyPairInfoList); try { servletResponse.getOutputStream().write(json.getBytes()); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.nike.cerberus.service.Ec2Service.java
License:Apache License
/** * Checks if a key pair is present in AWS EC2. * * @param keyName Friendly name for the key * @return If present// ww w . j a va 2s .c o m */ public boolean isKeyPairPresent(final String keyName) { final DescribeKeyPairsRequest request = new DescribeKeyPairsRequest().withKeyNames(keyName); try { final DescribeKeyPairsResult result = ec2Client.describeKeyPairs(request); return result.getKeyPairs().size() > 0; } catch (final AmazonServiceException ase) { if (ase.getErrorCode() == "InvalidKeyPair.NotFound") { return false; } throw ase; } }
From source file:com.zotoh.cloudapi.aws.Keypair.java
License:Open Source License
@Override public String getFingerprint(String name) throws InternalException, CloudException { tstEStrArg("keypair-name", name); String fp = null;//from w ww. j a va 2 s . c o m try { DescribeKeyPairsResult res = _svc.getCloud().getEC2() .describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(name)); List<KeyPairInfo> lst = res == null ? null : res.getKeyPairs(); KeyPairInfo p = isNil(lst) ? null : lst.get(0); fp = p == null ? null : p.getKeyFingerprint(); } catch (AmazonServiceException e) { if (!testSafeNonExistError(e, "InvalidKeyPair.NotFound")) { throw new CloudException(e); } } return fp; }
From source file:com.zotoh.cloudapi.aws.Keypair.java
License:Open Source License
@Override public Collection<String> list() throws InternalException, CloudException { DescribeKeyPairsResult res = _svc.getCloud().getEC2().describeKeyPairs(new DescribeKeyPairsRequest()); List<KeyPairInfo> lst = res == null ? null : res.getKeyPairs(); List<String> rc = LT(); if (lst != null) for (int i = 0; i < lst.size(); ++i) { rc.add(lst.get(i).getKeyName()); }//ww w. jav a 2s . c om return rc; }