public static void main(String[] args) { final String USAGE = "To run this example, supply a key pair name\n" + "Ex: CreateKeyPair <key-pair-name>\n"; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String key_name = args[0]; final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); CreateKeyPairRequest request = new CreateKeyPairRequest() .withKeyName(key_name); CreateKeyPairResult response = ec2.createKeyPair(request); System.out.printf( "Successfulyl created key pair named %s", key_name); }
private void createKeyPair() throws IOException, FileNotFoundException { DescribeKeyPairsResult response = ec2.describeKeyPairs(); boolean check_key=false; for(KeyPairInfo key_pair : response.getKeyPairs()) { if(key_pair.getKeyName().equalsIgnoreCase(MY_KEY)) { check_key=true; break; } } if(!check_key) { File file=new File(System.getProperty("user.home")+"/.aws/"+MY_KEY+".pem"); if(!file.exists()) file.createNewFile(); else{ System.err.println("Connot create the key pair to access to the cluster!"); System.exit(1); } System.out.println("Create new key pair ~/.aws/"+MY_KEY+".pem"); CreateKeyPairRequest request = new CreateKeyPairRequest() .withKeyName(MY_KEY); CreateKeyPairResult responsetocreate = ec2.createKeyPair(request); PrintWriter print = new PrintWriter(file); print.print(responsetocreate.getKeyPair().getKeyMaterial()); print.close(); Runtime.getRuntime().exec("chmod 0400 "+System.getProperty("user.home")+"/.aws/"+MY_KEY+".pem"); } }
@Override public String create(Instance instance) { CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(); createKeyPairRequest.withKeyName(instance.getKeyName()); CreateKeyPairResult createKeyPairResult = ec2UserClient.amazonEC2Async().createKeyPair(createKeyPairRequest); KeyPair keyPair = createKeyPairResult.getKeyPair(); return keyPair.getKeyMaterial(); }
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()); } }
/*** * Creates a 2048-bit RSA key pair with the specified name * * @param keyName Key name to use * @return Unencrypted PEM encoded PKCS#8 private key */ public String createKeyValuePair(String keyName) { final AmazonEC2 amazonEC2 = getEc2Client(); final CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName(keyName); final CreateKeyPairResult createKeyPairResult = amazonEC2.createKeyPair(createKeyPairRequest); final KeyPair keyPair = createKeyPairResult.getKeyPair(); final String material = keyPair.getKeyMaterial(); LOGGER.info("Created key: " + keyName); LOGGER.debug("Created material: " + material); return material; }
@Override public KeyPair createKeyPair(CreateKeyPairRequest request, ResultCapture<CreateKeyPairResult> extractor) { ActionResult result = service.performAction("CreateKeyPair", request, extractor); if (result == null) return null; return new KeyPairImpl(result.getResource()); }
@Override public KeyPair createKeyPair(String keyName, ResultCapture<CreateKeyPairResult> extractor) { CreateKeyPairRequest request = new CreateKeyPairRequest() .withKeyName(keyName); return createKeyPair(request, extractor); }
/** * Creates a key-pair with a randomly generated name, and sets the generated * name to the specified property. */ public void execute() { checkParams(); AmazonEC2Client client = getOrCreateClient(AmazonEC2Client.class); try { String keyName = Long.toString(System.currentTimeMillis()); client.createKeyPair(new CreateKeyPairRequest() .withKeyName(keyName)); getProject().setProperty(propertyNameForKeyName, keyName); } catch (Exception e) { throw new BuildException(e.getMessage(), e); } }
@Override public CreateKeyPairResult createKeyPair(CreateKeyPairRequest createKeyPairRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
public KeyPair createKeyPair(String keyName) { logger.info("create key pair, keyName={}", keyName); CreateKeyPairResult result = ec2.createKeyPair(new CreateKeyPairRequest().withKeyName(keyName)); return result.getKeyPair(); }
public CreateKeyPairResult createKeyPair(AmazonEC2Client client, String keyName) { CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName(keyName); CreateKeyPairResult createKeyPairResult = client.createKeyPair(createKeyPairRequest); return createKeyPairResult; }
@Override public KeyPair createKeyPair(CreateKeyPairRequest request) { return createKeyPair(request, null); }
/** * Performs the <code>CreateKeyPair</code> action. * * <p> * * @return The <code>KeyPair</code> resource object associated with the * result of this action. * @see CreateKeyPairRequest */ com.amazonaws.resources.ec2.KeyPair createKeyPair(CreateKeyPairRequest request);
/** * Performs the <code>CreateKeyPair</code> action and use a ResultCapture to * retrieve the low-level client response. * * <p> * * @return The <code>KeyPair</code> resource object associated with the * result of this action. * @see CreateKeyPairRequest */ com.amazonaws.resources.ec2.KeyPair createKeyPair(CreateKeyPairRequest request, ResultCapture<CreateKeyPairResult> extractor);