fix(controltower): give resource-tag conditions the tags of the landing zone or enabled control a request names (#8901)

The generated request resources give Control Tower's LandingZone and
EnabledControl types the aws:ResourceTag/ key, but no service answered
the controltower namespace's stored tags, so a condition on a landing
zone's or an enabled control's tags saw none. A new
ControlTowerStoredResourceArns answers it for the namespace, which two
services hold: a landing zone's tags are read in place under the Region
its ARN names, an enabled control's found by the ARN it records among
the Region's controls, as the tag handlers find it, both in the
request's account and matched on the whole ARN. Tag calls already give
the resource a new map; the tags fields are now volatile.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 16:00:15 -04:00
1 parent 3ffec01a76
commit 3f194c4c4f
9 files changed
+223 -10

No files matched your search

+2 -2
View File
@@ -3480,8 +3480,8 @@ floci populates:
custom action types, DataSync for its agents, locations and tasks, Global Accelerator for its accelerators, listeners
and endpoint groups, EMR Serverless for its applications, job runs and sessions, EMR on EKS for its virtual clusters,
job runs, managed endpoints, job templates and security configurations, Cost Explorer for its anomaly monitors and
subscriptions, and Security Hub for its hubs and configuration policies. Tag keys differing only in case are one
condition key holding every value.
subscriptions, Security Hub for its hubs and configuration policies, and Control Tower for its landing zones and
enabled controls. 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
@@ -54,9 +54,10 @@ import java.util.Optional;
* location or a task; Global Accelerator, for the tags of an accelerator, a listener or an endpoint group; EMR
* Serverless, for the tags of an application, a job run or a session; EMR on EKS, for the tags of a virtual cluster, a
* job run, a managed endpoint, a job template or a security configuration; Cost Explorer, for the tags of an anomaly
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy); 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>
* monitor or subscription; Security Hub, for the tags of a hub or a configuration policy; Control Tower, for the tags
* of a landing zone or an enabled control); 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
@@ -98,6 +98,20 @@ public class ControlTowerControlService {
.orElseThrow(() -> notFound("The request references an enabled control that does not exist."));
}
/**
* The tags of the enabled control whose ARN is {@code arn} in {@code region}, found as the tag handlers find it, by
* the ARN it records among the Region's controls in the request's account. {@link ControlTowerStoredResourceArns}
* asks it.
*/
public Map<String, String> storedTags(String region, String arn) {
return controls.scan(key -> key.startsWith(region + "::")).stream()
.filter(control -> arn.equals(control.getArn()))
.findFirst()
.map(EnabledControl::getTags)
.<Map<String, String>>map(LinkedHashMap::new)
.orElse(Map.of());
}
public synchronized Map<String, String> listTags(String region, String arn) {
Map<String, String> tags = get(region, arn).getTags();
return tags == null ? Map.of() : tags;
@@ -8,6 +8,7 @@ import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.UuidShapes;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.controltower.model.EnabledBaseline;
@@ -203,6 +204,20 @@ public class ControlTowerService {
return new CreateLandingZoneResult(arn, operationIdentifier);
}
/**
* The tags of the landing zone whose ARN is {@code arn}, held for {@code accountId} in {@code region}: read in
* place, a landing zone stored before storage was account-aware included, and matched on the whole ARN. Nothing is
* seeded here, so a Region whose landing zone was never read holds no tags. {@link ControlTowerStoredResourceArns}
* asks it.
*/
public Map<String, String> storedLandingZoneTags(String accountId, String region, String arn) {
return AccountAwareStorageBackend.readInAccount(landingZoneStore, accountId, region)
.filter(landingZone -> arn.equals(landingZone.getArn()))
.map(LandingZone::getTags)
.<Map<String, String>>map(LinkedHashMap::new)
.orElse(Map.of());
}
public synchronized Map<String, String> listLandingZoneTags(String accountId, String region, String arn) {
Map<String, String> tags = requireSeededLandingZone(accountId, region, arn).getTags();
return tags == null ? Map.of() : tags;
@@ -0,0 +1,70 @@
package io.floci.aws.services.controltower;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.StoredResourceArns;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* What IAM asks Control Tower of the resources a request names: the tags of a landing zone, which
* {@link ControlTowerService} holds, or of an enabled control, which {@link ControlTowerControlService} holds. One bean
* answers the {@code controltower} namespace, so the lookup is here and each service reads its own records.
*/
@ApplicationScoped
public class ControlTowerStoredResourceArns implements StoredResourceArns {
private static final String LANDING_ZONE_RESOURCE = "landingzone/";
private static final String ENABLED_CONTROL_RESOURCE = "enabledcontrol/";
private final RegionResolver regionResolver;
private final ControlTowerService landingZones;
private final ControlTowerControlService controls;
@Inject
public ControlTowerStoredResourceArns(RegionResolver regionResolver, ControlTowerService landingZones,
ControlTowerControlService controls) {
this.regionResolver = regionResolver;
this.landingZones = landingZones;
this.controls = controls;
}
@Override
public String namespace() {
return "controltower";
}
/** A request names a landing zone or an enabled control by its ARN or the id it ends in: nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* The tags of the landing zone or the enabled control whose ARN is {@code arn}, in the request's account, where
* the tag handlers read: an ARN of another account names none. An enabled baseline holds no tags in Floci, so its
* ARN reads none. Tags are only ever replaced by a new map, so the read is of one whole set.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "controltower")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
String account = regionResolver.getAccountId();
if (!account.equals(parsed.accountId()) || parsed.region().isEmpty()) {
return Map.of();
}
if (parsed.resource().startsWith(LANDING_ZONE_RESOURCE)) {
return landingZones.storedLandingZoneTags(account, parsed.region(), arn);
}
if (parsed.resource().startsWith(ENABLED_CONTROL_RESOURCE)) {
return controls.storedTags(parsed.region(), arn);
}
return Map.of();
}
}
@@ -14,7 +14,7 @@ public class EnabledControl {
private String driftStatus;
private String lastOperationIdentifier;
private JsonNode parameters;
private Map<String, String> tags;
private volatile Map<String, String> tags;
public EnabledControl() {
}
@@ -21,7 +21,7 @@ public class LandingZone {
private String driftStatus;
private JsonNode manifest;
private List<String> remediationTypes;
private Map<String, String> tags;
private volatile Map<String, String> tags;
public LandingZone() {
}
@@ -108,9 +108,7 @@ class ControlTowerIamIntegrationTest {
@Test
void enabledControlResourceAllowAndDenyUseEnabledArnRatherThanCatalogArn() throws Exception {
try (ControlTowerClient admin = client("test", "test")) {
String target = "arn:aws:organizations::000000000000:ou/o-test/ou-" + UUID.randomUUID();
String arn = admin.enableControl(r -> r.controlIdentifier("arn:aws:controlcatalog:::control/fixture")
.targetIdentifier(target)).arn();
String arn = enabledControl(admin, null);
String before = admin.getEnabledControl(r -> r.enabledControlIdentifier(arn))
.enabledControlDetails().statusSummary().lastOperationIdentifier();
try (SignedIamUsers.User user = user("Allow", "controltower:GetEnabledControl", arn);
@@ -127,6 +125,29 @@ class ControlTowerIamIntegrationTest {
}
}
/**
* An {@code aws:ResourceTag} condition reads the tags Control Tower holds on the enabled control a request names,
* as TagResource leaves them, so an allow on one team's tag lets the caller read that team's control and not
* another's.
*/
@Test
void aResourceTagConditionReadsTheTagsOfTheEnabledControl() throws Exception {
try (ControlTowerClient admin = client("test", "test")) {
String tagged = enabledControl(admin, "web");
String other = enabledControl(admin, "web");
admin.tagResource(r -> r.resourceArn(tagged).tags(Map.of("team", "data")));
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "ct-generated-", "access",
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"controltower:GetEnabledControl\",\"Resource\":\"*\","
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}]}");
ControlTowerClient caller = client(user.accessKeyId(), user.secretAccessKey())) {
assertEquals(tagged, caller.getEnabledControl(r -> r.enabledControlIdentifier(tagged))
.enabledControlDetails().arn());
denied(() -> caller.getEnabledControl(r -> r.enabledControlIdentifier(other)));
}
}
}
@Test
void enableControlChecksMintedResourceAndDenialDoesNotCreateControl() throws Exception {
String target = "arn:aws:organizations::000000000000:ou/o-test/ou-" + UUID.randomUUID();
@@ -227,6 +248,17 @@ class ControlTowerIamIntegrationTest {
+ "\",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}]}");
}
/** The ARN of a control the administrator enables on a target of its own, tagged with the team if one is given. */
private static String enabledControl(ControlTowerClient admin, String team) {
String target = "arn:aws:organizations::000000000000:ou/o-test/ou-" + UUID.randomUUID();
return admin.enableControl(r -> {
r.controlIdentifier("arn:aws:controlcatalog:::control/fixture").targetIdentifier(target);
if (team != null) {
r.tags(Map.of("team", team));
}
}).arn();
}
private ControlTowerClient client(String key, String secret) {
return AwsTestClients.create(ControlTowerClient.builder(), key, secret, Region.US_EAST_1, endpoint);
}
@@ -0,0 +1,81 @@
package io.floci.aws.services.controltower;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.controltower.model.LandingZone;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ControlTowerStoredResourceArnsTest {
private static final String ACCOUNT = "000000000101";
private static final String REGION = "us-east-1";
private final ObjectMapper objectMapper = new ObjectMapper();
private final InMemoryStorage<String, LandingZone> rawLandingZones = new InMemoryStorage<>();
private final ControlTowerService landingZones = new ControlTowerService(
new AccountAwareStorageBackend<>(rawLandingZones, null, ACCOUNT),
AccountAwareStorageBackend.inMemory(ACCOUNT));
private final ControlTowerControlService controls =
new ControlTowerControlService(AccountAwareStorageBackend.inMemory(ACCOUNT));
private final ControlTowerStoredResourceArns stored =
new ControlTowerStoredResourceArns(new RegionResolver(REGION, ACCOUNT), landingZones, controls);
/**
* IAM's lookup reads the tags of the landing zone or the enabled control the ARN names, as the tag calls leave
* them, and nothing for another account's, another Region's or an unknown one, an enabled baseline or another
* service.
*/
@Test
void storedTagsAreTheTagsOfTheLandingZoneOrTheEnabledControlTheArnNames() throws Exception {
String landingZone = landingZone("web");
landingZones.tagLandingZone(ACCOUNT, REGION, landingZone, Map.of("team", "data"));
String control = controls.enable(ACCOUNT, REGION, json("""
{"controlIdentifier":"arn:aws:controlcatalog:::control/fixture",
"targetIdentifier":"arn:aws:organizations::000000000101:ou/o-test/ou-stored1",
"tags":{"team":"web","env":"dev"}}
""")).arn();
controls.untagResource(REGION, control, List.of("env"));
assertEquals("controltower", stored.namespace());
assertEquals(Map.of("team", "data"), stored.storedTags(landingZone));
assertEquals(Map.of("team", "web"), stored.storedTags(control));
assertTrue(stored.storedTags(landingZone.replace(ACCOUNT, "111122223333")).isEmpty());
assertTrue(stored.storedTags(control.replace(REGION, "us-west-2")).isEmpty());
assertTrue(stored.storedTags(control + "0").isEmpty());
assertTrue(stored.storedTags("arn:aws:controltower:us-east-1:000000000101:enabledbaseline/ABC").isEmpty());
assertTrue(stored.storedTags("arn:aws:guardduty:us-east-1:000000000101:detector/abc").isEmpty());
}
/** A landing zone stored before storage was account-aware is read in place: the lookup migrates nothing. */
@Test
void storedTagsReadAPreAccountAwareLandingZoneInPlace() throws Exception {
String landingZone = landingZone("data");
rawLandingZones.put(REGION, rawLandingZones.get(ACCOUNT + "/" + REGION).orElseThrow());
rawLandingZones.delete(ACCOUNT + "/" + REGION);
assertEquals(Map.of("team", "data"), stored.storedTags(landingZone));
assertEquals(Set.of(REGION), rawLandingZones.keys());
}
/** The ARN of the landing zone created for the account in the Region, tagged with the team. */
private String landingZone(String team) throws Exception {
return landingZones.createLandingZone(ACCOUNT, REGION, json("""
{"version":"4.0","tags":{"team":"%s"},
"manifest":{"governedRegions":["us-east-1"],"accessManagement":{"enabled":true}}}
""".formatted(team))).arn();
}
private JsonNode json(String value) throws Exception {
return objectMapper.readTree(value);
}
}