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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 04:18:33 -04:00
1 parent e078bb7b00
commit da8cc6fb1b
6 files changed
+90 -11

No files matched your search

+2 -2
View File
@@ -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
@@ -22,8 +22,9 @@ import java.util.Optional;
* <p>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;
@@ -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<String, String> tags) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
current.getTags().putAll(tags);
Map<String, String> 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<String> tagKeys) {
PaymentCryptographyKey current = liveKey(key);
requireNotReplica(current, "a replica is read-only");
for (String tagKey : tagKeys) {
current.getTags().remove(tagKey);
}
Map<String, String> 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.
*
* <p>The copy takes the lock the tagging operations take, so it cannot read the map while
* another request is writing to it.
* <p>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<String, String> 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<String, String> 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<String, String> 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.
*
@@ -68,7 +68,7 @@ public class PaymentCryptographyKey {
private long usageStartTimestamp;
private long deletePendingTimestamp;
private long deleteTimestamp;
private Map<String, String> tags = new HashMap<>();
private volatile Map<String, String> 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. */
@@ -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.
@@ -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<String, String> 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<String, Boolean> modes) {