fix(accessanalyzer): give resource-tag conditions the tags of the analyzer a request names (#8778)

The generated request resources give Access Analyzer's Analyzer type the
aws:ResourceTag/ key, but no service answered the access-analyzer
namespace's stored tags, so a condition on an analyzer's tags saw none.
AccessAnalyzerService now answers it: storedTags reads the analyzer
stored under the Region the ARN names and its name, in the request's
account, where the handlers read, in place, and matched on the whole
ARN. An analyzer's tags are set when it is created and never change.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-17 18:57:32 -04:00
1 parent 523eba559e
commit 8e88ba0915
5 files changed
+97 -7

No files matched your search

+2 -2
View File
@@ -3315,8 +3315,8 @@ floci populates:
Payment Cryptography for its keys, Resource Explorer for its indexes and views, Application Auto Scaling for its
scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container
instances, ACM for its certificates, Firehose for its delivery streams, Cognito for its user pools and identity
pools, OpenSearch for its domains, and Signer for its signing profiles. Tag keys differing only in case are one
condition key holding every value.
pools, OpenSearch for its domains, Signer for its signing profiles, and Access Analyzer for its analyzers. 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
@@ -46,9 +46,9 @@ import java.util.Optional;
* reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity
* provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery
* stream; Cognito, for the tags of a user pool or an identity pool; OpenSearch, for the tags of a domain; Signer, for
* the tags of a signing profile); 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>
* the tags of a signing profile; Access Analyzer, for the tags of an analyzer); 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
@@ -2,11 +2,13 @@ package io.floci.aws.services.accessanalyzer;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
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.StorageFactory;
import io.floci.aws.services.accessanalyzer.model.Analyzer;
@@ -17,11 +19,12 @@ import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
@ApplicationScoped
public class AccessAnalyzerService implements Resettable {
public class AccessAnalyzerService implements Resettable, StoredResourceArns {
private static final Pattern ANALYZER_NAME = Pattern.compile("[A-Za-z][A-Za-z0-9_.-]*");
private static final Set<String> ANALYZER_TYPES = Set.of(
"ACCOUNT",
@@ -88,6 +91,40 @@ public class AccessAnalyzerService implements Resettable {
analyzers.delete(storageKey(region, analyzerName));
}
@Override
public String namespace() {
return "access-analyzer";
}
/** A request names an analyzer by its name, which its ARN carries whole, so there is nothing to find. */
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
/**
* The tags of the analyzer whose ARN is {@code arn}, in the request's account, where the handlers read: an ARN of
* another account names none. Read in place under the Region the ARN names and the analyzer's name, an analyzer
* stored before storage was account-aware included, and matched on the whole ARN. An analyzer's tags are set
* when it is created, before it is stored, and never change.
*/
@Override
public Map<String, String> storedTags(String arn) {
if (!AwsArnUtils.isArnFor(arn, "access-analyzer")) {
return Map.of();
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
if (!parsed.accountId().equals(regionResolver.getAccountId()) || !parsed.resource().startsWith("analyzer/")) {
return Map.of();
}
Map<String, String> tags = AccountAwareStorageBackend.readInAccount(analyzers, parsed.accountId(),
storageKey(parsed.region(), parsed.resource().substring("analyzer/".length())))
.filter(analyzer -> arn.equals(analyzer.getArn()))
.map(Analyzer::getTags)
.orElse(Map.of());
return new LinkedHashMap<>(tags);
}
private static int analyzerLimit(String type) {
return switch (type) {
case "ORGANIZATION", "ORGANIZATION_UNUSED_ACCESS" -> 5;
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
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.core.storage.StorageFactory;
import io.floci.aws.services.accessanalyzer.model.Analyzer;
import org.junit.jupiter.api.BeforeEach;
@@ -26,17 +27,19 @@ class AccessAnalyzerServiceTest {
private static final String ACCOUNT_ID = "123456789012";
private final ObjectMapper mapper = new ObjectMapper();
private final InMemoryStorage<String, Analyzer> raw = new InMemoryStorage<>();
private AccessAnalyzerService service;
@BeforeEach
@SuppressWarnings({"unchecked", "rawtypes"})
void setUp() {
StorageFactory storageFactory = mock(StorageFactory.class);
AccountAwareStorageBackend<Analyzer> store = AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
AccountAwareStorageBackend<Analyzer> store = new AccountAwareStorageBackend<>(raw, null, ACCOUNT_ID);
when(storageFactory.create(eq("accessanalyzer"), eq("accessanalyzer-analyzers.json"), any(TypeReference.class)))
.thenReturn((AccountAwareStorageBackend) store);
RegionResolver regionResolver = mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn(ACCOUNT_ID);
when(regionResolver.buildArn(eq("access-analyzer"), eq(REGION), any(String.class)))
.thenAnswer(invocation -> "arn:aws:access-analyzer:" + REGION + ":" + ACCOUNT_ID + ":"
+ invocation.getArgument(2, String.class));
@@ -68,6 +71,31 @@ class AccessAnalyzerServiceTest {
assertTrue(service.listAnalyzers(REGION, null, null, null).items().isEmpty());
}
/**
* IAM's tag lookup reads the tags an analyzer was created with, from the analyzer whose ARN it is given, and those
* of an analyzer stored before storage was account-aware, in place, without migrating it; nothing for an ARN of
* another account or Region, or of no analyzer.
*/
@Test
void storedTagsAreTheTagsOfTheAnalyzerTheArnNames() {
ObjectNode tagged = request("tagged", "ACCOUNT");
tagged.putObject("tags").put("team", "data");
String arn = service.createAnalyzer(tagged, REGION).getArn();
Analyzer legacy = new Analyzer();
legacy.setName("legacy");
legacy.setArn("arn:aws:access-analyzer:" + REGION + ":" + ACCOUNT_ID + ":analyzer/legacy");
legacy.setTags(Map.of("tier", "web"));
raw.put(REGION + "::legacy", legacy);
assertEquals(Map.of("team", "data"), service.storedTags(arn));
assertEquals(Map.of("tier", "web"), service.storedTags(legacy.getArn()));
assertTrue(raw.get(REGION + "::legacy").isPresent());
assertEquals(Map.of(), service.storedTags(arn.replace(ACCOUNT_ID, "111122223333")));
assertEquals(Map.of(), service.storedTags(arn.replace(REGION, "eu-west-1")));
assertEquals(Map.of(), service.storedTags(arn + "0"));
assertEquals(Map.of(), service.storedTags(arn + "/archive-rule/rule"));
}
private ObjectNode request(String name, String type) {
ObjectNode request = mapper.createObjectNode();
request.put("analyzerName", name);
@@ -877,6 +877,31 @@ class IamRestRouteEnforcementIntegrationTest {
as(user, "access-analyzer").delete("/analyzer/" + analyzer).then().statusCode(200);
}
/**
* An {@code aws:ResourceTag} condition reads the tags Access Analyzer holds on the analyzer a request names in its
* path, the ones it was created with, so an allow on one team's tag lets the caller read that team's analyzer and
* not another's.
*/
@Test
void anAccessAnalyzerResourceTagConditionReadsTheTagsOfTheAnalyzer() {
String tagged = analyzer("data");
String other = analyzer("web");
String user = user("route-analyzer-tags");
policy(user, onTeam("Allow", "access-analyzer:GetAnalyzer", "data"));
as(user, "access-analyzer").get("/analyzer/" + tagged).then().statusCode(200);
refused(as(user, "access-analyzer").get("/analyzer/" + other).then(), "access-analyzer:GetAnalyzer");
}
/** The name of an organization analyzer an administrator creates tagged with the team, deleted after the test. */
private String analyzer(String team) {
String name = "route-tags-" + suffix();
accessAnalyzer().body("{\"analyzerName\":\"" + name + "\",\"type\":\"ORGANIZATION\",\"tags\":{\"team\":\""
+ team + "\"}}").put("/analyzer").then().statusCode(200);
cleanup.register(() -> accessAnalyzer().delete("/analyzer/" + name));
return name;
}
/**
* A Security Hub request is checked on the caller's hub in the Region it is signed for, {@code hub/default}, the
* ARN Security Hub gives it, or on the hub its {@code HubArn} names: a caller with no grant is refused, a grant