|
3 | 3 |
|
4 | 4 | package com.example.acm; |
5 | 5 |
|
| 6 | +import software.amazon.awssdk.core.ResponseInputStream; |
6 | 7 | import software.amazon.awssdk.core.SdkBytes; |
7 | 8 | import software.amazon.awssdk.services.acm.AcmClient; |
8 | 9 | import software.amazon.awssdk.services.acm.model.ImportCertificateRequest; |
9 | 10 | import software.amazon.awssdk.services.acm.model.ImportCertificateResponse; |
| 11 | +import software.amazon.awssdk.services.s3.S3Client; |
| 12 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 13 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 14 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
10 | 15 | import software.amazon.awssdk.utils.IoUtils; |
11 | | -import java.io.FileInputStream; |
| 16 | + |
| 17 | +import java.io.ByteArrayOutputStream; |
12 | 18 | import java.io.IOException; |
13 | | -import java.io.InputStream; |
14 | 19 | import java.nio.ByteBuffer; |
15 | 20 |
|
16 | 21 | // snippet-start:[acm.java2.import_cert.main] |
|
25 | 30 | public class ImportCert { |
26 | 31 |
|
27 | 32 | public static void main(String[] args) { |
28 | | - |
29 | 33 | final String usage = """ |
30 | | -
|
31 | | - Usage: <certificatePath> <privateKeyPath> |
32 | | -
|
| 34 | + Usage: <bucketName> <certificateKey> <privateKeyKey> |
| 35 | + |
33 | 36 | Where: |
34 | | - certificatePath - the path to the SSL/TLS certificate file. |
35 | | - privateKeyPath - the path to the private key file associated with the SSL/TLS certificate. |
| 37 | + bucketName - The name of the S3 bucket containing the certificate and private key. |
| 38 | + certificateKey - The object key for the SSL/TLS certificate file in S3. |
| 39 | + privateKeyKey - The object key for the private key file in S3. |
36 | 40 | """; |
37 | 41 |
|
38 | | - if (args.length != 2) { |
39 | | - System.out.println(usage); |
40 | | - return; |
41 | | - } |
| 42 | + //if (args.length != 3) { |
| 43 | + // System.out.println(usage); |
| 44 | + // return; |
| 45 | + // } |
| 46 | + |
| 47 | + String bucketName = "certbucket100" ; //args[0]; |
| 48 | + String certificateKey = "certificate.pem" ; // args[1]; |
| 49 | + String privateKeyKey = "private_key.pem" ; //args[2]; |
42 | 50 |
|
43 | | - String certificatePath = args[0]; |
44 | | - String privateKeyPath = args[1]; |
45 | | - String certificateArn = importCertificate(certificatePath, privateKeyPath); |
| 51 | + String certificateArn = importCertificate(bucketName, certificateKey, privateKeyKey); |
46 | 52 | System.out.println("Certificate imported with ARN: " + certificateArn); |
47 | 53 | } |
48 | 54 |
|
49 | 55 | /** |
50 | | - * Imports an SSL/TLS certificate and private key into AWS Certificate Manager (ACM) for use with |
51 | | - * AWS services. |
| 56 | + * Imports an SSL/TLS certificate and private key from S3 into AWS Certificate Manager (ACM). |
52 | 57 | * |
53 | | - * @param certificatePath the file path to the SSL/TLS certificate |
54 | | - * @param privateKeyPath the file path to the private key associated with the certificate |
55 | | - * @throws IOException if there is an error reading the certificate or private key files |
| 58 | + * @param bucketName The name of the S3 bucket. |
| 59 | + * @param certificateKey The key for the SSL/TLS certificate file in S3. |
| 60 | + * @param privateKeyKey The key for the private key file in S3. |
| 61 | + * @return The ARN of the imported certificate. |
56 | 62 | */ |
57 | | - public static String importCertificate(String certificatePath, String privateKeyPath) { |
| 63 | + public static String importCertificate(String bucketName, String certificateKey, String privateKeyKey) { |
58 | 64 | AcmClient acmClient = AcmClient.create(); |
| 65 | + S3Client s3Client = S3Client.create(); |
| 66 | + |
59 | 67 | try { |
60 | | - byte[] certificateBytes = readFileBytes(certificatePath); |
61 | | - byte[] privateKeyBytes = readFileBytes(privateKeyPath); |
| 68 | + byte[] certificateBytes = downloadFileFromS3(s3Client, bucketName, certificateKey); |
| 69 | + byte[] privateKeyBytes = downloadFileFromS3(s3Client, bucketName, privateKeyKey); |
62 | 70 |
|
63 | 71 | ImportCertificateRequest request = ImportCertificateRequest.builder() |
64 | | - .certificate(SdkBytes.fromByteBuffer(ByteBuffer.wrap(certificateBytes))) |
65 | | - .privateKey(SdkBytes.fromByteBuffer(ByteBuffer.wrap(privateKeyBytes))) |
66 | | - .build(); |
| 72 | + .certificate(SdkBytes.fromByteBuffer(ByteBuffer.wrap(certificateBytes))) |
| 73 | + .privateKey(SdkBytes.fromByteBuffer(ByteBuffer.wrap(privateKeyBytes))) |
| 74 | + .build(); |
67 | 75 |
|
68 | 76 | ImportCertificateResponse response = acmClient.importCertificate(request); |
69 | | - String certificateArn = response.certificateArn(); |
70 | | - return certificateArn; |
| 77 | + return response.certificateArn(); |
| 78 | + |
71 | 79 | } catch (IOException e) { |
72 | | - System.err.println("Error reading certificate or private key file: " + e.getMessage()); |
| 80 | + System.err.println("Error downloading certificate or private key from S3: " + e.getMessage()); |
| 81 | + } catch (S3Exception e) { |
| 82 | + System.err.println("S3 error: " + e.awsErrorDetails().errorMessage()); |
73 | 83 | } |
74 | 84 | return ""; |
75 | 85 | } |
76 | 86 |
|
77 | | - private static byte[] readFileBytes(String filePath) throws IOException { |
78 | | - try (InputStream inputStream = new FileInputStream(filePath)) { |
79 | | - return IoUtils.toByteArray(inputStream); |
| 87 | + /** |
| 88 | + * Downloads a file from Amazon S3 and returns its contents as a byte array. |
| 89 | + * |
| 90 | + * @param s3Client The S3 client. |
| 91 | + * @param bucketName The name of the S3 bucket. |
| 92 | + * @param objectKey The key of the object in S3. |
| 93 | + * @return The file contents as a byte array. |
| 94 | + * @throws IOException If an I/O error occurs. |
| 95 | + */ |
| 96 | + private static byte[] downloadFileFromS3(S3Client s3Client, String bucketName, String objectKey) throws IOException { |
| 97 | + GetObjectRequest getObjectRequest = GetObjectRequest.builder() |
| 98 | + .bucket(bucketName) |
| 99 | + .key(objectKey) |
| 100 | + .build(); |
| 101 | + |
| 102 | + try (ResponseInputStream<GetObjectResponse> s3Object = s3Client.getObject(getObjectRequest); |
| 103 | + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { |
| 104 | + IoUtils.copy(s3Object, byteArrayOutputStream); |
| 105 | + return byteArrayOutputStream.toByteArray(); |
80 | 106 | } |
81 | 107 | } |
82 | 108 | } |
|
0 commit comments