diff --git a/docs/services/iam.md b/docs/services/iam.md index 0a85352e25..92cc9ac523 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -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 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 266c9f9ac7..0ebdd62e1e 100644 --- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java +++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java @@ -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} for - * the namespace a request or an ARN is of, so IAM never depends on that service directly.

+ * 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} for the namespace a request or an + * ARN is of, so IAM never depends on that service directly.

* *

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 diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftEventService.java b/src/main/java/io/floci/aws/services/redshift/RedshiftEventService.java index bc4cb2c246..a836e466dc 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftEventService.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftEventService.java @@ -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 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); diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java index 65ec401af6..5abd5ed1d2 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftService.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftService.java @@ -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 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 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 held = new LinkedHashMap<>(); + if (tags != null) { + tags.forEach((key, value) -> { + if (key != null && value != null) { + held.put(key, value); + } + }); + } + return held; + } + + /** + * The snapshot {@code /} 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 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 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 tags) { String subscriptionName = eventSubscriptionTagName(resourceName); if (subscriptionName != null) { diff --git a/src/main/java/io/floci/aws/services/redshift/RedshiftStoredResourceArns.java b/src/main/java/io/floci/aws/services/redshift/RedshiftStoredResourceArns.java index a92029b01e..5463986931 100644 --- a/src/main/java/io/floci/aws/services/redshift/RedshiftStoredResourceArns.java +++ b/src/main/java/io/floci/aws/services/redshift/RedshiftStoredResourceArns.java @@ -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 storedTags(String arn) { + return redshift.storedTags(arn); + } } diff --git a/src/main/java/io/floci/aws/services/redshift/model/Cluster.java b/src/main/java/io/floci/aws/services/redshift/model/Cluster.java index 13f340ce76..c5704ad214 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/Cluster.java +++ b/src/main/java/io/floci/aws/services/redshift/model/Cluster.java @@ -163,7 +163,7 @@ public class Cluster { public int getProxyPort() { return proxyPort; } public void setProxyPort(int proxyPort) { this.proxyPort = proxyPort; } - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public Map getTags() { return tags; } public void setTags(Map tags) { this.tags = tags; } diff --git a/src/main/java/io/floci/aws/services/redshift/model/ClusterParameterGroup.java b/src/main/java/io/floci/aws/services/redshift/model/ClusterParameterGroup.java index edc4cc797e..c56bc28681 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/ClusterParameterGroup.java +++ b/src/main/java/io/floci/aws/services/redshift/model/ClusterParameterGroup.java @@ -45,7 +45,7 @@ public class ClusterParameterGroup { this.description = description; } - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public Map getTags() { return tags; } public void setTags(Map tags) { this.tags = tags; } diff --git a/src/main/java/io/floci/aws/services/redshift/model/ClusterSubnetGroup.java b/src/main/java/io/floci/aws/services/redshift/model/ClusterSubnetGroup.java index 18de87ac03..56090f6595 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/ClusterSubnetGroup.java +++ b/src/main/java/io/floci/aws/services/redshift/model/ClusterSubnetGroup.java @@ -13,7 +13,7 @@ public class ClusterSubnetGroup { private String description; private String vpcId; private List subnetIds = new ArrayList<>(); - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); /** Subnet id to availability zone; null on groups persisted before zones were resolved. */ private Map subnetZones; diff --git a/src/main/java/io/floci/aws/services/redshift/model/EventSubscription.java b/src/main/java/io/floci/aws/services/redshift/model/EventSubscription.java index c3bc461b8b..3145adca11 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/EventSubscription.java +++ b/src/main/java/io/floci/aws/services/redshift/model/EventSubscription.java @@ -22,7 +22,7 @@ public class EventSubscription { private List eventCategoriesList = new ArrayList<>(); private String severity; private boolean enabled; - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public String getCustomerAwsId() { return customerAwsId; } public void setCustomerAwsId(String customerAwsId) { this.customerAwsId = customerAwsId; } diff --git a/src/main/java/io/floci/aws/services/redshift/model/Snapshot.java b/src/main/java/io/floci/aws/services/redshift/model/Snapshot.java index 0793e47005..c8772e838f 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/Snapshot.java +++ b/src/main/java/io/floci/aws/services/redshift/model/Snapshot.java @@ -178,7 +178,7 @@ public class Snapshot { this.manualSnapshotRetentionPeriod = manualSnapshotRetentionPeriod; } - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public Set getAccountsWithRestoreAccess() { return accountsWithRestoreAccess; } public void setAccountsWithRestoreAccess(Set value) { diff --git a/src/main/java/io/floci/aws/services/redshift/model/SnapshotCopyGrant.java b/src/main/java/io/floci/aws/services/redshift/model/SnapshotCopyGrant.java index 7336a60fd0..53ebd16827 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/SnapshotCopyGrant.java +++ b/src/main/java/io/floci/aws/services/redshift/model/SnapshotCopyGrant.java @@ -9,7 +9,7 @@ import java.util.Map; public class SnapshotCopyGrant { private String snapshotCopyGrantName; private String kmsKeyId; - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); public SnapshotCopyGrant() {} diff --git a/src/main/java/io/floci/aws/services/redshift/model/SnapshotSchedule.java b/src/main/java/io/floci/aws/services/redshift/model/SnapshotSchedule.java index 36c47a6c20..516bf7d4ad 100644 --- a/src/main/java/io/floci/aws/services/redshift/model/SnapshotSchedule.java +++ b/src/main/java/io/floci/aws/services/redshift/model/SnapshotSchedule.java @@ -17,7 +17,7 @@ public class SnapshotSchedule { private String scheduleIdentifier; private String scheduleDescription; private List scheduleDefinitions = new ArrayList<>(); - private Map tags = new LinkedHashMap<>(); + private volatile Map tags = new LinkedHashMap<>(); private Instant updatedAt; public Instant getUpdatedAt() { return updatedAt; } 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 459a624026..ea5365fa1e 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestResourceEnforcementIntegrationTest.java @@ -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:}, before the cluster is looked up. */ @Test void aRedshiftClusterIsCheckedOnItsIdentifier() { diff --git a/src/test/java/io/floci/aws/services/redshift/RedshiftEventServiceTest.java b/src/test/java/io/floci/aws/services/redshift/RedshiftEventServiceTest.java index 88f8dfb8b0..150868e7dc 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftEventServiceTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftEventServiceTest.java @@ -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 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); diff --git a/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java b/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java index 50dfb42295..bbed733182 100644 --- a/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java +++ b/src/test/java/io/floci/aws/services/redshift/RedshiftServiceTest.java @@ -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 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, () ->