From fa4e107572792b5cc4dc6e3f4b323695a4a9add7 Mon Sep 17 00:00:00 2001 From: Masatoshi Tada Date: Sat, 28 Mar 2026 04:06:48 +0900 Subject: [PATCH] fix(KMS): Allow arn and alias to encrypt (#69) Thanks again, @MasatoshiTada! This is a great addition to Floci. Your continued support really makes a difference. Hope to see you around soon! --------- * fix(KMS): Allow arn and alias to encrypt * fix(KMS): Allow alias-arn to encrypt --------- Co-authored-by: tada --- .../floci/services/kms/KmsService.java | 16 ++++++-- .../floci/services/kms/KmsServiceTest.java | 39 ++++++++++++++++++- 2 files changed, 51 insertions(+), 4 deletions(-) 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, () ->