fix(redshift): give resource-tag conditions the tags of the resource a request names (#8685)

The generated request resources give fifteen Redshift types the
aws:ResourceTag/ key, but RedshiftStoredResourceArns answered only a
snapshot's key, so a condition on a Redshift resource's tags saw none.
It now answers from RedshiftService.storedTags, which reads the
cluster, snapshot, parameter group, subnet group, snapshot copy grant,
snapshot schedule, event subscription or integration stored under the
ARN in the account and Region it names, in place, matched on the whole
ARN; a snapshot is found as tagging finds it, the copy kept in the
Region before the snapshot itself. CreateTags and DeleteTags already
put a new map in place of the held one under the service's lock; the
tag fields are now volatile, so the lookup reads one whole map.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-16 20:33:00 -04:00
1 parent bab6428f78
commit d606843c49
15 files changed
+203 -13

No files matched your search

+4 -2
View File
@@ -3157,8 +3157,10 @@ floci populates:
crawlers, triggers, workflows, connections, databases, tables and other resources it tags, Step Functions for its
state machines, their versions and executions (the state machine's tags) and activities, CodeBuild for its
projects and report groups, Athena for its workgroups and data catalogs, Redshift Serverless for its namespaces,
workgroups and snapshots, DynamoDB for its tables and their indexes (the table's tags), and ECR for its
repositories. Tag keys differing only in case are one condition key holding every value.
workgroups and snapshots, DynamoDB for its tables and their indexes (the table's tags), ECR for its
repositories, and Redshift for its clusters, snapshots, parameter groups, subnet groups, snapshot copy grants,
snapshot schedules, event subscriptions and integrations. 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
@@ -34,9 +34,11 @@ import java.util.Optional;
* versions and executions, and an activity; CodeBuild, for the project a build belongs to and the tags of a project or
* a report group; Athena, for the workgroup and the data catalog a request naming none uses and the tags of a
* workgroup or a data catalog; Redshift Serverless, for the tags of a namespace, a workgroup or a snapshot; DynamoDB,
* for the tags of a table and its indexes; ECR, for the tags of a repository); looked up lazily by
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
* for the tags of a table and its indexes; ECR, for the tags of a repository; Redshift, for the key a snapshot records
* and the tags of a cluster, a snapshot, a parameter group, a subnet group, a snapshot copy grant, a snapshot
* schedule, an event subscription or an integration); looked up lazily by {@code ResourceArnBuilder} and
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
* ARN is of, so IAM never depends on that service directly.</p>
*
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
@@ -424,6 +424,12 @@ public class RedshiftEventService {
&& (values == null || values.isEmpty() || values.contains(tag.getValue())));
}
/** The tags of the subscription {@code name} in an account's Region, read in place; null for none. */
public Map<String, String> storedTags(String accountId, String region, String name) {
return AccountAwareStorageBackend.readInAccount(subscriptions, accountId, key(region, name))
.map(EventSubscription::getTags).orElse(null);
}
private EventSubscription requireSubscription(String name) {
if (blankToNull(name) == null) {
throw new AwsException("InvalidParameterValue", "SubscriptionName is required", 400);
@@ -4073,6 +4073,73 @@ public class RedshiftService implements ManagedSecretRotator {
return Map.copyOf(resolveTagHandle(resourceName).tags());
}
/**
* The tags held on the cluster, snapshot, parameter group, subnet group, snapshot copy grant, snapshot schedule,
* event subscription or integration stored under {@code arn}, in the account and Region it names, read in place
* and matched on the whole ARN; empty for an ARN naming nothing stored.
*/
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "redshift")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String resource = parsed.resource();
int sep = resource.indexOf(':');
if (sep < 0 || !arn.equals(AwsArnUtils.Arn.of("redshift", parsed.region(), parsed.accountId(), resource)
.toString())) {
return Map.of();
}
String account = parsed.accountId();
String region = parsed.region();
String id = resource.substring(sep + 1);
Map<String, String> tags = switch (resource.substring(0, sep)) {
case "cluster" -> AccountAwareStorageBackend.readInAccount(clusters, account, id)
.filter(cluster -> clusterRegion(cluster).equals(region)).map(Cluster::getTags).orElse(null);
case "snapshot" -> storedSnapshot(arn, account, region, id).map(Snapshot::getTags).orElse(null);
case "parametergroup" -> AccountAwareStorageBackend.readInAccount(parameterGroups, account,
regionalKey(region, id)).map(ClusterParameterGroup::getTags).orElse(null);
case "subnetgroup" -> AccountAwareStorageBackend.readInAccount(subnetGroups, account,
regionalKey(region, id)).map(ClusterSubnetGroup::getTags).orElse(null);
case "snapshotcopygrant" -> AccountAwareStorageBackend.readInAccount(snapshotCopyGrants, account,
regionalKey(region, id)).map(SnapshotCopyGrant::getTags).orElse(null);
case "snapshotschedule" -> AccountAwareStorageBackend.readInAccount(snapshotSchedules, account,
regionalKey(region, id)).map(SnapshotSchedule::getTags).orElse(null);
case "eventsubscription" -> events == null ? null : events.storedTags(account, region, id);
case "integration" -> AccountAwareStorageBackend.readInAccount(integrations, account, id)
.filter(integration -> arn.equals(integration.getIntegrationArn()))
.map(Integration::getTags).orElse(null);
default -> null;
};
Map<String, String> held = new LinkedHashMap<>();
if (tags != null) {
tags.forEach((key, value) -> {
if (key != null && value != null) {
held.put(key, value);
}
});
}
return held;
}
/**
* The snapshot {@code <cluster>/<name>} names in {@code account}, read in place: the copy kept in {@code region}
* before the snapshot itself, as tagging finds it, each only when its ARN is {@code arn} (one recording none, from
* before snapshots did, when it is of that cluster).
*/
private Optional<Snapshot> storedSnapshot(String arn, String account, String region, String id) {
int slash = id.indexOf('/');
if (slash < 0) {
return Optional.empty();
}
String cluster = id.substring(0, slash);
String name = id.substring(slash + 1);
Predicate<Snapshot> named = snapshot -> snapshot.getSnapshotArn() == null
? cluster.equals(snapshot.getClusterIdentifier()) : arn.equals(snapshot.getSnapshotArn());
return AccountAwareStorageBackend.readInAccount(snapshotCopies, account, regionalKey(region, name))
.filter(named)
.or(() -> AccountAwareStorageBackend.readInAccount(snapshots, account, name).filter(named));
}
public synchronized void createTags(String resourceName, Map<String, String> tags) {
String subscriptionName = eventSubscriptionTagName(resourceName);
if (subscriptionName != null) {
@@ -6,11 +6,12 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* What IAM asks Redshift of the resources a request names: what a snapshot records as its {@code KmsKeyId}, the key a
* restore asks KMS to decrypt with as the caller.
* restore asks KMS to decrypt with as the caller, and the tags of the resource an ARN names.
*/
@ApplicationScoped
public class RedshiftStoredResourceArns implements StoredResourceArns {
@@ -40,4 +41,9 @@ public class RedshiftStoredResourceArns implements StoredResourceArns {
}
return redshift.findSnapshotAt(arn).map(Snapshot::getKmsKeyId).filter(key -> !key.isBlank());
}
@Override
public Map<String, String> storedTags(String arn) {
return redshift.storedTags(arn);
}
}
@@ -163,7 +163,7 @@ public class Cluster {
public int getProxyPort() { return proxyPort; }
public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; }
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
@@ -45,7 +45,7 @@ public class ClusterParameterGroup {
this.description = description;
}
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
@@ -13,7 +13,7 @@ public class ClusterSubnetGroup {
private String description;
private String vpcId;
private List<String> subnetIds = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
/** Subnet id to availability zone; null on groups persisted before zones were resolved. */
private Map<String, String> subnetZones;
@@ -22,7 +22,7 @@ public class EventSubscription {
private List<String> eventCategoriesList = new ArrayList<>();
private String severity;
private boolean enabled;
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
public String getCustomerAwsId() { return customerAwsId; }
public void setCustomerAwsId(String customerAwsId) { this.customerAwsId = customerAwsId; }
@@ -178,7 +178,7 @@ public class Snapshot {
this.manualSnapshotRetentionPeriod = manualSnapshotRetentionPeriod;
}
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
public Set<String> getAccountsWithRestoreAccess() { return accountsWithRestoreAccess; }
public void setAccountsWithRestoreAccess(Set<String> value) {
@@ -9,7 +9,7 @@ import java.util.Map;
public class SnapshotCopyGrant {
private String snapshotCopyGrantName;
private String kmsKeyId;
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
public SnapshotCopyGrant() {}
@@ -17,7 +17,7 @@ public class SnapshotSchedule {
private String scheduleIdentifier;
private String scheduleDescription;
private List<String> scheduleDefinitions = new ArrayList<>();
private Map<String, String> tags = new LinkedHashMap<>();
private volatile Map<String, String> tags = new LinkedHashMap<>();
private Instant updatedAt;
public Instant getUpdatedAt() { return updatedAt; }
@@ -1903,6 +1903,38 @@ class IamRequestResourceEnforcementIntegrationTest {
equalTo(arn));
}
/**
* An {@code aws:ResourceTag} condition reads the tags Redshift holds on the parameter group a request names, so an
* allow on one team's tag lets the caller read that team's group and not another's.
*/
@Test
void aRedshiftResourceTagConditionReadsTheTagsOfTheParameterGroup() {
String tagged = redshiftParameterGroup("res-tag-pg-" + suffix(), "data");
String other = redshiftParameterGroup("res-tag-pg-" + suffix(), "web");
User user = user("res-redshift-tags");
user.policy("{\"Effect\":\"Allow\",\"Action\":\"redshift:DescribeClusterParameters\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
describeClusterParameters(user, tagged).statusCode(200);
refused(describeClusterParameters(user, other), "redshift:DescribeClusterParameters");
}
/** A Redshift parameter group of the name, tagged with the team, deleted afterwards. */
private String redshiftParameterGroup(String name, String team) {
admin("redshift").formParam("Action", "CreateClusterParameterGroup").formParam("Version", "2012-12-01")
.formParam("ParameterGroupName", name).formParam("ParameterGroupFamily", "redshift-1.0")
.formParam("Description", "d").formParam("Tags.Tag.1.Key", "team").formParam("Tags.Tag.1.Value", team)
.post("/").then().statusCode(200);
cleanup.register(() -> admin("redshift").formParam("Action", "DeleteClusterParameterGroup")
.formParam("Version", "2012-12-01").formParam("ParameterGroupName", name).post("/"));
return name;
}
private static ValidatableResponse describeClusterParameters(User user, String group) {
return user.query("redshift", "DescribeClusterParameters").formParam("Version", "2012-12-01")
.formParam("ParameterGroupName", group).post("/").then();
}
/** A Redshift cluster is checked on {@code cluster:<identifier>}, before the cluster is looked up. */
@Test
void aRedshiftClusterIsCheckedOnItsIdentifier() {
@@ -594,6 +594,25 @@ class RedshiftEventServiceTest {
List.of("team"), List.of("data"), null, null)));
}
@Test
void storedTagsAreTheTagsOfTheSubscriptionInTheAccountAndRegionGivenAndTaggingPutsANewMap() {
service.createSubscription("watcher", TOPIC, null, List.of(), List.of(), null, null,
Map.of("team", "data"), ALL_SOURCES_EXIST);
RedshiftEventService west = new RedshiftEventService(factory, sns, authorizer,
new RegionResolver("us-west-2", "000000000000"), objectMapper, clock);
west.createSubscription("watcher", TOPIC, null, List.of(), List.of(), null, null,
Map.of("team", "west"), ALL_SOURCES_EXIST);
Map<String, String> held = service.storedTags("000000000000", "us-east-1", "watcher");
service.addSubscriptionTags("watcher", Map.of("env", "prod"));
assertEquals(Map.of("team", "data"), held);
assertEquals(Map.of("team", "data", "env", "prod"), service.storedTags("000000000000", "us-east-1", "watcher"));
assertEquals(Map.of("team", "west"), service.storedTags("000000000000", "us-west-2", "watcher"));
assertNull(service.storedTags("111111111111", "us-east-1", "watcher"));
assertNull(service.storedTags("000000000000", "us-east-1", "missing"));
}
@Test
void eventHistoryAndSubscriptionNamesAreRegional() {
create("watcher", null, List.of(), List.of(), null);
@@ -82,6 +82,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -4106,6 +4107,61 @@ class RedshiftServiceTest {
assertEquals(Map.of("team", "data"), cluster.getTags());
}
@Test
void storedTagsAreTheTagsOfTheResourceTheWholeArnNamesInItsAccountAndRegionAndTaggingPutsANewMap() {
String account = "111111111111";
Cluster cluster = storedCluster("warehouse", "us-east-1");
cluster.setTags(new LinkedHashMap<>(Map.of("team", "data")));
Snapshot snapshot = snapshotOf("nightly", "warehouse");
snapshot.setSnapshotArn("arn:aws:redshift:us-east-1:111111111111:snapshot:warehouse/nightly");
snapshot.setTags(new LinkedHashMap<>(Map.of("stage", "prod")));
when(snapshotBackend.getForAccount(account, "nightly")).thenReturn(Optional.of(snapshot));
ClusterParameterGroup ownGroup = new ClusterParameterGroup();
ownGroup.setTags(new LinkedHashMap<>(Map.of("owner", "us")));
when(parameterGroupBackend.getForAccount(account, regionalKey("tuned"))).thenReturn(Optional.of(ownGroup));
ClusterParameterGroup otherGroup = new ClusterParameterGroup();
otherGroup.setTags(new LinkedHashMap<>(Map.of("owner", "them")));
when(parameterGroupBackend.getForAccount("222222222222", regionalKey("tuned")))
.thenReturn(Optional.of(otherGroup));
SnapshotSchedule schedule = new SnapshotSchedule();
schedule.setTags(new LinkedHashMap<>(Map.of("cadence", "daily")));
when(snapshotScheduleBackend.getForAccount(account, regionalKey("daily"))).thenReturn(Optional.of(schedule));
Integration integration = new Integration();
integration.setIntegrationArn("arn:aws:redshift:us-east-1:111111111111:integration:abc");
integration.setTags(Map.of("feed", "orders"));
when(integrationBackend.getForAccount(account, "abc")).thenReturn(Optional.of(integration));
when(events.storedTags(account, "us-east-1", "alerts")).thenReturn(Map.of("route", "ops"));
assertEquals(Map.of("team", "data"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:cluster:warehouse"));
assertEquals(Map.of("stage", "prod"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:snapshot:warehouse/nightly"));
assertEquals(Map.of("owner", "us"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:parametergroup:tuned"));
assertEquals(Map.of("owner", "them"),
service.storedTags("arn:aws:redshift:us-east-1:222222222222:parametergroup:tuned"));
assertEquals(Map.of("cadence", "daily"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:snapshotschedule:daily"));
assertEquals(Map.of("feed", "orders"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:integration:abc"));
assertEquals(Map.of("route", "ops"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:eventsubscription:alerts"));
// Another Region's cluster of that name, another cluster's snapshot, an ARN whose partition is not its Region's
// and a type Floci keeps no record of hold none.
assertEquals(Map.of(), service.storedTags("arn:aws:redshift:eu-west-1:111111111111:cluster:warehouse"));
assertEquals(Map.of(), service.storedTags("arn:aws:redshift:us-east-1:111111111111:snapshot:other/nightly"));
assertEquals(Map.of(), service.storedTags("arn:aws-cn:redshift:us-east-1:111111111111:cluster:warehouse"));
assertEquals(Map.of(), service.storedTags("arn:aws:redshift:us-east-1:111111111111:datashare:ns/share"));
Map<String, String> held = cluster.getTags();
service.createTags("arn:aws:redshift:us-east-1:111111111111:cluster:warehouse", Map.of("env", "prod"));
assertNotSame(held, cluster.getTags());
assertEquals(Map.of("team", "data"), held);
assertEquals(Map.of("team", "data", "env", "prod"),
service.storedTags("arn:aws:redshift:us-east-1:111111111111:cluster:warehouse"));
}
@Test
void createTagsRejectsNonArnResourceName() {
assertThrows(AwsException.class, () ->