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 <tada20591@cs.saisoncard.co.jp>
This commit is contained in:
Masatoshi Tadaandtada authored and GitHub committed 2026-03-27 14:06:48 -05:00
1 parent 7b87bf2c1f
commit fa4e107572
2 files changed
+51 -4

No files matched your search

@@ -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));
}