From 034c64c8013b44360fbc0cef83639741e6993455 Mon Sep 17 00:00:00 2001 From: Scott Macdonald <57190223+scmacdon@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:04:37 -0500 Subject: [PATCH] Add AWS ACM Java V2 files (#7014) --- .doc_gen/metadata/acm_metadata.yaml | 90 ++++++++++++ .doc_gen/validation.yaml | 2 + javav2/example_code/acm/.gitignore | 38 +++++ javav2/example_code/acm/README.md | 87 +++++++++++ javav2/example_code/acm/pom.xml | 137 ++++++++++++++++++ .../com/example/acm/AddTagsToCertificate.java | 62 ++++++++ .../main/java/com/example/acm/DeleteCert.java | 58 ++++++++ .../java/com/example/acm/DescribeCert.java | 67 +++++++++ .../com/example/acm/ExportCertificate.java | 110 ++++++++++++++ .../main/java/com/example/acm/ImportCert.java | 83 +++++++++++ .../java/com/example/acm/ListCertTags.java | 63 ++++++++ .../main/java/com/example/acm/ListCerts.java | 52 +++++++ .../com/example/acm/RemoveTagsFromCert.java | 64 ++++++++ .../main/java/com/example/acm/RenewCert.java | 59 ++++++++ .../java/com/example/acm/RequestCert.java | 50 +++++++ .../acm/src/test/java/ACMTests.java | 102 +++++++++++++ .../main/java/com/example/ppe/S3Service.java | 80 ---------- .../main/java/com/example/ppe/SendEmail.java | 76 ---------- 18 files changed, 1124 insertions(+), 156 deletions(-) create mode 100644 javav2/example_code/acm/.gitignore create mode 100644 javav2/example_code/acm/README.md create mode 100644 javav2/example_code/acm/pom.xml create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/AddTagsToCertificate.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/DeleteCert.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/DescribeCert.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/ExportCertificate.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/ImportCert.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/ListCertTags.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/ListCerts.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/RemoveTagsFromCert.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/RenewCert.java create mode 100644 javav2/example_code/acm/src/main/java/com/example/acm/RequestCert.java create mode 100644 javav2/example_code/acm/src/test/java/ACMTests.java delete mode 100644 javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/S3Service.java delete mode 100644 javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/SendEmail.java diff --git a/.doc_gen/metadata/acm_metadata.yaml b/.doc_gen/metadata/acm_metadata.yaml index 679119fc936..3ea5866bdcb 100644 --- a/.doc_gen/metadata/acm_metadata.yaml +++ b/.doc_gen/metadata/acm_metadata.yaml @@ -1,6 +1,15 @@ # zexi 0.4.0 acm_DescribeCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.describe_cert.main .NET: versions: - sdk_version: 3 @@ -52,6 +61,15 @@ acm_GetCertificate: acm: {GetCertificate} acm_ListCertificates: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.list_certs.main .NET: versions: - sdk_version: 3 @@ -82,6 +100,15 @@ acm_ListCertificates: acm: {ListCertificates} acm_ImportCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.import_cert.main Python: versions: - sdk_version: 3 @@ -103,6 +130,15 @@ acm_ImportCertificate: acm: {ImportCertificate} acm_DeleteCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.del_cert.main Python: versions: - sdk_version: 3 @@ -124,6 +160,15 @@ acm_DeleteCertificate: acm: {DeleteCertificate} acm_AddTagsToCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.add_tags.main Python: versions: - sdk_version: 3 @@ -145,6 +190,15 @@ acm_AddTagsToCertificate: acm: {AddTagsToCertificate} acm_ExportCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.export_cert.main C++: versions: - sdk_version: 1 @@ -157,6 +211,15 @@ acm_ExportCertificate: acm: {ExportCertificate} acm_ListTagsForCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.list_cert_tags.main Python: versions: - sdk_version: 3 @@ -178,6 +241,15 @@ acm_ListTagsForCertificate: acm: {ListTagsForCertificate} acm_RemoveTagsFromCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.remove_tags.main Python: versions: - sdk_version: 3 @@ -199,6 +271,15 @@ acm_RemoveTagsFromCertificate: acm: {RemoveTagsFromCertificate} acm_RequestCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.request_cert.main Python: versions: - sdk_version: 3 @@ -241,6 +322,15 @@ acm_ResendValidationEmail: acm: {ResendValidationEmail} acm_RenewCertificate: languages: + Java: + versions: + - sdk_version: 2 + github: javav2/example_code/acm + sdkguide: + excerpts: + - description: + snippet_tags: + - acm.java2.renew_cert.main C++: versions: - sdk_version: 1 diff --git a/.doc_gen/validation.yaml b/.doc_gen/validation.yaml index 53d97e95ab8..9a3e68e56e8 100644 --- a/.doc_gen/validation.yaml +++ b/.doc_gen/validation.yaml @@ -208,6 +208,8 @@ allow_list: - "aws/s3/model/AbortMultipartUploadRequest" - "src/main/kotlin/com/example/ecr/HelloECR" - "aws/ec2/model/DisassociateAddressRequest" + - "src/main/java/com/example/acm/DeleteCert" + - "src/main/java/com/example/acm/ImportCert" sample_files: - "README.md" - "chat_sfn_state_machine.json" diff --git a/javav2/example_code/acm/.gitignore b/javav2/example_code/acm/.gitignore new file mode 100644 index 00000000000..5ff6309b719 --- /dev/null +++ b/javav2/example_code/acm/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/javav2/example_code/acm/README.md b/javav2/example_code/acm/README.md new file mode 100644 index 00000000000..17b5c132881 --- /dev/null +++ b/javav2/example_code/acm/README.md @@ -0,0 +1,87 @@ +# ACM code examples for the SDK for Java 2.x + +## Overview + +Shows how to use the AWS SDK for Java 2.x to work with AWS Certificate Manager (ACM). + + + + +_ACM helps you to provision, manage, and renew publicly trusted TLS certificates on AWS based websites._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder. + + + + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [AddTagsToCertificate](src/main/java/com/example/acm/AddTagsToCertificate.java#L12) +- [DeleteCertificate](src/main/java/com/example/acm/DeleteCert.java#L10) +- [DescribeCertificate](src/main/java/com/example/acm/DescribeCert.java#L11) +- [ExportCertificate](src/main/java/com/example/acm/ExportCertificate.java#L20) +- [ImportCertificate](src/main/java/com/example/acm/ImportCert.java#L16) +- [ListCertificates](src/main/java/com/example/acm/ListCerts.java#L12) +- [ListTagsForCertificate](src/main/java/com/example/acm/ListCertTags.java#L13) +- [RemoveTagsFromCertificate](src/main/java/com/example/acm/RemoveTagsFromCert.java#L13) +- [RenewCertificate](src/main/java/com/example/acm/RenewCert.java#L10) +- [RequestCertificate](src/main/java/com/example/acm/RequestCert.java#L12) + + + + + +## Run the examples + +### Instructions + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `javav2` folder. + + + + + + +## Additional resources + +- [ACM User Guide](https://docs.aws.amazon.com/acm/latest/userguide/acm-overview.html) +- [ACM API Reference](https://docs.aws.amazon.com/acm/latest/APIReference/Welcome.html) +- [SDK for Java 2.x ACM reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/ec2/package-summary.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/javav2/example_code/acm/pom.xml b/javav2/example_code/acm/pom.xml new file mode 100644 index 00000000000..4ef90905781 --- /dev/null +++ b/javav2/example_code/acm/pom.xml @@ -0,0 +1,137 @@ + + + 4.0.0 + + org.example + acm + 1.0-SNAPSHOT + + + UTF-8 + 17 + 17 + 17 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + IntegrationTest + + + + + + + + software.amazon.awssdk + bom + 2.26.15 + pom + import + + + org.apache.logging.log4j + log4j-bom + 2.23.1 + pom + import + + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + software.amazon.awssdk + netty-nio-client + + + org.junit.jupiter + junit-jupiter-engine + 5.9.2 + test + + + org.junit.platform + junit-platform-commons + 1.9.2 + + + software.amazon.awssdk + secretsmanager + + + com.google.code.gson + gson + 2.10.1 + + + org.junit.platform + junit-platform-launcher + 1.9.2 + test + + + com.fasterxml.jackson.core + jackson-databind + 2.17.0 + + + software.amazon.awssdk + batch + + + software.amazon.awssdk + sso + + + software.amazon.awssdk + ssooidc + + + org.apache.logging.log4j + log4j-core + + + org.slf4j + slf4j-api + 2.0.13 + + + org.apache.logging.log4j + log4j-slf4j2-impl + + + software.amazon.awssdk + acm + + + org.apache.logging.log4j + log4j-1.2-api + + + software.amazon.awssdk + url-connection-client + 2.18.13 + + + \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/AddTagsToCertificate.java b/javav2/example_code/acm/src/main/java/com/example/acm/AddTagsToCertificate.java new file mode 100644 index 00000000000..135e3bbcfe7 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/AddTagsToCertificate.java @@ -0,0 +1,62 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.AddTagsToCertificateRequest; +import software.amazon.awssdk.services.acm.model.Tag; +import java.util.List; + +// snippet-start:[acm.java2.add_tags.main] + +/** + * 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 AddTagsToCertificate { + + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + addTags(certArn); + } + + /** + * Adds tags to a certificate in AWS Certificate Manager (ACM). + * + * @param certArn the Amazon Resource Name (ARN) of the certificate to add tags to + */ + public static void addTags(String certArn) { + AcmClient acmClient = AcmClient.create(); + List expectedTags = List.of(Tag.builder().key("key").value("value").build()); + AddTagsToCertificateRequest addTagsToCertificateRequest = AddTagsToCertificateRequest.builder() + .certificateArn(certArn) + .tags(expectedTags) + .build(); + + try { + acmClient.addTagsToCertificate(addTagsToCertificateRequest); + System.out.println("Successfully added tags to a certificate"); + } catch (AcmException e) { + System.out.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.add_tags.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/DeleteCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/DeleteCert.java new file mode 100644 index 00000000000..4a3ae1bcef0 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/DeleteCert.java @@ -0,0 +1,58 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.DeleteCertificateRequest; + +// snippet-start:[acm.java2.del_cert.main] +/** + * 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 DeleteCert { + + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + deleteCertificate(certArn); + } + + /** + * Deletes an SSL/TLS certificate from the AWS Certificate Manager (ACM). + * + * @param certArn the Amazon Resource Name (ARN) of the certificate to be deleted + */ + public static void deleteCertificate( String certArn) { + AcmClient acmClient = AcmClient.create(); + DeleteCertificateRequest request = DeleteCertificateRequest.builder() + .certificateArn(certArn) + .build(); + + try { + acmClient.deleteCertificate(request); + System.out.println("The certificate was deleted"); + + } catch (AcmException e) { + System.out.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.del_cert.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/DescribeCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/DescribeCert.java new file mode 100644 index 00000000000..1cdb318d255 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/DescribeCert.java @@ -0,0 +1,67 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.DescribeCertificateRequest; +import software.amazon.awssdk.services.acm.model.DescribeCertificateResponse; + +// snippet-start:[acm.java2.describe_cert.main] + +/** + * 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 DescribeCert { + + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + describeCertificate(certArn); + } + + /** + * Describes the details of an SSL/TLS certificate. + * + * @param certArn the Amazon Resource Name (ARN) of the certificate to describe + * @throws AcmException if an error occurs while describing the certificate + */ + public static void describeCertificate(String certArn) { + AcmClient acmClient = AcmClient.create(); + DescribeCertificateRequest req = DescribeCertificateRequest.builder() + .certificateArn(certArn) + .build(); + + try { + DescribeCertificateResponse response = acmClient.describeCertificate(req); + + // Print the certificate details. + System.out.println("Certificate ARN: " + response.certificate().certificateArn()); + System.out.println("Domain Name: " + response.certificate().domainName()); + System.out.println("Issued By: " + response.certificate().issuer()); + System.out.println("Issued On: " + response.certificate().issuedAt()); + System.out.println("Status: " + response.certificate().status()); + } catch (AcmException e) { + System.out.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.describe_cert.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/ExportCertificate.java b/javav2/example_code/acm/src/main/java/com/example/acm/ExportCertificate.java new file mode 100644 index 00000000000..fceffbe7713 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/ExportCertificate.java @@ -0,0 +1,110 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.ExportCertificateRequest; +import software.amazon.awssdk.services.acm.model.ExportCertificateResponse; +import software.amazon.awssdk.services.acm.model.InvalidArnException; +import software.amazon.awssdk.services.acm.model.InvalidTagException; +import software.amazon.awssdk.services.acm.model.ResourceNotFoundException; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +// snippet-start:[acm.java2.export_cert.main] + +/** + * 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 ExportCertificate { + + public static void main(String[] args) throws Exception { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + exportCert(certArn); + } + + /** + * Exports an SSL/TLS certificate and its associated private key and certificate chain from AWS Certificate Manager (ACM). + * + * @param certArn The Amazon Resource Name (ARN) of the certificate that you want to export. + * @throws IOException If an I/O error occurs while reading the private key passphrase file or exporting the certificate. + */ + public static void exportCert(String certArn) throws IOException { + AcmClient acmClient = AcmClient.create(); + + // Initialize a file descriptor for the passphrase file. + RandomAccessFile filePassphrase = null; + ByteBuffer bufPassphrase = null; + + // Create a file stream for reading the private key passphrase. + try { + filePassphrase = new RandomAccessFile("C:\\AWS\\password.txt", "r"); + } catch (IllegalArgumentException | SecurityException | FileNotFoundException ex) { + throw ex; + } + + // Create a channel to map the file. + FileChannel channelPassphrase = filePassphrase.getChannel(); + + // Map the file to the buffer. + try { + bufPassphrase = channelPassphrase.map(FileChannel.MapMode.READ_ONLY, 0, channelPassphrase.size()); + channelPassphrase.close(); + filePassphrase.close(); + } catch (IOException ex) { + throw ex; + } + + // Create a request object. + ExportCertificateRequest req = ExportCertificateRequest.builder() + .certificateArn(certArn) + .passphrase(SdkBytes.fromByteBuffer(bufPassphrase)) + .build(); + + // Export the certificate. + ExportCertificateResponse result = null; + try { + result = acmClient.exportCertificate(req); + } catch (InvalidArnException | InvalidTagException | ResourceNotFoundException ex) { + throw ex; + } + + // Clear the buffer. + bufPassphrase.clear(); + + // Display the certificate and certificate chain. + String certificate = result.certificate(); + System.out.println(certificate); + + String certificateChain = result.certificateChain(); + System.out.println(certificateChain); + + // This example retrieves but does not display the private key. + String privateKey = result.privateKey(); + System.out.println("The example is complete"); + } +} +// snippet-end:[acm.java2.export_cert.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/ImportCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/ImportCert.java new file mode 100644 index 00000000000..bb69c094483 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/ImportCert.java @@ -0,0 +1,83 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.ImportCertificateRequest; +import software.amazon.awssdk.services.acm.model.ImportCertificateResponse; +import software.amazon.awssdk.utils.IoUtils; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; + +// snippet-start:[acm.java2.import_cert.main] +/** + * 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 ImportCert { + + public static void main(String[] args) { + + final String usage = """ + + Usage: + + Where: + certificatePath - the path to the SSL/TLS certificate file. + privateKeyPath - the path to the private key file associated with the SSL/TLS certificate. + """; + + if (args.length != 2) { + System.out.println(usage); + return; + } + + String certificatePath = args[0]; + String privateKeyPath = args[1]; + String certificateArn = importCertificate(certificatePath, privateKeyPath); + System.out.println("Certificate imported with ARN: " + certificateArn); + } + + /** + * Imports an SSL/TLS certificate and private key into AWS Certificate Manager (ACM) for use with + * AWS services. + * + * @param certificatePath the file path to the SSL/TLS certificate + * @param privateKeyPath the file path to the private key associated with the certificate + * @throws IOException if there is an error reading the certificate or private key files + */ + public static String importCertificate(String certificatePath, String privateKeyPath) { + AcmClient acmClient = AcmClient.create(); + try { + byte[] certificateBytes = readFileBytes(certificatePath); + byte[] privateKeyBytes = readFileBytes(privateKeyPath); + + ImportCertificateRequest request = ImportCertificateRequest.builder() + .certificate(SdkBytes.fromByteBuffer(ByteBuffer.wrap(certificateBytes))) + .privateKey(SdkBytes.fromByteBuffer(ByteBuffer.wrap(privateKeyBytes))) + .build(); + + ImportCertificateResponse response = acmClient.importCertificate(request); + String certificateArn = response.certificateArn(); + return certificateArn; + } catch (IOException e) { + System.err.println("Error reading certificate or private key file: " + e.getMessage()); + } + return ""; + } + + private static byte[] readFileBytes(String filePath) throws IOException { + try (InputStream inputStream = new FileInputStream(filePath)) { + return IoUtils.toByteArray(inputStream); + } + } +} +// snippet-end:[acm.java2.import_cert.main] diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/ListCertTags.java b/javav2/example_code/acm/src/main/java/com/example/acm/ListCertTags.java new file mode 100644 index 00000000000..a058671ff11 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/ListCertTags.java @@ -0,0 +1,63 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.ListTagsForCertificateRequest; +import software.amazon.awssdk.services.acm.model.ListTagsForCertificateResponse; +import software.amazon.awssdk.services.acm.model.Tag; + +import java.util.List; + +// snippet-start:[acm.java2.list_cert_tags.main] + +/** + * 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 ListCertTags { + + public static void main(String[] args) { + + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + listCertTags(certArn); + } + + /** + * Lists the tags associated with an AWS Certificate Manager (ACM) certificate. + * + * @param certArn the Amazon Resource Name (ARN) of the ACM certificate + */ + public static void listCertTags(String certArn) { + AcmClient acmClient = AcmClient.create(); + + ListTagsForCertificateRequest request = ListTagsForCertificateRequest.builder() + .certificateArn(certArn) + .build(); + + ListTagsForCertificateResponse response = acmClient.listTagsForCertificate(request); + List tagList = response.tags(); + tagList.forEach(tag -> { + System.out.println("Key: " + tag.key()); + System.out.println("Value: " + tag.value()); + }); + } +} +// snippet-end:[acm.java2.list_cert_tags.main] diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/ListCerts.java b/javav2/example_code/acm/src/main/java/com/example/acm/ListCerts.java new file mode 100644 index 00000000000..3d35cf7bed2 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/ListCerts.java @@ -0,0 +1,52 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.CertificateStatus; +import software.amazon.awssdk.services.acm.model.ListCertificatesRequest; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.paginators.ListCertificatesIterable; + +// snippet-start:[acm.java2.list_certs.main] +/** + * 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 ListCerts { + public static void main(String[] args) { + listCertificates(); + } + + /** + * Lists all the certificates managed by AWS Certificate Manager (ACM) that have a status of "ISSUED". + */ + public static void listCertificates() { + AcmClient acmClient = AcmClient.create(); + try { + ListCertificatesRequest listRequest = ListCertificatesRequest.builder() + .certificateStatuses(CertificateStatus.ISSUED) + .maxItems(100) + .build(); + ListCertificatesIterable listResponse = acmClient.listCertificatesPaginator(listRequest); + + // Print the certificate details using streams + listResponse.certificateSummaryList().stream() + .forEach(certificate -> { + System.out.println("Certificate ARN: " + certificate.certificateArn()); + System.out.println("Certificate Domain Name: " + certificate.domainName()); + System.out.println("Certificate Status: " + certificate.statusAsString()); + System.out.println("---"); + }); + + } catch (AcmException e) { + System.err.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.list_certs.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/RemoveTagsFromCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/RemoveTagsFromCert.java new file mode 100644 index 00000000000..12c4f4a3000 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/RemoveTagsFromCert.java @@ -0,0 +1,64 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.RemoveTagsFromCertificateRequest; +import software.amazon.awssdk.services.acm.model.Tag; + +import java.util.List; + +// snippet-start:[acm.java2.remove_tags.main] + +/** + * 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 RemoveTagsFromCert { + + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + removeTags(certArn); + } + + /** + * Removes tags from an AWS Certificate Manager (ACM) certificate. + * + * @param certArn the Amazon Resource Name (ARN) of the certificate from which to remove tags + */ + public static void removeTags(String certArn) { + AcmClient acmClient = AcmClient.create(); + List expectedTags = List.of(Tag.builder().key("key").value("value").build()); + RemoveTagsFromCertificateRequest req = RemoveTagsFromCertificateRequest.builder() + .certificateArn(certArn) + .tags(expectedTags) + .build(); + + try { + acmClient.removeTagsFromCertificate(req); + System.out.println("Successfully removed tags from the certificate"); + } catch (AcmException e) { + System.err.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.remove_tags.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/RenewCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/RenewCert.java new file mode 100644 index 00000000000..2188eccda90 --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/RenewCert.java @@ -0,0 +1,59 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.RenewCertificateRequest; + +// snippet-start:[acm.java2.renew_cert.main] +/** + * 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 RenewCert { + public static void main(String[] args) { + final String usage = """ + + Usage: + + Where: + certArn - the ARN of the certificate. + """; + if (args.length != 1) { + System.out.println(usage); + return; + } + + String certArn = args[0]; + renewCertificate(certArn); + } + + /** + * Renews an existing SSL/TLS certificate in AWS Certificate Manager (ACM). + * + * @param certArn The Amazon Resource Name (ARN) of the certificate to be renewed. + * @throws AcmException If there is an error renewing the certificate. + */ + public static void renewCertificate(String certArn) { + AcmClient acmClient = AcmClient.create(); + + RenewCertificateRequest certificateRequest = RenewCertificateRequest.builder() + .certificateArn(certArn) + .build(); + + try { + acmClient.renewCertificate(certificateRequest); + System.out.println("The certificate was renewed"); + } catch(AcmException e){ + System.out.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.renew_cert.main] \ No newline at end of file diff --git a/javav2/example_code/acm/src/main/java/com/example/acm/RequestCert.java b/javav2/example_code/acm/src/main/java/com/example/acm/RequestCert.java new file mode 100644 index 00000000000..afea06efcda --- /dev/null +++ b/javav2/example_code/acm/src/main/java/com/example/acm/RequestCert.java @@ -0,0 +1,50 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.acm; + +import software.amazon.awssdk.services.acm.AcmClient; +import software.amazon.awssdk.services.acm.model.AcmException; +import software.amazon.awssdk.services.acm.model.RequestCertificateRequest; +import software.amazon.awssdk.services.acm.model.RequestCertificateResponse; +import java.util.ArrayList; + +// snippet-start:[acm.java2.request_cert.main] + +/** + * 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 RequestCert { + + public static void main(String[] args) { + requestCertificate(); + } + + /** + * Requests a certificate from the AWS Certificate Manager (ACM) service. + */ + public static void requestCertificate() { + AcmClient acmClient = AcmClient.create(); + ArrayList san = new ArrayList<>(); + san.add("www.example.com"); + + RequestCertificateRequest req = RequestCertificateRequest.builder() + .domainName("example.com") + .idempotencyToken("1Aq25pTy") + .subjectAlternativeNames(san) + .build(); + + try { + RequestCertificateResponse response = acmClient.requestCertificate(req); + System.out.println("Cert ARN IS " + response.certificateArn()); + } catch (AcmException e) { + System.err.println(e.getMessage()); + } + } +} +// snippet-end:[acm.java2.request_cert.main] diff --git a/javav2/example_code/acm/src/test/java/ACMTests.java b/javav2/example_code/acm/src/test/java/ACMTests.java new file mode 100644 index 00000000000..81437448bdc --- /dev/null +++ b/javav2/example_code/acm/src/test/java/ACMTests.java @@ -0,0 +1,102 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + + +import com.example.acm.AddTagsToCertificate; +import com.example.acm.DeleteCert; +import com.example.acm.DescribeCert; +import com.example.acm.ImportCert; +import com.example.acm.ListCertTags; +import com.example.acm.RemoveTagsFromCert; +import com.example.acm.RequestCert; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestMethodOrder; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@TestInstance(TestInstance.Lifecycle.PER_METHOD) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class ACMTests { + + private static String certificatePath = ""; + private static String privateKeyPath = ""; + + private static String certificateArn; + + @BeforeAll + public static void setUp() { + + certificatePath = "C:\\Users\\scmacdon\\cert_example\\certificate.pem"; + privateKeyPath = "C:\\Users\\scmacdon\\cert_example\\private_key.pem"; + } + + @Test + @Tag("IntegrationTest") + @Order(1) + public void testImportCert() { + assertDoesNotThrow(() -> { + certificateArn = ImportCert.importCertificate(certificatePath, privateKeyPath); + assertNotNull(certificateArn); + }); + } + + @Test + @Tag("IntegrationTest") + @Order(2) + public void testAddTags() { + assertDoesNotThrow(() -> { + AddTagsToCertificate.addTags(certificateArn); + }); + } + + @Test + @Tag("IntegrationTest") + @Order(3) + public void testDescribeCert() { + assertDoesNotThrow(() -> { + DescribeCert.describeCertificate(certificateArn); + }); + } + + @Test + @Tag("IntegrationTest") + @Order(4) + public void testListCertTags() { + assertDoesNotThrow(() -> { + ListCertTags.listCertTags(certificateArn); + }); + } + + @Test + @Tag("IntegrationTest") + @Order(5) + public void testRemoveTagsFromCert() { + assertDoesNotThrow(() -> { + RemoveTagsFromCert.removeTags(certificateArn); + }); + } + + + @Test + @Tag("IntegrationTest") + @Order(6) + public void testRequestCert() { + assertDoesNotThrow(() -> { + RequestCert.requestCertificate(); + }); + } + + @Test + @Tag("IntegrationTest") + @Order(7) + public void testDeleteCert() { + assertDoesNotThrow(() -> { + DeleteCert.deleteCertificate(certificateArn); + }); + } +} diff --git a/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/S3Service.java b/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/S3Service.java deleted file mode 100644 index 7068de36a75..00000000000 --- a/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/S3Service.java +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.ppe; - -import software.amazon.awssdk.core.ResponseBytes; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.s3.S3Client; -import software.amazon.awssdk.services.s3.model.GetObjectRequest; -import software.amazon.awssdk.services.s3.model.GetObjectResponse; -import software.amazon.awssdk.services.s3.model.S3Exception; -import software.amazon.awssdk.services.s3.model.ListObjectsResponse; -import software.amazon.awssdk.services.s3.model.S3Object; -import software.amazon.awssdk.services.s3.model.ListObjectsRequest; -import java.util.ArrayList; -import java.util.List; - -public class S3Service { - - private S3Client getClient() { - - Region region = Region.US_WEST_2; - return S3Client.builder() - .region(region) - .build(); - } - - public byte[] getObjectBytes(String bucketName, String keyName) { - - S3Client s3 = getClient(); - - try { - - GetObjectRequest objectRequest = GetObjectRequest - .builder() - .key(keyName) - .bucket(bucketName) - .build(); - - // Return the byte[] from this object. - ResponseBytes objectBytes = s3.getObjectAsBytes(objectRequest); - return objectBytes.asByteArray(); - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - return null; - } - - // Returns the names of all images in the given bucket. - public List listBucketObjects(String bucketName) { - - S3Client s3 = getClient(); - String keyName; - - List keys = new ArrayList<>(); - - try { - ListObjectsRequest listObjects = ListObjectsRequest - .builder() - .bucket(bucketName) - .build(); - - ListObjectsResponse res = s3.listObjects(listObjects); - List objects = res.contents(); - - for (S3Object myValue : objects) { - keyName = myValue.key(); - keys.add(keyName); - } - return keys; - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - return null; - } -} diff --git a/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/SendEmail.java b/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/SendEmail.java deleted file mode 100644 index a89dff146da..00000000000 --- a/javav2/usecases/creating_lambda_ppe/src/main/java/com/example/ppe/SendEmail.java +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.ppe; - -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.ses.SesClient; -import software.amazon.awssdk.services.ses.model.Destination; -import software.amazon.awssdk.services.ses.model.Content; -import software.amazon.awssdk.services.ses.model.SendEmailRequest; -import software.amazon.awssdk.services.ses.model.SesException; -import software.amazon.awssdk.services.ses.model.Message; -import software.amazon.awssdk.services.ses.model.Body; -import java.util.Set; - -public class SendEmail { - - public void sendMsg(Set unqiueKeys) { - - Region region = Region.US_EAST_1; - SesClient client = SesClient.builder() - .region(region) - .build(); - - String sender = ""; - String recipient = ""; - - // Set the HTML body. - String bodyHTML = "

The following images contains PPE gear " + - "

    "; - - // Persist the data into a DynamoDB table. - for (String myKey : unqiueKeys) { - bodyHTML = bodyHTML + "
  1. " + myKey + "
  2. "; - - } - - bodyHTML = bodyHTML + "

"; - Destination destination = Destination.builder() - .toAddresses(recipient) - .build(); - - Content content = Content.builder() - .data(bodyHTML) - .build(); - - Content sub = Content.builder() - .data("PPE Information") - .build(); - - Body body = Body.builder() - .html(content) - .build(); - - Message msg = Message.builder() - .subject(sub) - .body(body) - .build(); - - SendEmailRequest emailRequest = SendEmailRequest.builder() - .destination(destination) - .message(msg) - .source(sender) - .build(); - - try { - System.out.println("Attempting to send an email through Amazon SES " - + "using the AWS SDK for Java..."); - client.sendEmail(emailRequest); - - } catch (SesException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } -}