mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): use AWS-managed policy documents (#3237)
* fix(iam): use AWS-managed policy documents * fix(iam): include managed policy documents in native images
This commit is contained in:
1 parent
8a122d2b8c
commit
379910b008
7 files changed
+120
-22
No files matched your search
+12
-8
@@ -35,6 +35,8 @@ class IamManagedPolicyAccountScopeTest {
|
||||
private static final String DEFAULT_ACCT = "000000000000";
|
||||
private static final String REQUEST_ACCT = "111111111111";
|
||||
private static final String OTHER_ACCT = "222222222222";
|
||||
private static final String CUSTOMER_DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Instance<RequestContext> requestContextFor(String accountId) {
|
||||
@@ -56,7 +58,7 @@ class IamManagedPolicyAccountScopeTest {
|
||||
String customerArn = "arn:aws:iam::" + DEFAULT_ACCT + ":policy/customer-policy";
|
||||
policies.putForAccount(DEFAULT_ACCT, customerArn,
|
||||
new IamPolicy("ANPACUSTOMER0001", "customer-policy", "/", customerArn,
|
||||
"customer", AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
"customer", CUSTOMER_DOCUMENT));
|
||||
|
||||
IamService service = new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
@@ -129,13 +131,13 @@ class IamManagedPolicyAccountScopeTest {
|
||||
String customerArn = "arn:aws:iam::" + REQUEST_ACCT + ":policy/app-policy";
|
||||
reqPolicies.putForAccount(REQUEST_ACCT, customerArn,
|
||||
new IamPolicy("ANPAAPP000000001", "app-policy", "/", customerArn,
|
||||
"app", AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
"app", CUSTOMER_DOCUMENT));
|
||||
|
||||
// Simulate the seed-time mirror: a managed policy copied into the default account store.
|
||||
String mirroredManagedArn = AwsManagedPolicies.ARN_PREFIX + "/AdministratorAccess";
|
||||
reqPolicies.putForAccount(DEFAULT_ACCT, mirroredManagedArn,
|
||||
new IamPolicy("ANPAADMIN0000001", "AdministratorAccess", "/", mirroredManagedArn,
|
||||
"admin", AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
"admin", CUSTOMER_DOCUMENT));
|
||||
|
||||
IamService reqService = new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
@@ -180,7 +182,7 @@ class IamManagedPolicyAccountScopeTest {
|
||||
String customerArn = "arn:aws:iam::" + REQUEST_ACCT + ":policy/app-policy";
|
||||
policies.putForAccount(REQUEST_ACCT, customerArn,
|
||||
new IamPolicy("ANPAAPP000000001", "app-policy", "/", customerArn,
|
||||
"app", AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
"app", CUSTOMER_DOCUMENT));
|
||||
|
||||
IamUser user = new IamUser("AIDAUSER00000001", "app-user", "/",
|
||||
"arn:aws:iam::" + REQUEST_ACCT + ":user/app-user");
|
||||
@@ -204,7 +206,7 @@ class IamManagedPolicyAccountScopeTest {
|
||||
// the attached managed policy's document for a non-default-account principal.
|
||||
CallerContext caller = service.resolvePrincipalContext(
|
||||
"arn:aws:iam::" + REQUEST_ACCT + ":user/app-user");
|
||||
assertTrue(caller.identityPolicies().contains(AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
assertTrue(caller.identityPolicies().contains(CUSTOMER_DOCUMENT));
|
||||
|
||||
// Control: the customer policy is genuinely account-scoped — invisible from another account.
|
||||
Instance<RequestContext> otherCtx = requestContextFor("222222222222");
|
||||
@@ -238,7 +240,7 @@ class IamManagedPolicyAccountScopeTest {
|
||||
String customerArn = "arn:aws:iam::" + REQUEST_ACCT + ":policy/group-policy";
|
||||
policies.putForAccount(REQUEST_ACCT, customerArn,
|
||||
new IamPolicy("ANPAGRP000000001", "group-policy", "/", customerArn,
|
||||
"grp", AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
"grp", CUSTOMER_DOCUMENT));
|
||||
|
||||
IamGroup group = new IamGroup("AGPAGROUP0000001", "app-group", "/",
|
||||
"arn:aws:iam::" + REQUEST_ACCT + ":group/app-group");
|
||||
@@ -291,8 +293,10 @@ class IamManagedPolicyAccountScopeTest {
|
||||
// the attached managed policy and the managed permissions boundary for a non-default account.
|
||||
CallerContext caller = service.resolvePrincipalContext(
|
||||
"arn:aws:iam::" + REQUEST_ACCT + ":role/task-role");
|
||||
assertTrue(caller.identityPolicies().contains(AwsManagedPolicies.PERMISSIVE_DOCUMENT));
|
||||
assertEquals(AwsManagedPolicies.PERMISSIVE_DOCUMENT, caller.boundaryPolicyDocument());
|
||||
assertTrue(caller.identityPolicies().stream()
|
||||
.anyMatch(document -> document.contains("logs:CreateLogGroup")));
|
||||
assertTrue(caller.boundaryPolicyDocument().contains("NotAction"));
|
||||
assertFalse(caller.boundaryPolicyDocument().contains("\"Action\":\"*\""));
|
||||
}
|
||||
/**
|
||||
* The alias is the only IAM entity keyed by a constant rather than a caller-supplied name, so
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamGroup;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
@@ -348,6 +350,74 @@ class IamServiceTest {
|
||||
assertEquals(doc, policy.getDefaultDocument());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsManagedReadOnlyPolicyAllowsReadsButDeniesWrites() {
|
||||
String arn = AwsManagedPolicies.ARN_PREFIX + "/AmazonS3ReadOnlyAccess";
|
||||
IamPolicy policy = iamService.getPolicy(arn);
|
||||
|
||||
assertEquals("AmazonS3ReadOnlyAccess", policy.getPolicyName());
|
||||
assertEquals("v1", 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"));
|
||||
assertEquals(IamPolicyEvaluator.Decision.DENY,
|
||||
new IamPolicyEvaluator(new com.fasterxml.jackson.databind.ObjectMapper())
|
||||
.evaluate(List.of(policy.getDefaultDocument()), "s3:PutObject", "arn:aws:s3:::bucket/key"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceScopedAndAdministratorManagedPoliciesUseTheirDocumentedScopes() {
|
||||
IamPolicy s3ReadOnly = iamService.getPolicy(
|
||||
AwsManagedPolicies.ARN_PREFIX + "/AmazonS3ReadOnlyAccess");
|
||||
IamPolicy administrator = iamService.getPolicy(
|
||||
AwsManagedPolicies.ARN_PREFIX + "/AdministratorAccess");
|
||||
IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
|
||||
|
||||
assertEquals(IamPolicyEvaluator.Decision.DENY,
|
||||
evaluator.evaluate(List.of(s3ReadOnly.getDefaultDocument()),
|
||||
"ec2:RunInstances", "*"));
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
evaluator.evaluate(List.of(administrator.getDefaultDocument()),
|
||||
"s3:PutObject", "arn:aws:s3:::bucket/key"));
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
evaluator.evaluate(List.of(administrator.getDefaultDocument()),
|
||||
"ec2:RunInstances", "*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void managedPolicyActsAsPermissionsBoundary() {
|
||||
IamUser user = iamService.createUser("boundary-user", "/");
|
||||
String adminArn = AwsManagedPolicies.ARN_PREFIX + "/AdministratorAccess";
|
||||
String boundaryArn = AwsManagedPolicies.ARN_PREFIX + "/AmazonS3ReadOnlyAccess";
|
||||
iamService.attachUserPolicy(user.getUserName(), adminArn);
|
||||
iamService.putUserPermissionsBoundary(user.getUserName(), boundaryArn);
|
||||
|
||||
CallerContext caller = iamService.resolvePrincipalContext(user.getArn());
|
||||
IamPolicyEvaluator evaluator = new IamPolicyEvaluator(new ObjectMapper());
|
||||
|
||||
assertEquals(IamPolicyEvaluator.Decision.ALLOW,
|
||||
evaluator.evaluate(caller, null, "s3:GetObject", "arn:aws:s3:::bucket/key", null));
|
||||
assertEquals(IamPolicyEvaluator.Decision.DENY,
|
||||
evaluator.evaluate(caller, null, "s3:PutObject", "arn:aws:s3:::bucket/key", null));
|
||||
assertEquals(IamPolicyEvaluator.Decision.DENY,
|
||||
evaluator.evaluate(caller, null, "ec2:RunInstances", "*", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void managedPolicyRetrievalPreservesFieldsAndUnavailableVersionFailsWithNoSuchEntity() {
|
||||
String arn = AwsManagedPolicies.ARN_PREFIX + "/AmazonS3ReadOnlyAccess";
|
||||
IamPolicy policy = iamService.getPolicy(arn);
|
||||
assertEquals("AmazonS3ReadOnlyAccess", policy.getPolicyName());
|
||||
assertEquals(arn, policy.getArn());
|
||||
assertEquals("v1", 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());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createPolicyVersionAndSetDefault() {
|
||||
String doc1 = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\"}]}";
|
||||
|
||||
Reference in new issue
Block a user