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:
Matheus André authored and GitHub committed 2026-09-09 22:55:17 -05:00
1 parent 460aa0718e
commit abfa8e949e
10 files changed
+8082 -21

No files matched your search

@@ -10,15 +10,18 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* Catalog of AWS managed policies, loaded from {@code iam/managed-policies.yaml}.
*
* <p>Floci resolves {@code arn:aws:iam::aws:policy/*} ARNs against this catalog, so an ARN
* that is absent returns {@code NoSuchEntity} — the same as real AWS. Carrying the full
* that is absent returns {@code NoSuchEntity}, the same as real AWS. Carrying the full
* published list is what keeps that faithful in both directions: policies AWS actually
* publishes attach cleanly, while typos and invented names are still rejected. A curated
* subset would reject valid configurations; resolving every well-formed ARN would accept
@@ -27,6 +30,18 @@ import java.util.Map;
* <p>Policy documents are loaded from the versioned data generated from the public AWS managed
* policy dataset. Entries without a document are omitted so an unavailable policy fails closed
* through the normal {@code NoSuchEntity} path rather than receiving broader permissions.
*
* <p>Each document is the policy's <em>current default version</em>, and the version id it is
* served under comes from {@code iam/managed-policy-versions.json} rather than being assumed to
* be {@code v1}: AWS revises its managed policies in place, so {@code AmazonS3ReadOnlyAccess}
* is on {@code v3} while {@code AdministratorAccess} is still {@code v1}. That file also carries
* the policy's own creation date and the date of its current version, which back the
* {@code CreateDate} and {@code UpdateDate} of {@code GetPolicy} respectively. Only the default
* version's document is bundled, since the dataset does not carry superseded documents, so an
* older version id is reported as {@code NoSuchEntity}, as AWS itself does once it prunes a
* managed policy's history down to five versions. A policy missing from the versions file
* keeps the {@code v1} default so a partial refresh degrades to the old behaviour instead of
* dropping the policy.
*/
final class AwsManagedPolicies {
@@ -36,8 +51,22 @@ final class AwsManagedPolicies {
private static final String CATALOG_RESOURCE_NAME = "iam/managed-policies.yaml";
private static final String DOCUMENTS_RESOURCE_NAME = "iam/managed-policy-documents.json";
private static final String VERSIONS_RESOURCE_NAME = "iam/managed-policy-versions.json";
record ManagedPolicyDef(String name, String path, String description, String document) {
private static final String FALLBACK_VERSION_ID = "v1";
private static final Pattern VERSION_ID = Pattern.compile("v[1-9][0-9]*");
/**
* One resolvable AWS managed policy.
*
* @param document the document of the policy's current default version
* @param defaultVersionId the version id AWS reports for that document, e.g. {@code v3}
* @param createDate when AWS first published the policy, or {@code null} if unknown
* @param updateDate when AWS published the current default version, or {@code null}
* if unknown
*/
record ManagedPolicyDef(String name, String path, String description, String document,
String defaultVersionId, Instant createDate, Instant updateDate) {
String arn() {
return ARN_PREFIX + path + name;
}
@@ -58,6 +87,7 @@ final class AwsManagedPolicies {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Catalog catalog = mapper.readValue(in, Catalog.class);
Map<String, JsonNode> documents = loadDocuments();
Map<String, VersionEntry> versions = loadVersions();
List<ManagedPolicyDef> defs = new ArrayList<>();
for (CatalogEntry entry : catalog.policies == null ? List.<CatalogEntry>of() : catalog.policies) {
if (entry.name == null || entry.name.isBlank() || entry.path == null || entry.path.isBlank()) {
@@ -69,10 +99,14 @@ final class AwsManagedPolicies {
entry.name);
continue;
}
defs.add(new ManagedPolicyDef(entry.name, entry.path, entry.description, document.toString()));
VersionEntry version = versions.get(entry.name);
defs.add(new ManagedPolicyDef(entry.name, entry.path, entry.description, document.toString(),
defaultVersionId(entry.name, version),
parseDate(entry.name, "createDate", version == null ? null : version.createDate),
parseDate(entry.name, "updateDate", version == null ? null : version.updateDate)));
}
LOG.debugv("Loaded {0} AWS managed policies from {1} and {2}",
defs.size(), CATALOG_RESOURCE_NAME, DOCUMENTS_RESOURCE_NAME);
LOG.debugv("Loaded {0} AWS managed policies from {1}, {2} and {3}",
defs.size(), CATALOG_RESOURCE_NAME, DOCUMENTS_RESOURCE_NAME, VERSIONS_RESOURCE_NAME);
return List.copyOf(defs);
} catch (IOException e) {
throw new UncheckedIOException(
@@ -80,6 +114,32 @@ final class AwsManagedPolicies {
}
}
private static String defaultVersionId(String name, VersionEntry version) {
if (version == null || version.defaultVersionId == null || version.defaultVersionId.isBlank()) {
LOG.debugv("No AWS managed policy version recorded for {0}; reporting it as {1}",
name, FALLBACK_VERSION_ID);
return FALLBACK_VERSION_ID;
}
if (!VERSION_ID.matcher(version.defaultVersionId).matches()) {
LOG.warnv("Ignoring malformed AWS managed policy version {0} for {1}; reporting it as {2}",
version.defaultVersionId, name, FALLBACK_VERSION_ID);
return FALLBACK_VERSION_ID;
}
return version.defaultVersionId;
}
private static Instant parseDate(String name, String field, String value) {
if (value == null || value.isBlank()) {
return null;
}
try {
return Instant.parse(value);
} catch (DateTimeParseException e) {
LOG.warnv("Ignoring malformed AWS managed policy {0} {1} for {2}", field, value, name);
return null;
}
}
private static Map<String, JsonNode> loadDocuments() throws IOException {
try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(DOCUMENTS_RESOURCE_NAME)) {
@@ -93,6 +153,19 @@ final class AwsManagedPolicies {
}
}
private static Map<String, VersionEntry> loadVersions() throws IOException {
try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(VERSIONS_RESOURCE_NAME)) {
if (in == null) {
throw new IllegalStateException(
"AWS managed policy versions not found on the classpath: " + VERSIONS_RESOURCE_NAME);
}
ObjectMapper jsonMapper = new ObjectMapper();
return jsonMapper.readValue(in, jsonMapper.getTypeFactory()
.constructMapType(Map.class, String.class, VersionEntry.class));
}
}
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
static final class Catalog {
@@ -106,4 +179,15 @@ final class AwsManagedPolicies {
public String path;
public String description;
}
/** The version metadata AWS publishes for one policy. */
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
static final class VersionEntry {
public String defaultVersionId;
/** ISO-8601 instant at which AWS created the policy. */
public String createDate;
/** ISO-8601 instant at which AWS created the current default version. */
public String updateDate;
}
}
@@ -276,8 +276,17 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
Map<String, IamPolicy> catalog = new LinkedHashMap<>();
for (AwsManagedPolicies.ManagedPolicyDef def : AwsManagedPolicies.POLICIES) {
String arn = def.arn();
// The bundled document is the policy's current default version, served under the
// version id AWS actually reports for it (v3 for AmazonS3ReadOnlyAccess, v1 for
// AdministratorAccess) so GetPolicy/ListPolicyVersions match a real account.
// Superseded versions are not bundled, so they resolve to NoSuchEntity.
Instant now = Instant.now();
Instant updateDate = def.updateDate() != null ? def.updateDate() : now;
Instant createDate = def.createDate() != null ? def.createDate() : updateDate;
PolicyVersion defaultVersion = new PolicyVersion(
def.defaultVersionId(), def.document(), true, updateDate);
catalog.put(arn, new IamPolicy("ANPA" + randomId(16), def.name(), def.path(), arn,
def.description(), def.document()));
def.description(), defaultVersion, createDate, updateDate));
}
return catalog;
}
@@ -40,16 +40,36 @@ public class IamPolicy {
public IamPolicy(String policyId, String policyName, String path, String arn,
String description, String document) {
this(policyId, policyName, path, arn, description, new PolicyVersion("v1", document, true));
}
private IamPolicy(String policyId, String policyName, String path, String arn,
String description, PolicyVersion v1) {
this(policyId, policyName, path, arn, description, v1, v1.getCreateDate(), v1.getCreateDate());
}
/**
* Creates a policy whose single stored version is {@code defaultVersion}, which need not
* be {@code v1}. AWS-managed policies are bundled at whatever version AWS has revised them
* to (for example {@code v3}), with only that version's document available, so the version
* counter starts just past it. {@code createDate} is when the policy itself was first
* published and {@code updateDate} when its current default version was, the two dates
* {@code GetPolicy} reports separately.
*/
public IamPolicy(String policyId, String policyName, String path, String arn,
String description, PolicyVersion defaultVersion,
Instant createDate, Instant updateDate) {
this.policyId = policyId;
this.policyName = policyName;
this.path = path;
this.arn = arn;
this.description = description;
this.createDate = Instant.now();
this.updateDate = Instant.now();
PolicyVersion v1 = new PolicyVersion("v1", document, true);
this.versions.put("v1", v1);
this.nextVersionNumber = 2;
this.createDate = createDate;
this.updateDate = updateDate;
defaultVersion.setDefaultVersion(true);
this.defaultVersionId = defaultVersion.getVersionId();
this.versions.put(defaultVersion.getVersionId(), defaultVersion);
this.nextVersionNumber = Integer.parseInt(defaultVersion.getVersionId().substring(1)) + 1;
}
public String getDefaultDocument() {
@@ -17,10 +17,14 @@ public class PolicyVersion {
public PolicyVersion() {}
public PolicyVersion(String versionId, String document, boolean defaultVersion) {
this(versionId, document, defaultVersion, Instant.now());
}
public PolicyVersion(String versionId, String document, boolean defaultVersion, Instant createDate) {
this.versionId = versionId;
this.document = document;
this.defaultVersion = defaultVersion;
this.createDate = Instant.now();
this.createDate = createDate;
}
public String getVersionId() { return versionId; }