mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
Add IAM provisioning preflight support (#1250)
* fix(iam): seed provisioning managed policies * feat(iam): support policy simulation preflight * fix(iam): address provisioning preflight review feedback * fix(iam): normalize condition context in evaluator
This commit is contained in:
1 parent
2d958b380f
commit
3acab2bf27
10 files changed
+508
-15
No files matched your search
+17
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -119,6 +120,22 @@ class IamEnforcementIntegrationTest {
|
||||
evaluator.evaluate(List.of("not-json"), "s3:GetObject", "*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void conditionContextKeysAreCaseInsensitive() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:GetObject","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:SourceIp":"127.0.0.1"}}}
|
||||
]}""";
|
||||
|
||||
assertEquals(Decision.ALLOW,
|
||||
evaluator.simulateCustomPolicy(
|
||||
List.of(policy),
|
||||
"s3:GetObject",
|
||||
"arn:aws:s3:::bucket/key",
|
||||
Map.of("AWS:SourceIP", "127.0.0.1")));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// IamPolicyEvaluator.globMatches — unit tests
|
||||
// =========================================================================
|
||||
|
||||
@@ -29,6 +29,10 @@ class IamIntegrationTest {
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
|
||||
|
||||
private static final String EXPLICIT_DENY_POLICY_DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Deny\","
|
||||
+ "\"Action\":\"ec2:RunInstances\",\"Resource\":\"*\"}]}";
|
||||
|
||||
private static String createdPolicyArn;
|
||||
|
||||
// =========================================================================
|
||||
@@ -154,7 +158,7 @@ class IamIntegrationTest {
|
||||
// The standard EKS managed policies the EKS console/SDK and the
|
||||
// terraform-aws-modules/eks module attach to cluster and node roles (#1092).
|
||||
@ParameterizedTest
|
||||
@Order(6)
|
||||
@Order(15)
|
||||
@ValueSource(strings = {
|
||||
"arn:aws:iam::aws:policy/AmazonEKSClusterPolicy",
|
||||
"arn:aws:iam::aws:policy/AmazonEKSServicePolicy",
|
||||
@@ -177,6 +181,163 @@ class IamIntegrationTest {
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.Arn", equalTo(arn));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void getSsmManagedInstanceCorePolicy() {
|
||||
given()
|
||||
.formParam("Action", "GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.PolicyName",
|
||||
equalTo("AmazonSSMManagedInstanceCore"))
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.Arn",
|
||||
equalTo("arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void getCloudWatchAgentServerPolicy() {
|
||||
given()
|
||||
.formParam("Action", "GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.PolicyName",
|
||||
equalTo("CloudWatchAgentServerPolicy"))
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.Arn",
|
||||
equalTo("arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void getEcrReadOnlyPolicy() {
|
||||
given()
|
||||
.formParam("Action", "GetPolicy")
|
||||
.formParam("PolicyArn", "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.PolicyName",
|
||||
equalTo("AmazonEC2ContainerRegistryReadOnly"))
|
||||
.body("GetPolicyResponse.GetPolicyResult.Policy.Arn",
|
||||
equalTo("arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void stsGetCallerIdentityHonoursSeededFlociAccessKey() {
|
||||
given()
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=floci/20260227/us-east-1/sts/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Account", equalTo("000000000000"))
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn",
|
||||
equalTo("arn:aws:iam::000000000000:user/floci-deployer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(34)
|
||||
void simulatePrincipalPolicyEvaluatesIdentityPolicies() {
|
||||
given()
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserName", "simulate-user")
|
||||
.formParam("Path", "/")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String policyArn = given()
|
||||
.formParam("Action", "CreatePolicy")
|
||||
.formParam("PolicyName", "SimulatePolicy")
|
||||
.formParam("Path", "/")
|
||||
.formParam("PolicyDocument", POLICY_DOCUMENT)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
|
||||
String denyPolicyArn = given()
|
||||
.formParam("Action", "CreatePolicy")
|
||||
.formParam("PolicyName", "SimulateExplicitDenyPolicy")
|
||||
.formParam("Path", "/")
|
||||
.formParam("PolicyDocument", EXPLICIT_DENY_POLICY_DOCUMENT)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
|
||||
given()
|
||||
.formParam("Action", "AttachUserPolicy")
|
||||
.formParam("UserName", "simulate-user")
|
||||
.formParam("PolicyArn", policyArn)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "AttachUserPolicy")
|
||||
.formParam("UserName", "simulate-user")
|
||||
.formParam("PolicyArn", denyPolicyArn)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "SimulatePrincipalPolicy")
|
||||
.formParam("PolicySourceArn", "arn:aws:iam::000000000000:user/simulate-user")
|
||||
.formParam("ActionNames.member.1", "s3:GetObject")
|
||||
.formParam("ActionNames.member.2", "ec2:RunInstances")
|
||||
.formParam("ActionNames.member.3", "ssm:GetParameter")
|
||||
.formParam("ResourceArns.member.1", "*")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("SimulatePrincipalPolicyResponse.SimulatePrincipalPolicyResult.EvaluationResults.member.find { it.EvalActionName == 's3:GetObject' }.EvalDecision",
|
||||
equalTo("allowed"))
|
||||
.body("SimulatePrincipalPolicyResponse.SimulatePrincipalPolicyResult.EvaluationResults.member.find { it.EvalActionName == 'ec2:RunInstances' }.EvalDecision",
|
||||
equalTo("explicitDeny"))
|
||||
.body("SimulatePrincipalPolicyResponse.SimulatePrincipalPolicyResult.EvaluationResults.member.find { it.EvalActionName == 'ssm:GetParameter' }.EvalDecision",
|
||||
equalTo("implicitDeny"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(35)
|
||||
void attachManagedPolicyToRole() {
|
||||
@@ -270,7 +431,7 @@ class IamIntegrationTest {
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ListUsersResponse.ListUsersResult.Users.member.UserName",
|
||||
equalTo("test-user"));
|
||||
hasItem("test-user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -481,6 +481,18 @@ class IamServiceTest {
|
||||
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole");
|
||||
assertEquals("AWSLambdaBasicExecutionRole", lambda.getPolicyName());
|
||||
assertEquals("/service-role/", lambda.getPath());
|
||||
|
||||
IamPolicy ssm = iamService.getPolicy("arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore");
|
||||
assertEquals("AmazonSSMManagedInstanceCore", ssm.getPolicyName());
|
||||
assertEquals("/", ssm.getPath());
|
||||
|
||||
IamPolicy cloudWatchAgent = iamService.getPolicy("arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy");
|
||||
assertEquals("CloudWatchAgentServerPolicy", cloudWatchAgent.getPolicyName());
|
||||
assertEquals("/", cloudWatchAgent.getPath());
|
||||
|
||||
IamPolicy ecrReadOnly = iamService.getPolicy("arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly");
|
||||
assertEquals("AmazonEC2ContainerRegistryReadOnly", ecrReadOnly.getPolicyName());
|
||||
assertEquals("/", ecrReadOnly.getPath());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -494,6 +506,29 @@ class IamServiceTest {
|
||||
assertEquals(firstId, secondId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void seedDefaultsCreatesDefaultDeployerPrincipal() {
|
||||
iamService.seedDefaults();
|
||||
|
||||
IamUser user = iamService.getUser("floci-deployer");
|
||||
assertEquals("arn:aws:iam::000000000000:user/floci-deployer", user.getArn());
|
||||
assertTrue(user.getAttachedPolicyArns().contains("arn:aws:iam::aws:policy/AdministratorAccess"));
|
||||
assertEquals(
|
||||
"arn:aws:iam::000000000000:user/floci-deployer",
|
||||
iamService.resolveCallerArn("floci").orElseThrow());
|
||||
assertNotNull(iamService.resolveCallerContext("floci"));
|
||||
assertNotNull(iamService.resolvePrincipalContext(user.getArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void simulatePrincipalPolicyRejectsUnsupportedPrincipalArnType() {
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> iamService.resolvePrincipalContext("arn:aws:iam::000000000000:group/admins"));
|
||||
|
||||
assertEquals("InvalidInput", ex.getErrorCode());
|
||||
assertEquals("PolicySourceArn must identify an IAM user or role.", ex.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachManagedPolicyToRole() {
|
||||
iamService.seedAwsManagedPolicies();
|
||||
@@ -507,6 +542,45 @@ class IamServiceTest {
|
||||
assertEquals(policyArn, attached.getFirst().getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachSsmManagedInstanceCorePolicyToRole() {
|
||||
iamService.seedAwsManagedPolicies();
|
||||
iamService.createRole("Ec2Exec", "/", "{}", null, 0, null);
|
||||
|
||||
String policyArn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore";
|
||||
iamService.attachRolePolicy("Ec2Exec", policyArn);
|
||||
|
||||
List<IamPolicy> attached = iamService.listAttachedRolePolicies("Ec2Exec", null);
|
||||
assertEquals(1, attached.size());
|
||||
assertEquals(policyArn, attached.getFirst().getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachCloudWatchAgentServerPolicyToRole() {
|
||||
iamService.seedAwsManagedPolicies();
|
||||
iamService.createRole("Ec2Exec", "/", "{}", null, 0, null);
|
||||
|
||||
String policyArn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy";
|
||||
iamService.attachRolePolicy("Ec2Exec", policyArn);
|
||||
|
||||
List<IamPolicy> attached = iamService.listAttachedRolePolicies("Ec2Exec", null);
|
||||
assertEquals(1, attached.size());
|
||||
assertEquals(policyArn, attached.getFirst().getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachEcrReadOnlyPolicyToRole() {
|
||||
iamService.seedAwsManagedPolicies();
|
||||
iamService.createRole("Ec2Exec", "/", "{}", null, 0, null);
|
||||
|
||||
String policyArn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly";
|
||||
iamService.attachRolePolicy("Ec2Exec", policyArn);
|
||||
|
||||
List<IamPolicy> attached = iamService.listAttachedRolePolicies("Ec2Exec", null);
|
||||
assertEquals(1, attached.size());
|
||||
assertEquals(policyArn, attached.getFirst().getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsManagedPolicyDeleteRejected() {
|
||||
iamService.seedAwsManagedPolicies();
|
||||
|
||||
Reference in new issue
Block a user