feat(iam): enforce AssumeRole trust policies when enforcement is enabled (#1552)

* feat(iam): enforce AssumeRole trust policies when enforcement is enabled

When floci.iam.enforcement-enabled is true, STS AssumeRole now evaluates the
target role's trust policy (AssumeRolePolicyDocument) against the caller and
returns AccessDenied if it is not permitted. Enforcement is off by default,
so existing behavior is unchanged.

Trust policies are principal-centric and carry no Resource element, so the
identity/resource-oriented IamPolicyEvaluator cannot evaluate them. A focused
AssumeRolePolicyEvaluator matches each statement's Action (sts:AssumeRole) and
AWS Principal (account id, account-root ARN, exact principal ARN, or "*")
against the caller, with explicit Deny taking precedence. The role is resolved
in its own account (from the role ARN) via a new IamService.findRole overload,
since roles are account-namespaced. Roles unknown to Floci stay permissive to
preserve backward compatibility.

Tests: unit coverage for the evaluator across principal/action/deny/malformed
cases, plus an enforcement-enabled integration test asserting a permitted
caller succeeds, an unauthorized caller is denied, and an unknown role stays
permissive.

* docs(sts): restore Configuration section and note NotAction limitation

Greptile review on #1552:
- Restore the FLOCI_SERVICES_STS_ENABLED Configuration section that was dropped
  when the Trust Policy Enforcement section was added.
- Document that NotAction is not evaluated (a Deny with NotAction won't block).

* fix(iam): honor NotAction in AssumeRole trust statements

evaluateStatement only inspected the Action key, so a trust statement using
NotAction returned NO_MATCH and was skipped entirely. A Deny that should block
sts:AssumeRole (NotAction excluding some other action) was therefore ignored,
over-permitting AssumeRole when IAM enforcement is enabled. Add actionApplies()
mirroring IamPolicyEvaluator's Action/NotAction semantics: Action applies when
any pattern matches, NotAction applies when none match. Covered by three new
AssumeRolePolicyEvaluatorTest cases.

* fix(iam): match an assumed-role caller against a role-ARN trust principal

When a caller uses assumed-role temporary credentials, resolveCallerArn (rightly,
for GetCallerIdentity) yields the STS assumed-role ARN
(arn:aws:sts::ACCT:assumed-role/Role/session). enforceTrustPolicy passes that to
the trust-policy evaluator, but role trust policies name the role's IAM principal
ARN (arn:aws:iam::ACCT:role/Role) — the canonical form AWS resolves the session
back to. So a legitimate role-to-role AssumeRole was denied.

matchesAwsPrincipal now also normalizes an assumed-role caller ARN to its
underlying IAM role ARN and matches the principal against that, while still
matching an exact session-ARN principal. resolveCallerArn is unchanged, so
GetCallerIdentity keeps returning the assumed-role ARN.

Adds AssumeRolePolicyEvaluatorTest cases for role-ARN match, differing-role
denial, and exact session-ARN match.
This commit is contained in:
Alex Banna authored and GitHub committed 2026-06-30 21:44:04 -05:00
1 parent c496091ac3
commit d54a307ca9
6 files changed
+534 -1

No files matched your search

@@ -0,0 +1,167 @@
package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AssumeRolePolicyEvaluatorTest {
private final AssumeRolePolicyEvaluator evaluator = new AssumeRolePolicyEvaluator(new ObjectMapper());
private static final String CALLER_ARN = "arn:aws:iam::111111111111:user/alice";
private static final String CALLER_ACCOUNT = "111111111111";
private static String trust(String principal) {
return """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":%s,"Action":"sts:AssumeRole"}]}
""".formatted(principal);
}
@Test
void allowsAccountRootPrincipal() {
assertTrue(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::111111111111:root\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsBareAccountPrincipal() {
assertTrue(evaluator.allows(trust("{\"AWS\":\"111111111111\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsExactPrincipalArn() {
assertTrue(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::111111111111:user/alice\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsWildcardPrincipal() {
assertTrue(evaluator.allows(trust("\"*\""), CALLER_ARN, CALLER_ACCOUNT));
assertTrue(evaluator.allows(trust("{\"AWS\":\"*\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsWhenPrincipalListContainsCaller() {
assertTrue(evaluator.allows(
trust("{\"AWS\":[\"arn:aws:iam::999999999999:root\",\"arn:aws:iam::111111111111:root\"]}"),
CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void deniesWhenAccountDoesNotMatch() {
assertFalse(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::999999999999:root\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void deniesWhenPrincipalArnDiffers() {
assertFalse(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::111111111111:user/bob\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void explicitDenyOverridesAllow() {
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"sts:AssumeRole"},
{"Effect":"Deny","Principal":{"AWS":"arn:aws:iam::111111111111:root"},"Action":"sts:AssumeRole"}]}
""";
assertFalse(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void deniesWhenActionIsNotAssumeRole() {
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"sts:TagSession"}]}
""";
assertFalse(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsWildcardAction() {
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"111111111111"},"Action":"sts:*"}]}
""";
assertTrue(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void deniesWhenDenyNotActionExcludesAssumeRole() {
// Deny applies to every action except sts:TagSession — which includes sts:AssumeRole — so it blocks.
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"sts:AssumeRole"},
{"Effect":"Deny","Principal":{"AWS":"*"},"NotAction":"sts:TagSession"}]}
""";
assertFalse(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsWhenDenyNotActionIncludesAssumeRole() {
// Deny applies to every action except sts:AssumeRole, so it does NOT block the assume.
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"*"},"Action":"sts:AssumeRole"},
{"Effect":"Deny","Principal":{"AWS":"*"},"NotAction":"sts:AssumeRole"}]}
""";
assertTrue(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsWhenAllowNotActionDoesNotCoverAssumeRole() {
// Allow applies to every action except sts:GetSessionToken, so it grants sts:AssumeRole.
String doc = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Principal":{"AWS":"111111111111"},"NotAction":"sts:GetSessionToken"}]}
""";
assertTrue(evaluator.allows(doc, CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void allowsAssumedRoleCallerAgainstRolePrincipalArn() {
// The caller used assumed-role temp creds (callerArn is the STS assumed-role ARN), but the
// trust policy names the underlying IAM role ARN — the canonical trust-policy form. It must
// match, since AWS resolves the session back to its role for trust evaluation.
String assumedRoleArn = "arn:aws:sts::111111111111:assumed-role/AppRole/session-abc";
assertTrue(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::111111111111:role/AppRole\"}"),
assumedRoleArn, CALLER_ACCOUNT));
}
@Test
void deniesAssumedRoleCallerWhenRolePrincipalDiffers() {
// A trust policy naming a different role must not be satisfied by this session.
String assumedRoleArn = "arn:aws:sts::111111111111:assumed-role/AppRole/session-abc";
assertFalse(evaluator.allows(
trust("{\"AWS\":\"arn:aws:iam::111111111111:role/OtherRole\"}"),
assumedRoleArn, CALLER_ACCOUNT));
}
@Test
void allowsAssumedRoleCallerAgainstExactSessionArn() {
// A trust policy that names the exact assumed-role session ARN still matches directly.
String assumedRoleArn = "arn:aws:sts::111111111111:assumed-role/AppRole/session-abc";
assertTrue(evaluator.allows(
trust("{\"AWS\":\"arn:aws:sts::111111111111:assumed-role/AppRole/session-abc\"}"),
assumedRoleArn, CALLER_ACCOUNT));
}
@Test
void deniesServiceOnlyPrincipal() {
assertFalse(evaluator.allows(
trust("{\"Service\":\"lambda.amazonaws.com\"}"), CALLER_ARN, CALLER_ACCOUNT));
}
@Test
void deniesBlankOrMalformedDocument() {
assertFalse(evaluator.allows(null, CALLER_ARN, CALLER_ACCOUNT));
assertFalse(evaluator.allows("", CALLER_ARN, CALLER_ACCOUNT));
assertFalse(evaluator.allows("{}", CALLER_ARN, CALLER_ACCOUNT));
assertFalse(evaluator.allows("not json", CALLER_ARN, CALLER_ACCOUNT));
}
}
@@ -0,0 +1,105 @@
package io.github.hectorvent.floci.services.iam;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
/**
* Verifies that, with {@code iam.enforcement-enabled=true}, STS AssumeRole honors the target role's
* trust policy: a caller the trust policy permits succeeds; one it does not is denied; and a role
* Floci does not know about stays permissive (backward-compatible).
*/
@QuarkusTest
@TestProfile(AssumeRoleTrustPolicyIntegrationTest.EnforcementProfile.class)
class AssumeRoleTrustPolicyIntegrationTest {
public static class EnforcementProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("floci.services.iam.enforcement-enabled", "true");
}
}
private static final String ACCOUNT_A = "111111111111";
private static final String ACCOUNT_B = "222222222222";
private static final String ACCOUNT_C = "333333333333";
private static final String TRUST_ALLOW_A = "{\"Version\":\"2012-10-17\",\"Statement\":[{"
+ "\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::" + ACCOUNT_A + ":root\"},"
+ "\"Action\":\"sts:AssumeRole\"}]}";
private static String auth(String account, String service) {
return "AWS4-HMAC-SHA256 Credential=" + account + "/20260215/us-east-1/" + service
+ "/aws4_request, SignedHeaders=host, Signature=abc";
}
private static void createRoleInB(String roleName) {
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "CreateRole")
.formParam("RoleName", roleName)
.formParam("AssumeRolePolicyDocument", TRUST_ALLOW_A)
.header("Authorization", auth(ACCOUNT_B, "iam"))
.when().post("/")
.then().statusCode(200);
}
@Test
void permittedCallerCanAssumeRole() {
String role = "trust-ok-" + UUID.randomUUID().toString().substring(0, 8);
createRoleInB(role);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "AssumeRole")
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_B + ":role/" + role)
.formParam("RoleSessionName", "s")
.header("Authorization", auth(ACCOUNT_A, "sts"))
.when().post("/")
.then().statusCode(200)
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA"));
}
@Test
void unauthorizedCallerIsDenied() {
String role = "trust-deny-" + UUID.randomUUID().toString().substring(0, 8);
createRoleInB(role);
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "AssumeRole")
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_B + ":role/" + role)
.formParam("RoleSessionName", "s")
.header("Authorization", auth(ACCOUNT_C, "sts"))
.when().post("/")
.then().statusCode(403)
.body(containsString("AccessDenied"))
// AWS prefixes the denial with the caller and names the action and resource.
.body(containsString("User: "))
.body(containsString("is not authorized to perform: sts:AssumeRole on resource: "
+ "arn:aws:iam::" + ACCOUNT_B + ":role/" + role));
}
@Test
void unknownRoleStaysPermissive() {
// No role created — enforcement must not block roles Floci has never seen.
given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "AssumeRole")
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_B + ":role/never-created-"
+ UUID.randomUUID().toString().substring(0, 8))
.formParam("RoleSessionName", "s")
.header("Authorization", auth(ACCOUNT_C, "sts"))
.when().post("/")
.then().statusCode(200)
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA"));
}
}