From da8cc6fb1b3a431c0205cbceb314de513ed7ebc2 Mon Sep 17 00:00:00 2001
From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com>
Date: Tue, 17 Nov 2026 04:18:33 -0400
Subject: [PATCH] fix(paymentcryptography): give resource-tag conditions the
tags of the key a request names (#8716)
The generated request resources give Payment Cryptography's key type
the aws:ResourceTag/ key, but PaymentCryptographyService answered no
tags, so a condition on a key's tags saw none. Its storedTags now reads
the key stored under the ARN in the account and Region it names, in
place, a record saved before the Region joined its storage name
included, matched on the key's whole ARN. TagResource and UntagResource
edited the held map in place, which only ListTagsForResource's lock
kept from a half-written read; they now put a new map in place of it,
and the field is volatile, so the lookup reads one whole map without
the lock.
---
docs/services/iam.md | 4 +-
.../aws/core/common/StoredResourceArns.java | 5 ++-
.../PaymentCryptographyService.java | 45 ++++++++++++++++---
.../model/PaymentCryptographyKey.java | 2 +-
...estResourceEnforcementIntegrationTest.java | 26 +++++++++++
.../PaymentCryptographyServiceTest.java | 19 ++++++++
6 files changed, 90 insertions(+), 11 deletions(-)
diff --git a/docs/services/iam.md b/docs/services/iam.md
index ed44813582..0f3d599c0f 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -3200,8 +3200,8 @@ floci populates:
repositories, Redshift for its clusters, snapshots, parameter groups, subnet groups, snapshot copy grants,
snapshot schedules, event subscriptions and integrations, SSM for its parameters, documents and associations,
EMR for its clusters, CloudFront for every resource it tags, by its ARN, IAM Identity Center for its instances,
- permission sets, applications and trusted token issuers, OAM for its sinks and links, and RAM for its resource
- shares. Tag keys differing only in case are one condition key holding every value.
+ permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares,
+ and Payment Cryptography for its keys. Tag keys differing only in case are one condition key holding every value.
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
index 1c2d2dd5fa..c27d2683dc 100644
--- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
+++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
@@ -22,8 +22,9 @@ import java.util.Optional;
*
Implemented by the service that owns such resources or whose requests name them (EventBridge, for its
* connections and API destinations; Application Auto Scaling, for its scalable targets; Redshift Data, for the
* Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for
- * the key an alias names; EMR, for the clusters of its instance groups and a tag call's resource and the tags of a
- * cluster; IAM Identity Center, for the accounts its permission sets are provisioned to and the tags of an instance,
+ * the key an alias names and the tags of a key; EMR, for the clusters of its instance groups and a tag call's
+ * resource and the tags of a cluster; IAM Identity Center, for the accounts its permission sets are provisioned to
+ * and the tags of an instance,
* a permission set, an application or a trusted token issuer; Resource Explorer, for its default view; OAM and RAM,
* for the sinks, invitations and shares of other accounts they act on, OAM for the tags of a sink or a link, and
* RAM for the tags of a resource share;
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 9b7540f66f..54d33d53ff 100644
--- a/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java
+++ b/src/main/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyService.java
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.Resettable;
import io.floci.aws.core.common.StoredResourceArns;
+import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
@@ -516,7 +517,9 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
public synchronized void tag(PaymentCryptographyKey key, Map tags) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
- current.getTags().putAll(tags);
+ Map updated = new LinkedHashMap<>(current.getTags());
+ updated.putAll(tags);
+ current.setTags(updated);
storeKey(current);
refreshReplicas(current, current.getReplicationRegions());
}
@@ -564,9 +567,9 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
public synchronized void untag(PaymentCryptographyKey key, List tagKeys) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
- for (String tagKey : tagKeys) {
- current.getTags().remove(tagKey);
- }
+ Map updated = new LinkedHashMap<>(current.getTags());
+ tagKeys.forEach(updated::remove);
+ current.setTags(updated);
storeKey(current);
refreshReplicas(current, current.getReplicationRegions());
}
@@ -618,8 +621,8 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
/**
* The tags of a key.
*
- * The copy takes the lock the tagging operations take, so it cannot read the map while
- * another request is writing to it.
+ *
The tagging operations put a new map in place of the held one, so the copy is of one
+ * whole set; it still takes their lock, so it reads the set the last of them left.
*/
public synchronized Map listTags(PaymentCryptographyKey key) {
return new LinkedHashMap<>(key.getTags());
@@ -1106,6 +1109,36 @@ public class PaymentCryptographyService implements Resettable, StoredResourceArn
return regionResolver.buildArn(SERVICE, region, "alias/" + alias);
}
+ /**
+ * The tags held on the key stored under {@code arn}, in the account and Region it names, read in place without the
+ * service's lock: the tagging operations put a new map in place of the held one, so the read is of one whole set.
+ * A key saved before the Region became part of its storage name is found as {@link #findKeyRecord} finds it, and
+ * either is matched on its whole stored ARN. Empty for an ARN naming no key.
+ */
+ @Override
+ public Map storedTags(String arn) {
+ if (!AwsArnUtils.isArnFor(arn, SERVICE)) {
+ return Map.of();
+ }
+ AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
+ String account = parsed.accountId();
+ if (account.isEmpty() || !parsed.resource().startsWith("key/")) {
+ return Map.of();
+ }
+ String keyId = parsed.resource().substring("key/".length());
+ Map held = new LinkedHashMap<>();
+ AccountAwareStorageBackend.readInAccount(keyStore, account, storageKey(parsed.region(), keyId))
+ .or(() -> AccountAwareStorageBackend.readInAccount(keyStore, account, keyId))
+ .filter(key -> arn.equals(key.getArn()))
+ .map(PaymentCryptographyKey::getTags)
+ .ifPresent(tags -> tags.forEach((tagKey, value) -> {
+ if (tagKey != null && value != null) {
+ held.put(tagKey, value);
+ }
+ }));
+ return held;
+ }
+
/**
* The storage key of a key.
*
diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyKey.java b/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyKey.java
index 216ba040b9..04b748e768 100644
--- a/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyKey.java
+++ b/src/main/java/io/floci/aws/services/paymentcryptography/model/PaymentCryptographyKey.java
@@ -68,7 +68,7 @@ public class PaymentCryptographyKey {
private long usageStartTimestamp;
private long deletePendingTimestamp;
private long deleteTimestamp;
- private Map tags = new HashMap<>();
+ private volatile Map tags = new HashMap<>();
/** The region of the primary of a multi-region key, and its own region otherwise. */
private String primaryRegion;
/** The regions a multi-region key is in, besides the primary's. */
diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
index 5b085ce6b3..2c63616237 100644
--- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
+++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java
@@ -554,6 +554,32 @@ class IamRequestResourceEnforcementIntegrationTest {
refusedJson(getPaymentKey(user, alias), "payment-cryptography:GetKey");
}
+ /**
+ * An {@code aws:ResourceTag} condition reads the tags Payment Cryptography holds on the key a request names, so an
+ * allow on one team's tag lets the caller read that team's key and not another's.
+ */
+ @Test
+ void aPaymentCryptographyResourceTagConditionReadsTheTagsOfTheKey() {
+ String tagged = taggedPaymentKey("data");
+ String other = taggedPaymentKey("web");
+
+ User user = user("res-key-tags");
+ user.policy("{\"Effect\":\"Allow\",\"Action\":\"payment-cryptography:GetKey\",\"Resource\":\"*\","
+ + "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
+ getPaymentKey(user, tagged).statusCode(200);
+ refusedJson(getPaymentKey(user, other), "payment-cryptography:GetKey");
+ }
+
+ /** A Payment Cryptography key an administrator creates and tags with the team; its ARN. */
+ private String taggedPaymentKey(String team) {
+ String key = createPaymentKey();
+ admin("payment-cryptography").header("X-Amz-Target", "PaymentCryptographyControlPlane.TagResource")
+ .contentType("application/x-amz-json-1.0").body("{\"ResourceArn\":\"" + key
+ + "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}")
+ .post("/").then().statusCode(200);
+ return key;
+ }
+
/**
* A tagging request names its key by the ARN of one, as the reference takes it there, so a request naming it by
* its id names no key for the check and is refused: a deny on the key is not skipped by naming it another way.
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 8ee3c0c7db..c0ea51d83e 100644
--- a/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java
+++ b/src/test/java/io/floci/aws/services/paymentcryptography/PaymentCryptographyServiceTest.java
@@ -43,6 +43,25 @@ class PaymentCryptographyServiceTest {
new PaymentCryptographyRequestContext());
}
+ /**
+ * IAM's tag lookup reads the tags held on the key the whole ARN names, without the service's lock: tagging puts a
+ * new map in place of the held one, so a set read before a change stays whole; nothing for another Region's ARN.
+ */
+ @Test
+ void storedTagsAreTheTagsOfTheKeyTheArnNamesAndTaggingPutsANewMap() {
+ PaymentCryptographyKey key = createKey("us-east-1");
+ service.tag(key, Map.of("team", "data"));
+ Map held = service.storedTags(key.getArn());
+
+ service.tag(key, Map.of("env", "prod"));
+ service.untag(key, List.of("team"));
+
+ assertEquals(Map.of("team", "data"), held);
+ assertEquals(Map.of("env", "prod"), service.storedTags(key.getArn()));
+ assertEquals(Map.of(), service.storedTags(key.getArn().replace(":us-east-1:", ":eu-west-1:")));
+ assertEquals(Map.of(), service.storedTags("arn:aws:s3:::bucket"));
+ }
+
/** A key of the given algorithm and class, for the tests that need one. */
private PaymentCryptographyKey createKey(String region, String algorithm, String keyClass,
String usage, Map modes) {