mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): set iam: condition keys from a table generated from the Service Reference (#6762)
This commit is contained in:
1 parent
0c549d267b
commit
967e87d7f7
12 files changed
+708
-15
No files matched your search
+57
@@ -201,6 +201,63 @@ class IamConditionContextResolverTest {
|
||||
formRequest("PolicyArn=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2FTeam%2FA%2BB%252F")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamPermissionsBoundaryComesFromTheBoundaryParameterOfEachActionThatTakesIt() {
|
||||
for (String action : List.of("iam:CreateRole", "iam:CreateUser", "iam:PutRolePermissionsBoundary",
|
||||
"iam:PutUserPermissionsBoundary")) {
|
||||
assertEquals(Map.of("iam:PermissionsBoundary", List.of("arn:aws:iam::123456789012:policy/Boundary")),
|
||||
resolver.resolve("iam", action,
|
||||
formRequest("PermissionsBoundary=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2FBoundary")),
|
||||
action);
|
||||
assertNull(resolver.resolve("iam", action, formRequest("RoleName=r&UserName=u")), action);
|
||||
}
|
||||
assertNull(resolver.resolve("iam", "iam:GetRole", formRequest("PermissionsBoundary=example")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamAwsServiceNameComesFromCreateServiceLinkedRole() {
|
||||
assertEquals(Map.of("iam:AWSServiceName", List.of("replication.ecr.amazonaws.com")),
|
||||
resolver.resolve("iam", "iam:CreateServiceLinkedRole",
|
||||
formRequest("AWSServiceName=replication.ecr.amazonaws.com")));
|
||||
assertNull(resolver.resolve("iam", "iam:CreateServiceLinkedRole", formRequest("Description=d")));
|
||||
assertNull(resolver.resolve("iam", "iam:DeleteServiceLinkedRole",
|
||||
formRequest("AWSServiceName=replication.ecr.amazonaws.com")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamNotificationChannelComesFromCreateDelegationRequest() {
|
||||
assertEquals(Map.of("iam:NotificationChannel", List.of("arn:aws:sns:us-east-1:123456789012:t")),
|
||||
resolver.resolve("iam", "iam:CreateDelegationRequest",
|
||||
formRequest("NotificationChannel=arn%3Aaws%3Asns%3Aus-east-1%3A123456789012%3At")));
|
||||
assertNull(resolver.resolve("iam", "iam:CreateDelegationRequest", formRequest("Description=d")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamOrganizationsPolicyIdComesFromGenerateOrganizationsAccessReport() {
|
||||
assertEquals(Map.of("iam:OrganizationsPolicyId", List.of("p-12345678")),
|
||||
resolver.resolve("iam", "iam:GenerateOrganizationsAccessReport",
|
||||
formRequest("EntityPath=o-a%2Fr-b&OrganizationsPolicyId=p-12345678")));
|
||||
assertNull(resolver.resolve("iam", "iam:GenerateOrganizationsAccessReport",
|
||||
formRequest("EntityPath=o-a%2Fr-b")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPermissionsBoundaryConditionAllowsOnlyACreateRoleThatSetsThatBoundary() {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"iam:CreateRole",
|
||||
"Resource":"*","Condition":{"StringEquals":
|
||||
{"iam:PermissionsBoundary":"arn:aws:iam::123456789012:policy/Boundary"}}}]}""";
|
||||
|
||||
assertEquals(Decision.ALLOW, evaluator.simulateCustomPolicy(List.of(policy), "iam:CreateRole", "*",
|
||||
resolver.resolve("iam", "iam:CreateRole", formRequest(
|
||||
"RoleName=r&PermissionsBoundary=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2FBoundary"))));
|
||||
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(policy), "iam:CreateRole", "*",
|
||||
resolver.resolve("iam", "iam:CreateRole", formRequest(
|
||||
"RoleName=r&PermissionsBoundary=arn%3Aaws%3Aiam%3A%3A123456789012%3Apolicy%2FOther"))));
|
||||
assertEquals(Decision.DENY, evaluator.simulateCustomPolicy(List.of(policy), "iam:CreateRole", "*",
|
||||
resolver.resolve("iam", "iam:CreateRole", formRequest("RoleName=r"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryGetConditionsUseTheFirstDecodedUrlValue() {
|
||||
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* {@code AmazonEC2ContainerRegistryFullAccess} allows {@code iam:CreateServiceLinkedRole} only with
|
||||
* {@code StringEquals} {@code iam:AWSServiceName} {@code replication.ecr.amazonaws.com}, as most AWS
|
||||
* managed policies that create a service-linked role do. The key comes from the request's
|
||||
* {@code AWSServiceName} member.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class IamAwsServiceNameConditionEnforcementIntegrationTest {
|
||||
|
||||
private static final String ADMIN = "test";
|
||||
private static final String POLICY = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess";
|
||||
private static final String ROLE = "AWSServiceRoleForECRReplication";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void aManagedPolicyAllowsOnlyTheServiceLinkedRoleItNames() {
|
||||
String userName = "ecr-admin-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
UserCredentials user = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ADMIN, "iam"),
|
||||
userName);
|
||||
iam(ADMIN, "AttachUserPolicy").formParam("UserName", userName).formParam("PolicyArn", POLICY)
|
||||
.post("/").then().statusCode(200);
|
||||
cleanup.register(() -> iam(ADMIN, "DeleteServiceLinkedRole").formParam("RoleName", ROLE).post("/"));
|
||||
|
||||
iam(user.accessKeyId(), "CreateServiceLinkedRole")
|
||||
.formParam("AWSServiceName", "memorydb.amazonaws.com").post("/").then()
|
||||
.statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"))
|
||||
.body(containsString("iam:CreateServiceLinkedRole"));
|
||||
iam(user.accessKeyId(), "CreateServiceLinkedRole")
|
||||
.formParam("AWSServiceName", "replication.ecr.amazonaws.com").post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<RoleName>" + ROLE + "</RoleName>"));
|
||||
}
|
||||
|
||||
private static RequestSpecification iam(String accessKeyId, String action) {
|
||||
return given().contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", auth(accessKeyId, "iam"))
|
||||
.formParam("Action", action);
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* A delegated administrator may create roles and users only with a given permissions boundary, as in
|
||||
* the IAM user guide's "Delegating responsibility to others using permissions boundaries" example:
|
||||
* an allow conditioned on {@code StringEquals} {@code iam:PermissionsBoundary}. The Service
|
||||
* Authorization Reference lists the key on {@code CreateRole}, {@code CreateUser},
|
||||
* {@code PutRolePermissionsBoundary} and {@code PutUserPermissionsBoundary}, which carry the boundary
|
||||
* in their {@code PermissionsBoundary} parameter.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class IamPermissionsBoundaryConditionEnforcementIntegrationTest {
|
||||
|
||||
private static final String ADMIN = "test";
|
||||
private static final String TRUST_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"ecs-tasks.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
static URI baseUri;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@Test
|
||||
void createRoleIsAllowedOnlyWithTheBoundaryTheConditionNames() {
|
||||
String boundary = boundaryPolicy("boundary");
|
||||
String other = boundaryPolicy("other");
|
||||
UserCredentials delegated = delegatedAdmin(boundary, "iam:CreateRole");
|
||||
String role = "bounded-" + suffix();
|
||||
cleanup.register(() -> EnforcementFixtures.removeRole(baseUri.getPort(), auth(ADMIN, "iam"), role));
|
||||
|
||||
denied(createRole(delegated, role).post("/").then(), "iam:CreateRole");
|
||||
denied(createRole(delegated, role).formParam("PermissionsBoundary", other).post("/").then(),
|
||||
"iam:CreateRole");
|
||||
createRole(delegated, role).formParam("PermissionsBoundary", boundary).post("/").then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<PermissionsBoundaryArn>" + boundary + "</PermissionsBoundaryArn>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createUserAndPutUserPermissionsBoundaryAreAllowedOnlyWithThatBoundary() {
|
||||
String boundary = boundaryPolicy("boundary");
|
||||
String other = boundaryPolicy("other");
|
||||
UserCredentials delegated = delegatedAdmin(boundary, "iam:CreateUser",
|
||||
"iam:PutUserPermissionsBoundary");
|
||||
String user = "bounded-" + suffix();
|
||||
cleanup.register(() -> EnforcementFixtures.removeUser(baseUri.getPort(), auth(ADMIN, "iam"), user));
|
||||
|
||||
denied(iam(delegated.accessKeyId(), "CreateUser").formParam("UserName", user).post("/").then(),
|
||||
"iam:CreateUser");
|
||||
iam(delegated.accessKeyId(), "CreateUser").formParam("UserName", user)
|
||||
.formParam("PermissionsBoundary", boundary).post("/").then().statusCode(200);
|
||||
|
||||
denied(iam(delegated.accessKeyId(), "PutUserPermissionsBoundary").formParam("UserName", user)
|
||||
.formParam("PermissionsBoundary", other).post("/").then(), "iam:PutUserPermissionsBoundary");
|
||||
iam(delegated.accessKeyId(), "PutUserPermissionsBoundary").formParam("UserName", user)
|
||||
.formParam("PermissionsBoundary", boundary).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void putRolePermissionsBoundaryIsAllowedOnlyWithThatBoundary() {
|
||||
String boundary = boundaryPolicy("boundary");
|
||||
String other = boundaryPolicy("other");
|
||||
UserCredentials delegated = delegatedAdmin(boundary, "iam:PutRolePermissionsBoundary");
|
||||
String role = "rebounded-" + suffix();
|
||||
cleanup.register(() -> EnforcementFixtures.removeRole(baseUri.getPort(), auth(ADMIN, "iam"), role));
|
||||
createRole(ADMIN, role).post("/").then().statusCode(200);
|
||||
|
||||
denied(iam(delegated.accessKeyId(), "PutRolePermissionsBoundary").formParam("RoleName", role)
|
||||
.formParam("PermissionsBoundary", other).post("/").then(), "iam:PutRolePermissionsBoundary");
|
||||
iam(delegated.accessKeyId(), "PutRolePermissionsBoundary").formParam("RoleName", role)
|
||||
.formParam("PermissionsBoundary", boundary).post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private String boundaryPolicy(String name) {
|
||||
String arn = iam(ADMIN, "CreatePolicy")
|
||||
.formParam("PolicyName", name + "-" + suffix())
|
||||
.formParam("PolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}""")
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().path("CreatePolicyResponse.CreatePolicyResult.Policy.Arn");
|
||||
cleanup.register(() -> EnforcementFixtures.removePolicy(baseUri.getPort(), auth(ADMIN, "iam"), arn));
|
||||
return arn;
|
||||
}
|
||||
|
||||
private UserCredentials delegatedAdmin(String boundary, String... actions) {
|
||||
String userName = "delegated-admin-" + suffix();
|
||||
UserCredentials credentials = EnforcementFixtures.createUser(cleanup, baseUri.getPort(),
|
||||
auth(ADMIN, "iam"), userName);
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ADMIN, "iam"), userName, "OnlyWithBoundary", """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["%s"],"Resource":"*",
|
||||
"Condition":{"StringEquals":{"iam:PermissionsBoundary":"%s"}}}]}"""
|
||||
.formatted(String.join("\",\"", actions), boundary));
|
||||
return credentials;
|
||||
}
|
||||
|
||||
private RequestSpecification createRole(UserCredentials caller, String roleName) {
|
||||
return createRole(caller.accessKeyId(), roleName);
|
||||
}
|
||||
|
||||
private RequestSpecification createRole(String accessKeyId, String roleName) {
|
||||
return iam(accessKeyId, "CreateRole").formParam("RoleName", roleName)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY);
|
||||
}
|
||||
|
||||
private static RequestSpecification iam(String accessKeyId, String action) {
|
||||
return given().contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", auth(accessKeyId, "iam"))
|
||||
.formParam("Action", action);
|
||||
}
|
||||
|
||||
private static void denied(ValidatableResponse response, String action) {
|
||||
response.statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"))
|
||||
.body(containsString(action));
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user