AWS code-library documentation change
Summary
Updated ACM certificate import example to use S3 as certificate source instead of local files. Changed parameters from file paths to S3 bucket/object keys, added S3 client integration, and implemented S3 download logic.
Security assessment
The change demonstrates secure handling of certificates through S3 storage rather than local files, but there's no evidence of addressing a specific vulnerability. It documents security best practices for certificate management.
Diff
diff --git a/code-library/latest/ug/java_2_acm_code_examples.md b/code-library/latest/ug/java_2_acm_code_examples.md index 090ae22f4..f8585ab7f 100644 --- a//code-library/latest/ug/java_2_acm_code_examples.md +++ b//code-library/latest/ug/java_2_acm_code_examples.md @@ -358,2 +357 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - - Usage: <certificatePath> <privateKeyPath> + Usage: <bucketName> <certificateKey> <privateKeyKey> @@ -362,2 +360,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - certificatePath - the path to the SSL/TLS certificate file. - privateKeyPath - the path to the private key file associated with the SSL/TLS certificate. + bucketName - The name of the S3 bucket containing the certificate and private key. + certificateKey - The object key for the SSL/TLS certificate file in S3. + privateKeyKey - The object key for the private key file in S3. @@ -366,4 +365,8 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - if (args.length != 2) { - System.out.println(usage); - return; - } + //if (args.length != 3) { + // System.out.println(usage); + // return; + // } + + String bucketName = "certbucket100" ; //args[0]; + String certificateKey = "certificate.pem" ; // args[1]; + String privateKeyKey = "private_key.pem" ; //args[2]; @@ -371,3 +374 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - String certificatePath = args[0]; - String privateKeyPath = args[1]; - String certificateArn = importCertificate(certificatePath, privateKeyPath); + String certificateArn = importCertificate(bucketName, certificateKey, privateKeyKey); @@ -378,2 +379 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - * Imports an SSL/TLS certificate and private key into AWS Certificate Manager (ACM) for use with - * AWS services. + * Imports an SSL/TLS certificate and private key from S3 into AWS Certificate Manager (ACM). @@ -381,3 +381,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - * @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 + * @param bucketName The name of the S3 bucket. + * @param certificateKey The key for the SSL/TLS certificate file in S3. + * @param privateKeyKey The key for the private key file in S3. + * @return The ARN of the imported certificate. @@ -385 +386 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - public static String importCertificate(String certificatePath, String privateKeyPath) { + public static String importCertificate(String bucketName, String certificateKey, String privateKeyKey) { @@ -386,0 +388,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + S3Client s3Client = S3Client.create(); + @@ -388,2 +391,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - byte[] certificateBytes = readFileBytes(certificatePath); - byte[] privateKeyBytes = readFileBytes(privateKeyPath); + byte[] certificateBytes = downloadFileFromS3(s3Client, bucketName, certificateKey); + byte[] privateKeyBytes = downloadFileFromS3(s3Client, bucketName, privateKeyKey); @@ -397,2 +400,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - String certificateArn = response.certificateArn(); - return certificateArn; + return response.certificateArn(); + @@ -400 +403,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - System.err.println("Error reading certificate or private key file: " + e.getMessage()); + System.err.println("Error downloading certificate or private key from S3: " + e.getMessage()); + } catch (S3Exception e) { + System.err.println("S3 error: " + e.awsErrorDetails().errorMessage()); @@ -405,3 +410,19 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - private static byte[] readFileBytes(String filePath) throws IOException { - try (InputStream inputStream = new FileInputStream(filePath)) { - return IoUtils.toByteArray(inputStream); + /** + * Downloads a file from Amazon S3 and returns its contents as a byte array. + * + * @param s3Client The S3 client. + * @param bucketName The name of the S3 bucket. + * @param objectKey The key of the object in S3. + * @return The file contents as a byte array. + * @throws IOException If an I/O error occurs. + */ + private static byte[] downloadFileFromS3(S3Client s3Client, String bucketName, String objectKey) throws IOException { + GetObjectRequest getObjectRequest = GetObjectRequest.builder() + .bucket(bucketName) + .key(objectKey) + .build(); + + try (ResponseInputStream<GetObjectResponse> s3Object = s3Client.getObject(getObjectRequest); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { + IoUtils.copy(s3Object, byteArrayOutputStream); + return byteArrayOutputStream.toByteArray();