feat(iam): expand wildcard service grants against a vendored namespace catalog (#4544)

GetServiceLastAccessedDetails reports the services an entity can reach, but
a grant that names no namespace could not be expanded: "Action": "*", a
globbed prefix such as s3*, and any NotAction all denote a set no policy
document spells out. A user holding only AdministratorAccess came back with
an empty report. Closes #4487.

The list of namespaces is vendored in aws/iam-service-namespaces.json and
generated by tools/aws/regen_service_namespaces.py from AWS's Service
Reference Information index, whose per-service "service" field is the IAM
namespace itself. botocore is deliberately not the source, even though every
other vendored file here comes from it: a namespace is not any botocore
field, since CloudWatch's endpointPrefix is monitoring and its serviceId is
CloudWatch against an IAM namespace of cloudwatch. A rule over botocore
metadata is wrong for some service, and a wrong namespace is worse than a
missing one because it names something IAM does not have.

That index is a live endpoint rather than a pinned dependency, so unlike
regen_partitions.py a fresh generation is not byte-identical and byte
equality is the wrong gate. What must be guaranteed is that nothing in the
file is invented, which is checked directly: make iam-namespaces-check
validates the file's shape offline, and make iam-namespaces-verify checks
online that every namespace in it still exists upstream, failing on one that
does not. Services AWS has added since the last sync are reported without
failing, since the file being behind makes the report incomplete rather than
wrong.

A namespace a policy names outright is kept whether or not the catalog knows
it, so a catalog trailing an AWS launch still reports an explicitly named
service; only wildcard expansion depends on it being current. A globbed
prefix now expands to the namespaces it matches rather than being treated as
every service and then dropped.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-28 22:02:13 -05:00
1 parent b830ade6cd
commit 26146a3f94
12 files changed
+1258 -42

No files matched your search

@@ -0,0 +1,98 @@
package io.github.hectorvent.floci.core.common;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** Verifies the vendored IAM service namespace catalog and how it fails when malformed. */
class AwsServiceNamespacesTest {
@Test
void theVendoredCatalogCarriesEveryServiceAwsPublishes() {
List<String> namespaces = AwsServiceNamespaces.all();
// AWS publishes several hundred; a catalog an order of magnitude smaller means the
// vendored file was truncated rather than regenerated.
assertTrue(namespaces.size() > 300,
"expected several hundred namespaces, got " + namespaces.size());
}
/**
* The whole reason this catalog is generated from AWS's service reference rather than botocore:
* CloudWatch's credential scope and endpoint prefix are both {@code monitoring}, while its IAM
* namespace is {@code cloudwatch}. Deriving it from botocore metadata produces the former.
*/
@Test
void cloudWatchIsCarriedByItsIamNamespaceNotItsCredentialScope() {
assertTrue(AwsServiceNamespaces.all().contains("cloudwatch"),
"cloudwatch is CloudWatch's IAM namespace and must be present");
assertFalse(AwsServiceNamespaces.all().contains("monitoring"),
"monitoring is a credential scope, not an IAM namespace");
}
@Test
void theCatalogIsSortedAndFreeOfDuplicates() {
List<String> namespaces = AwsServiceNamespaces.all();
assertEquals(namespaces.stream().sorted().toList(), namespaces, "expected a sorted catalog");
assertEquals(namespaces.size(), namespaces.stream().distinct().count(),
"expected no duplicate namespaces");
}
@Test
void everyNamespaceIsUsableInAPolicyActionPrefix() {
for (String namespace : AwsServiceNamespaces.all()) {
assertTrue(namespace.matches("[a-z0-9][a-z0-9-]{0,63}"),
namespace + " is not usable as an IAM action prefix");
}
}
@Test
void servicesFlociEmulatesArePresent() {
for (String namespace : List.of("iam", "s3", "sts", "dynamodb", "lambda", "sqs", "sns",
"logs", "events", "kms", "secretsmanager", "states", "ecs", "eks")) {
assertTrue(AwsServiceNamespaces.all().contains(namespace),
namespace + " should be in the catalog");
}
}
@Test
void aCatalogWithNoNamespacesIsABuildDefect() {
String json = "{\"serviceNamespaces\": []}";
assertThrows(IllegalStateException.class, () -> AwsServiceNamespaces.parse(
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
}
@Test
void aCatalogWithABlankNamespaceIsABuildDefect() {
String json = "{\"serviceNamespaces\": [\"s3\", \"\"]}";
assertThrows(IllegalStateException.class, () -> AwsServiceNamespaces.parse(
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
}
@Test
void aCatalogMissingTheFieldEntirelyIsABuildDefect() {
String json = "{\"somethingElse\": [\"s3\"]}";
assertThrows(IllegalStateException.class, () -> AwsServiceNamespaces.parse(
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
}
@Test
void aWellFormedCatalogParsesInOrder() throws IOException {
String json = "{\"serviceNamespaces\": [\"cloudwatch\", \"iam\", \"s3\"]}";
assertEquals(List.of("cloudwatch", "iam", "s3"), AwsServiceNamespaces.parse(
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
}
}
@@ -172,6 +172,103 @@ class ServiceLastAccessedIntegrationTest {
.body(not(containsString("<ServiceNamespace>dynamodb</ServiceNamespace>")));
}
/**
* A policy granting {@code "Action": "*"} reaches every service, and AWS lists them all. The
* grant names no namespace, so this only works with a catalog to expand it against (#4487).
*/
@Test
void getServiceLastAccessedDetailsExpandsAWildcardActionToEveryService() {
String tag = suffix();
String userName = "laa-star-" + tag;
String arn = createUser(userName);
iam("PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "laa-star-policy-" + tag)
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"*\",\"Resource\":\"*\"}]}")
.when().post("/").then().statusCode(200);
// MaxItems is raised so one page can show the whole expansion.
String body = iam("GetServiceLastAccessedDetails").formParam("JobId", generateJobFor(arn))
.formParam("MaxItems", "1000")
.when().post("/").then()
.statusCode(200)
// Services the policy never names, reachable only through the wildcard.
.body(containsString("<ServiceNamespace>s3</ServiceNamespace>"))
.body(containsString("<ServiceNamespace>dynamodb</ServiceNamespace>"))
.body(containsString("<ServiceNamespace>cloudwatch</ServiceNamespace>"))
.extract().asString();
int reported = body.split("<ServiceNamespace>", -1).length - 1;
assertTrue(reported > 300, "expected a wildcard grant to reach every service, got " + reported);
// CloudWatch's credential scope must not leak in as though it were an IAM namespace.
assertTrue(!body.contains("<ServiceNamespace>monitoring</ServiceNamespace>"),
"monitoring is a credential scope, not an IAM namespace, in: " + body);
}
/** A globbed prefix names a set of namespaces, so it expands to the ones it matches. */
@Test
void getServiceLastAccessedDetailsExpandsAGlobbedServicePrefix() {
String tag = suffix();
String userName = "laa-glob-" + tag;
String arn = createUser(userName);
iam("PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "laa-glob-policy-" + tag)
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"dynamodb*:*\",\"Resource\":\"*\"}]}")
.when().post("/").then().statusCode(200);
iam("GetServiceLastAccessedDetails").formParam("JobId", generateJobFor(arn))
.formParam("MaxItems", "1000")
.when().post("/").then()
.statusCode(200)
.body(containsString("<ServiceNamespace>dynamodb</ServiceNamespace>"))
// The glob covers the dynamodb family only, not every service.
.body(not(containsString("<ServiceNamespace>s3</ServiceNamespace>")));
}
/** {@code Allow} with {@code NotAction} reaches every service the list does not carve out. */
@Test
void getServiceLastAccessedDetailsExpandsNotActionToEverythingNotCarvedOut() {
String tag = suffix();
String userName = "laa-notaction-expand-" + tag;
String arn = createUser(userName);
iam("PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "laa-notaction-expand-policy-" + tag)
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"NotAction\":\"iam:*\",\"Resource\":\"*\"}]}")
.when().post("/").then().statusCode(200);
// The whole expansion has to be in view for the carve-out to be the reason iam is absent.
iam("GetServiceLastAccessedDetails").formParam("JobId", generateJobFor(arn))
.formParam("MaxItems", "1000")
.when().post("/").then()
.statusCode(200)
.body(containsString("<ServiceNamespace>s3</ServiceNamespace>"))
// iam is carved out entirely, so it is the one service not reachable.
.body(not(containsString("<ServiceNamespace>iam</ServiceNamespace>")));
}
/**
* A namespace the policy names outright is reported whether or not the vendored catalog knows
* it, so a catalog behind an AWS launch still reports an explicitly named service.
*/
@Test
void getServiceLastAccessedDetailsKeepsALiterallyNamedNamespaceUnknownToTheCatalog() {
String tag = suffix();
String userName = "laa-unknown-ns-" + tag;
String arn = createUser(userName);
iam("PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "laa-unknown-ns-policy-" + tag)
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"somefutureservice:DoThing\",\"Resource\":\"*\"}]}")
.when().post("/").then().statusCode(200);
iam("GetServiceLastAccessedDetails").formParam("JobId", generateJobFor(arn))
.when().post("/").then()
.statusCode(200)
.body(containsString("<ServiceNamespace>somefutureservice</ServiceNamespace>"));
}
/** A managed-policy ARN reports on that policy's own document, not on any identity. */
@Test
void getServiceLastAccessedDetailsForAPolicyArnUsesThatPolicysDocument() {