mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(iam): seed AWS managed policies at startup (#400)
* feat(iam): seed AWS managed policies at startup Seed 14 commonly-used AWS managed policies (e.g. AWSLambdaBasicExecutionRole, AdministratorAccess) into the IAM policy store on startup via @PostConstruct. This ensures that documented examples using attach-role-policy with standard AWS policy ARNs work out of the box. Also adds Scope filter support to ListPolicies (AWS/Local/All). Closes #329 * fix(iam): reject mutations on AWS managed policies and validate Scope param AWS managed policies (arn:aws:iam::aws:policy/...) are now immutable — deletePolicy, createPolicyVersion, deletePolicyVersion, setDefaultPolicyVersion, tagPolicy, and untagPolicy return AccessDenied (403) for these ARNs, matching real AWS behavior. Also adds Scope validation to listPolicies: unrecognized values now return ValidationError (400) instead of silently falling through to All.
This commit is contained in:
1 parent
682f05d898
commit
300bfa4356
4 files changed
+251
No files matched your search
@@ -0,0 +1,56 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Catalog of commonly-used AWS managed policies seeded at startup.
|
||||
* Policy documents use a permissive wildcard because floci does not
|
||||
* enforce IAM policy evaluation.
|
||||
*/
|
||||
final class AwsManagedPolicies {
|
||||
|
||||
static final String ARN_PREFIX = "arn:aws:iam::aws:policy";
|
||||
|
||||
static final String PERMISSIVE_DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":"
|
||||
+ "[{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}";
|
||||
|
||||
record ManagedPolicyDef(String name, String path, String description) {
|
||||
String arn() {
|
||||
return ARN_PREFIX + path + name;
|
||||
}
|
||||
}
|
||||
|
||||
static final List<ManagedPolicyDef> POLICIES = List.of(
|
||||
new ManagedPolicyDef("AdministratorAccess", "/",
|
||||
"Provides full access to AWS services and resources."),
|
||||
new ManagedPolicyDef("PowerUserAccess", "/",
|
||||
"Provides full access to AWS services and resources, but does not allow management of Users and groups."),
|
||||
new ManagedPolicyDef("ReadOnlyAccess", "/",
|
||||
"Provides read-only access to AWS services and resources."),
|
||||
new ManagedPolicyDef("IAMFullAccess", "/",
|
||||
"Provides full access to IAM."),
|
||||
new ManagedPolicyDef("AmazonS3FullAccess", "/",
|
||||
"Provides full access to all buckets via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonS3ReadOnlyAccess", "/",
|
||||
"Provides read-only access to all buckets via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonDynamoDBFullAccess", "/",
|
||||
"Provides full access to Amazon DynamoDB via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonEC2FullAccess", "/",
|
||||
"Provides full access to Amazon EC2 via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonSQSFullAccess", "/",
|
||||
"Provides full access to Amazon SQS via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonSNSFullAccess", "/",
|
||||
"Provides full access to Amazon SNS via the AWS Management Console."),
|
||||
new ManagedPolicyDef("AmazonVPCFullAccess", "/",
|
||||
"Provides full access to Amazon VPC via the AWS Management Console."),
|
||||
new ManagedPolicyDef("CloudWatchFullAccess", "/",
|
||||
"Provides full access to CloudWatch."),
|
||||
new ManagedPolicyDef("AWSLambdaBasicExecutionRole", "/service-role/",
|
||||
"Provides write permissions to CloudWatch Logs."),
|
||||
new ManagedPolicyDef("AWSLambdaFullAccess", "/",
|
||||
"Provides full access to Lambda, S3, DynamoDB, CloudWatch Metrics and Logs.")
|
||||
);
|
||||
|
||||
private AwsManagedPolicies() {}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -69,6 +70,25 @@ public class IamService {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void seedAwsManagedPolicies() {
|
||||
int seeded = 0;
|
||||
for (AwsManagedPolicies.ManagedPolicyDef def : AwsManagedPolicies.POLICIES) {
|
||||
String arn = def.arn();
|
||||
if (policies.get(arn).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);
|
||||
seeded++;
|
||||
}
|
||||
if (seeded > 0) {
|
||||
LOG.infov("Seeded {0} AWS managed policies", seeded);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Users
|
||||
// =========================================================================
|
||||
@@ -325,7 +345,15 @@ public class IamService {
|
||||
"Policy " + policyArn + " does not exist.", 404));
|
||||
}
|
||||
|
||||
private void rejectIfAwsManaged(String policyArn) {
|
||||
if (policyArn != null && policyArn.startsWith(AwsManagedPolicies.ARN_PREFIX)) {
|
||||
throw new AwsException("AccessDenied",
|
||||
"Cannot modify or delete AWS managed policy: " + policyArn, 403);
|
||||
}
|
||||
}
|
||||
|
||||
public void deletePolicy(String policyArn) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
if (policy.getAttachmentCount() > 0) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
@@ -336,13 +364,30 @@ public class IamService {
|
||||
}
|
||||
|
||||
public List<IamPolicy> listPolicies(String scope, String pathPrefix) {
|
||||
if (scope != null && !scope.isBlank()
|
||||
&& !"All".equalsIgnoreCase(scope)
|
||||
&& !"AWS".equalsIgnoreCase(scope)
|
||||
&& !"Local".equalsIgnoreCase(scope)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value '" + scope + "' at 'scope' failed to satisfy constraint: "
|
||||
+ "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();
|
||||
}
|
||||
|
||||
public PolicyVersion createPolicyVersion(String policyArn, String document, boolean setAsDefault) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
int nextVersionNum = policy.getVersions().size() + 1;
|
||||
if (nextVersionNum > 5) {
|
||||
@@ -372,6 +417,7 @@ public class IamService {
|
||||
}
|
||||
|
||||
public void deletePolicyVersion(String policyArn, String versionId) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
if (versionId.equals(policy.getDefaultVersionId())) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
@@ -390,6 +436,7 @@ public class IamService {
|
||||
}
|
||||
|
||||
public void setDefaultPolicyVersion(String policyArn, String versionId) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
if (!policy.getVersions().containsKey(versionId)) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
@@ -402,12 +449,14 @@ public class IamService {
|
||||
}
|
||||
|
||||
public void tagPolicy(String policyArn, Map<String, String> newTags) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
policy.getTags().putAll(newTags);
|
||||
policies.put(policyArn, policy);
|
||||
}
|
||||
|
||||
public void untagPolicy(String policyArn, List<String> tagKeys) {
|
||||
rejectIfAwsManaged(policyArn);
|
||||
IamPolicy policy = getPolicy(policyArn);
|
||||
tagKeys.forEach(policy.getTags()::remove);
|
||||
policies.put(policyArn, policy);
|
||||
|
||||
Reference in new issue
Block a user