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
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-11-20 07:11:09 -04:00
1 parent 42707716cd
commit 704b5f1c3b
3 files changed
+66 -12

No files matched your search

+3 -3
View File
@@ -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
@@ -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})",
@@ -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<Future<KmsKey>> 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<KmsKey> r : results) {
assertEquals("Enabled", r.get(10, TimeUnit.SECONDS).getKeyState());
}
} finally {
pool.shutdownNow();
}
}
/**