mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
2b3c22a3a5
commit
10445d89d8
9 files changed
+315
-5
No files matched your search
@@ -118,6 +118,20 @@ create_key() {
|
||||
assert_failure
|
||||
[[ "$output" == *"ValidationException"* ]]
|
||||
}
|
||||
@test "Payment Cryptography control plane: a key is stopped and started again" {
|
||||
KEY_ARN=$(create_key AES_256)
|
||||
[ -n "$KEY_ARN" ]
|
||||
|
||||
run aws_cmd payment-cryptography stop-key-usage --key-identifier "$KEY_ARN"
|
||||
assert_success
|
||||
[ "$(json_get "$output" '.Key.Enabled')" = "false" ]
|
||||
[ -n "$(json_get "$output" '.Key.UsageStopTimestamp')" ]
|
||||
|
||||
run aws_cmd payment-cryptography start-key-usage --key-identifier "$KEY_ARN"
|
||||
assert_success
|
||||
[ "$(json_get "$output" '.Key.Enabled')" = "true" ]
|
||||
[ -n "$(json_get "$output" '.Key.UsageStartTimestamp')" ]
|
||||
}
|
||||
|
||||
@test "Payment Cryptography control plane: a key pair is created with the usage its derived keys have" {
|
||||
# The usage a derived key is for is named when the key pair is made, as the export
|
||||
|
||||
+12
@@ -755,6 +755,18 @@ class PaymentCryptographyTest {
|
||||
.policy("{\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":\"*\"}]}")))
|
||||
.isInstanceOf(PublicPolicyException.class);
|
||||
}
|
||||
@Test
|
||||
void aKeyIsStoppedAndStartedAgain() {
|
||||
Key key = createAesKey();
|
||||
|
||||
Key stopped = client.stopKeyUsage(r -> r.keyIdentifier(key.keyArn())).key();
|
||||
assertThat(stopped.enabled()).isFalse();
|
||||
assertThat(stopped.usageStopTimestamp()).isNotNull();
|
||||
|
||||
Key started = client.startKeyUsage(r -> r.keyIdentifier(key.keyArn())).key();
|
||||
assertThat(started.enabled()).isTrue();
|
||||
assertThat(started.usageStartTimestamp()).isNotNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* A delete answers with the key it deleted, its state DELETE_PENDING and the
|
||||
|
||||
@@ -34,7 +34,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
|
||||
| [CloudHSM v2](cloudhsmv2.md) | `POST /` + `X-Amz-Target: BaldrApiService.*` | JSON 1.1 | 18 |
|
||||
| [Kinesis](kinesis.md) | `POST /` + `X-Amz-Target: Kinesis_20131202.*` | JSON 1.1 | 32 |
|
||||
| [Managed Service for Apache Flink](kinesisanalytics.md) | `POST /` + `X-Amz-Target: KinesisAnalytics_20180523.*` | JSON 1.1 | 15 |
|
||||
| [Payment Cryptography](payment-cryptography.md) | `POST /` + `X-Amz-Target: PaymentCryptographyControlPlane.*` | JSON 1.0 | 25 |
|
||||
| [Payment Cryptography](payment-cryptography.md) | `POST /` + `X-Amz-Target: PaymentCryptographyControlPlane.*` | JSON 1.0 | 27 |
|
||||
| [Payment Cryptography data plane](payment-cryptography-data.md) | `POST /keys/{KeyIdentifier}/encrypt` and the rest | REST JSON | 12 |
|
||||
| [Secrets Manager](secrets-manager.md) | `POST /` + `X-Amz-Target: secretsmanager.*` | JSON 1.1 | 23 |
|
||||
| [Step Functions](step-functions.md) | `POST /` + `X-Amz-Target: AmazonStatesService.*` | JSON 1.1 | 32 |
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
| `GetKey` | Get a key's attributes, state and check value |
|
||||
| `ListKeys` | List the keys of a region |
|
||||
| `DeleteKey` | Delete a key |
|
||||
| `StartKeyUsage` | Start the use of a key |
|
||||
| `StopKeyUsage` | Stop the use of a key |
|
||||
| `CreateAlias` | Point an alias name at a key |
|
||||
| `GetAlias` | Get the key an alias name points at |
|
||||
| `ListAliases` | List the aliases of a region |
|
||||
@@ -244,8 +246,16 @@
|
||||
primary key alone, so a replica takes none and a request that names one is refused. The
|
||||
access the policy grants is not enforced: the service's operations are not among the ones
|
||||
IAM checks.
|
||||
- **Not yet emulated:** the key usage operations, the key restore operation, the
|
||||
multi-party approval operations and the certificate signing request.
|
||||
- **Key usage:** `StopKeyUsage` disables the key and records when it stopped, and
|
||||
`StartKeyUsage` enables it again and records when it started. The moment recorded is the
|
||||
moment the key changed state, so a request that names a key that is already in the state
|
||||
it asks for changes nothing and keeps the moment it changed. A stopped key stays
|
||||
readable and its material stays where it is, so it can be started again. A replica is
|
||||
read-only, so the change is made on the primary, and a request that names a replica is
|
||||
refused. The change reaches the replicas: a stop on the primary stops the use of the
|
||||
replicas too, as the reference says.
|
||||
- **Not yet emulated:** the key restore operation, the multi-party approval operations
|
||||
and the certificate signing request.
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
+19
@@ -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());
|
||||
}
|
||||
|
||||
+59
-2
@@ -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");
|
||||
|
||||
+7
@@ -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) {
|
||||
|
||||
+61
@@ -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() {
|
||||
|
||||
+130
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user