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

@@ -68,6 +68,13 @@ class PaymentCryptographyIntegrationTest {
private static final String CREATE_KEY = CREATE_KEY_BODY.formatted("");
/** The member that makes a key disabled, as the request names it. */
private static final String DISABLED = """
,"Enabled":false""";
/** A key that is made disabled when it is created. */
private static final String CREATE_DISABLED_KEY = CREATE_KEY_BODY.formatted(DISABLED);
private static final String CREATE_WRAPPING_KEY = """
{"KeyAttributes":{"KeyAlgorithm":"AES_256","KeyClass":"SYMMETRIC_KEY",
"KeyUsage":"TR31_K1_KEY_BLOCK_PROTECTION_KEY",
@@ -896,6 +903,60 @@ class PaymentCryptographyIntegrationTest {
.then().statusCode(400)
.body("__type", containsString("ValidationException"));
}
@Test
void aKeyIsStoppedAndStartedAgain() {
String arn = call("CreateKey", CREATE_KEY).then().statusCode(200)
.extract().path("Key.KeyArn");
cleanup.register(() -> call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}"));
call("StopKeyUsage", "{\"KeyIdentifier\":\"" + arn + "\"}")
.then().statusCode(200)
.body("Key.Enabled", equalTo(false))
.body("Key.UsageStopTimestamp", notNullValue());
assertKeyIsStarted(call("StartKeyUsage", "{\"KeyIdentifier\":\"" + arn + "\"}"));
}
@Test
void aKeyCreatedDisabledIsStarted() {
// A key may be made disabled, and it is enabled again with the same operation that
// starts a key that was stopped.
String arn = call("CreateKey", CREATE_DISABLED_KEY)
.then().statusCode(200)
.body("Key.Enabled", equalTo(false))
.extract().path("Key.KeyArn");
cleanup.register(() -> call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}"));
assertKeyIsStarted(call("StartKeyUsage", "{\"KeyIdentifier\":\"" + arn + "\"}"));
}
@Test
void aStoppedKeyStopsItsReplica() {
// The reference says a stop on the primary stops the usage of the replicas too, so
// the replica of the key is disabled as well.
String arn = call("CreateKey", createReplicatedKeyBody("[\"us-west-2\"]"))
.then().statusCode(200)
.extract().path("Key.KeyArn");
// The replica goes with the primary, so one delete takes both.
cleanup.register(() -> call("DeleteKey", "{\"KeyIdentifier\":\"" + arn + "\"}"));
String replicaArn = arn.replace(":us-east-1:", ":us-west-2:");
call("StopKeyUsage", "{\"KeyIdentifier\":\"" + arn + "\"}")
.then().statusCode(200)
.body("Key.Enabled", equalTo(false));
callIn("us-west-2", "GetKey", "{\"KeyIdentifier\":\"" + replicaArn + "\"}")
.then().statusCode(200)
.body("Key.Enabled", equalTo(false))
.body("Key.UsageStopTimestamp", notNullValue());
}
/** A key that is started is enabled again, and it says when its use began. */
private static void assertKeyIsStarted(Response answer) {
answer.then().statusCode(200)
.body("Key.Enabled", equalTo(true))
.body("Key.UsageStartTimestamp", notNullValue());
}
@Test
void aKeyThatDoesNotExistIsNotFound() {
@@ -4,8 +4,10 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.acm.CertificateGenerator;
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyAlias;
import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey;
import io.floci.aws.testing.ManualClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -13,6 +15,9 @@ import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.Instant;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
@@ -484,6 +489,131 @@ class PaymentCryptographyServiceTest {
assertEquals("ResourceNotFoundException", deleted.getErrorCode());
}
@Test
void aKeyIsStoppedAndStartedAgain() {
PaymentCryptographyKey key = createKey("us-east-1");
PaymentCryptographyKey stopped = service.stopKeyUsage(key);
assertFalse(stopped.isEnabled(), "the key is still enabled after it was stopped");
assertTrue(stopped.getUsageStopTimestamp() > 0, "the stop moment was not recorded");
// The reference says a stopped key is deactivated rather than deleted, so the key is
// still there and its state is unchanged.
assertEquals("CREATE_COMPLETE", stopped.getKeyState());
assertNotNull(service.findKey("us-east-1", key.getKeyId()),
"a stopped key is not in the store");
PaymentCryptographyKey started = service.startKeyUsage(key);
assertTrue(started.isEnabled(), "the key is disabled after it was started");
assertTrue(started.getUsageStartTimestamp() > 0, "the start moment was not recorded");
assertEquals(0, started.getUsageStopTimestamp(),
"a key in use still carries the moment it was stopped");
}
@Test
void aDeletedKeyIsNotStartedOrStopped() {
PaymentCryptographyKey key = createKey("us-east-1");
service.deleteKey(key);
AwsException start = assertThrows(AwsException.class,
() -> service.startKeyUsage(key));
assertEquals("ResourceNotFoundException", start.getErrorCode());
AwsException stop = assertThrows(AwsException.class,
() -> service.stopKeyUsage(key));
assertEquals("ResourceNotFoundException", stop.getErrorCode());
}
@Test
void aStoppedKeyStopsItsReplicas() {
// The reference says a stop on the primary stops the usage of the replicas too, so
// the replica carries the primary's enablement and the moments it changed.
PaymentCryptographyKey key = createReplicatedKey(service, "us-east-1", List.of("us-west-2"));
PaymentCryptographyKey stopped = service.stopKeyUsage(key);
PaymentCryptographyKey replica = service.findKey("us-west-2", key.getKeyId());
assertFalse(replica.isEnabled(), "the replica is still enabled after the key was stopped");
assertEquals(stopped.getUsageStopTimestamp(), replica.getUsageStopTimestamp(),
"the replica does not carry the moment the key was stopped");
service.startKeyUsage(key);
replica = service.findKey("us-west-2", key.getKeyId());
assertTrue(replica.isEnabled(), "the replica is still disabled after the key was started");
assertEquals(0, replica.getUsageStopTimestamp(),
"a replica in use still carries the moment the key was stopped");
}
@Test
void aReplicaIsNotStartedOrStopped() {
// A replica is read-only, so the use of a key is started and stopped on the primary.
PaymentCryptographyKey key = createReplicatedKey(service, "us-east-1", List.of("us-west-2"));
PaymentCryptographyKey replica = service.findKey("us-west-2", key.getKeyId());
AwsException start = assertThrows(AwsException.class,
() -> service.startKeyUsage(replica));
assertEquals("ValidationException", start.getErrorCode());
AwsException stop = assertThrows(AwsException.class,
() -> service.stopKeyUsage(replica));
assertEquals("ValidationException", stop.getErrorCode());
}
@Test
void aKeyThatIsStoppedAgainKeepsTheMomentItStopped() {
// The guide says the moment the use stops is when the key became inactive, so a
// request that names a key that is already stopped changes nothing. The clock moves
// between the two requests, so a moment that followed the request would be told
// apart from the moment of the change.
ManualClock clock = new ManualClock(Instant.parse("2026-01-01T00:00:00Z"));
PaymentCryptographyService timed = serviceAt(clock);
PaymentCryptographyKey key = timed.createKey("us-east-1", dataKeyAttributes(),
dataKeyModes(), true, true, Map.of());
PaymentCryptographyKey stopped = timed.stopKeyUsage(key);
// The answer is the record of the key itself, so the moment is read before the
// next request changes it.
long moment = stopped.getUsageStopTimestamp();
clock.advance(Duration.ofHours(1));
PaymentCryptographyKey again = timed.stopKeyUsage(key);
assertFalse(again.isEnabled(), "the key is enabled after it was stopped again");
assertEquals(moment, again.getUsageStopTimestamp(),
"the moment the key stopped is the moment of the request, not of the change");
}
@Test
void aKeyThatIsStartedAgainKeepsTheMomentItBegan() {
// The guide says the moment the use starts is when the key became active, so a
// request that names a key that is already in use keeps the moment it became
// active. The clock moves between the two requests, so a moment that followed the
// request would be told apart from the moment of the change.
ManualClock clock = new ManualClock(Instant.parse("2026-01-01T00:00:00Z"));
PaymentCryptographyService timed = serviceAt(clock);
PaymentCryptographyKey key = timed.createKey("us-east-1", dataKeyAttributes(),
dataKeyModes(), true, true, Map.of());
long moment = key.getUsageStartTimestamp();
clock.advance(Duration.ofHours(1));
PaymentCryptographyKey again = timed.startKeyUsage(key);
assertTrue(again.isEnabled(), "the key is disabled after it was started again");
assertEquals(moment, again.getUsageStartTimestamp(),
"the moment the key began is the moment of the request, not of the change");
}
/** A service whose notion of time the test moves itself. */
private PaymentCryptographyService serviceAt(ManualClock clock) {
return new PaymentCryptographyService(
new InMemoryStorage<>(),
new InMemoryStorage<>(),
new RegionResolver("us-east-1", "000000000000"),
new SecureRandom(),
new PaymentCryptographyRequestContext(),
new CertificateGenerator(),
clock);
}
@Test
void theKeysAreListedAPageAtATime() {
createKey("us-east-1");