From 704b5f1c3b386be959eaaa009fc79ce473dadcf0 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Fri, 20 Nov 2026 05:11:09 -0600 Subject: [PATCH] fix(kms): keep import parameters valid across concurrent identical imports (#9004) * fix(kms): keep import parameters valid across concurrent identical imports * fix(kms): ImportKeyMaterial no longer re-enables a Disabled key --- docs/services/kms.md | 6 +- .../io/floci/aws/services/kms/KmsService.java | 12 ++-- .../aws/services/kms/KmsServiceTest.java | 60 +++++++++++++++++-- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/docs/services/kms.md b/docs/services/kms.md index 77a9306d1f..614215a744 100644 --- a/docs/services/kms.md +++ b/docs/services/kms.md @@ -156,9 +156,9 @@ wrapping key spec: `GetParametersForImport` rejects that combination with The hybrid `RSA_AES_KEY_WRAP_*` algorithms require a 256-bit AES key. `RSAES_PKCS1_V1_5` is rejected, matching AWS, which stopped supporting it on October 10, 2023. -An import token is scoped to one key and spent by the import that uses it, and a second -`GetParametersForImport` call invalidates the token the previous one returned. Tokens expire 24 -hours after they are issued. +An import token is scoped to one key. It stays valid until it expires and can be used again; a +reimport must carry the key's original material. Unlike AWS, a later `GetParametersForImport` call +still replaces the earlier token. Tokens expire 24 hours after they are issued. `ExpirationModel=KEY_MATERIAL_EXPIRES` (the default) requires `ValidTo`, which must be in the future and no more than 365 days out. Once `ValidTo` passes, the material is dropped and the key diff --git a/src/main/java/io/floci/aws/services/kms/KmsService.java b/src/main/java/io/floci/aws/services/kms/KmsService.java index c5b0c064e7..76d6e3c711 100644 --- a/src/main/java/io/floci/aws/services/kms/KmsService.java +++ b/src/main/java/io/floci/aws/services/kms/KmsService.java @@ -1074,7 +1074,9 @@ public class KmsService implements ResourceProvider { /** * Unwraps and installs key material, which takes the key from PendingImport to Enabled. The - * import token is spent by the call that uses it, as it is on real KMS. + * import token stays valid until it expires, so concurrent imports of the same material all + * succeed; a different material is refused by {@code requireSameMaterialAsFirstImport}, not by + * the token. Unlike AWS, a later GetParametersForImport still replaces the earlier token. */ public KmsKey importKeyMaterial(String keyId, String importToken, byte[] encryptedKeyMaterial, String expirationModel, Long validTo, String importType, String region) { @@ -1106,9 +1108,11 @@ public class KmsService implements ResourceProvider { key.setKeyMaterialId(keyMaterialId); key.setExpirationModel(effectiveExpirationModel); key.setValidTo(KEY_MATERIAL_EXPIRES.equals(effectiveExpirationModel) ? validTo : 0L); - key.setKeyState("Enabled"); - key.setEnabled(true); - key.setImportParameters(null); + // A key that already had material keeps its state: a re-import does not re-enable a disabled key. + if (PENDING_IMPORT.equals(key.getKeyState())) { + key.setKeyState("Enabled"); + key.setEnabled(true); + } keyStore.put(region + "::" + key.getKeyId(), key); LOG.infov("Imported key material into KMS key {0} in {1} ({2})", diff --git a/src/test/java/io/floci/aws/services/kms/KmsServiceTest.java b/src/test/java/io/floci/aws/services/kms/KmsServiceTest.java index fb77e9cf39..e2fe692b18 100644 --- a/src/test/java/io/floci/aws/services/kms/KmsServiceTest.java +++ b/src/test/java/io/floci/aws/services/kms/KmsServiceTest.java @@ -2760,7 +2760,6 @@ class KmsServiceTest { assertEquals("Enabled", imported.getKeyState()); assertTrue(imported.isEnabled()); - assertNull(imported.getImportParameters()); assertNotNull(kmsService.encrypt(key.getKeyId(), "hello".getBytes(StandardCharsets.UTF_8), REGION)); } @@ -2828,7 +2827,25 @@ class KmsServiceTest { } @Test - void anImportTokenIsSpentByTheImportThatUsedIt() throws Exception { + void reimportingOnADisabledKeyLeavesItDisabled() throws Exception { + KmsKey key = externalSymmetricKey(); + KmsService.ImportParameters parameters = + kmsService.getParametersForImport(key.getKeyId(), OAEP_SHA_256, "RSA_2048", REGION); + byte[] wrapped = wrapOAEP(parameters.publicKeyEncoded(), OAEP_SHA_256, material(32, (byte) 3)); + kmsService.importKeyMaterial(key.getKeyId(), parameters.importToken(), wrapped, + "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION); + kmsService.disableKey(key.getKeyId(), REGION); + + KmsKey again = kmsService.importKeyMaterial(key.getKeyId(), parameters.importToken(), wrapped, + "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION); + + assertEquals("Disabled", again.getKeyState()); + assertEquals("Disabled", kmsService.describeKey(key.getKeyId(), REGION).getKeyState()); + assertFalse(kmsService.describeKey(key.getKeyId(), REGION).isEnabled()); + } + + @Test + void anImportTokenStaysValidForIdenticalMaterialUntilItExpires() throws Exception { KmsKey key = externalSymmetricKey(); KmsService.ImportParameters parameters = kmsService.getParametersForImport(key.getKeyId(), OAEP_SHA_256, "RSA_2048", REGION); @@ -2836,11 +2853,44 @@ class KmsServiceTest { kmsService.importKeyMaterial(key.getKeyId(), parameters.importToken(), wrapped, "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION); + KmsKey again = kmsService.importKeyMaterial(key.getKeyId(), parameters.importToken(), wrapped, + "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION); + assertEquals("Enabled", again.getKeyState()); + + byte[] other = wrapOAEP(parameters.publicKeyEncoded(), OAEP_SHA_256, material(32, (byte) 4)); String keyId = key.getKeyId(); - String spentToken = parameters.importToken(); + String token = parameters.importToken(); AwsException ex = assertThrows(AwsException.class, () -> kmsService.importKeyMaterial( - keyId, spentToken, wrapped, "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION)); - assertEquals("InvalidImportTokenException", ex.getErrorCode()); + keyId, token, other, "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION)); + assertEquals("IncorrectKeyMaterialException", ex.getErrorCode()); + } + + @Test + void concurrentImportsOfIdenticalMaterialAllSucceed() throws Exception { + KmsKey key = externalSymmetricKey(); + KmsService.ImportParameters parameters = + kmsService.getParametersForImport(key.getKeyId(), OAEP_SHA_256, "RSA_2048", REGION); + byte[] raw = material(32, (byte) 3); + int n = 8; + ExecutorService pool = Executors.newFixedThreadPool(n); + try { + CountDownLatch go = new CountDownLatch(1); + List> results = new ArrayList<>(); + for (int i = 0; i < n; i++) { + byte[] wrapped = wrapOAEP(parameters.publicKeyEncoded(), OAEP_SHA_256, raw); + results.add(pool.submit(() -> { + go.await(); + return kmsService.importKeyMaterial(key.getKeyId(), parameters.importToken(), wrapped, + "KEY_MATERIAL_DOES_NOT_EXPIRE", null, null, REGION); + })); + } + go.countDown(); + for (Future r : results) { + assertEquals("Enabled", r.get(10, TimeUnit.SECONDS).getKeyState()); + } + } finally { + pool.shutdownNow(); + } } /**