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

@@ -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