mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): report the real default version of AWS-managed policies (#3275)
Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
1 parent
460aa0718e
commit
abfa8e949e
10 files changed
+8082
-21
No files matched your search
+78
-1
@@ -4,13 +4,19 @@ 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 io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
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 java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@@ -19,7 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
* 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
|
||||
* <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
|
||||
@@ -97,6 +103,77 @@ class IamManagedPolicyCatalogTest {
|
||||
assertDoesNotThrow(() -> iamService.getPolicy("arn:aws:iam::aws:policy/AWSLambdaFullAccess"));
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS revises managed policies in place, so their default version reflects that history
|
||||
* rather than being {@code v1} for everything: {@code AmazonS3ReadOnlyAccess} is on
|
||||
* {@code v3} (last revised August 2023) while {@code AdministratorAccess} genuinely never
|
||||
* left {@code v1}. Reporting {@code v1} across the board only matched real AWS by accident.
|
||||
*/
|
||||
@Test
|
||||
void defaultVersionReflectsAwsRevisionHistory() {
|
||||
IamPolicy s3ReadOnly = iamService.getPolicy("arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess");
|
||||
IamPolicy administrator = iamService.getPolicy("arn:aws:iam::aws:policy/AdministratorAccess");
|
||||
|
||||
assertEquals("v3", s3ReadOnly.getDefaultVersionId());
|
||||
// A revised policy keeps its original creation date; only UpdateDate moves with v3.
|
||||
assertEquals(Instant.parse("2015-02-06T18:40:00Z"), s3ReadOnly.getCreateDate());
|
||||
assertEquals(Instant.parse("2023-08-10T21:31:39Z"), s3ReadOnly.getUpdateDate());
|
||||
assertEquals("v1", administrator.getDefaultVersionId());
|
||||
assertEquals(Instant.parse("2015-02-06T18:39:46Z"), administrator.getCreateDate());
|
||||
assertEquals(administrator.getCreateDate(), administrator.getUpdateDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listedVersionsCarryTheDefaultVersionIdAndItsDocument() {
|
||||
String arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess";
|
||||
IamPolicy policy = iamService.getPolicy(arn);
|
||||
|
||||
List<PolicyVersion> versions = iamService.listPolicyVersions(arn);
|
||||
assertEquals(1, versions.size(), "only the default version's document is bundled");
|
||||
PolicyVersion listed = versions.get(0);
|
||||
assertEquals("v3", listed.getVersionId());
|
||||
assertTrue(listed.isDefaultVersion());
|
||||
assertEquals(policy.getDefaultDocument(), listed.getDocument());
|
||||
assertEquals(policy.getUpdateDate(), listed.getCreateDate());
|
||||
|
||||
PolicyVersion fetched = iamService.getPolicyVersion(arn, "v3");
|
||||
assertEquals(listed.getDocument(), fetched.getDocument());
|
||||
assertTrue(fetched.isDefaultVersion());
|
||||
}
|
||||
|
||||
/**
|
||||
* The dataset only carries each policy's current document, so a superseded version cannot
|
||||
* be served. Answering {@code NoSuchEntity} matches what AWS returns for a version it has
|
||||
* pruned, and is preferable to serving the current document under an old id.
|
||||
*/
|
||||
@Test
|
||||
void supersededVersionOfARevisedPolicyIsNotServedUnderTheWrongId() {
|
||||
AwsException e = assertThrows(AwsException.class, () -> iamService.getPolicyVersion(
|
||||
"arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess", "v1"));
|
||||
|
||||
assertEquals("NoSuchEntity", e.getErrorCode());
|
||||
assertEquals(404, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyCatalogEntryHasAWellFormedVersionIdAndDates() {
|
||||
for (AwsManagedPolicies.ManagedPolicyDef def : AwsManagedPolicies.POLICIES) {
|
||||
assertTrue(def.defaultVersionId().matches("v[1-9][0-9]*"),
|
||||
def.name() + " has version id " + def.defaultVersionId());
|
||||
assertNotNull(def.updateDate(), def.name() + " has no version date");
|
||||
if (def.createDate() != null) {
|
||||
assertFalse(def.createDate().isAfter(def.updateDate()),
|
||||
def.name() + " was created after its current version");
|
||||
}
|
||||
}
|
||||
for (IamPolicy policy : iamService.listPolicies("AWS", null)) {
|
||||
assertFalse(policy.getCreateDate().isAfter(policy.getUpdateDate()),
|
||||
policy.getPolicyName() + " reports CreateDate after UpdateDate");
|
||||
}
|
||||
assertTrue(AwsManagedPolicies.POLICIES.stream().anyMatch(p -> !"v1".equals(p.defaultVersionId())),
|
||||
"the catalog should carry revised policies, not v1 everywhere");
|
||||
}
|
||||
|
||||
@Test
|
||||
void inventedManagedPolicyIsRejectedTheWayAwsRejectsIt() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
|
||||
@@ -356,7 +356,7 @@ class IamServiceTest {
|
||||
IamPolicy policy = iamService.getPolicy(arn);
|
||||
|
||||
assertEquals("AmazonS3ReadOnlyAccess", policy.getPolicyName());
|
||||
assertEquals("v1", policy.getDefaultVersionId());
|
||||
assertEquals("v3", policy.getDefaultVersionId());
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
new IamPolicyEvaluator(new com.fasterxml.jackson.databind.ObjectMapper())
|
||||
.evaluate(List.of(policy.getDefaultDocument()), "s3:GetObject", "arn:aws:s3:::bucket/key"));
|
||||
@@ -409,13 +409,21 @@ class IamServiceTest {
|
||||
IamPolicy policy = iamService.getPolicy(arn);
|
||||
assertEquals("AmazonS3ReadOnlyAccess", policy.getPolicyName());
|
||||
assertEquals(arn, policy.getArn());
|
||||
assertEquals("v1", policy.getDefaultVersionId());
|
||||
// AWS revised this policy twice; a real account reports v3, not v1.
|
||||
assertEquals("v3", policy.getDefaultVersionId());
|
||||
assertTrue(policy.getDefaultDocument().contains("s3:Get*"));
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> iamService.getPolicyVersion(arn, "v9"));
|
||||
assertEquals("NoSuchEntity", error.getErrorCode());
|
||||
assertEquals(404, error.getHttpStatus());
|
||||
PolicyVersion current = iamService.getPolicyVersion(arn, "v3");
|
||||
assertTrue(current.isDefaultVersion());
|
||||
assertEquals(policy.getDefaultDocument(), current.getDocument());
|
||||
|
||||
// Neither a future version nor a superseded one whose document is not bundled resolves.
|
||||
for (String versionId : new String[] {"v9", "v1"}) {
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> iamService.getPolicyVersion(arn, versionId));
|
||||
assertEquals("NoSuchEntity", error.getErrorCode());
|
||||
assertEquals(404, error.getHttpStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user