Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(KMS): Allow arn and alias to encrypt (#69)

Masatoshi Tadafa4e10757279
diff --git a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
index 4aecb62cf6..70eca8a4ab 100644
--- a/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/kms/KmsService.java
@@ -109,9 +109,9 @@ public class KmsService {
// ──────────────────────────── Crypto Ops (Mocks) ────────────────────────────
public byte[] encrypt(String keyId, byte[] plaintext, String region) {
- resolveKey(keyId, region);
+ KmsKey kmsKey = resolveKey(keyId, region);
// Local mock: prefix with keyId and base64
- String mock = "kms:" + keyId + ":" + Base64.getEncoder().encodeToString(plaintext);
+ String mock = "kms:" + kmsKey.getKeyId() + ":" + Base64.getEncoder().encodeToString(plaintext);
return mock.getBytes(StandardCharsets.UTF_8);
}
@@ -183,15 +183,25 @@ public class KmsService {
private KmsKey resolveKey(String keyIdOrArn, String region) {
String id = keyIdOrArn;
- if (id.startsWith("arn:aws:kms:")) {
+ // Alias arn
+ if (id.contains(":alias/")) {
+ String aliasName = id.substring(id.lastIndexOf(":") + 1);
+ String aliasKey = region + "::" + aliasName;
+ id = aliasStore.get(aliasKey)
+ .map(KmsAlias::getTargetKeyId)
+ .orElseThrow(() -> new AwsException("NotFoundException", "Alias not found: " + keyIdOrArn, 404));
+ } else if (id.startsWith("arn:aws:kms:")) {
+ // Key arn
id = id.substring(id.lastIndexOf("/") + 1);
} else if (id.startsWith("alias/")) {
+ // Alias name
String aliasKey = region + "::" + id;
id = aliasStore.get(aliasKey)
.map(KmsAlias::getTargetKeyId)
.orElseThrow(() -> new AwsException("NotFoundException", "Alias not found: " + keyIdOrArn, 404));
}
+ // Key id
return keyStore.get(region + "::" + id)
.orElseThrow(() -> new AwsException("NotFoundException", "Key not found: " + keyIdOrArn, 404));
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
index d034214c40..4bba21e60b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/kms/KmsServiceTest.java
@@ -127,7 +127,7 @@ class KmsServiceTest {
}
@Test
- void encryptAndDecrypt() {
+ void encryptAndDecryptWithId() {
KmsKey key = kmsService.createKey(null, REGION);
byte[] plaintext = "hello world".getBytes(StandardCharsets.UTF_8);
@@ -137,6 +137,43 @@ class KmsServiceTest {
assertArrayEquals(plaintext, decrypted);
}
+ @Test
+ void encryptAndDecryptWithArn() {
+ KmsKey key = kmsService.createKey(null, REGION);
+ byte[] plaintext = "hello world".getBytes(StandardCharsets.UTF_8);
+
+ byte[] ciphertext = kmsService.encrypt(key.getArn(), plaintext, REGION);
+ byte[] decrypted = kmsService.decrypt(ciphertext, REGION);
+
+ assertArrayEquals(plaintext, decrypted);
+ }
+
+ @Test
+ void encryptAndDecryptWithAliasName() {
+ KmsKey key = kmsService.createKey(null, REGION);
+ String aliasName = "alias/my-alias";
+ kmsService.createAlias(aliasName, key.getKeyId(), REGION);
+ byte[] plaintext = "hello world".getBytes(StandardCharsets.UTF_8);
+
+ byte[] ciphertext = kmsService.encrypt(aliasName, plaintext, REGION);
+ byte[] decrypted = kmsService.decrypt(ciphertext, REGION);
+
+ assertArrayEquals(plaintext, decrypted);
+ }
+
+ @Test
+ void encryptAndDecryptWithAliasArn() {
+ KmsKey key = kmsService.createKey(null, REGION);
+ String aliasName = "alias/my-alias";
+ kmsService.createAlias(aliasName, key.getKeyId(), REGION);
+ byte[] plaintext = "hello world".getBytes(StandardCharsets.UTF_8);
+
+ byte[] ciphertext = kmsService.encrypt("arn:aws:kms:" + REGION + ":000000000000:" + aliasName, plaintext, REGION);
+ byte[] decrypted = kmsService.decrypt(ciphertext, REGION);
+
+ assertArrayEquals(plaintext, decrypted);
+ }
+
@Test
void decryptInvalidCiphertextThrows() {
assertThrows(AwsException.class, () ->