feat(paymentcryptography): a deletion is scheduled, and it can be cancelled (#8799)

DeleteKey removed a key at once, so there was no waiting period and nothing to cancel. It now schedules the deletion: the key is disabled and kept as DELETE_PENDING, with the moment the deletion takes effect after the DeleteKeyInDays waiting period (seven days by default), and it keeps its aliases, tags and resource policy. Floci does not sweep the period, so the key stays pending. RestoreKey cancels the deletion: the key returns to CREATE_COMPLETE without a pending timestamp and stays disabled until StartKeyUsage, as the guide's example shows. Replicas carry the pending state and its timestamp, a replica is not restored on its own, and a pending key refuses another deletion, StartKeyUsage and StopKeyUsage with ConflictException.
This commit is contained in:
Murtuza Chhil authored and GitHub committed 2026-11-17 21:23:28 -04:00
1 parent c2b9957032
commit 33ed71437d
8 files changed
+378 -100

No files matched your search

@@ -65,6 +65,7 @@ public class PaymentCryptographyJsonHandler {
case "GetKey" -> () -> getKey(request, region);
case "ListKeys" -> () -> listKeys(request, region);
case "DeleteKey" -> () -> deleteKey(request, region);
case "RestoreKey" -> () -> restoreKey(request, region);
case "StartKeyUsage" -> () -> startKeyUsage(request, region);
case "StopKeyUsage" -> () -> stopKeyUsage(request, region);
case "CreateAlias" -> () -> createAlias(request, region);
@@ -181,14 +182,24 @@ public class PaymentCryptographyJsonHandler {
}
/**
* Deletes a key and answers with the key, as the reference does.
* Schedules a key for deletion and answers with the key, as the reference does.
*
* <p>The waiting period the reference offers is read, and the key is
* deleted at once all the same: the service deletes a key the moment it
* is asked to.
* <p>The waiting period is the one the request names, or the reference's default of
* seven days. The model settles the range, so a period outside it is refused before
* the key is touched.
*/
private Map<String, Object> deleteKey(JsonNode request, String region) {
PaymentCryptographyKey key = service.deleteKey(requiredKey(request, region, KEY_IDENTIFIER));
int days = request.hasNonNull("DeleteKeyInDays")
? request.path("DeleteKeyInDays").asInt() : 7;
PaymentCryptographyKey key = service.deleteKey(
requiredKey(request, region, KEY_IDENTIFIER), days);
return Map.of("Key", keyObject(key));
}
/** Cancels the scheduled deletion of a key. */
private Map<String, Object> restoreKey(JsonNode request, String region) {
PaymentCryptographyKey key = service.restoreKey(
requiredKey(request, region, KEY_IDENTIFIER));
return Map.of("Key", keyObject(key));
}
@@ -22,7 +22,6 @@ import jakarta.inject.Inject;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
@@ -359,40 +358,72 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
}
/**
* Removes a key and answers the key as the service does when a deletion is
* scheduled: the key's own metadata, its state {@code DELETE_PENDING} and
* the pending timestamp of that deletion.
*
* <p>The service deletes a key the moment it is asked to, so the waiting
* period the service offers is not one: the key is gone when the answer
* goes out. The answer carries only the pending timestamp of that
* deletion; the delete timestamp stays unset, because the key is already
* gone when the answer goes out.
*
* <p>The lock is the same one the tagging operations take, so a key that
* is being tagged cannot be written back after it was deleted.
* Schedules a key for deletion after the reference's default waiting period, seven days.
*/
public synchronized PaymentCryptographyKey deleteKey(PaymentCryptographyKey key) {
return deleteKey(key, 7);
}
/**
* Schedules a key for deletion and answers with the key as it now stands.
*
* <p>The key is disabled and stays in the store, in the state {@code DELETE_PENDING},
* until the waiting period ends. The key is therefore still readable and still named by
* its aliases, and {@link #restoreKey(PaymentCryptographyKey)} can cancel the deletion.
* The waiting period is a promise to the caller, not a sweep this service runs: a key
* stays pending rather than disappearing on a timer the emulator does not have.
*
* <p>The pending timestamp is the moment the deletion takes effect, and the deletion
* timestamp is unset while the key has not been deleted.
*
* <p>A key that is already pending deletion is refused with {@code ConflictException},
* as the reference refuses it: the deletion is already under way.
*/
public synchronized PaymentCryptographyKey deleteKey(PaymentCryptographyKey key, int days) {
PaymentCryptographyKey current = liveKey(key);
// A replica is read-only: only the primary may be deleted.
requireNotReplica(key, "only the primary may be deleted");
requireNotReplica(current, "only the primary may be deleted");
requireNotPendingDeletion(current, "it cannot be deleted again");
long now = clock.instant().getEpochSecond();
key.setKeyState("DELETE_PENDING");
key.setEnabled(false);
key.setDeletePendingTimestamp(now);
// The replicas of a key go when the key goes. A replica left behind would be a
// record of a key that is gone, and it could not be deleted on its own.
for (String target : key.getReplicationRegions()) {
keyStore.delete(storageKey(target, key.getKeyId()));
}
keyStore.delete(storageKey(key.getRegion(), key.getKeyId()));
// A key saved before the region became part of its name is under that name too,
// and it goes with the key: a record left behind would be a key that was deleted
// and still there.
keyStore.delete(key.getKeyId());
// The aliases that name the key go with it, in every region: a replica shares the
// identifier of its primary, so these are the aliases of the replicas too.
deleteAliasesOf(null, key.getKeyId());
return key;
current.setKeyState("DELETE_PENDING");
current.setEnabled(false);
current.setDeletePendingTimestamp(now + days * 86_400L);
current.setDeleteTimestamp(0);
storeKey(current);
// The replicas of a key carry the state of their primary, so a key that is pending
// deletion is pending deletion in every region it replicates into.
refreshReplicas(current, current.getReplicationRegions());
return current;
}
/**
* Cancels the scheduled deletion of a key and answers with the key as it now stands.
*
* <p>Only a key in the state {@code DELETE_PENDING} can be restored; one that is not
* pending deletion is refused with {@code ConflictException}, as the reference refuses
* it.
*
* <p>The reference's own example of the restore says that two members change: the state
* goes back to {@code CREATE_COMPLETE} and the pending timestamp is removed. The key
* stays disabled, because the deletion disabled it and it is {@code StartKeyUsage} that
* enables a key again, and the moments of its use are the ones it had.
*
* <p>A replica is read-only, so the deletion is cancelled on the primary: a replica
* that was restored on its own would be usable while the key it is a copy of is still
* pending deletion.
*/
public synchronized PaymentCryptographyKey restoreKey(PaymentCryptographyKey key) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "only the primary may be restored");
requirePendingDeletion(current, "it cannot be restored");
current.setKeyState("CREATE_COMPLETE");
current.setDeletePendingTimestamp(0);
current.setDeleteTimestamp(0);
storeKey(current);
// The replicas of a key carry the state of their primary, so a key that is restored
// is restored in every region it replicates into.
refreshReplicas(current, current.getReplicationRegions());
return current;
}
// ---- key usage -----------------------------------------------------------
@@ -411,6 +442,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
public synchronized PaymentCryptographyKey startKeyUsage(PaymentCryptographyKey key) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
requireNotPendingDeletion(current, "its use cannot be started");
if (!current.isEnabled()) {
current.setEnabled(true);
current.setUsageStartTimestamp(clock.instant().getEpochSecond());
@@ -437,6 +469,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
public synchronized PaymentCryptographyKey stopKeyUsage(PaymentCryptographyKey key) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
requireNotPendingDeletion(current, "its use cannot be stopped");
if (current.isEnabled()) {
current.setEnabled(false);
current.setUsageStopTimestamp(clock.instant().getEpochSecond());
@@ -649,6 +682,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
replica.setEnabled(primary.isEnabled());
replica.setUsageStartTimestamp(primary.getUsageStartTimestamp());
replica.setUsageStopTimestamp(primary.getUsageStopTimestamp());
replica.setDeletePendingTimestamp(primary.getDeletePendingTimestamp());
replica.setDeleteTimestamp(primary.getDeleteTimestamp());
replica.setUsingDefaultReplicationRegions(primary.isUsingDefaultReplicationRegions());
replica.setPrimaryRegion(primary.getPrimaryRegion());
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
@@ -674,6 +709,42 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
}
}
/**
* The refusal of a request that names a key whose deletion is under way.
*
* <p>A key that is pending deletion is disabled and on its way out, so the use of it is
* not the caller's to change: the deletion is cancelled with
* {@link #restoreKey(PaymentCryptographyKey)} instead. The reference does not publish
* the answer for this case, so the service refuses the request with
* {@code ConflictException}, which is the code it uses for a request that conflicts with
* the state of a key.
*
* @param key the key the request names
* @param what what the request wanted to do with the key, for the message
*/
private static void requireNotPendingDeletion(PaymentCryptographyKey key, String what) {
if ("DELETE_PENDING".equals(key.getKeyState())) {
throw new AwsException("ConflictException",
"the key " + key.getArn() + " is pending deletion, and " + what, 400);
}
}
/**
* The refusal of a request that names a key whose deletion is not under way.
*
* <p>A scheduled deletion is cancelled while it is under way, so a key that is not
* pending deletion has nothing to cancel.
*
* @param key the key the request names
* @param what what the request wanted to do with the key, for the message
*/
private static void requirePendingDeletion(PaymentCryptographyKey key, String what) {
if (!"DELETE_PENDING".equals(key.getKeyState())) {
throw new AwsException("ConflictException",
"the key " + key.getArn() + " is not pending deletion, and " + what, 400);
}
}
/**
* The tags of a key.
*
@@ -1671,6 +1742,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
replica.setCreateTimestamp(primary.getCreateTimestamp());
replica.setUsageStartTimestamp(primary.getUsageStartTimestamp());
replica.setUsageStopTimestamp(primary.getUsageStopTimestamp());
replica.setDeletePendingTimestamp(primary.getDeletePendingTimestamp());
replica.setDeleteTimestamp(primary.getDeleteTimestamp());
replica.setPrimaryRegion(primary.getPrimaryRegion());
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
replica.setMultiRegionKeyType("REPLICA");