Skip to content

Commit cfe9c85

Browse files
committed
updated POM to use JDK 21
1 parent 5fb4033 commit cfe9c85

File tree

6 files changed

+132
-100
lines changed

6 files changed

+132
-100
lines changed

javav2/example_code/acm/pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<version>1.0-SNAPSHOT</version>
1010
<properties>
1111
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12-
<java.version>17</java.version>
13-
<maven.compiler.target>17</maven.compiler.target>
14-
<maven.compiler.source>17</maven.compiler.source>
12+
<java.version>21</java.version>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<maven.compiler.source>21</maven.compiler.source>
1515
</properties>
1616
<build>
1717
<plugins>
@@ -36,7 +36,7 @@
3636
<dependency>
3737
<groupId>software.amazon.awssdk</groupId>
3838
<artifactId>bom</artifactId>
39-
<version>2.29.45</version>
39+
<version>2.31.8</version>
4040
<type>pom</type>
4141
<scope>import</scope>
4242
</dependency>
@@ -64,6 +64,10 @@
6464
<groupId>software.amazon.awssdk</groupId>
6565
<artifactId>secretsmanager</artifactId>
6666
</dependency>
67+
<dependency>
68+
<groupId>software.amazon.awssdk</groupId>
69+
<artifactId>s3</artifactId>
70+
</dependency>
6771
<dependency>
6872
<groupId>com.google.code.gson</groupId>
6973
<artifactId>gson</artifactId>

javav2/example_code/acm/src/main/java/com/example/acm/ImportCert.java

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
package com.example.acm;
55

6+
import software.amazon.awssdk.core.ResponseInputStream;
67
import software.amazon.awssdk.core.SdkBytes;
78
import software.amazon.awssdk.services.acm.AcmClient;
89
import software.amazon.awssdk.services.acm.model.ImportCertificateRequest;
910
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;
1015
import software.amazon.awssdk.utils.IoUtils;
11-
import java.io.FileInputStream;
16+
17+
import java.io.ByteArrayOutputStream;
1218
import java.io.IOException;
13-
import java.io.InputStream;
1419
import java.nio.ByteBuffer;
1520

1621
// snippet-start:[acm.java2.import_cert.main]
@@ -25,58 +30,79 @@
2530
public class ImportCert {
2631

2732
public static void main(String[] args) {
28-
2933
final String usage = """
30-
31-
Usage: <certificatePath> <privateKeyPath>
32-
34+
Usage: <bucketName> <certificateKey> <privateKeyKey>
35+
3336
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.
3640
""";
3741

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];
4250

43-
String certificatePath = args[0];
44-
String privateKeyPath = args[1];
45-
String certificateArn = importCertificate(certificatePath, privateKeyPath);
51+
String certificateArn = importCertificate(bucketName, certificateKey, privateKeyKey);
4652
System.out.println("Certificate imported with ARN: " + certificateArn);
4753
}
4854

4955
/**
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).
5257
*
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.
5662
*/
57-
public static String importCertificate(String certificatePath, String privateKeyPath) {
63+
public static String importCertificate(String bucketName, String certificateKey, String privateKeyKey) {
5864
AcmClient acmClient = AcmClient.create();
65+
S3Client s3Client = S3Client.create();
66+
5967
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);
6270

6371
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();
6775

6876
ImportCertificateResponse response = acmClient.importCertificate(request);
69-
String certificateArn = response.certificateArn();
70-
return certificateArn;
77+
return response.certificateArn();
78+
7179
} 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());
7383
}
7484
return "";
7585
}
7686

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();
80106
}
81107
}
82108
}

javav2/example_code/acm/src/test/java/ACMTests.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,42 @@
99
import com.example.acm.ListCertTags;
1010
import com.example.acm.RemoveTagsFromCert;
1111
import com.example.acm.RequestCert;
12-
import org.junit.jupiter.api.BeforeAll;
13-
import org.junit.jupiter.api.MethodOrderer;
14-
import org.junit.jupiter.api.Order;
15-
import org.junit.jupiter.api.Tag;
16-
import org.junit.jupiter.api.Test;
17-
import org.junit.jupiter.api.TestInstance;
18-
import org.junit.jupiter.api.TestMethodOrder;
12+
import com.google.gson.Gson;
13+
import org.junit.jupiter.api.*;
14+
import software.amazon.awssdk.regions.Region;
15+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
16+
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
17+
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
18+
19+
import java.io.IOException;
20+
1921
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2022
import static org.junit.jupiter.api.Assertions.assertNotNull;
2123

