From e6f4758bc11d847b56f7fa2b82d3fee620b99306 Mon Sep 17 00:00:00 2001 From: Murtuza Chhil Date: Wed, 18 Nov 2026 08:22:57 +0530 Subject: [PATCH] feat(paymentcryptography): the Multi-Party Approval team of an operation (#8812) * feat(paymentcryptography): the Multi-Party Approval team of an operation * docs(paymentcryptography): what the Multi-Party Approval does not make * docs(paymentcryptography): the action table in the order the handler names * style(paymentcryptography): the multi-region work comes with the branch * fix(paymentcryptography): the tests of the team share the operation they name * fix(paymentcryptography): the removal does not change what a lookup holds --- .../payment-cryptography-control-plane.bats | 23 +++ .../floci/test/PaymentCryptographyTest.java | 24 +++ docs/services/index.md | 2 +- docs/services/payment-cryptography.md | 28 ++- .../PaymentCryptographyJsonHandler.java | 45 +++++ .../PaymentCryptographyService.java | 96 +++++++++- ...PaymentCryptographyMpaTeamAssociation.java | 46 +++++ .../PaymentCryptographyIntegrationTest.java | 28 +++ .../PaymentCryptographyServiceTest.java | 166 +++++++++++++++--- 9 files changed, 424 insertions(+), 34 deletions(-) create mode 100644 src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyMpaTeamAssociation.java diff --git a/compatibility-tests/sdk-test-awscli/test/payment-cryptography-control-plane.bats b/compatibility-tests/sdk-test-awscli/test/payment-cryptography-control-plane.bats index 1426e1a112..6e2e17d245 100644 --- a/compatibility-tests/sdk-test-awscli/test/payment-cryptography-control-plane.bats +++ b/compatibility-tests/sdk-test-awscli/test/payment-cryptography-control-plane.bats @@ -168,3 +168,26 @@ create_key() { assert_success [ "$(json_get "$output" '.Key.DeriveKeyUsage')" = "TR31_K1_KEY_BLOCK_PROTECTION_KEY" ] } + +@test "Payment Cryptography control plane: a Multi-Party Approval team is associated and removed" { + # The team's ARN is kept as the request names it: the emulator has no Multi-Party + # Approval service to check it against. + local team="arn:aws:mpa:us-east-1:000000000000:approval-team/team-alpha" + + run aws_cmd payment-cryptography associate-mpa-team \ + --action IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE \ + --mpa-team-arn "$team" + assert_success + [ "$(json_get "$output" '.MpaTeamAssociation.MpaTeamArn')" = "$team" ] + [ "$(json_get "$output" '.MpaTeamAssociation.AssociationState')" = "ACTIVE" ] + + run aws_cmd payment-cryptography get-mpa-team-association \ + --action IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE + assert_success + [ "$(json_get "$output" '.MpaTeamAssociation.MpaTeamArn')" = "$team" ] + + run aws_cmd payment-cryptography disassociate-mpa-team \ + --action IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE + assert_success + [ "$(json_get "$output" '.MpaTeamAssociation.AssociationState')" = "DELETE_PENDING" ] +} diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyTest.java index 99ab1f7e7a..963624c00a 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/PaymentCryptographyTest.java @@ -15,6 +15,7 @@ import software.amazon.awssdk.services.paymentcryptography.PaymentCryptographyCl import software.amazon.awssdk.services.paymentcryptography.model.AddKeyReplicationRegionsResponse; import software.amazon.awssdk.services.paymentcryptography.model.Alias; import software.amazon.awssdk.services.paymentcryptography.model.As2805KeyVariant; +import software.amazon.awssdk.services.paymentcryptography.model.AssociationState; import software.amazon.awssdk.services.paymentcryptography.model.CreateKeyRequest; import software.amazon.awssdk.services.paymentcryptography.model.DeriveKeyUsage; import software.amazon.awssdk.services.paymentcryptography.model.EnableDefaultKeyReplicationRegionsResponse; @@ -34,6 +35,8 @@ import software.amazon.awssdk.services.paymentcryptography.model.KeyReplicationS import software.amazon.awssdk.services.paymentcryptography.model.KeyState; import software.amazon.awssdk.services.paymentcryptography.model.KeyUsage; import software.amazon.awssdk.services.paymentcryptography.model.ListKeysResponse; +import software.amazon.awssdk.services.paymentcryptography.model.MpaOperation; +import software.amazon.awssdk.services.paymentcryptography.model.MpaTeamAssociation; import software.amazon.awssdk.services.paymentcryptography.model.MultiRegionKeyType; import software.amazon.awssdk.services.paymentcryptography.model.PublicPolicyException; import software.amazon.awssdk.services.paymentcryptography.model.RemoveKeyReplicationRegionsResponse; @@ -421,6 +424,27 @@ class PaymentCryptographyTest { return arn.substring(arn.lastIndexOf('/') + 1); } + @Test + void aMultiPartyApprovalTeamIsAssociatedReadAndRemoved() { + // The team's ARN is kept as the request names it: the emulator has no Multi-Party + // Approval service to check it against. + String team = "arn:aws:mpa:us-east-1:000000000000:approval-team/team-alpha"; + + MpaTeamAssociation association = client.associateMpaTeam(r -> r + .action(MpaOperation.IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE) + .mpaTeamArn(team)).mpaTeamAssociation(); + assertThat(association.mpaTeamArn()).isEqualTo(team); + assertThat(association.associationState()).isEqualTo(AssociationState.ACTIVE); + + assertThat(client.getMpaTeamAssociation(r -> r + .action(MpaOperation.IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE)) + .mpaTeamAssociation().mpaTeamArn()).isEqualTo(team); + + assertThat(client.disassociateMpaTeam(r -> r + .action(MpaOperation.IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE)) + .mpaTeamAssociation().associationState()).isEqualTo(AssociationState.DELETE_PENDING); + } + @Test void aKeyIsCreatedAndReadBackWithItsModesOfUse() { Key created = createAesKey(); diff --git a/docs/services/index.md b/docs/services/index.md index db3a3e1280..2812cf6c72 100644 --- a/docs/services/index.md +++ b/docs/services/index.md @@ -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 | 28 | +| [Payment Cryptography](payment-cryptography.md) | `POST /` + `X-Amz-Target: PaymentCryptographyControlPlane.*` | JSON 1.0 | 31 | | [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 | diff --git a/docs/services/payment-cryptography.md b/docs/services/payment-cryptography.md index 29eccbfb87..c98e6c236f 100644 --- a/docs/services/payment-cryptography.md +++ b/docs/services/payment-cryptography.md @@ -36,6 +36,9 @@ | `PutResourcePolicy` | Attach or replace the resource-based policy of a key | | `GetResourcePolicy` | Get the resource-based policy of a key | | `DeleteResourcePolicy` | Remove the resource-based policy of a key | +| `AssociateMpaTeam` | Associate a Multi-Party Approval team with a protected operation | +| `GetMpaTeamAssociation` | The Multi-Party Approval team of a protected operation | +| `DisassociateMpaTeam` | Remove the Multi-Party Approval team of a protected operation | ## Emulation Behavior @@ -87,9 +90,25 @@ summary the model names: its identifier, its state, its attributes, its check value, its two flags and, for a key that is replicated, the kind of key it is and the region it is the primary of. -- **The members a request may name that the service does not read:** `ImportKey` takes - `RequesterComment`, and the service reads none of it. No approval is recorded, because - the multi-party approval is not emulated. +- **The members a request may name that the service does not read:** `ImportKey`, + `AssociateMpaTeam` and `DisassociateMpaTeam` take `RequesterComment`, and the service + reads none of it. +- **The Multi-Party Approval team of a protected operation:** a protected operation has one + team, so a second association replaces the first. The association answers with the + operation, the team and the state of the association, and removing it answers with the + association as it was, its state now `DELETE_PENDING`, and the association is gone from + the service. An operation with no team is refused as the resource the reference cannot + find. The team's ARN is kept as the request names it: the emulator has no Multi-Party + Approval service to check it against. The requester's comment is read by nobody, so no + approval session is recorded. +- **The approval session itself is not made:** the reference holds the import of a root + certificate pending until the team approves it, and answers with a key whose state is + `CREATE_IN_PROGRESS` and whose `MpaStatus` carries the session. The emulator has no + Multi-Party Approval service to ask, so the import is not held pending and the key is + imported at once. The protected operation is the one the reference names, the import of + a root public key certificate. The disassociation is not held either: the reference holds + it pending the approval of the team that is associated, and the emulator answers it with + the state `DELETE_PENDING` and takes the association away at once. - **An alias may name no key:** `UpdateAlias` names the key of an alias, and an update that names none removes the key the alias names and leaves the alias. `CreateAlias` refuses an alias without a key, as the reference does, though its model requires @@ -269,8 +288,7 @@ to start or stop: the reference does not publish the answer for that case, so the request is refused with `ConflictException`, which is the code the service uses for a request that conflicts with the state of a key. -- **Not yet emulated:** the multi-party approval operations and the certificate signing - request. +- **Not yet emulated:** the certificate signing request. ## Configuration diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyJsonHandler.java b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyJsonHandler.java index 8b9ea19560..a46a71f53b 100644 --- a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyJsonHandler.java +++ b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyJsonHandler.java @@ -10,6 +10,7 @@ import io.floci.aws.core.common.Pagination; import io.floci.aws.services.iam.PolicyDocument; import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyAlias; import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey; +import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyMpaTeamAssociation; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; @@ -89,6 +90,9 @@ public class PaymentCryptographyJsonHandler { case "PutResourcePolicy" -> () -> putResourcePolicy(request, region); case "GetResourcePolicy" -> () -> getResourcePolicy(request, region); case "DeleteResourcePolicy" -> () -> deleteResourcePolicy(request, region); + case "AssociateMpaTeam" -> () -> associateMpaTeam(request, region); + case "GetMpaTeamAssociation" -> () -> getMpaTeamAssociation(request, region); + case "DisassociateMpaTeam" -> () -> disassociateMpaTeam(request, region); default -> throw new AwsException("UnknownOperationException", "Operation " + action + " is not supported by floci", 400); }; @@ -483,6 +487,47 @@ public class PaymentCryptographyJsonHandler { return true; } + // ---- multi-party approval ------------------------------------------------ + + /** + * Associates a Multi-Party Approval team with a protected operation. + * + *

An operation has one team, so a second association replaces the first, as the + * reference replaces one. + */ + private Map associateMpaTeam(JsonNode request, String region) { + PaymentCryptographyMpaTeamAssociation association = service.associateMpaTeam(region, + request.path("Action").asText(), request.path("MpaTeamArn").asText()); + return Map.of("MpaTeamAssociation", mpaAssociation(association)); + } + + /** The Multi-Party Approval team associated with a protected operation. */ + private Map getMpaTeamAssociation(JsonNode request, String region) { + PaymentCryptographyMpaTeamAssociation association = + service.mpaTeamAssociation(region, request.path("Action").asText()); + return Map.of("MpaTeamAssociation", mpaAssociation(association)); + } + + /** + * Removes the Multi-Party Approval team associated with a protected operation. + * + *

The answer carries the association as it was, its state now {@code DELETE_PENDING}. + */ + private Map disassociateMpaTeam(JsonNode request, String region) { + PaymentCryptographyMpaTeamAssociation association = + service.disassociateMpaTeam(region, request.path("Action").asText()); + return Map.of("MpaTeamAssociation", mpaAssociation(association)); + } + + /** The association, as the model names its members. */ + private static Map mpaAssociation(PaymentCryptographyMpaTeamAssociation association) { + Map out = new LinkedHashMap<>(); + out.put("Action", association.getAction()); + out.put("MpaTeamArn", association.getMpaTeamArn()); + out.put("AssociationState", association.getAssociationState()); + return out; + } + /** The tags of a request, as the name and the value of each. */ private static Map tagsOf(JsonNode request) { Map tags = new LinkedHashMap<>(); diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java index 8693a61514..287751381a 100644 --- a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java +++ b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java @@ -16,6 +16,7 @@ import io.floci.aws.services.acm.CertificateGenerator; import io.floci.aws.services.paymentcryptography.crypto.DukptCrypto; import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyAlias; import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyKey; +import io.floci.aws.services.paymentcryptography.model.PaymentCryptographyMpaTeamAssociation; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -84,6 +85,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn private final StorageBackend keyStore; private final StorageBackend aliasStore; + private final StorageBackend mpaTeamStore; /** The key exchange: the forms a key may leave in, and the certificates that carry them. */ private final PaymentCryptographyKeyExchange exchange; @@ -104,6 +106,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn new TypeReference<>() {}), storageFactory.create(STORAGE_KEY, "payment-cryptography-settings.json", new TypeReference>>() {}), + storageFactory.create(STORAGE_KEY, "payment-cryptography-mpa-teams.json", + new TypeReference<>() {}), regionResolver, new SecureRandom(), requestContext, @@ -115,8 +119,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn StorageBackend aliasStore, RegionResolver regionResolver, PaymentCryptographyRequestContext requestContext) { - this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, new SecureRandom(), - requestContext, new CertificateGenerator(), Clock.systemUTC()); + this(keyStore, aliasStore, new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, + new SecureRandom(), requestContext, new CertificateGenerator(), Clock.systemUTC()); } /** @@ -128,8 +132,9 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn PaymentCryptographyService(StorageBackend keyStore, StorageBackend aliasStore, RegionResolver regionResolver) { - this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, new SecureRandom(), - new PaymentCryptographyRequestContext(), new CertificateGenerator(), Clock.systemUTC()); + this(keyStore, aliasStore, new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, + new SecureRandom(), new PaymentCryptographyRequestContext(), + new CertificateGenerator(), Clock.systemUTC()); } PaymentCryptographyService(StorageBackend keyStore, @@ -137,8 +142,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn RegionResolver regionResolver, SecureRandom secureRandom, PaymentCryptographyRequestContext requestContext) { - this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, secureRandom, - requestContext, new CertificateGenerator(), Clock.systemUTC()); + this(keyStore, aliasStore, new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, + secureRandom, requestContext, new CertificateGenerator(), Clock.systemUTC()); } PaymentCryptographyService(StorageBackend keyStore, @@ -148,13 +153,14 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn PaymentCryptographyRequestContext requestContext, CertificateGenerator certificates, Clock clock) { - this(keyStore, aliasStore, new InMemoryStorage<>(), regionResolver, secureRandom, - requestContext, certificates, clock); + this(keyStore, aliasStore, new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, + secureRandom, requestContext, certificates, clock); } PaymentCryptographyService(StorageBackend keyStore, StorageBackend aliasStore, StorageBackend> settingsStore, + StorageBackend mpaTeamStore, RegionResolver regionResolver, SecureRandom secureRandom, PaymentCryptographyRequestContext requestContext, @@ -163,6 +169,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn this.keyStore = keyStore; this.aliasStore = aliasStore; this.settingsStore = settingsStore; + this.mpaTeamStore = mpaTeamStore; this.regionResolver = regionResolver; this.exchange = new PaymentCryptographyKeyExchange(this, certificates, clock); this.secureRandom = secureRandom; @@ -837,6 +844,78 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn return current; } + // ---- multi-party approval ------------------------------------------------ + + /** + * Associates a Multi-Party Approval team with a protected operation. + * + *

An operation has one association, so a second one replaces the first. The team's + * ARN is stored as the request names it: there is no MPA service here to check it + * against, and an ARN the model accepts names a team the emulator cannot see. + * + *

The store holds the association, and the caller gets a copy of it, so what the + * caller holds is a snapshot that a later change of the stored association does not + * reach. + */ + public synchronized PaymentCryptographyMpaTeamAssociation associateMpaTeam( + String region, String action, String mpaTeamArn) { + PaymentCryptographyMpaTeamAssociation association = + new PaymentCryptographyMpaTeamAssociation(action, region, mpaTeamArn, "ACTIVE"); + mpaTeamStore.put(storageKey(region, action), association); + return copyOf(association); + } + + /** + * The MPA team associated with a protected operation. + * + *

The store holds the association and the object it holds is mutable, so the read + * takes a copy of it while it holds the lock: what the caller holds is a snapshot, and + * a removal that runs after the read does not change it. + */ + public synchronized PaymentCryptographyMpaTeamAssociation mpaTeamAssociation( + String region, String action) { + PaymentCryptographyMpaTeamAssociation association = + mpaTeamStore.get(storageKey(region, action)).orElse(null); + if (association == null) { + throw new AwsException("ResourceNotFoundException", + "No MPA team is associated with " + action, 400); + } + return copyOf(association); + } + + /** + * Removes the association between an MPA team and a protected operation. + * + *

The answer carries the association as it was, its state now {@code DELETE_PENDING}, + * and the association is gone from the store. The state belongs to that answer alone: + * the stored association is not changed, so a lookup that overlaps the removal answers + * with the association that is there or with the resource the reference cannot find. + */ + public synchronized PaymentCryptographyMpaTeamAssociation disassociateMpaTeam( + String region, String action) { + PaymentCryptographyMpaTeamAssociation association = mpaTeamAssociation(region, action); + mpaTeamStore.delete(storageKey(region, action)); + return new PaymentCryptographyMpaTeamAssociation(association.getAction(), + association.getRegion(), association.getMpaTeamArn(), "DELETE_PENDING"); + } + + /** + * A copy of an association. + * + *

The store holds the association and the object it holds is mutable, so what a + * caller gets is a copy: what the caller holds is a snapshot, and a change of the + * stored association does not reach it. + * + * @param association the association to copy + * @return the copy + */ + private static PaymentCryptographyMpaTeamAssociation copyOf( + PaymentCryptographyMpaTeamAssociation association) { + return new PaymentCryptographyMpaTeamAssociation(association.getAction(), + association.getRegion(), association.getMpaTeamArn(), + association.getAssociationState()); + } + // ---- the key exchange ---------------------------------------------------- /** @@ -1416,6 +1495,7 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn keyStore.clear(); settingsStore.clear(); aliasStore.clear(); + mpaTeamStore.clear(); } /** The key store, for the tests. */ diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyMpaTeamAssociation.java b/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyMpaTeamAssociation.java new file mode 100644 index 0000000000..c74ed9deb8 --- /dev/null +++ b/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyMpaTeamAssociation.java @@ -0,0 +1,46 @@ +package io.floci.aws.services.paymentcryptography.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.quarkus.runtime.annotations.RegisterForReflection; + +/** + * A Multi-Party Approval team associated with a protected operation. + * + *

