diff --git a/.doc_gen/metadata/ec2_metadata.yaml b/.doc_gen/metadata/ec2_metadata.yaml index fc424893507..e1568abd7ba 100644 --- a/.doc_gen/metadata/ec2_metadata.yaml +++ b/.doc_gen/metadata/ec2_metadata.yaml @@ -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: diff --git a/javav2/example_code/ec2/README.md b/javav2/example_code/ec2/README.md index d39ac28b398..017539272d7 100644 --- a/javav2/example_code/ec2/README.md +++ b/javav2/example_code/ec2/README.md @@ -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) diff --git a/javav2/example_code/ec2/src/main/java/com/example/ec2/GetPasswordData.java b/javav2/example_code/ec2/src/main/java/com/example/ec2/GetPasswordData.java new file mode 100644 index 00000000000..4b9f6405c20 --- /dev/null +++ b/javav2/example_code/ec2/src/main/java/com/example/ec2/GetPasswordData.java @@ -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: + + + 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. + * + *

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.

+ * + * @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] \ No newline at end of file diff --git a/javav2/example_code/ec2/src/test/java/EC2Test.java b/javav2/example_code/ec2/src/test/java/EC2Test.java index afa59b8d561..f282b898902 100644 --- a/javav2/example_code/ec2/src/test/java/EC2Test.java +++ b/javav2/example_code/ec2/src/test/java/EC2Test.java @@ -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. @@ -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; @@ -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()) @@ -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. @@ -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); @@ -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); @@ -370,6 +390,8 @@ class SecretValues { private String myIpAddressSc; + private String winServer; + public String getAmi() { return ami; } @@ -417,5 +439,7 @@ public String getVpcIdSc() { public String getMyIpAddressSc() { return myIpAddressSc; } + + public String getWinServer(){return winServer;} } }