mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(iam): resolve AWS-managed policies from any account context (#1573)
* fix(iam): resolve AWS-managed policies from any account context AWS-managed policies (arn:aws:iam::aws:policy/...) are global — not owned by any account — but they were seeded into the account-partitioned policy store at startup under the default account only. A caller in any other account (e.g. an assumed-role / 12-digit access-key credential) got NoSuchEntity from GetPolicy, breaking Terraform's `data "aws_iam_policy"` lookups of managed policies such as AWSLambdaBasicExecutionRole and AmazonECSTaskExecutionRolePolicy. Serve managed policies from a global in-memory catalog keyed by ARN, built once from AwsManagedPolicies, so getPolicy (and the version/tag reads that route through it) resolve them for any caller regardless of account. The catalog is still mirrored into the default-account store so ListPolicies(Scope=AWS) keeps listing them. Also add three commonly-attached managed policies: AWSCloudFormationFullAccess, AWSXRayDaemonWriteAccess, AmazonElasticFileSystemClientFullAccess. * fix(iam): list AWS-managed policies from the global catalog for any account ListPolicies(Scope=AWS) and Scope=All scanned only the request account's partition, so AWS-managed policies (mirrored into the default account at seed time) were invisible to callers in any other account — the same account-scoping bug already fixed for GetPolicy in this PR. It also broke name lookup, since data.aws_iam_policy resolves through ListPolicies plus a name filter. Serve managed policies from the global awsManagedPolicies catalog for the AWS and All scopes, and exclude managed ARNs from the account scan so the default account does not list them twice. GetPolicy and ListPolicies now line up.
This commit is contained in:
1 parent
4a53acf0b3
commit
eea1e70d93
3 files changed
+218
-18
No files matched your search
@@ -53,6 +53,12 @@ final class AwsManagedPolicies {
|
||||
"Provides permissions required to use the CloudWatch agent on servers."),
|
||||
new ManagedPolicyDef("AWSLambdaFullAccess", "/",
|
||||
"Provides full access to Lambda, S3, DynamoDB, CloudWatch Metrics and Logs."),
|
||||
new ManagedPolicyDef("AWSCloudFormationFullAccess", "/",
|
||||
"Provides full access to AWS CloudFormation."),
|
||||
new ManagedPolicyDef("AWSXRayDaemonWriteAccess", "/",
|
||||
"Allows write permissions to the AWS X-Ray daemon."),
|
||||
new ManagedPolicyDef("AmazonElasticFileSystemClientFullAccess", "/",
|
||||
"Provides root client access to an Amazon EFS file system."),
|
||||
|
||||
// Lambda execution role policies
|
||||
new ManagedPolicyDef("AWSLambdaBasicExecutionRole", "/service-role/",
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -51,6 +52,13 @@ public class IamService {
|
||||
private final RegionResolver regionResolver;
|
||||
private final boolean seedDeployerPrincipal;
|
||||
|
||||
/**
|
||||
* AWS-managed policies (arn:aws:iam::aws:policy/...), keyed by ARN. These are global —
|
||||
* not owned by any account — so they live here rather than in the account-partitioned
|
||||
* {@link #policies} store, and {@link #getPolicy} resolves them for any caller.
|
||||
*/
|
||||
private final Map<String, IamPolicy> awsManagedPolicies = buildAwsManagedPolicies();
|
||||
|
||||
@Inject
|
||||
public IamService(StorageFactory storageFactory, EmulatorConfig config, RegionResolver regionResolver) {
|
||||
this(
|
||||
@@ -105,17 +113,25 @@ public class IamService {
|
||||
}
|
||||
}
|
||||
|
||||
void seedAwsManagedPolicies() {
|
||||
int seeded = 0;
|
||||
private static Map<String, IamPolicy> buildAwsManagedPolicies() {
|
||||
Map<String, IamPolicy> catalog = new LinkedHashMap<>();
|
||||
for (AwsManagedPolicies.ManagedPolicyDef def : AwsManagedPolicies.POLICIES) {
|
||||
String arn = def.arn();
|
||||
if (policies.get(arn).isPresent()) {
|
||||
catalog.put(arn, new IamPolicy("ANPA" + randomId(16), def.name(), def.path(), arn,
|
||||
def.description(), AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
}
|
||||
return catalog;
|
||||
}
|
||||
|
||||
void seedAwsManagedPolicies() {
|
||||
// Managed policies are served globally from the catalog (see getPolicy); this also
|
||||
// mirrors them into the default-account store so ListPolicies(Scope=AWS) lists them.
|
||||
int seeded = 0;
|
||||
for (Map.Entry<String, IamPolicy> entry : awsManagedPolicies.entrySet()) {
|
||||
if (policies.get(entry.getKey()).isPresent()) {
|
||||
continue;
|
||||
}
|
||||
String policyId = "ANPA" + randomId(16);
|
||||
IamPolicy policy = new IamPolicy(policyId, def.name(), def.path(), arn,
|
||||
def.description(), AwsManagedPolicies.PERMISSIVE_DOCUMENT);
|
||||
policies.put(arn, policy);
|
||||
policies.put(entry.getKey(), entry.getValue());
|
||||
seeded++;
|
||||
}
|
||||
if (seeded > 0) {
|
||||
@@ -398,6 +414,16 @@ public class IamService {
|
||||
}
|
||||
|
||||
public IamPolicy getPolicy(String policyArn) {
|
||||
// AWS-managed policies (arn:aws:iam::aws:policy/...) are global — not owned by any
|
||||
// account — so they are served from the catalog rather than the account-partitioned
|
||||
// store, which would otherwise make them visible only to the default account.
|
||||
if (policyArn != null && policyArn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
|
||||
IamPolicy managed = awsManagedPolicies.get(policyArn);
|
||||
if (managed != null) {
|
||||
return managed;
|
||||
}
|
||||
throw new AwsException("NoSuchEntity", "Policy " + policyArn + " does not exist.", 404);
|
||||
}
|
||||
return policies.get(policyArn)
|
||||
.orElseThrow(() -> new AwsException("NoSuchEntity",
|
||||
"Policy " + policyArn + " does not exist.", 404));
|
||||
@@ -431,17 +457,29 @@ public class IamService {
|
||||
+ "Member must satisfy enum value set: [All, AWS, Local]", 400);
|
||||
}
|
||||
String prefix = pathPrefix != null ? pathPrefix : "/";
|
||||
return policies.scan(k -> true).stream()
|
||||
.filter(p -> p.getPath().startsWith(prefix))
|
||||
.filter(p -> {
|
||||
if ("AWS".equalsIgnoreCase(scope)) {
|
||||
return p.getArn().startsWith(AwsManagedPolicies.ARN_PREFIX);
|
||||
} else if ("Local".equalsIgnoreCase(scope)) {
|
||||
return !p.getArn().startsWith(AwsManagedPolicies.ARN_PREFIX);
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.toList();
|
||||
boolean blankScope = scope == null || scope.isBlank();
|
||||
boolean includeAws = blankScope || "All".equalsIgnoreCase(scope) || "AWS".equalsIgnoreCase(scope);
|
||||
boolean includeLocal = blankScope || "All".equalsIgnoreCase(scope) || "Local".equalsIgnoreCase(scope);
|
||||
|
||||
List<IamPolicy> result = new ArrayList<>();
|
||||
if (includeLocal) {
|
||||
// Customer-managed policies live in the account-partitioned store. Exclude any
|
||||
// AWS-managed ARNs mirrored into the default account at seed time — those are
|
||||
// served from the global catalog below so the default account does not see them twice.
|
||||
policies.scan(k -> true).stream()
|
||||
.filter(p -> !p.getArn().startsWith(AwsManagedPolicies.ARN_PREFIX))
|
||||
.filter(p -> p.getPath().startsWith(prefix))
|
||||
.forEach(result::add);
|
||||
}
|
||||
if (includeAws) {
|
||||
// AWS-managed policies are global (account-less arn:aws:iam::aws:policy/... ARN), so
|
||||
// every caller sees the full set regardless of the request account — mirroring the
|
||||
// getPolicy fix, and keeping the ListPolicies and GetPolicy read paths consistent.
|
||||
awsManagedPolicies.values().stream()
|
||||
.filter(p -> p.getPath().startsWith(prefix))
|
||||
.forEach(result::add);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public PolicyVersion createPolicyVersion(String policyArn, String document, boolean setAsDefault) {
|
||||
|
||||
Reference in new issue
Block a user