mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(kms): list completed key rotations (#8146)
Record completed rotation history, validate filtering and pagination, preserve concurrent key updates, and publish primary and replica material as one atomic storage batch. Revalidate the initially resolved key before atomically publishing import parameters, preserving deletion safety and alias identity. Share the malformed rotation request fixture. Use shared striped monitors keyed by resolved key ID, preserving replica publication and import identity while permitting unrelated key operations. Reuse shared concurrency and SDK fixtures. Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
919f25ea0b
commit
c3551d351e
12 files changed
+1330
-202
No files matched your search
@@ -45,14 +45,14 @@ public class KmsJsonHandler {
|
||||
"GenerateDataKeyWithoutPlaintext", "GenerateDataKeyPair", "GenerateDataKeyPairWithoutPlaintext", "Sign", "Verify", "GenerateMac", "VerifyMac", "ScheduleKeyDeletion",
|
||||
"CancelKeyDeletion", "TagResource", "UntagResource", "ListResourceTags", "GetKeyPolicy", "PutKeyPolicy",
|
||||
"ListKeyPolicies", "UpdateKeyDescription", "GetKeyRotationStatus", "EnableKeyRotation",
|
||||
"DisableKeyRotation", "EnableKey", "DisableKey", "RotateKeyOnDemand", "GetParametersForImport",
|
||||
"DisableKeyRotation", "EnableKey", "DisableKey", "RotateKeyOnDemand", "ListKeyRotations", "GetParametersForImport",
|
||||
"ImportKeyMaterial", "DeleteImportedKeyMaterial");
|
||||
|
||||
private static final Set<String> KEY_ONLY_OPERATIONS = Set.of(
|
||||
"ReplicateKey", "CreateGrant", "ListGrants", "RevokeGrant", "ScheduleKeyDeletion", "CancelKeyDeletion", "TagResource",
|
||||
"UntagResource", "ListResourceTags", "GetKeyPolicy", "PutKeyPolicy", "ListKeyPolicies",
|
||||
"UpdateKeyDescription", "GetKeyRotationStatus", "EnableKeyRotation", "DisableKeyRotation", "EnableKey",
|
||||
"DisableKey", "RotateKeyOnDemand", "GetParametersForImport", "ImportKeyMaterial",
|
||||
"DisableKey", "RotateKeyOnDemand", "ListKeyRotations", "GetParametersForImport", "ImportKeyMaterial",
|
||||
"DeleteImportedKeyMaterial");
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
@@ -109,6 +109,7 @@ public class KmsJsonHandler {
|
||||
case "EnableKey" -> handleEnableKey(request, region);
|
||||
case "DisableKey" -> handleDisableKey(request, region);
|
||||
case "RotateKeyOnDemand" -> handleRotateKeyOnDemand(request, region);
|
||||
case "ListKeyRotations" -> handleListKeyRotations(request, region);
|
||||
case "GetParametersForImport" -> handleGetParametersForImport(request, region);
|
||||
case "ImportKeyMaterial" -> handleImportKeyMaterial(request, region);
|
||||
case "DeleteImportedKeyMaterial" -> handleDeleteImportedKeyMaterial(request, region);
|
||||
@@ -118,6 +119,38 @@ public class KmsJsonHandler {
|
||||
};
|
||||
}
|
||||
|
||||
private Response handleListKeyRotations(JsonNode request, String region) {
|
||||
JsonNode include = request.path("IncludeKeyMaterial");
|
||||
if (!include.isMissingNode() && !include.isNull() && !include.isTextual()) {
|
||||
throw new AwsException("SerializationException", jsonToken(include) + " can not be converted to a String", 400);
|
||||
}
|
||||
KmsService.Page<KmsService.KeyRotation> page = service.listKeyRotations(request.path("KeyId").asText(),
|
||||
include.asText(null), region, marker(request), limit(request));
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode rotations = response.putArray("Rotations");
|
||||
for (KmsService.KeyRotation rotation : page.items()) {
|
||||
ObjectNode entry = rotations.addObject();
|
||||
entry.put("KeyId", rotation.keyId());
|
||||
entry.put("KeyMaterialId", rotation.keyMaterialId());
|
||||
entry.put("KeyMaterialState", rotation.keyMaterialState());
|
||||
if (rotation.rotationDate() != null) {
|
||||
entry.put("RotationDate", rotation.rotationDate() / 1000.0);
|
||||
entry.put("RotationType", "ON_DEMAND");
|
||||
}
|
||||
if (rotation.importState() != null) {
|
||||
entry.put("ImportState", rotation.importState());
|
||||
}
|
||||
if (rotation.expirationModel() != null) {
|
||||
entry.put("ExpirationModel", rotation.expirationModel());
|
||||
}
|
||||
if (rotation.validTo() != null) {
|
||||
entry.put("ValidTo", rotation.validTo());
|
||||
}
|
||||
}
|
||||
putTruncation(response, page);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleCreateKey(JsonNode request, String region) {
|
||||
String description = request.path("Description").asText(null);
|
||||
String keyUsage = request.path("KeyUsage").asText("ENCRYPT_DECRYPT");
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.ReservedTags;
|
||||
import io.floci.aws.core.common.SignedMarkers;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.StripedLocks;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
import io.floci.aws.core.resource.SupportedResourceType;
|
||||
@@ -36,6 +37,7 @@ import java.security.*;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -67,7 +69,10 @@ public class KmsService implements ResourceProvider {
|
||||
private final KmsKeyTypes keyTypes;
|
||||
// Guards the check-generate-put sequence in ensureBackingKeyMaterial so two concurrent
|
||||
// first uses of the same legacy key cannot each mint a different backing key.
|
||||
private final Object backingKeyMaterialLock = new Object();
|
||||
// Hold from resolving a stored key through publishing its changes. Rotation publishes a
|
||||
// copy, so a writer holding an older live storage value must never put that value back.
|
||||
// Replica registration uses the same lock so rotation cannot miss a new replica.
|
||||
private final StripedLocks backingKeyMaterialLocks = new StripedLocks(256);
|
||||
// Guards the scan-and-mint sequence in ensureAwsManagedKeys so two concurrent first reads of
|
||||
// a region's alias namespace cannot each mint a key for the same alias/aws/* name.
|
||||
private final Object awsManagedKeyLock = new Object();
|
||||
@@ -221,15 +226,10 @@ public class KmsService implements ResourceProvider {
|
||||
* missing field). Material is generated lazily on first use and persisted immediately,
|
||||
* mirroring how {@link #expireImportedKeyMaterialIfDue} self-heals imported-key state.
|
||||
*
|
||||
* <p>Double-checked: the cheap unsynchronized check below lets an already-healed key (the
|
||||
* overwhelming majority of calls, once a key has been used once) return without taking the
|
||||
* lock. Only a key that still needs healing pays for entering the {@code synchronized}
|
||||
* block, where the key is re-read from {@code keyStore} and re-checked before generating,
|
||||
* so that if two callers race to heal the same legacy key concurrently, only one backing key
|
||||
* is ever minted and every caller ends up with (and returns) the same persisted instance.
|
||||
* Without this, two concurrent first uses could each generate a different backing key and
|
||||
* each {@code put} their own copy, leaving one of the two backing keys, and any ciphertext
|
||||
* encrypted under it, unreachable from the persisted key.
|
||||
* <p>The caller resolves the stored key and runs healing under the same lock used by
|
||||
* rotation and metadata writers. Re-reading before generating legacy material preserves
|
||||
* the current stored instance, so concurrent first uses cannot publish different backing
|
||||
* keys and invalidate each other's ciphertext.
|
||||
*
|
||||
* @return the key instance to use for this call: either {@code key} unchanged, or the
|
||||
* re-read, healed instance that was just persisted.
|
||||
@@ -240,7 +240,7 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
if (EXTERNAL_ORIGIN.equals(key.getOrigin())) {
|
||||
if (!hasBackingKeyMaterialSafely(key) && key.getPrivateKeyEncoded() != null) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
if (!hasBackingKeyMaterial(key)) {
|
||||
byte[] material = decodeBackingMaterial(key.getPrivateKeyEncoded());
|
||||
String materialId = key.getKeyMaterialId() == null
|
||||
@@ -255,7 +255,7 @@ public class KmsService implements ResourceProvider {
|
||||
if (hasBackingKeyMaterialSafely(key)) {
|
||||
return key;
|
||||
}
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
KmsKey current = keyStore.get(region + "::" + key.getKeyId()).orElse(key);
|
||||
if (hasBackingKeyMaterial(current)) {
|
||||
return current;
|
||||
@@ -273,7 +273,7 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
private boolean hasBackingKeyMaterialSafely(KmsKey key) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
return hasBackingKeyMaterial(key);
|
||||
}
|
||||
}
|
||||
@@ -316,54 +316,57 @@ public class KmsService implements ResourceProvider {
|
||||
if (replicaRegion == null || replicaRegion.isBlank()) {
|
||||
throw new AwsException("ValidationException", "ReplicaRegion is required.", 400);
|
||||
}
|
||||
String storedKeyId = resolveStoredKeyId(keyId, primaryRegion);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey primary = resolveKey(keyId, storedKeyId, primaryRegion);
|
||||
if (!primary.isMultiRegion() || !MULTI_REGION_PRIMARY.equals(primary.getMultiRegionKeyType())) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
primary.getArn() + " is not a multi-Region primary key.", 400);
|
||||
}
|
||||
requireKeyCanBeReplicated(primary);
|
||||
if (!AwsRegions.partitionFor(primaryRegion).equals(AwsRegions.partitionFor(replicaRegion))) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"The replica region must be in the same AWS partition as the primary key.", 400);
|
||||
}
|
||||
|
||||
KmsKey primary = resolveKey(keyId, primaryRegion);
|
||||
if (!primary.isMultiRegion() || !MULTI_REGION_PRIMARY.equals(primary.getMultiRegionKeyType())) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
primary.getArn() + " is not a multi-Region primary key.", 400);
|
||||
}
|
||||
requireKeyCanBeReplicated(primary);
|
||||
if (!AwsRegions.partitionFor(primaryRegion).equals(AwsRegions.partitionFor(replicaRegion))) {
|
||||
throw new AwsException("UnsupportedOperationException",
|
||||
"The replica region must be in the same AWS partition as the primary key.", 400);
|
||||
}
|
||||
String storageKey = replicaRegion + "::" + primary.getKeyId();
|
||||
if (keyStore.get(storageKey).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"A replica for this multi-Region key already exists in " + replicaRegion + ".", 400);
|
||||
}
|
||||
|
||||
String storageKey = replicaRegion + "::" + primary.getKeyId();
|
||||
if (keyStore.get(storageKey).isPresent()) {
|
||||
throw new AwsException("AlreadyExistsException",
|
||||
"A replica for this multi-Region key already exists in " + replicaRegion + ".", 400);
|
||||
}
|
||||
KmsKey replica = new KmsKey();
|
||||
replica.setKeyId(primary.getKeyId());
|
||||
replica.setArn(regionResolver.buildArn("kms", replicaRegion, "key/" + primary.getKeyId()));
|
||||
replica.setDescription(description == null ? "" : description);
|
||||
replica.setKeyUsage(primary.getKeyUsage());
|
||||
replica.setKeySpec(primary.getKeySpec());
|
||||
replica.setPolicy(policy == null ? buildDefaultKeyPolicy() : policy);
|
||||
replica.getTags().putAll(ReservedTags.stripReservedTags(tags));
|
||||
replica.setOrigin(primary.getOrigin());
|
||||
replica.setKeyRotationEnabled(primary.isKeyRotationEnabled());
|
||||
replica.setMultiRegion(true);
|
||||
replica.setMultiRegionKeyType(MULTI_REGION_REPLICA);
|
||||
replica.setMultiRegionPrimaryRegion(primaryRegion);
|
||||
replica.setKeyMaterialId(primary.getKeyMaterialId());
|
||||
if (EXTERNAL_ORIGIN.equals(primary.getOrigin())) {
|
||||
replica.setEnabled(false);
|
||||
replica.setKeyState(PENDING_IMPORT);
|
||||
replica.setBackingKeys(new HashMap<>());
|
||||
} else {
|
||||
replica.setEnabled(primary.isEnabled());
|
||||
replica.setKeyState(primary.getKeyState());
|
||||
replica.setPrivateKeyEncoded(primary.getPrivateKeyEncoded());
|
||||
replica.setPublicKeyEncoded(primary.getPublicKeyEncoded());
|
||||
replica.setBackingKeys(new HashMap<>(primary.getBackingKeys()));
|
||||
replica.setCurrentBackingKeyId(primary.getCurrentBackingKeyId());
|
||||
replica.setBackingKeyRotationDates(new HashMap<>(primary.getBackingKeyRotationDates()));
|
||||
}
|
||||
keyStore.put(storageKey, replica);
|
||||
|
||||
KmsKey replica = new KmsKey();
|
||||
replica.setKeyId(primary.getKeyId());
|
||||
replica.setArn(regionResolver.buildArn("kms", replicaRegion, "key/" + primary.getKeyId()));
|
||||
replica.setDescription(description == null ? "" : description);
|
||||
replica.setKeyUsage(primary.getKeyUsage());
|
||||
replica.setKeySpec(primary.getKeySpec());
|
||||
replica.setPolicy(policy == null ? buildDefaultKeyPolicy() : policy);
|
||||
replica.getTags().putAll(ReservedTags.stripReservedTags(tags));
|
||||
replica.setOrigin(primary.getOrigin());
|
||||
replica.setKeyRotationEnabled(primary.isKeyRotationEnabled());
|
||||
replica.setMultiRegion(true);
|
||||
replica.setMultiRegionKeyType(MULTI_REGION_REPLICA);
|
||||
replica.setMultiRegionPrimaryRegion(primaryRegion);
|
||||
replica.setKeyMaterialId(primary.getKeyMaterialId());
|
||||
if (EXTERNAL_ORIGIN.equals(primary.getOrigin())) {
|
||||
replica.setEnabled(false);
|
||||
replica.setKeyState(PENDING_IMPORT);
|
||||
replica.setBackingKeys(new HashMap<>());
|
||||
} else {
|
||||
replica.setEnabled(primary.isEnabled());
|
||||
replica.setKeyState(primary.getKeyState());
|
||||
replica.setPrivateKeyEncoded(primary.getPrivateKeyEncoded());
|
||||
replica.setPublicKeyEncoded(primary.getPublicKeyEncoded());
|
||||
replica.setBackingKeys(new HashMap<>(primary.getBackingKeys()));
|
||||
replica.setCurrentBackingKeyId(primary.getCurrentBackingKeyId());
|
||||
LOG.infov("Replicated KMS key {0} from {1} to {2}", primary.getKeyId(), primaryRegion, replicaRegion);
|
||||
return replica;
|
||||
}
|
||||
keyStore.put(storageKey, replica);
|
||||
|
||||
LOG.infov("Replicated KMS key {0} from {1} to {2}", primary.getKeyId(), primaryRegion, replicaRegion);
|
||||
return replica;
|
||||
}
|
||||
|
||||
private static void requireKeyCanBeReplicated(KmsKey key) {
|
||||
@@ -757,12 +760,15 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void scheduleKeyDeletion(String keyId, int pendingWindowInDays, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:ScheduleKeyDeletion");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setKeyState("PendingDeletion");
|
||||
key.setDeletionDate(Instant.now().plusSeconds((long) pendingWindowInDays * 86400).getEpochSecond());
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:ScheduleKeyDeletion");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setKeyState("PendingDeletion");
|
||||
key.setDeletionDate(Instant.now().plusSeconds((long) pendingWindowInDays * 86400).getEpochSecond());
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -771,16 +777,19 @@ public class KmsService implements ResourceProvider {
|
||||
* state that no cryptographic operation could actually serve.
|
||||
*/
|
||||
public void cancelKeyDeletion(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:CancelKeyDeletion");
|
||||
if (lacksImportedKeyMaterial(key)) {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
} else {
|
||||
key.setKeyState("Enabled");
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:CancelKeyDeletion");
|
||||
if (lacksImportedKeyMaterial(key)) {
|
||||
key.setKeyState(PENDING_IMPORT);
|
||||
key.setEnabled(false);
|
||||
} else {
|
||||
key.setKeyState("Enabled");
|
||||
}
|
||||
key.setDeletionDate(0);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
key.setDeletionDate(0);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
|
||||
private static boolean lacksImportedKeyMaterial(KmsKey key) {
|
||||
@@ -796,11 +805,14 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void putKeyPolicy(String keyId, String policy, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:PutKeyPolicy");
|
||||
key.setPolicy(policy);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:PutKeyPolicy");
|
||||
key.setPolicy(policy);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Updated key policy for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -825,16 +837,68 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void updateKeyDescription(String keyId, String description, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:UpdateKeyDescription");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setDescription(description);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Updated description for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:UpdateKeyDescription");
|
||||
requireNotPendingDeletion(key);
|
||||
key.setDescription(description);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Updated description for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Key Rotation ────────────────────────────
|
||||
|
||||
public record KeyRotation(String keyId, String keyMaterialId, String keyMaterialState,
|
||||
Long rotationDate, String importState, String expirationModel, Long validTo) {
|
||||
private String cursor() {
|
||||
return String.format(Locale.ROOT, "%019d:%s", rotationDate == null ? 0L : rotationDate, keyMaterialId);
|
||||
}
|
||||
}
|
||||
|
||||
public Page<KeyRotation> listKeyRotations(String keyId, String includeKeyMaterial, String region,
|
||||
String marker, Integer limit) {
|
||||
String include = includeKeyMaterial == null ? "ROTATIONS_ONLY" : includeKeyMaterial;
|
||||
if (!Set.of("ROTATIONS_ONLY", "ALL_KEY_MATERIAL").contains(include)) {
|
||||
throw SmithyValidationErrors.validationException("Value '" + include
|
||||
+ "' at 'includeKeyMaterial' failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[ALL_KEY_MATERIAL, ROTATIONS_ONLY]");
|
||||
}
|
||||
if (limit != null && (limit < 1 || limit > MAX_LIST_KEYS_LIMIT)) {
|
||||
throw SmithyValidationErrors.validationException("Limit must be between 1 and 1000.");
|
||||
}
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
if (includeKeyMaterial != null) {
|
||||
validateRotationKeySpec(key);
|
||||
}
|
||||
List<KeyRotation> rotations = new ArrayList<>();
|
||||
boolean includeAll = "ALL_KEY_MATERIAL".equals(include);
|
||||
boolean external = EXTERNAL_ORIGIN.equals(key.getOrigin());
|
||||
if (key.getBackingKeys() != null) {
|
||||
for (Map.Entry<String, String> material : key.getBackingKeys().entrySet()) {
|
||||
Long rotationDate = key.getBackingKeyRotationDates().get(material.getKey());
|
||||
if (includeAll || rotationDate != null) {
|
||||
String materialId = keyMaterialId(key.getKeyId(), decodeBackingMaterial(material.getValue()));
|
||||
String state = material.getKey().equals(key.getCurrentBackingKeyId()) ? "CURRENT" : "NON_CURRENT";
|
||||
rotations.add(new KeyRotation(key.getKeyId(), materialId, state, rotationDate,
|
||||
external ? "IMPORTED" : null, external ? key.getExpirationModel() : null,
|
||||
external && KEY_MATERIAL_EXPIRES.equals(key.getExpirationModel()) ? key.getValidTo() : null));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (includeAll && external && rotations.isEmpty() && key.getKeyMaterialId() != null) {
|
||||
rotations.add(new KeyRotation(key.getKeyId(), key.getKeyMaterialId(), "CURRENT", null,
|
||||
"PENDING_IMPORT", null, null));
|
||||
}
|
||||
return page(rotations, KeyRotation::cursor,
|
||||
"ListKeyRotations\n" + key.getArn() + "\n" + include, marker,
|
||||
limit == null ? 100 : limit, MAX_LIST_KEYS_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getKeyRotationStatus(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
if (KmsKeyUsage.ENCRYPT_DECRYPT != key.getKeyUsage()
|
||||
@@ -845,52 +909,65 @@ public class KmsService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public void enableKeyRotation(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
validateRotationKeySpec(key);
|
||||
key.setKeyRotationEnabled(true);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Enabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
validateRotationKeySpec(key);
|
||||
key.setKeyRotationEnabled(true);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Enabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
public void disableKeyRotation(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
key.setKeyRotationEnabled(false);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Disabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKeyRotation");
|
||||
validateRotationOrigin(key);
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
key.setKeyRotationEnabled(false);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Disabled key rotation for KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
public void enableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(true);
|
||||
key.setKeyState("Enabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Enabled KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:EnableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(true);
|
||||
key.setKeyState("Enabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Enabled KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
public void disableKey(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(false);
|
||||
key.setKeyState("Disabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Disabled KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:DisableKey");
|
||||
requireNotPendingDeletion(key);
|
||||
requireImportedKeyMaterial(key);
|
||||
key.setEnabled(false);
|
||||
key.setKeyState("Disabled");
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Disabled KMS key: {0} in {1}", key.getKeyId(), region);
|
||||
}
|
||||
}
|
||||
|
||||
private static final int ON_DEMAND_ROTATION_LIMIT = 25;
|
||||
public String rotateKeyOnDemand(String keyId, String region) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:RotateKeyOnDemand");
|
||||
validateKeyIsUsableForCryptoOperations(key);
|
||||
if (MULTI_REGION_REPLICA.equals(key.getMultiRegionKeyType())) {
|
||||
@@ -906,28 +983,37 @@ public class KmsService implements ResourceProvider {
|
||||
throw new AwsException("LimitExceededException",
|
||||
"On-demand rotation quota for KMS key " + key.getKeyId() + " is exceeded.", 400);
|
||||
}
|
||||
key.setOnDemandRotationCount(key.getOnDemandRotationCount() + 1);
|
||||
KmsKey rotated = new KmsKey(key);
|
||||
rotated.setOnDemandRotationCount(rotated.getOnDemandRotationCount() + 1);
|
||||
// AWS keeps prior backing keys after rotation so ciphertext encrypted under them keeps
|
||||
// decrypting.
|
||||
keyTypes.symmetric().addBackingKey(key);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
if (MULTI_REGION_PRIMARY.equals(key.getMultiRegionKeyType())) {
|
||||
synchronizeMultiRegionReplicas(key);
|
||||
keyTypes.symmetric().addBackingKey(rotated);
|
||||
rotated.getBackingKeyRotationDates().put(rotated.getCurrentBackingKeyId(), Instant.now().toEpochMilli());
|
||||
if (MULTI_REGION_PRIMARY.equals(rotated.getMultiRegionKeyType())) {
|
||||
publishMultiRegionRotation(rotated);
|
||||
} else {
|
||||
keyStore.put(region + "::" + rotated.getKeyId(), rotated);
|
||||
}
|
||||
return key.getKeyId();
|
||||
return rotated.getKeyId();
|
||||
}
|
||||
}
|
||||
|
||||
private void synchronizeMultiRegionReplicas(KmsKey primary) {
|
||||
private void publishMultiRegionRotation(KmsKey primary) {
|
||||
Map<String, KmsKey> updates = new LinkedHashMap<>();
|
||||
String primaryRegion = AwsArnUtils.parse(primary.getArn()).region();
|
||||
updates.put(primaryRegion + "::" + primary.getKeyId(), primary);
|
||||
for (KmsKey candidate : listAllMultiRegionKeys(primary.getKeyId())) {
|
||||
if (!MULTI_REGION_REPLICA.equals(candidate.getMultiRegionKeyType())) {
|
||||
continue;
|
||||
}
|
||||
candidate.setBackingKeys(new HashMap<>(primary.getBackingKeys()));
|
||||
candidate.setCurrentBackingKeyId(primary.getCurrentBackingKeyId());
|
||||
String replicaRegion = AwsArnUtils.parse(candidate.getArn()).region();
|
||||
keyStore.put(replicaRegion + "::" + candidate.getKeyId(), candidate);
|
||||
KmsKey replica = new KmsKey(candidate);
|
||||
replica.setBackingKeys(new HashMap<>(primary.getBackingKeys()));
|
||||
replica.setCurrentBackingKeyId(primary.getCurrentBackingKeyId());
|
||||
replica.setBackingKeyRotationDates(new HashMap<>(primary.getBackingKeyRotationDates()));
|
||||
String replicaRegion = AwsArnUtils.parse(replica.getArn()).region();
|
||||
updates.put(replicaRegion + "::" + replica.getKeyId(), replica);
|
||||
}
|
||||
keyStore.putAllAtomically(updates);
|
||||
}
|
||||
|
||||
private static void validateRotationOrigin(KmsKey key) {
|
||||
@@ -950,24 +1036,39 @@ public class KmsService implements ResourceProvider {
|
||||
|
||||
public ImportParameters getParametersForImport(String keyId, String wrappingAlgorithm,
|
||||
String wrappingKeySpec, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm, wrappingKeySpec);
|
||||
String keyArn;
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm, wrappingKeySpec);
|
||||
keyArn = key.getArn();
|
||||
}
|
||||
|
||||
KmsKeyImport.WrappingKeyPair wrappingKeyPair = KmsKeyImport.generateWrappingKeyPair(wrappingKeySpec);
|
||||
KmsKeyImport.WrappingKeyPair wrappingKeyPair = generateImportWrappingKeyPair(wrappingKeySpec);
|
||||
KmsImportParameters parameters = new KmsImportParameters();
|
||||
parameters.setWrappingPrivateKeyEncoded(wrappingKeyPair.privateKeyEncoded());
|
||||
parameters.setWrappingAlgorithm(wrappingAlgorithm);
|
||||
parameters.setImportToken(newImportToken());
|
||||
parameters.setParametersValidTo(Instant.now().plus(IMPORT_PARAMETERS_VALIDITY).getEpochSecond());
|
||||
key.setImportParameters(parameters);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyArn, storedKeyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
KmsKeyImport.validateWrappingAlgorithm(key.getKeySpec(), wrappingAlgorithm, wrappingKeySpec);
|
||||
key.setImportParameters(parameters);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
|
||||
LOG.infov("Issued import parameters for KMS key {0} in {1} ({2}/{3})",
|
||||
key.getKeyId(), region, wrappingAlgorithm, wrappingKeySpec);
|
||||
return new ImportParameters(key.getArn(), wrappingKeyPair.publicKeyEncoded(),
|
||||
parameters.getImportToken(), parameters.getParametersValidTo());
|
||||
LOG.infov("Issued import parameters for KMS key {0} in {1} ({2}/{3})",
|
||||
key.getKeyId(), region, wrappingAlgorithm, wrappingKeySpec);
|
||||
return new ImportParameters(key.getArn(), wrappingKeyPair.publicKeyEncoded(),
|
||||
parameters.getImportToken(), parameters.getParametersValidTo());
|
||||
}
|
||||
}
|
||||
|
||||
KmsKeyImport.WrappingKeyPair generateImportWrappingKeyPair(String wrappingKeySpec) {
|
||||
return KmsKeyImport.generateWrappingKeyPair(wrappingKeySpec);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -976,40 +1077,43 @@ public class KmsService implements ResourceProvider {
|
||||
*/
|
||||
public KmsKey importKeyMaterial(String keyId, String importToken, byte[] encryptedKeyMaterial,
|
||||
String expirationModel, Long validTo, String importType, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
validateImportType(importType, key);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireExternalOrigin(key);
|
||||
requireNotPendingDeletion(key);
|
||||
validateImportType(importType, key);
|
||||
|
||||
KmsImportParameters parameters = requireCurrentImportToken(key);
|
||||
if (!parameters.getImportToken().equals(importToken)) {
|
||||
throw new AwsException("InvalidImportTokenException",
|
||||
"The import token is invalid or was not issued for this KMS key.", 400);
|
||||
KmsImportParameters parameters = requireCurrentImportToken(key);
|
||||
if (!parameters.getImportToken().equals(importToken)) {
|
||||
throw new AwsException("InvalidImportTokenException",
|
||||
"The import token is invalid or was not issued for this KMS key.", 400);
|
||||
}
|
||||
String effectiveExpirationModel = resolveExpirationModel(expirationModel, validTo);
|
||||
|
||||
byte[] material = KmsKeyImport.unwrap(parameters.getWrappingPrivateKeyEncoded(),
|
||||
parameters.getWrappingAlgorithm(), encryptedKeyMaterial);
|
||||
String keyMaterialId = keyMaterialId(key.getKeyId(), material);
|
||||
requireSameMaterialAsFirstImport(key, keyMaterialId);
|
||||
|
||||
KmsKeyType keyType = keyTypes.of(key.getKeySpec());
|
||||
keyType.importKeyMaterial(key, material);
|
||||
|
||||
if (KmsKeySpec.SYMMETRIC_DEFAULT == key.getKeySpec()) {
|
||||
installImportedBackingKey(key, keyMaterialId, material);
|
||||
}
|
||||
key.setKeyMaterialId(keyMaterialId);
|
||||
key.setExpirationModel(effectiveExpirationModel);
|
||||
key.setValidTo(KEY_MATERIAL_EXPIRES.equals(effectiveExpirationModel) ? validTo : 0L);
|
||||
key.setKeyState("Enabled");
|
||||
key.setEnabled(true);
|
||||
key.setImportParameters(null);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
|
||||
LOG.infov("Imported key material into KMS key {0} in {1} ({2})",
|
||||
key.getKeyId(), region, effectiveExpirationModel);
|
||||
return key;
|
||||
}
|
||||
String effectiveExpirationModel = resolveExpirationModel(expirationModel, validTo);
|
||||
|
||||
byte[] material = KmsKeyImport.unwrap(parameters.getWrappingPrivateKeyEncoded(),
|
||||
parameters.getWrappingAlgorithm(), encryptedKeyMaterial);
|
||||
String keyMaterialId = keyMaterialId(key.getKeyId(), material);
|
||||
requireSameMaterialAsFirstImport(key, keyMaterialId);
|
||||
|
||||
KmsKeyType keyType = keyTypes.of(key.getKeySpec());
|
||||
keyType.importKeyMaterial(key, material);
|
||||
|
||||
if (KmsKeySpec.SYMMETRIC_DEFAULT == key.getKeySpec()) {
|
||||
installImportedBackingKey(key, keyMaterialId, material);
|
||||
}
|
||||
key.setKeyMaterialId(keyMaterialId);
|
||||
key.setExpirationModel(effectiveExpirationModel);
|
||||
key.setValidTo(KEY_MATERIAL_EXPIRES.equals(effectiveExpirationModel) ? validTo : 0L);
|
||||
key.setKeyState("Enabled");
|
||||
key.setEnabled(true);
|
||||
key.setImportParameters(null);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
|
||||
LOG.infov("Imported key material into KMS key {0} in {1} ({2})",
|
||||
key.getKeyId(), region, effectiveExpirationModel);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1018,7 +1122,7 @@ public class KmsService implements ResourceProvider {
|
||||
* material after a delete or expiry reinstates the id that earlier ciphertext names.
|
||||
*/
|
||||
private void installImportedBackingKey(KmsKey key, String keyMaterialId, byte[] material) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
Map<String, String> backingKeys = new HashMap<>();
|
||||
backingKeys.put(keyMaterialId, Base64.getEncoder().encodeToString(material));
|
||||
key.setBackingKeys(backingKeys);
|
||||
@@ -1031,12 +1135,15 @@ public class KmsService implements ResourceProvider {
|
||||
* as it does on AWS: PendingDeletion outranks the PendingImport this would otherwise set.
|
||||
*/
|
||||
public KmsKey deleteImportedKeyMaterial(String keyId, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireExternalOrigin(key);
|
||||
clearImportedKeyMaterial(key);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Deleted imported key material for KMS key {0} in {1}", key.getKeyId(), region);
|
||||
return key;
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireExternalOrigin(key);
|
||||
clearImportedKeyMaterial(key);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
LOG.infov("Deleted imported key material for KMS key {0} in {1}", key.getKeyId(), region);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1065,7 +1172,7 @@ public class KmsService implements ResourceProvider {
|
||||
* a later re-import, so what the key was originally given has to outlive the material itself.
|
||||
*/
|
||||
private void clearImportedKeyMaterial(KmsKey key) {
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
key.setPrivateKeyEncoded(null);
|
||||
key.setBackingKeys(new HashMap<>());
|
||||
key.setCurrentBackingKeyId(null);
|
||||
@@ -1753,7 +1860,7 @@ public class KmsService implements ResourceProvider {
|
||||
private byte[] encryptEnvelope(KmsKey key, byte[] plaintext, Map<String, String> encryptionContext) {
|
||||
String backingKeyId;
|
||||
byte[] dek;
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
backingKeyId = key.getCurrentBackingKeyId();
|
||||
String materialB64 = key.getBackingKeys() == null ? null : key.getBackingKeys().get(backingKeyId);
|
||||
if (materialB64 == null) {
|
||||
@@ -1800,7 +1907,7 @@ public class KmsService implements ResourceProvider {
|
||||
*/
|
||||
private byte[] decryptEnvelopeV3(EnvelopeV3 envelope, KmsKey key, Map<String, String> encryptionContext) {
|
||||
String materialB64;
|
||||
synchronized (backingKeyMaterialLock) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(key.getKeyId())) {
|
||||
materialB64 = key.getBackingKeys() == null ? null : key.getBackingKeys().get(envelope.backingKeyId());
|
||||
}
|
||||
if (materialB64 == null) {
|
||||
@@ -2177,31 +2284,42 @@ public class KmsService implements ResourceProvider {
|
||||
// ──────────────────────────── Tags ────────────────────────────
|
||||
|
||||
public void tagResource(String keyId, Map<String, String> tags, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:TagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
ReservedTags.rejectReservedTagsOnUpdate(tags);
|
||||
key.getTags().putAll(tags);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:TagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
ReservedTags.rejectReservedTagsOnUpdate(tags);
|
||||
key.getTags().putAll(tags);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagResource(String keyId, List<String> tagKeys, String region) {
|
||||
KmsKey key = resolveKey(keyId, region);
|
||||
requireCustomerManaged(key, "kms:UntagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
tagKeys.forEach(key.getTags()::remove);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
String storedKeyId = resolveStoredKeyId(keyId, region);
|
||||
synchronized (backingKeyMaterialLocks.lockFor(storedKeyId)) {
|
||||
KmsKey key = resolveKey(keyId, storedKeyId, region);
|
||||
requireCustomerManaged(key, "kms:UntagResource");
|
||||
requireNotPendingDeletion(key);
|
||||
tagKeys.forEach(key.getTags()::remove);
|
||||
keyStore.put(region + "::" + key.getKeyId(), key);
|
||||
}
|
||||
}
|
||||
|
||||
// ──────────────────────────── Helpers ────────────────────────────
|
||||
|
||||
private KmsKey resolveKey(String keyIdOrArn, String region) {
|
||||
String keyId = resolveStoredKeyId(keyIdOrArn, region);
|
||||
KmsKey key = keyStore.get(region + "::" + keyId)
|
||||
.orElseThrow(() -> keyNotFound(keyIdOrArn, keyId, region));
|
||||
key = expireImportedKeyMaterialIfDue(key, region);
|
||||
key = ensureBackingKeyMaterial(key, region);
|
||||
return key;
|
||||
return resolveKey(keyIdOrArn, resolveStoredKeyId(keyIdOrArn, region), region);
|
||||
}
|
||||
|
||||
private KmsKey resolveKey(String keyIdOrArn, String keyId, String region) {
|
||||
synchronized (backingKeyMaterialLocks.lockFor(keyId)) {
|
||||
KmsKey key = keyStore.get(region + "::" + keyId)
|
||||
.orElseThrow(() -> keyNotFound(keyIdOrArn, keyId, region));
|
||||
key = expireImportedKeyMaterialIfDue(key, region);
|
||||
key = ensureBackingKeyMaterial(key, region);
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,11 +45,44 @@ public class KmsKey {
|
||||
// and KmsService lazily generates material for them on first use.
|
||||
private Map<String, String> backingKeys = new HashMap<>();
|
||||
private String currentBackingKeyId;
|
||||
// Only completed rotations have a timestamp. The first material and material loaded
|
||||
// from older snapshots have no recorded rotation date.
|
||||
private Map<String, Long> backingKeyRotationDates = new HashMap<>();
|
||||
|
||||
public KmsKey() {
|
||||
this.creationDate = Instant.now().getEpochSecond();
|
||||
}
|
||||
|
||||
public KmsKey(KmsKey source) {
|
||||
this.keyId = source.keyId;
|
||||
this.arn = source.arn;
|
||||
this.description = source.description;
|
||||
this.enabled = source.enabled;
|
||||
this.keyState = source.keyState;
|
||||
this.origin = source.origin;
|
||||
this.keyManager = source.keyManager;
|
||||
this.expirationModel = source.expirationModel;
|
||||
this.validTo = source.validTo;
|
||||
this.importParameters = source.importParameters;
|
||||
this.keyMaterialId = source.keyMaterialId;
|
||||
this.keyUsage = source.keyUsage;
|
||||
this.keySpec = source.keySpec;
|
||||
this.multiRegion = source.multiRegion;
|
||||
this.multiRegionKeyType = source.multiRegionKeyType;
|
||||
this.multiRegionPrimaryRegion = source.multiRegionPrimaryRegion;
|
||||
this.creationDate = source.creationDate;
|
||||
this.deletionDate = source.deletionDate;
|
||||
this.policy = source.policy;
|
||||
this.keyRotationEnabled = source.keyRotationEnabled;
|
||||
this.tags = source.tags == null ? null : new HashMap<>(source.tags);
|
||||
this.privateKeyEncoded = source.privateKeyEncoded;
|
||||
this.publicKeyEncoded = source.publicKeyEncoded;
|
||||
this.onDemandRotationCount = source.onDemandRotationCount;
|
||||
this.backingKeys = source.backingKeys == null ? null : new HashMap<>(source.backingKeys);
|
||||
this.currentBackingKeyId = source.currentBackingKeyId;
|
||||
this.backingKeyRotationDates = source.backingKeyRotationDates == null ? null : new HashMap<>(source.backingKeyRotationDates);
|
||||
}
|
||||
|
||||
public String getKeyId() { return keyId; }
|
||||
public void setKeyId(String keyId) { this.keyId = keyId; }
|
||||
|
||||
@@ -131,4 +164,9 @@ public class KmsKey {
|
||||
|
||||
public String getCurrentBackingKeyId() { return currentBackingKeyId; }
|
||||
public void setCurrentBackingKeyId(String currentBackingKeyId) { this.currentBackingKeyId = currentBackingKeyId; }
|
||||
|
||||
public Map<String, Long> getBackingKeyRotationDates() { return backingKeyRotationDates; }
|
||||
public void setBackingKeyRotationDates(Map<String, Long> dates) {
|
||||
this.backingKeyRotationDates = dates == null ? new HashMap<>() : dates;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user