-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKmsHelper.java
More file actions
82 lines (69 loc) · 3.2 KB
/
KmsHelper.java
File metadata and controls
82 lines (69 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package common;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.kms.KmsClient;
import software.amazon.awssdk.services.kms.model.GetPublicKeyRequest;
import software.amazon.awssdk.services.kms.model.GetPublicKeyResponse;
import java.net.URI;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* Helper class for interacting with KMS (or LocalStack KMS) in e2e tests.
*
* This allows tests to dynamically fetch public keys from KMS rather than
* relying on hardcoded keys, which is necessary when using LocalStack since
* it generates its own RSA key material.
*/
public final class KmsHelper {
private static final String LOCALSTACK_ENDPOINT = "http://localstack:5001";
private static final String KMS_KEY_ID = "ff275b92-0def-4dfc-b0f6-87c96b26c6c7";
private static final Region REGION = Region.US_EAST_1;
private KmsHelper() {
}
/**
* Fetches the public key from LocalStack KMS for the configured key ID.
*
* @return The public key as a base64-encoded string
* @throws Exception if the key cannot be fetched or parsed
*/
public static String getPublicKeyFromLocalstack() throws Exception {
try (KmsClient kmsClient = createLocalstackKmsClient()) {
GetPublicKeyRequest request = GetPublicKeyRequest.builder()
.keyId(KMS_KEY_ID)
.build();
GetPublicKeyResponse response = kmsClient.getPublicKey(request);
byte[] publicKeyBytes = response.publicKey().asByteArray();
// Return as base64-encoded string (format expected by JwtService)
return Base64.getEncoder().encodeToString(publicKeyBytes);
}
}
/**
* Fetches the public key from LocalStack KMS and returns it as a Java PublicKey object.
*
* @return The PublicKey object
* @throws Exception if the key cannot be fetched or parsed
*/
public static PublicKey getPublicKeyObjectFromLocalstack() throws Exception {
try (KmsClient kmsClient = createLocalstackKmsClient()) {
GetPublicKeyRequest request = GetPublicKeyRequest.builder()
.keyId(KMS_KEY_ID)
.build();
GetPublicKeyResponse response = kmsClient.getPublicKey(request);
byte[] publicKeyBytes = response.publicKey().asByteArray();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(keySpec);
}
}
private static KmsClient createLocalstackKmsClient() {
return KmsClient.builder()
.endpointOverride(URI.create(LOCALSTACK_ENDPOINT))
.region(REGION)
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create("test", "test")))
.build();
}
}