fix(iam): seed the full AWS managed policy catalog (#2194)

* fix(iam): seed the full AWS managed policy catalog

AttachRolePolicy and GetPolicy resolved arn:aws:iam::aws:policy/* against a
hand-maintained list of 59 entries, so any policy outside it returned
NoSuchEntity even though AWS publishes it. Real Terraform and CloudFormation
configurations failed for a reason unrelated to the user's setup — attaching
AmazonEC2ReadOnlyAccess or service-role/AmazonElasticMapReduceRole was enough
to break a stack — and the list could only be extended one policy per PR.

The catalog now carries the full published list (1566 entries), loaded from
iam/managed-policies.yaml following the existing ec2/image-catalog.yaml
pattern. AwsManagedPolicies keeps its POLICIES and ManagedPolicyDef surface,
so callers and tests are unaffected.

Unknown ARNs still return NoSuchEntity/404. That matters as much as the
additions: resolving every well-formed ARN would accept typos that real AWS
rejects and would stop CloudFormation rolling a stack back on a bad
attachment. Seeding the real catalog is what makes the check faithful in both
directions rather than trading one wrong answer for another.

The catalog is a union with the previously seeded entries, so a policy AWS has
since retired but which Floci already resolved — AWSLambdaFullAccess — keeps
working. Curated descriptions are preserved.

seedAwsManagedPolicies no longer mirrors the catalog into the account store.
That copy was never read back, since listPolicies filters AWS-managed ARNs out
of the local scan and serves them from the catalog, and writing 1566 entries on
every start cost ~1.4 MB of persisted state and ~3.7s of persistent-mode
startup.

Policy names and paths derive from the public AWS managed policy list via
https://github.com/iann0036/iam-dataset (MIT); only names and paths are used.
Regenerate against a real account with `aws iam list-policies --scope AWS`.

* fix(iam): register the managed-policy catalog for native image builds

AwsManagedPolicies reads iam/managed-policies.yaml through
ClassLoader#getResourceAsStream, which Quarkus's static resource analysis
cannot see. Without an explicit registration the file is absent from the
native image, the stream comes back null and IAM aborts startup — taking the
whole application with it.

Registered in both places the EC2 image catalog already uses:
quarkus.native.resources.includes in application.yml, and the resources
pattern in META-INF/native-image/resource-config.json.

Verified against the native binary rather than the build alone, since a
successful build does not prove the resource is embedded: the native image
starts, ListPolicies(Scope=AWS) reports all 1566 entries, previously missing
policies attach, and a misspelled ARN still returns NoSuchEntity.

Reported by Greptile on #2194.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-08-12 17:49:57 -05:00
1 parent cdb2c36dfe
commit 527615bdf0
8 files changed
+3450 -172

No files matched your search

@@ -0,0 +1,128 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.iam.model.IamPolicy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* The managed-policy catalog carries the full published AWS list rather than a curated
* subset, so that attaching a real policy succeeds and attaching an invented one still
* fails the way it does on AWS.
*
* <p>A subset produced false negatives — {@code AttachRolePolicy} returned
* {@code NoSuchEntity} for policies AWS genuinely publishes, breaking valid Terraform and
* CloudFormation configurations. Resolving every well-formed ARN instead would produce
* false positives, silently accepting typos that real AWS rejects and defeating stack
* rollback. Both directions matter, so both are asserted here.
*/
class IamManagedPolicyCatalogTest {
private IamService iamService;
@BeforeEach
void setUp() {
iamService = new IamService(
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(),
new RegionResolver("us-east-1", "000000000000"));
}
@Test
void catalogCarriesTheFullPublishedListNotAHandfulOfEntries() {
assertTrue(AwsManagedPolicies.POLICIES.size() > 1000,
"expected the full AWS catalog, found " + AwsManagedPolicies.POLICIES.size());
}
/** Policies a real ECS/EMR estate attaches; every one of these used to 404. */
@ParameterizedTest
@ValueSource(strings = {
"arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess",
"arn:aws:iam::aws:policy/AmazonSSMFullAccess",
"arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM",
"arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole",
"arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role",
"arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforAutoScalingRole",
"arn:aws:iam::aws:policy/service-role/AmazonEMRServicePolicy_v2",
})
void realWorldPoliciesResolveAndAttach(String policyArn) {
IamPolicy policy = assertDoesNotThrow(() -> iamService.getPolicy(policyArn));
assertEquals(policyArn, policy.getArn());
iamService.createRole("attach-target", "/", "{}", null, 3600, null);
assertDoesNotThrow(() -> iamService.attachRolePolicy("attach-target", policyArn));
}
@Test
void everyPathPrefixAwsUsesIsRepresented() {
for (String path : new String[] {"/", "/service-role/", "/aws-service-role/", "/job-function/"}) {
assertTrue(AwsManagedPolicies.POLICIES.stream().anyMatch(p -> path.equals(p.path())),
"no policy carries the " + path + " path");
}
}
@Test
void arnsAreBuiltFromNameAndPath() {
AwsManagedPolicies.ManagedPolicyDef def = AwsManagedPolicies.POLICIES.stream()
.filter(p -> "AmazonEC2RoleforSSM".equals(p.name()))
.findFirst().orElseThrow();
assertEquals("/service-role/", def.path());
assertEquals("arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforSSM", def.arn());
}
@Test
void curatedDescriptionsSurviveTheBulkCatalog() {
IamPolicy policy = iamService.getPolicy("arn:aws:iam::aws:policy/AdministratorAccess");
assertEquals("Provides full access to AWS services and resources.", policy.getDescription());
}
/**
* A policy AWS has since retired but which Floci previously resolved: keeping it means
* upgrading the catalog never takes away something that already worked.
*/
@Test
void policiesRetiredByAwsButPreviouslyResolvableAreRetained() {
assertDoesNotThrow(() -> iamService.getPolicy("arn:aws:iam::aws:policy/AWSLambdaFullAccess"));
}
@Test
void inventedManagedPolicyIsRejectedTheWayAwsRejectsIt() {
AwsException e = assertThrows(AwsException.class,
() -> iamService.getPolicy("arn:aws:iam::aws:policy/DefinitelyNotARealPolicy"));
assertEquals("NoSuchEntity", e.getErrorCode());
assertEquals(404, e.getHttpStatus(),
"AWS answers GetPolicy for a nonexistent policy with NoSuchEntity/404");
}
@Test
void attachingAnInventedManagedPolicyFails() {
iamService.createRole("typo-role", "/", "{}", null, 3600, null);
AwsException e = assertThrows(AwsException.class, () -> iamService.attachRolePolicy(
"typo-role", "arn:aws:iam::aws:policy/AmazonS3FullAcess"));
assertEquals("NoSuchEntity", e.getErrorCode(),
"a typo must still fail, otherwise CloudFormation never rolls back");
}
@Test
void unknownCustomerPolicyStillReturnsNoSuchEntity() {
AwsException e = assertThrows(AwsException.class,
() -> iamService.getPolicy("arn:aws:iam::000000000000:policy/nope"));
assertEquals("NoSuchEntity", e.getErrorCode());
}
}