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

@@ -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. */