Skip to content

Commit

Permalink
Add example to get an EC2 password (awsdocs#6685)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored Jul 26, 2024
1 parent 8025bf7 commit 5512ed9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .doc_gen/metadata/ec2_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ ec2_Hello:
- ec2.ruby.hello_ec2
services:
ec2: {DescribeSecurityGroups}
ec2_GetPasswordData:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/ec2
sdkguide:
excerpts:
- description:
snippet_tags:
- ec2.java2.get_password.main
services:
ec2: {GetPasswordData}
ec2_CreateKeyPair:
languages:
.NET:
Expand Down
1 change: 1 addition & 0 deletions javav2/example_code/ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Code excerpts that show you how to call individual service functions.
- [DescribeKeyPairs](src/main/java/com/example/ec2/EC2Scenario.java#L623)
- [DescribeSecurityGroups](src/main/java/com/example/ec2/EC2Scenario.java#L556)
- [DisassociateAddress](src/main/java/com/example/ec2/EC2Scenario.java#L314)
- [GetPasswordData](src/main/java/com/example/ec2/GetPasswordData.java#L7)
- [ReleaseAddress](src/main/java/com/example/ec2/EC2Scenario.java#L298)
- [RunInstances](src/main/java/com/example/ec2/CreateInstance.java#L6)
- [StartInstances](src/main/java/com/example/ec2/EC2Scenario.java#L368)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0


package com.example.ec2;

// snippet-start:[ec2.java2.get_password.main]
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkServiceException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.model.GetPasswordDataRequest;
import software.amazon.awssdk.services.ec2.model.GetPasswordDataResponse;
import software.amazon.awssdk.services.secretsmanager.model.ResourceNotFoundException;

/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class GetPasswordData {

public static void main(String[] args) {
final String usage = """
Usage:
<instanceId>
Where:
instanceId - An instance id from which the password is obtained.\s
""";

if (args.length != 1) {
System.out.println(usage);
return;
}

Region region = Region.US_EAST_1;
Ec2Client ec2 = Ec2Client.builder()
.region(region)
.build();

String instanceId = args[0];
getPasswordData(ec2,instanceId);
}

/**
* Retrieves and prints the encrypted administrator password data for a specified EC2 instance.
*
* <p>The password data is encrypted using the key pair that was specified when the instance was launched.
* To decrypt the password data, you can use the private key of the key pair.</p>
*
* @param ec2 The {@link Ec2Client} to use for making the request.
* @param instanceId The ID of the instance for which to get the encrypted password data.
*/
public static void getPasswordData(Ec2Client ec2,String instanceId) {
GetPasswordDataRequest getPasswordDataRequest = GetPasswordDataRequest.builder()
.instanceId(instanceId)
.build();

try {
GetPasswordDataResponse getPasswordDataResponse = ec2.getPasswordData(getPasswordDataRequest);
String encryptedPasswordData = getPasswordDataResponse.passwordData();
System.out.println("Encrypted Password Data: " + encryptedPasswordData);

} catch (Ec2Exception e) {
String errorCode = e.awsErrorDetails().errorCode();
if (errorCode.matches("InvalidInstanceID.NotFound")) {
System.err.println("Instance ID not found, unable to retrieve password data.");
} else {
System.err.println("There was a problem retrieving password data. Details:");
e.printStackTrace();
}
}
}
}
// snippet-end:[ec2.java2.get_password.main]
26 changes: 25 additions & 1 deletion javav2/example_code/ec2/src/test/java/EC2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public class EC2Test {

private static Ec2Client ec2;

private static Ec2Client ec2East;
private static SsmClient ssmClient;

// Define the data members required for the tests.
Expand All @@ -44,6 +46,8 @@ public class EC2Test {
private static String vpcIdSc = "";
private static String myIpAddressSc = "";

private static String winServer = "";

@BeforeAll
public static void setUp() throws IOException {
Region region = Region.US_WEST_2;
Expand All @@ -52,6 +56,12 @@ public static void setUp() throws IOException {
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();

Region regionEast = Region.US_EAST_1;
ec2East = Ec2Client.builder()
.region(regionEast)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
.build();

ssmClient = SsmClient.builder()
.region(region)
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
Expand All @@ -73,6 +83,7 @@ public static void setUp() throws IOException {
groupNameSc = values.getGroupDescSc() + java.util.UUID.randomUUID();
vpcIdSc = values.getVpcIdSc();
myIpAddressSc = values.getMyIpAddressSc();
winServer = values.getWinServer();

// Uncomment this code block if you prefer using a config.properties file to
// retrieve AWS values required for these tests.
Expand Down Expand Up @@ -221,7 +232,15 @@ public void TerminateInstance() {
@Test
@Tag("IntegrationTest")
@Order(15)
public void TestEC2Scenario() throws InterruptedException {
public void testGetPassword() {
GetPasswordData.getPasswordData(ec2East, winServer);
System.out.println(EC2Scenario.DASHES);
}

@Test
@Tag("IntegrationTest")
@Order(16)
public void TestEC2Scenario() {
System.out.println(EC2Scenario.DASHES);
System.out.println("1. Create an RSA key pair and save the private key material as a .pem file.");
EC2Scenario.createKeyPair(ec2, keyNameSc, fileNameSc);
Expand Down Expand Up @@ -314,6 +333,7 @@ public void TestEC2Scenario() throws InterruptedException {
EC2Scenario.releaseEC2Address(ec2, allocationId);
System.out.println(EC2Scenario.DASHES);


System.out.println(EC2Scenario.DASHES);
System.out.println("15. Terminate the instance.");
EC2Scenario.terminateEC2(ec2, newInstanceId);
Expand Down Expand Up @@ -370,6 +390,8 @@ class SecretValues {

private String myIpAddressSc;

private String winServer;

public String getAmi() {
return ami;
}
Expand Down Expand Up @@ -417,5 +439,7 @@ public String getVpcIdSc() {
public String getMyIpAddressSc() {
return myIpAddressSc;
}

public String getWinServer(){return winServer;}
}
}

0 comments on commit 5512ed9

Please sign in to comment.