The association is the account's, one per protected operation: a second association + * of the same operation replaces the first. The emulator keeps the team's ARN but has no + * MPA service to check it against, so an ARN the model accepts is stored as it came. + */ +@RegisterForReflection +@JsonIgnoreProperties(ignoreUnknown = true) +public class PaymentCryptographyMpaTeamAssociation { + + private String action; + private String region; + private String mpaTeamArn; + private String associationState = "ACTIVE"; + + public PaymentCryptographyMpaTeamAssociation() { + } + + public PaymentCryptographyMpaTeamAssociation(String action, String region, String mpaTeamArn, + String associationState) { + this.action = action; + this.region = region; + this.mpaTeamArn = mpaTeamArn; + this.associationState = associationState; + } + + public String getAction() { return action; } + public void setAction(String action) { this.action = action; } + + public String getRegion() { return region; } + public void setRegion(String region) { this.region = region; } + + public String getMpaTeamArn() { return mpaTeamArn; } + public void setMpaTeamArn(String mpaTeamArn) { this.mpaTeamArn = mpaTeamArn; } + + public String getAssociationState() { return associationState; } + public void setAssociationState(String associationState) { + this.associationState = associationState; + } +} diff --git a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyIntegrationTest.java b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyIntegrationTest.java index d50da6e7e6..91a526f169 100644 --- a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyIntegrationTest.java @@ -611,6 +611,34 @@ class PaymentCryptographyIntegrationTest { .body("message", containsString("ReplicationRegions")); } + @Test + void aMultiPartyApprovalTeamIsAssociatedReadAndRemoved() { + // The team's ARN is kept as the request names it: the emulator has no Multi-Party + // Approval service to check it against. + String team = "arn:aws:mpa:us-east-1:000000000000:approval-team/team-alpha"; + String association = """ + {"Action":"IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE"}"""; + + call("AssociateMpaTeam", """ + {"Action":"IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE","MpaTeamArn":"%s"}""".formatted(team)) + .then().statusCode(200) + .body("MpaTeamAssociation.Action", equalTo("IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE")) + .body("MpaTeamAssociation.MpaTeamArn", equalTo(team)) + .body("MpaTeamAssociation.AssociationState", equalTo("ACTIVE")); + + call("GetMpaTeamAssociation", association) + .then().statusCode(200) + .body("MpaTeamAssociation.MpaTeamArn", equalTo(team)); + + call("DisassociateMpaTeam", association) + .then().statusCode(200) + .body("MpaTeamAssociation.AssociationState", equalTo("DELETE_PENDING")); + + call("GetMpaTeamAssociation", association) + .then().statusCode(400) + .body("__type", containsString("ResourceNotFoundException")); + } + @Test void aReplicationRegionOfAnotherPartitionIsRefused() { // A key is replicated within the partition it is in, as the reference says, so a diff --git a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java index 61c01e9e84..610c274f6e 100644 --- a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java +++ b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java @@ -7,6 +7,7 @@ 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.services.paymentcryptography.model.PaymentCryptographyMpaTeamAssociation; import io.floci.aws.testing.ManualClock; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -23,6 +24,12 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -1102,6 +1109,32 @@ class PaymentCryptographyServiceTest { assertEquals("ValidationException", refused.getErrorCode()); } + @Test + void anImportedKeyIsReplicatedToTheRegionsTheRequestNames() { + // The model gives an import the same replication regions a creation does, so a key + // that comes in can be replicated as soon as it lands. + PaymentCryptographyKey protection = createKey("us-east-1", "AES_256", "SYMMETRIC_KEY", + "TR31_K1_KEY_BLOCK_PROTECTION_KEY", + Map.of("Wrap", Boolean.TRUE, "Unwrap", Boolean.TRUE)); + PaymentCryptographyKey key = createKey("us-east-1", "AES_256", "SYMMETRIC_KEY", + "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY", + Map.of("Encrypt", Boolean.TRUE, "Decrypt", Boolean.TRUE)); + String block = exportTr31("us-east-1", key.getKeyId(), protection.getKeyId()); + + PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr31KeyBlock", Map.of( + "WrappingKeyIdentifier", protection.getKeyId(), + "WrappedKeyBlock", block), "", true, Map.of(), List.of("us-west-2")); + + assertEquals("PRIMARY", imported.getMultiRegionKeyType()); + assertEquals("us-east-1", imported.getPrimaryRegion()); + assertEquals(List.of("us-west-2"), imported.getReplicationRegions()); + + PaymentCryptographyKey replica = service.findKey("us-west-2", imported.getKeyId()); + assertNotNull(replica, "the key was not replicated"); + assertEquals("REPLICA", replica.getMultiRegionKeyType()); + assertEquals(imported.getKeyMaterialHex(), replica.getKeyMaterialHex()); + } + @Test void aKeyIsCreatedInEveryRegionItIsReplicatedTo() { PaymentCryptographyKey primary = createReplicatedKey(service, "us-east-1", List.of("us-west-2")); @@ -1652,30 +1685,101 @@ class PaymentCryptographyServiceTest { "WrappedKeyBlock", block), "", true, Map.of()); } + /** The ARN of a Multi-Party Approval team, for the tests that need one. */ + private static final String MPA_TEAM = + "arn:aws:mpa:us-east-1:000000000000:approval-team/team-alpha"; + + /** The ARN of another team of the same account, for the test that replaces one. */ + private static final String MPA_TEAM_OTHER = + "arn:aws:mpa:us-east-1:000000000000:approval-team/team-beta"; + + /** The operation the reference protects, and the model names. */ + private static final String MPA_OPERATION = "IMPORT_ROOT_PUBLIC_KEY_CERTIFICATE"; + @Test - void anImportedKeyIsReplicatedToTheRegionsTheRequestNames() { - // The model gives an import the same replication regions a creation does, so a key - // that comes in can be replicated as soon as it lands. - PaymentCryptographyKey protection = createKey("us-east-1", "AES_256", "SYMMETRIC_KEY", - "TR31_K1_KEY_BLOCK_PROTECTION_KEY", - Map.of("Wrap", Boolean.TRUE, "Unwrap", Boolean.TRUE)); - PaymentCryptographyKey key = createKey("us-east-1", "AES_256", "SYMMETRIC_KEY", - "TR31_D0_SYMMETRIC_DATA_ENCRYPTION_KEY", - Map.of("Encrypt", Boolean.TRUE, "Decrypt", Boolean.TRUE)); - String block = exportTr31("us-east-1", key.getKeyId(), protection.getKeyId()); + void aMultiPartyApprovalTeamIsAssociatedReadAndRemoved() { + // The reference associates a team with the import of a root public key + // certificate, and the model names that operation. + PaymentCryptographyMpaTeamAssociation association = + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM); - PaymentCryptographyKey imported = service.importKey("us-east-1", "Tr31KeyBlock", Map.of( - "WrappingKeyIdentifier", protection.getKeyId(), - "WrappedKeyBlock", block), "", true, Map.of(), List.of("us-west-2")); + assertEquals(MPA_OPERATION, association.getAction()); + assertEquals(MPA_TEAM, association.getMpaTeamArn()); + assertEquals("ACTIVE", association.getAssociationState()); - assertEquals("PRIMARY", imported.getMultiRegionKeyType()); - assertEquals("us-east-1", imported.getPrimaryRegion()); - assertEquals(List.of("us-west-2"), imported.getReplicationRegions()); + PaymentCryptographyMpaTeamAssociation read = + service.mpaTeamAssociation("us-east-1", MPA_OPERATION); + assertEquals(MPA_TEAM, read.getMpaTeamArn()); - PaymentCryptographyKey replica = service.findKey("us-west-2", imported.getKeyId()); - assertNotNull(replica, "the key was not replicated"); - assertEquals("REPLICA", replica.getMultiRegionKeyType()); - assertEquals(imported.getKeyMaterialHex(), replica.getKeyMaterialHex()); + PaymentCryptographyMpaTeamAssociation removed = + service.disassociateMpaTeam("us-east-1", MPA_OPERATION); + assertEquals("DELETE_PENDING", removed.getAssociationState()); + + AwsException refused = assertThrows(AwsException.class, + () -> service.mpaTeamAssociation("us-east-1", MPA_OPERATION)); + assertEquals("ResourceNotFoundException", refused.getErrorCode()); + } + + @Test + void theRemovalDoesNotChangeTheAssociationALookupHolds() { + // The state DELETE_PENDING belongs to the answer of the removal alone: the store + // holds the association, and what a lookup holds is a snapshot of it, so the + // removal does not reach what an earlier lookup was given. + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM); + + PaymentCryptographyMpaTeamAssociation read = + service.mpaTeamAssociation("us-east-1", MPA_OPERATION); + service.disassociateMpaTeam("us-east-1", MPA_OPERATION); + + assertEquals("ACTIVE", read.getAssociationState(), + "the removal changed the association a lookup holds"); + } + + @Test + void aLookupThatOverlapsTheRemovalNeverSeesTheRemovalPending() throws Exception { + // A lookup that overlaps the removal answers with the association that is there or + // with the resource the reference cannot find, and never with the state the removal + // answers with. + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + for (int round = 0; round < 200; round++) { + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM); + CountDownLatch start = new CountDownLatch(1); + List seen = new CopyOnWriteArrayList<>(); + + Future removal = pool.submit(() -> { + awaitQuietly(start); + service.disassociateMpaTeam("us-east-1", MPA_OPERATION); + }); + Future lookup = pool.submit(() -> { + awaitQuietly(start); + try { + seen.add(service.mpaTeamAssociation("us-east-1", MPA_OPERATION) + .getAssociationState()); + } catch (AwsException refused) { + seen.add(refused.getErrorCode()); + } + }); + + start.countDown(); + removal.get(10, TimeUnit.SECONDS); + lookup.get(10, TimeUnit.SECONDS); + + assertFalse(seen.contains("DELETE_PENDING"), + "a lookup saw the removal pending: " + seen); + } + } finally { + pool.shutdownNow(); + } + } + + /** Waits for the latch, and lets an interruption end the wait. */ + private static void awaitQuietly(CountDownLatch latch) { + try { + latch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } @Test @@ -1695,6 +1799,28 @@ class PaymentCryptographyServiceTest { assertNull(after.getTags().get("owner"), "the tag was not taken away"); } + @Test + void aSecondTeamReplacesTheFirst() { + // A protected operation has one team, so a second association replaces the first. + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM); + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM_OTHER); + + PaymentCryptographyMpaTeamAssociation read = + service.mpaTeamAssociation("us-east-1", MPA_OPERATION); + assertEquals(MPA_TEAM_OTHER, read.getMpaTeamArn()); + } + + @Test + void aTeamIsTheOneOfItsOwnRegion() { + // The association belongs to the account and the Region, so the team of one Region + // is not the team of another. + service.associateMpaTeam("us-east-1", MPA_OPERATION, MPA_TEAM); + + AwsException refused = assertThrows(AwsException.class, + () -> service.mpaTeamAssociation("us-west-2", MPA_OPERATION)); + assertEquals("ResourceNotFoundException", refused.getErrorCode()); + } + /** The attributes of a key with that algorithm and usage. */ private static Map attributes(String algorithm, String usage) { return attributes(algorithm, "SYMMETRIC_KEY", usage);