com.amazonaws.services.ec2.model.KeyPair Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.KeyPair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestEC2AutoRecovery.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final String vpcStackName = "vpc-2azs-" + this.random8String();
    final String stackName = "ec2-auto-recovery-" + this.random8String();
    final String classB = "10";
    final String keyName = "key-" + this.random8String();
    try {
        final KeyPair key = this.createKey(keyName);
        try {
            this.createStack(vpcStackName,
                    "vpc/vpc-2azs.yaml",
                    new Parameter().withParameterKey("ClassB").withParameterValue(classB)
            );
            try {
                this.createStack(stackName,
                        "ec2/ec2-auto-recovery.yaml",
                        new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName),
                        new Parameter().withParameterKey("KeyName").withParameterValue(keyName)
                );
                final String host = this.getStackOutputValue(stackName, "IPAddress");
                this.probeSSH(host, key);
            } finally {
                this.deleteStack(stackName);
            }
        } finally {
            this.deleteStack(vpcStackName);
        }
    } finally {
        this.deleteKey(keyName);
    }
}
 
Example #2
Source File: TestAL2MutablePublic.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final String vpcStackName = "vpc-2azs-" + this.random8String();
    final String stackName = "al2-mutable-public-" + this.random8String();
    final String classB = "10";
    final String keyName = "key-" + this.random8String();
    try {
        final KeyPair key = this.createKey(keyName);
        try {
            this.createStack(vpcStackName,
                    "vpc/vpc-2azs.yaml",
                    new Parameter().withParameterKey("ClassB").withParameterValue(classB)
            );
            try {
                this.createStack(stackName,
                        "ec2/al2-mutable-public.yaml",
                        new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName),
                        new Parameter().withParameterKey("KeyName").withParameterValue(keyName)
                );
                final String host = this.getStackOutputValue(stackName, "PublicIPAddress");
                this.probeSSH(host, key);
            } finally {
                this.deleteStack(stackName);
            }
        } finally {
            this.deleteStack(vpcStackName);
        }
    } finally {
        this.deleteKey(keyName);
    }
}
 
Example #3
Source File: ATest.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
protected final void probeSSH(final String host, final KeyPair key) {
    final Callable<Boolean> callable = () -> {
        final JSch jsch = new JSch();
        final Session session = jsch.getSession("ec2-user", host);
        jsch.addIdentity(key.getKeyName(), key.getKeyMaterial().getBytes(), null, null);
        jsch.setConfig("StrictHostKeyChecking", "no"); // for testing this should be fine. adding the host key seems to be only possible via a file which is not very useful here
        session.connect(10000);
        session.disconnect();
        return true;
    };
    Assert.assertTrue(this.retry(callable));
}
 
Example #4
Source File: ATest.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
protected final Session tunnelSSH(final String host, final KeyPair key, final Integer localPort, final String remoteHost, final Integer remotePort) throws JSchException {
    final JSch jsch = new JSch();
    final Session session = jsch.getSession("ec2-user", host);
    jsch.addIdentity(key.getKeyName(), key.getKeyMaterial().getBytes(), null, null);
    jsch.setConfig("StrictHostKeyChecking", "no"); // for testing this should be fine. adding the host key seems to be only possible via a file which is not very useful here
    session.setPortForwardingL(localPort, remoteHost, remotePort);
    session.connect(10000);
    return session;
}
 
Example #5
Source File: TestVPCSshBastion.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final String vpcStackName = "vpc-2azs-" + this.random8String();
    final String bastionStackName = "vpc-ssh-bastion-" + this.random8String();
    final String classB = "10";
    final String keyName = "key-" + this.random8String();
    try {
        final KeyPair key = this.createKey(keyName);
        try {
            this.createStack(vpcStackName,
                    "vpc/vpc-2azs.yaml",
                    new Parameter().withParameterKey("ClassB").withParameterValue(classB)
            );
            try {
                this.createStack(bastionStackName,
                        "vpc/vpc-ssh-bastion.yaml",
                        new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName),
                        new Parameter().withParameterKey("KeyName").withParameterValue(keyName)
                );
                final String host = this.getStackOutputValue(bastionStackName, "IPAddress");
                this.probeSSH(host, key);
            } finally {
                this.deleteStack(bastionStackName);
            }
        } finally {
            this.deleteStack(vpcStackName);
        }
    } finally {
        this.deleteKey(keyName);
    }
}
 
Example #6
Source File: TestVPCVpnBastion.java    From aws-cf-templates with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    final String vpcStackName = "vpc-2azs-" + this.random8String();
    final String bastionStackName = "vpc-vpn-bastion-" + this.random8String();
    final String classB = "10";
    final String keyName = "key-" + this.random8String();
    final String vpnPSK = this.random8String();
    final String vpnUserPassword = this.random8String();
    final String vpnAdminPassword = this.random8String();
    try {
        final KeyPair key = this.createKey(keyName);
        try {
            this.createStack(vpcStackName,
                    "vpc/vpc-2azs.yaml",
                    new Parameter().withParameterKey("ClassB").withParameterValue(classB)
            );
            try {
                this.createStack(bastionStackName,
                        "vpc/vpc-vpn-bastion.yaml",
                        new Parameter().withParameterKey("ParentVPCStack").withParameterValue(vpcStackName),
                        new Parameter().withParameterKey("KeyName").withParameterValue(keyName),
                        new Parameter().withParameterKey("VPNPSK").withParameterValue(vpnPSK),
                        new Parameter().withParameterKey("VPNUserName").withParameterValue("test"),
                        new Parameter().withParameterKey("VPNUserPassword").withParameterValue(vpnUserPassword),
                        new Parameter().withParameterKey("VPNAdminPassword").withParameterValue(vpnAdminPassword)
                );
                // TODO how can we check if this stack works?
            } finally {
                this.deleteStack(bastionStackName);
            }
        } finally {
            this.deleteStack(vpcStackName);
        }
    } finally {
        this.deleteKey(keyName);
    }
}
 
Example #7
Source File: KeyPairConverter.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected KeyPair convertObject(KeyPairInfo from) {
    KeyPair to = new KeyPair();

    to.setKeyName(from.getKeyName());
    to.setKeyMaterial(from.getKeyMaterial());
    to.setKeyFingerprint(from.getKeyFingerprint());

    return to;
}
 
Example #8
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/***
 * 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;
}