Example usage for com.amazonaws.services.ec2.model ImportKeyPairRequest ImportKeyPairRequest

List of usage examples for com.amazonaws.services.ec2.model ImportKeyPairRequest ImportKeyPairRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model ImportKeyPairRequest ImportKeyPairRequest.

Prototype

public ImportKeyPairRequest() 

Source Link

Document

Default constructor for ImportKeyPairRequest object.

Usage

From source file:com.axemblr.provisionr.amazon.activities.EnsureKeyPairExists.java

License:Apache License

private void importPoolPublicKeyPair(AmazonEC2 client, String keyName, String publicKey) {
    ImportKeyPairResult result = client/*w  ww  . j  av  a 2  s.  c om*/
            .importKeyPair(new ImportKeyPairRequest().withKeyName(keyName).withPublicKeyMaterial(publicKey));
    LOG.info("<< Created remote key with fingerprint {}", result.getKeyFingerprint());
}

From source file:com.cloudera.director.aws.ec2.EC2Provider.java

License:Apache License

/**
 * Adds the AWS key name corresponding to a private key to the given
 * configuration.//from  w  w  w. ja v a 2s  .co  m
 *
 * @param configuration    the configuration to be enhanced
 * @param privateKeyString private key, in serialized form
 * @return the enhanced configuration
 * @throws IllegalArgumentException if the key could not be deserialized, or if no key known to
 *                                  AWS matches this key's fingerprint
 */
private Configured addKeyName(Configured configuration, LocalizationContext templateLocalizationContext,
        String privateKeyString, String publicKeyString) {
    PrivateKey privateKey;
    PublicKey publicKey;
    try {
        KeySerialization keySerialization = new KeySerialization();
        privateKey = keySerialization.deserializePrivateKey(privateKeyString);
        if (publicKeyString != null) {
            publicKey = keySerialization.deserializePublicKey(publicKeyString);
        } else {
            publicKey = null;
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Unable to deserialize private key from compute instance template",
                e);
    }
    String privateKeyFingerprint = getSha1Fingerprint(privateKey);
    String publicKeyFingerprint = getMd5Fingerprint(publicKey);
    String keyName = lookupKeyName(privateKeyFingerprint, publicKeyFingerprint);
    if (keyName == null) {
        if (importKeyPairIfMissing) {
            keyName = PUBLIC_KEY_PREFIX + publicKeyFingerprint;
            LOG.info("KeyPair not found. Adding public key to EC2 with key name : {}", keyName);
            client.importKeyPair(new ImportKeyPairRequest().withKeyName(keyName)
                    .withPublicKeyMaterial(BaseEncoding.base64().encode(publicKey.getEncoded())));
        } else {
            throw new IllegalArgumentException(
                    "No private key in EC2 matches the fingerprint " + privateKeyFingerprint);
        }
    } else {
        LOG.info("Found EC2 key name {} for fingerprint", keyName);
    }
    Map<String, String> configMap = Maps
            .newHashMap(configuration.getConfiguration(templateLocalizationContext));
    configMap.put(KEY_NAME.unwrap().getConfigKey(), keyName);
    return new SimpleConfiguration(configMap);
}

From source file:jp.primecloud.auto.tool.management.iaasgw.AwsIaasGatewayScriptService.java

License:Open Source License

@Override
public void importKeyPair(String keyName, String publicKey) throws AutoException {
    // ???????????
    DescribeKeyPairsRequest request = new DescribeKeyPairsRequest();
    DescribeKeyPairsResult result = ec2Client.describeKeyPairs(request);
    List<KeyPairInfo> keyPairs = result.getKeyPairs();

    for (KeyPairInfo keyPair : keyPairs) {
        if (keyPair.getKeyName().equals(keyName)) {
            log.info(platform.getPlatformName() + " ? " + keyName
                    + " ?????????????");
            System.out.println("IMPORT_SKIPPED");
            return;
        }//  ww  w .  j  a  v  a  2  s  .  c  o m
    }

    // ?
    ImportKeyPairRequest request2 = new ImportKeyPairRequest();
    request2.withKeyName(keyName);
    request2.withPublicKeyMaterial(publicKey);
    ec2Client.importKeyPair(request2);

    log.info(keyName + "??????");
}