2224
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
2325
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2426
public class ACMTests {
25-
2627
private static String certificatePath = "";
2728
private static String privateKeyPath = "";
28-
29+
private static String bucketName = "";
2930
private static String certificateArn;
3031

3132
@BeforeAll
32-
public static void setUp() {
33-
34-
certificatePath = "C:\\Users\\scmacdon\\cert_example\\certificate.pem";
35-
privateKeyPath = "C:\\Users\\scmacdon\\cert_example\\private_key.pem";
33+
public static void setUp() throws IOException {
34+
Gson gson = new Gson();
35+
String json = getSecretValues();
36+
SecretValues values = gson.fromJson(json, SecretValues.class);
37+
certificatePath = values.getCertificatePath();
38+
privateKeyPath = values.getPrivateKeyPath();
39+
bucketName = values.getBucketName();
3640
}
3741

3842
@Test
3943
@Tag("IntegrationTest")
4044
@Order(1)
4145
public void testImportCert() {
4246
assertDoesNotThrow(() -> {
43-
certificateArn = ImportCert.importCertificate(certificatePath, privateKeyPath);
47+
certificateArn = ImportCert.importCertificate(bucketName, certificatePath, privateKeyPath);
4448
assertNotNull(certificateArn);
4549
});
4650
}
@@ -66,15 +70,6 @@ public void testDescribeCert() {
6670
@Test
6771
@Tag("IntegrationTest")
6872
@Order(4)
69-
public void testListCertTags() {
70-
assertDoesNotThrow(() -> {
71-
ListCertTags.listCertTags(certificateArn);
72-
});
73-
}
74-
75-
@Test
76-
@Tag("IntegrationTest")
77-
@Order(5)
7873
public void testRemoveTagsFromCert() {
7974
assertDoesNotThrow(() -> {
8075
RemoveTagsFromCert.removeTags(certificateArn);
@@ -84,7 +79,7 @@ public void testRemoveTagsFromCert() {
8479

8580
@Test
8681
@Tag("IntegrationTest")
87-
@Order(6)
82+
@Order(5)
8883
public void testRequestCert() {
8984
assertDoesNotThrow(() -> {
9085
RequestCert.requestCertificate();
@@ -93,10 +88,48 @@ public void testRequestCert() {
9388

9489
@Test
9590
@Tag("IntegrationTest")
96-
@Order(7)
91+
@Order(6)
9792
public void testDeleteCert() {
9893
assertDoesNotThrow(() -> {
9994
DeleteCert.deleteCertificate(certificateArn);
10095
});
10196
}
97+
98+
99+
private static String getSecretValues() {
100+
SecretsManagerClient secretClient = SecretsManagerClient.builder()
101+
.region(Region.US_EAST_1)
102+
.build();
103+
String secretName = "test/acm";
104+
105+
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
106+
.secretId(secretName)
107+
.build();
108+
109+
GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest);
110+
return valueResponse.secretString();
111+
}
112+
113+
@Nested
114+
@DisplayName("A class used to get test values from test/cognito (an AWS Secrets Manager secret)")
115+
class SecretValues {
116+
private String certificatePath;
117+
private String privateKeyPath;
118+
private String bucketName;
119+
120+
// Getter for certificatePath
121+
public String getCertificatePath() {
122+
return certificatePath;
123+
}
124+
125+
// Getter for privateKeyPath
126+
public String getPrivateKeyPath() {
127+
return privateKeyPath;
128+
}
129+
130+
// Getter for bucketName
131+
public String getBucketName() {
132+
return bucketName;
133+
}
134+
}
102135
}

javav2/example_code/apigateway/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<version>1.0-SNAPSHOT</version>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11-
<java.version>17</java.version>
12-
<maven.compiler.target>17</maven.compiler.target>
13-
<maven.compiler.source>17</maven.compiler.source>
11+
<java.version>21</java.version>
12+
<maven.compiler.target>21</maven.compiler.target>
13+
<maven.compiler.source>21</maven.compiler.source>
1414
</properties>
1515
<build>
1616
<plugins>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>software.amazon.awssdk</groupId>
2828
<artifactId>bom</artifactId>
29-
<version>2.29.45</version>
29+
<version>2.31.8</version>
3030
<type>pom</type>
3131
<scope>import</scope>
3232
</dependency>

javav2/example_code/apigateway/src/test/java/APIGatewayTest.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import com.example.gateway.*;
55
import com.google.gson.Gson;
6-
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
76
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
87
import org.junit.jupiter.api.*;
98
import software.amazon.awssdk.regions.Region;
@@ -45,35 +44,6 @@ public static void setUp() throws IOException {
4544
httpMethod = values.getHttpMethod();
4645
restApiName = values.getRestApiName() + randomNum;
4746
stageName = values.getStageName();
48-
49-
// Uncomment this code block if you prefer using a config.properties file to
50-
// retrieve AWS values required for these tests.
51-
/*
52-
* try (InputStream input =
53-
* APIGatewayTest.class.getClassLoader().getResourceAsStream("config.properties"
54-
* )) {
55-
*
56-
* Properties prop = new Properties();
57-
*
58-
* if (input == null) {
59-
* System.out.println("Sorry, unable to find config.properties");
60-
* return;
61-
* }
62-
*
63-
* //load a properties file from class path, inside static method
64-
* prop.load(input);
65-
*
66-
* // Populate the data members required for all tests
67-
* restApiId = prop.getProperty("restApiId");
68-
* resourceId = prop.getProperty("resourceId");
69-
* httpMethod = prop.getProperty("httpMethod");
70-
* restApiName = prop.getProperty("restApiName");
71-
* stageName = prop.getProperty("stageName");
72-
*
73-
* } catch (IOException ex) {
74-
* ex.printStackTrace();
75-
* }
76-
*/
7747
}
7848

7949
@Test
@@ -116,7 +86,6 @@ public void DeleteRestApi() {
11686
private static String getSecretValues() {
11787
SecretsManagerClient secretClient = SecretsManagerClient.builder()
11888
.region(Region.US_EAST_1)
119-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
12089
.build();
12190
String secretName = "test/apigateway";
12291

javav2/example_code/appautoscale/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<version>1.0-SNAPSHOT</version>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11-
<java.version>17</java.version>
12-
<maven.compiler.target>17</maven.compiler.target>
13-
<maven.compiler.source>17</maven.compiler.source>
11+
<java.version>21</java.version>
12+
<maven.compiler.target>21</maven.compiler.target>
13+
<maven.compiler.source>21</maven.compiler.source>
1414
</properties>
1515
<build>
1616
<plugins>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>software.amazon.awssdk</groupId>
2828
<artifactId>bom</artifactId>
29-
<version>2.29.45</version>
29+
<version>2.31.8</version>
3030
<type>pom</type>
3131
<scope>import</scope>
3232
</dependency>

0 commit comments

Comments
 (0)