feat(paymentcryptography): the use of a key can be stopped and started (#8749)

StartKeyUsage and StopKeyUsage enable and disable a key and answer with the key as it now stands. The key changes only when its enablement changes: a stop records UsageStopTimestamp, a start records UsageStartTimestamp and clears the stop moment, and a request that names a key already in that state keeps the moment it changed. A request that names a replica is refused, and a change on the primary reaches its replicas. The service takes those moments from the clock it is given, and the new integration tests delete the keys they make.
This commit is contained in:
Murtuza Chhil authored and GitHub committed 2026-11-17 12:49:56 -04:00
1 parent 2b3c22a3a5
commit 10445d89d8
9 files changed
+315 -5

No files matched your search

@@ -65,6 +65,8 @@ public class PaymentCryptographyJsonHandler {
case "GetKey" -> () -> getKey(request, region);
case "ListKeys" -> () -> listKeys(request, region);
case "DeleteKey" -> () -> deleteKey(request, region);
case "StartKeyUsage" -> () -> startKeyUsage(request, region);
case "StopKeyUsage" -> () -> stopKeyUsage(request, region);
case "CreateAlias" -> () -> createAlias(request, region);
case "GetAlias" -> () -> getAlias(request, region);
case "ListAliases" -> () -> listAliases(request, region);
@@ -190,6 +192,20 @@ public class PaymentCryptographyJsonHandler {
return Map.of("Key", keyObject(key));
}
/** Starts the use of a key. */
private Map<String, Object> startKeyUsage(JsonNode request, String region) {
PaymentCryptographyKey key = service.startKeyUsage(
requiredKey(request, region, KEY_IDENTIFIER));
return Map.of("Key", keyObject(key));
}
/** Stops the use of a key. */
private Map<String, Object> stopKeyUsage(JsonNode request, String region) {
PaymentCryptographyKey key = service.stopKeyUsage(
requiredKey(request, region, KEY_IDENTIFIER));
return Map.of("Key", keyObject(key));
}
// ---- aliases ------------------------------------------------------------
private Map<String, Object> createAlias(JsonNode request, String region) {
@@ -572,6 +588,9 @@ public class PaymentCryptographyJsonHandler {
out.put("Exportable", key.isExportable());
out.put("CreateTimestamp", key.getCreateTimestamp());
out.put("UsageStartTimestamp", key.getUsageStartTimestamp());
if (key.getUsageStopTimestamp() > 0) {
out.put("UsageStopTimestamp", key.getUsageStopTimestamp());
}
if (key.getDeletePendingTimestamp() > 0) {
out.put("DeletePendingTimestamp", key.getDeletePendingTimestamp());
}
@@ -92,6 +92,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
private final RegionResolver regionResolver;
private final SecureRandom secureRandom;
private final PaymentCryptographyRequestContext requestContext;
/** The service's notion of time, the one every moment it records is taken from. */
private final Clock clock;
@Inject
public PaymentCryptographyService(StorageFactory storageFactory, RegionResolver regionResolver,
@@ -166,6 +168,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
this.exchange = new PaymentCryptographyKeyExchange(this, certificates, clock);
this.secureRandom = secureRandom;
this.requestContext = requestContext;
this.clock = clock;
}
// ---- keys ---------------------------------------------------------------
@@ -292,7 +295,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
}
}
}
long now = Instant.now().getEpochSecond();
long now = clock.instant().getEpochSecond();
key.setCreateTimestamp(now);
key.setUsageStartTimestamp(now);
keyStore.put(storageKey(region, key.getKeyId()), key);
@@ -372,7 +375,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
public synchronized PaymentCryptographyKey deleteKey(PaymentCryptographyKey key) {
// A replica is read-only: only the primary may be deleted.
requireNotReplica(key, "only the primary may be deleted");
long now = Instant.now().getEpochSecond();
long now = clock.instant().getEpochSecond();
key.setKeyState("DELETE_PENDING");
key.setEnabled(false);
key.setDeletePendingTimestamp(now);
@@ -392,6 +395,57 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
return key;
}
// ---- key usage -----------------------------------------------------------
/**
* Starts the use of a key, and answers with the key as it now stands.
*
* <p>The moment the use starts is the moment the key became active, so a key that is
* already in use keeps the moment it became active and a request that names it changes
* nothing. A key that had been stopped is usable again: the stop moment is cleared,
* because a key that is in use has no such moment.
*
* <p>A replica is read-only, so the change is made on the primary, and it reaches the
* replicas, as the reference says it does.
*/
public synchronized PaymentCryptographyKey startKeyUsage(PaymentCryptographyKey key) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
if (!current.isEnabled()) {
current.setEnabled(true);
current.setUsageStartTimestamp(clock.instant().getEpochSecond());
current.setUsageStopTimestamp(0);
storeKey(current);
refreshReplicas(current, current.getReplicationRegions());
}
return current;
}
/**
* Stops the use of a key, and answers with the key as it now stands.
*
* <p>A stopped key stays readable, and its material stays where it is: only the use of
* it stops, so a key may be started again.
*
* <p>The moment the use stops is the moment the key became inactive, so a key that is
* already stopped keeps the moment it stopped and a request that names it changes
* nothing.
*
* <p>A replica is read-only, so the change is made on the primary, and it reaches the
* replicas, as the reference says it does.
*/
public synchronized PaymentCryptographyKey stopKeyUsage(PaymentCryptographyKey key) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
if (current.isEnabled()) {
current.setEnabled(false);
current.setUsageStopTimestamp(clock.instant().getEpochSecond());
storeKey(current);
refreshReplicas(current, current.getReplicationRegions());
}
return current;
}
// ---- aliases ------------------------------------------------------------
/**
@@ -593,6 +647,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
replica.setTags(new LinkedHashMap<>(primary.getTags()));
replica.setKeyState(primary.getKeyState());
replica.setEnabled(primary.isEnabled());
replica.setUsageStartTimestamp(primary.getUsageStartTimestamp());
replica.setUsageStopTimestamp(primary.getUsageStopTimestamp());
replica.setUsingDefaultReplicationRegions(primary.isUsingDefaultReplicationRegions());
replica.setPrimaryRegion(primary.getPrimaryRegion());
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
@@ -1614,6 +1670,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
replica.setTags(new LinkedHashMap<>(primary.getTags()));
replica.setCreateTimestamp(primary.getCreateTimestamp());
replica.setUsageStartTimestamp(primary.getUsageStartTimestamp());
replica.setUsageStopTimestamp(primary.getUsageStopTimestamp());
replica.setPrimaryRegion(primary.getPrimaryRegion());
replica.setReplicationRegions(new ArrayList<>(primary.getReplicationRegions()));
replica.setMultiRegionKeyType("REPLICA");
@@ -66,6 +66,7 @@ public class PaymentCryptographyKey {
private String keyCertificatePem;
private long createTimestamp;
private long usageStartTimestamp;
private long usageStopTimestamp;
private long deletePendingTimestamp;
private long deleteTimestamp;
private volatile Map<String, String> tags = new HashMap<>();
@@ -162,6 +163,12 @@ public class PaymentCryptographyKey {
this.usageStartTimestamp = usageStartTimestamp;
}
/** When the key was last stopped, zero while it has not been stopped. */
public long getUsageStopTimestamp() { return usageStopTimestamp; }
public void setUsageStopTimestamp(long usageStopTimestamp) {
this.usageStopTimestamp = usageStopTimestamp;
}
/** When the key is deleted, zero while it is not scheduled for deletion. */
public long getDeletePendingTimestamp() { return deletePendingTimestamp; }
public void setDeletePendingTimestamp(long deletePendingTimestamp) {