Skip to content

Commit 9fcf126

Browse files
authored
Java V2 updates the POM files to use JDK 21 (#7327)
1 parent 6c45783 commit 9fcf126

File tree

275 files changed

+5690
-5477
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

275 files changed

+5690
-5477
lines changed

javav2/example_code/acm/pom.xml

+8-4
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

+58-32
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Configuration status="WARN">
2+
<Appenders>
3+
<Console name="ConsoleAppender" target="SYSTEM_OUT">
4+
<PatternLayout pattern="%msg%n"/>
5+
</Console>
6+
<Console name="AlignedConsoleAppender" target="SYSTEM_OUT">
7+
<PatternLayout pattern="%m%n"/>
8+
</Console>
9+
</Appenders>
10+
<Loggers>
11+
<!-- Root logger configuration -->
12+
<Root level="info">
13+
<!-- Specify which appenders to use -->
14+
<AppenderRef ref="ConsoleAppender" />
15+
</Root>
16+
</Loggers>
17+
</Configuration>

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

+67-25
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,48 @@
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 org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
import software.amazon.awssdk.regions.Region;
17+
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
18+
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest;
19+
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse;
20+
21+
import java.io.IOException;
22+
1923
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2024
import static org.junit.jupiter.api.Assertions.assertNotNull;
2125

2226
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
2327
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2428
public class ACMTests {
25-
29+
private static final Logger logger = LoggerFactory.getLogger(ACMTests.class);
2630
private static String certificatePath = "";
2731
private static String privateKeyPath = "";
28-
32+
private static String bucketName = "";
2933
private static String certificateArn;
3034

3135
@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";
36+
public static void setUp() throws IOException {
37+
Gson gson = new Gson();
38+
String json = getSecretValues();
39+
SecretValues values = gson.fromJson(json, SecretValues.class);
40+
certificatePath = values.getCertificatePath();
41+
privateKeyPath = values.getPrivateKeyPath();
42+
bucketName = values.getBucketName();
3643
}
3744

3845
@Test
3946
@Tag("IntegrationTest")
4047
@Order(1)
4148
public void testImportCert() {
4249
assertDoesNotThrow(() -> {
43-
certificateArn = ImportCert.importCertificate(certificatePath, privateKeyPath);
50+
certificateArn = ImportCert.importCertificate(bucketName, certificatePath, privateKeyPath);
4451
assertNotNull(certificateArn);
4552
});
53+
logger.info("Test 1 passed");
4654
}
4755

4856
@Test
@@ -52,6 +60,7 @@ public void testAddTags() {
5260
assertDoesNotThrow(() -> {
5361
AddTagsToCertificate.addTags(certificateArn);
5462
});
63+
logger.info("Test 2 passed");
5564
}
5665

5766
@Test
@@ -61,42 +70,75 @@ public void testDescribeCert() {
6170
assertDoesNotThrow(() -> {
6271
DescribeCert.describeCertificate(certificateArn);
6372
});
73+
logger.info("Test 3 passed");
6474
}
6575

6676
@Test
6777
@Tag("IntegrationTest")
6878
@Order(4)
69-
public void testListCertTags() {
70-
assertDoesNotThrow(() -> {
71-
ListCertTags.listCertTags(certificateArn);
72-
});
73-
}
74-
75-
@Test
76-
@Tag("IntegrationTest")
77-
@Order(5)
7879
public void testRemoveTagsFromCert() {
7980
assertDoesNotThrow(() -> {
8081
RemoveTagsFromCert.removeTags(certificateArn);
8182
});
83+
logger.info("Test 4 passed");
8284
}
8385

8486

8587
@Test
8688
@Tag("IntegrationTest")
87-
@Order(6)
89+
@Order(5)
8890
public void testRequestCert() {
8991
assertDoesNotThrow(() -> {
9092
RequestCert.requestCertificate();
9193
});
94+
logger.info("Test 5 passed");
9295
}
9396

9497
@Test
9598
@Tag("IntegrationTest")
96-
@Order(7)
99+
@Order(6)
97100
public void testDeleteCert() {
98101
assertDoesNotThrow(() -> {
99102
DeleteCert.deleteCertificate(certificateArn);
100103
});
104+
logger.info("Test 6 passed");
105+
}
106+
107+
108+
private static String getSecretValues() {
109+
SecretsManagerClient secretClient = SecretsManagerClient.builder()
110+
.region(Region.US_EAST_1)
111+
.build();
112+
String secretName = "test/acm";
113+
114+
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
115+
.secretId(secretName)
116+
.build();
117+
118+
GetSecretValueResponse valueResponse = secretClient.getSecretValue(valueRequest);
119+
return valueResponse.secretString();
120+
}
121+
122+
@Nested
123+
@DisplayName("A class used to get test values from test/cognito (an AWS Secrets Manager secret)")
124+
class SecretValues {
125+
private String certificatePath;
126+
private String privateKeyPath;
127+
private String bucketName;
128+
129+
// Getter for certificatePath
130+
public String getCertificatePath() {
131+
return certificatePath;
132+
}
133+
134+
// Getter for privateKeyPath
135+
public String getPrivateKeyPath() {
136+
return privateKeyPath;
137+
}
138+
139+
// Getter for bucketName
140+
public String getBucketName() {
141+
return bucketName;
142+
}
101143
}
102144
}

javav2/example_code/apigateway/pom.xml

+28-4
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,14 @@
2626
<dependency>
2727
<groupId>software.amazon.awssdk</groupId>
2828
<artifactId>bom</artifactId>
29-
<version>2.29.45</version>
29+
<version>2.31.8</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.logging.log4j</groupId>
35+
<artifactId>log4j-bom</artifactId>
36+
<version>2.23.1</version>
3037
<type>pom</type>
3138
<scope>import</scope>
3239
</dependency>
@@ -60,5 +67,22 @@
6067
<groupId>software.amazon.awssdk</groupId>
6168
<artifactId>ssooidc</artifactId>
6269
</dependency>
70+
<dependency>
71+
<groupId>org.apache.logging.log4j</groupId>
72+
<artifactId>log4j-core</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.slf4j</groupId>
76+
<artifactId>slf4j-api</artifactId>
77+
<version>2.0.13</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.apache.logging.log4j</groupId>
81+
<artifactId>log4j-slf4j2-impl</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.logging.log4j</groupId>
85+
<artifactId>log4j-1.2-api</artifactId>
86+
</dependency>
6387
</dependencies>
6488
</project>

0 commit comments

Comments
 (0)