Java源码示例:com.amazonaws.services.ec2.model.KeyPair

示例1
@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);
    }
}
 
示例2
@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);
    }
}
 
示例3
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));
}
 
示例4
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;
}
 
示例5
@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);
    }
}
 
示例6
@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);
    }
}
 
示例7
@Override
protected KeyPair convertObject(KeyPairInfo from) {
    KeyPair to = new KeyPair();

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

    return to;
}
 
示例8
/***
 * 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;
}