mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(redshift-serverless): give resource-tag conditions the tags of the namespace, workgroup or snapshot a request names (#8615)
The generated request resources give Redshift Serverless's namespace, workgroup, snapshot and recovery point types aws:ResourceTag/ keys, but RedshiftServerlessService did not answer storedTags, so a condition on their tags saw none. It now reads the namespace, the workgroup or the snapshot stored under the ARN in the account and Region it names, a pre-multi-account one of the default account included, in place, matched on the whole ARN. A tag change is already stored on a copy, so the tags read are one whole set.
This commit is contained in:
1 parent
d8460bc305
commit
a5064a2f5a
5 files changed
+126
-5
No files matched your search
@@ -3150,8 +3150,8 @@ floci populates:
|
||||
tags), MicroVM images and network connectors, Glue for its schema registries and schemas and for the jobs,
|
||||
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, and Athena for its workgroups and data catalogs. Tag keys differing only in case are
|
||||
one condition key holding every value.
|
||||
projects and report groups, Athena for its workgroups and data catalogs, and Redshift Serverless for its
|
||||
namespaces, workgroups and snapshots. 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
|
||||
|
||||
@@ -33,9 +33,10 @@ import java.util.Optional;
|
||||
* network connector; Glue, for the tags of a resource it tags; Step Functions, for the tags of a state machine, its
|
||||
* 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); 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>
|
||||
* workgroup or a data catalog; Redshift Serverless, for the tags of a namespace, a workgroup or a snapshot); 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
|
||||
|
||||
@@ -51,6 +51,7 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -1093,6 +1094,48 @@ public class RedshiftServerlessService implements Resettable, StoredResourceArns
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the namespace, the workgroup or the snapshot stored under {@code arn} in the account and Region it
|
||||
* names, a pre-multi-account one of the default account included, read in place and matched on the whole ARN;
|
||||
* empty for an ARN naming nothing stored. A tag change is stored on a copy, so the tags read are one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "redshift-serverless")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = parsed.accountId();
|
||||
String region = parsed.region();
|
||||
Map<String, String> tags = tagsIn(namespaces, account, region, arn, Namespace::getNamespaceArn,
|
||||
Namespace::getTags)
|
||||
.or(() -> tagsIn(workgroups, account, region, arn, Workgroup::getWorkgroupArn, Workgroup::getTags))
|
||||
.or(() -> tagsIn(snapshots, account, region, arn, RedshiftServerlessSnapshot::getSnapshotArn,
|
||||
RedshiftServerlessSnapshot::getTags))
|
||||
.orElse(Map.of());
|
||||
Map<String, String> copy = new LinkedHashMap<>();
|
||||
tags.forEach((key, value) -> {
|
||||
if (value != null) {
|
||||
copy.put(key, value);
|
||||
}
|
||||
});
|
||||
return copy;
|
||||
}
|
||||
|
||||
private static <V> Optional<Map<String, String>> tagsIn(AccountAwareStorageBackend<V> store, String account,
|
||||
String region, String arn, Function<V, String> arnOf,
|
||||
Function<V, Map<String, String>> tagsOf) {
|
||||
List<V> held = new ArrayList<>(store.scanForAccount(account, key -> key.startsWith(region + "::")));
|
||||
if (account.equals(store.defaultAccountId())) {
|
||||
held.addAll(store.scanUnscopedLegacy(stored -> arn.equals(arnOf.apply(stored))));
|
||||
}
|
||||
return held.stream()
|
||||
.filter(stored -> arn.equals(arnOf.apply(stored)))
|
||||
.map(tagsOf)
|
||||
.filter(Objects::nonNull)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/** The snapshot {@code snapshotName} of {@code accountId} in {@code region}, whoever is calling. */
|
||||
public Optional<RedshiftServerlessSnapshot> findSnapshotOfAccount(String accountId, String snapshotName,
|
||||
String region) {
|
||||
|
||||
+45
@@ -1530,6 +1530,51 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags Redshift Serverless holds on a namespace and a workgroup, so
|
||||
* an allow on one team's tag lets the caller read that team's and not another's.
|
||||
*/
|
||||
@Test
|
||||
void aRedshiftServerlessResourceTagConditionReadsTheTagsOfTheNamespaceAndTheWorkgroup() {
|
||||
when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
|
||||
any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
|
||||
.thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
|
||||
String tagged = "res-tag-rss-" + suffix();
|
||||
String other = "res-tag-rss-" + suffix();
|
||||
taggedWorkgroup(tagged, "data");
|
||||
taggedWorkgroup(other, "web");
|
||||
|
||||
User user = user("res-rss-tags");
|
||||
user.policy("{\"Effect\":\"Allow\",\"Action\":[\"redshift-serverless:GetNamespace\","
|
||||
+ "\"redshift-serverless:GetWorkgroup\"],\"Resource\":\"*\","
|
||||
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}");
|
||||
redshiftServerless(user, "GetNamespace", "{\"namespaceName\":\"" + tagged + "-ns\"}").statusCode(200);
|
||||
refusedJson(redshiftServerless(user, "GetNamespace", "{\"namespaceName\":\"" + other + "-ns\"}"),
|
||||
"redshift-serverless:GetNamespace");
|
||||
redshiftServerless(user, "GetWorkgroup", "{\"workgroupName\":\"" + tagged + "\"}").statusCode(200);
|
||||
refusedJson(redshiftServerless(user, "GetWorkgroup", "{\"workgroupName\":\"" + other + "\"}"),
|
||||
"redshift-serverless:GetWorkgroup");
|
||||
}
|
||||
|
||||
/** Creates a serverless workgroup and its namespace as the admin, both tagged with the team. */
|
||||
private void taggedWorkgroup(String name, String team) {
|
||||
String tags = ",\"tags\":[{\"key\":\"team\",\"value\":\"" + team + "\"}]";
|
||||
adminJson("redshift-serverless", "RedshiftServerless.CreateNamespace")
|
||||
.body("{\"namespaceName\":\"" + name + "-ns\",\"adminUsername\":\"admin\","
|
||||
+ "\"adminUserPassword\":\"Secret123!\"" + tags + "}").post("/").then().statusCode(200);
|
||||
cleanup.register(() -> adminJson("redshift-serverless", "RedshiftServerless.DeleteNamespace")
|
||||
.body("{\"namespaceName\":\"" + name + "-ns\"}").post("/"));
|
||||
adminJson("redshift-serverless", "RedshiftServerless.CreateWorkgroup")
|
||||
.body("{\"workgroupName\":\"" + name + "\",\"namespaceName\":\"" + name + "-ns\"" + tags + "}")
|
||||
.post("/").then().statusCode(200);
|
||||
cleanup.register(() -> adminJson("redshift-serverless", "RedshiftServerless.DeleteWorkgroup")
|
||||
.body("{\"workgroupName\":\"" + name + "\"}").post("/"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse redshiftServerless(User user, String operation, String body) {
|
||||
return user.json("redshift-serverless", "RedshiftServerless." + operation, "1.1").body(body).post("/").then();
|
||||
}
|
||||
|
||||
/** Creates a serverless workgroup and its namespace as the admin and gives the workgroup's ARN. */
|
||||
private String createWorkgroup(String name) {
|
||||
adminJson("redshift-serverless", "RedshiftServerless.CreateNamespace")
|
||||
|
||||
+32
@@ -37,6 +37,7 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -415,6 +416,37 @@ class RedshiftServerlessServiceTest {
|
||||
service.listTagsForResource(created.getNamespaceArn(), REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsReadTheNamespaceOrTheSnapshotTheArnNamesInItsAccountAndRegion() {
|
||||
String namespace = service.createNamespace("stored-tags-ns", "admin", null, null, null, null, null,
|
||||
Map.of("team", "data"), REGION).getNamespaceArn();
|
||||
String snapshot = service.createSnapshot("stored-tags-snap", "stored-tags-ns", null, Map.of("team", "backup"),
|
||||
REGION).getSnapshotArn();
|
||||
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(namespace));
|
||||
assertEquals(Map.of("team", "backup"), service.storedTags(snapshot));
|
||||
assertEquals(Map.of(), service.storedTags(namespace.replace(ACCOUNT_ID, "111111111111")));
|
||||
assertEquals(Map.of(), service.storedTags(namespace.replace(REGION, "us-west-2")));
|
||||
assertEquals(Map.of(), service.storedTags(namespace + "0"));
|
||||
|
||||
service.tagResource(namespace, Map.of("stage", "prod"), REGION);
|
||||
service.untagResource(namespace, List.of("team"), REGION);
|
||||
|
||||
assertEquals(Map.of("stage", "prod"), service.storedTags(namespace));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedTagsReadAPreMultiAccountNamespaceAsTheDefaultAccountsAndAsNoOthers() {
|
||||
Namespace legacy = new Namespace();
|
||||
legacy.setNamespaceArn("arn:aws:redshift-serverless:" + REGION + ":" + ACCOUNT_ID + ":namespace/legacy-id");
|
||||
legacy.setTags(Map.of("team", "legacy"));
|
||||
doAnswer(invocation -> Stream.of(legacy).filter(invocation.<Predicate<Namespace>>getArgument(0)).toList())
|
||||
.when(store).scanUnscopedLegacy(any());
|
||||
|
||||
assertEquals(Map.of("team", "legacy"), service.storedTags(legacy.getNamespaceArn()));
|
||||
assertEquals(Map.of(), service.storedTags(legacy.getNamespaceArn().replace(ACCOUNT_ID, "111111111111")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceMergesAndUntagResourceRemovesByKey() {
|
||||
Namespace created = service.createNamespace("merge-ns", "admin", null, null, null, null, null,
|
||||
|
||||
Reference in new issue
Block a user