fix(sts): let a same-account trust policy that names a user grant AssumeRole (#8918)

A role's trust policy now reaches IAM enforcement through a
ResourcePolicyProvider, the path every other resource-based policy
takes, instead of a branch on sts:AssumeRole in the enforcement filter.
RoleTrustPolicyProvider answers an STS action on a role ARN with the
role's stored trust policy, each statement that names no Resource given
the role's ARN, and with the account in the role's ARN as its owner.
IAM actions on the role, such as iam:GetRole and iam:PassRole, get
nothing from it.

evaluateResourcePolicyFor already returns ALLOW_DIRECT_IAM_USER for an
Allow naming the caller's user ARN, so a same-account grant to a user
is unchanged, and AssumeRolePolicyEvaluator.grantsToIamUser and its
request-key helper are removed. Within the role's account, an Allow
naming a role session's role, or "*", now grants the call too, as any
same-account resource-based policy does. Across accounts the filter
needs the trust policy's Allow beside the caller's identity policy, so
it refuses an IAM user or a role session the trust policy does not
allow before STS does, with its own AccessDenied message;
AssumeRoleTrustConditionsIntegrationTest checks that message for those
callers.

The STS guide's sentence that the caller's identity policy must allow
sts:AssumeRole now names the same-account exception.
This commit is contained in:
Jeremy Chapeau authored and GitHub committed 2026-11-19 19:10:54 -04:00
1 parent 3aba098ee8
commit b8632351e8
7 files changed
+505 -18

No files matched your search

@@ -0,0 +1,114 @@
package io.floci.aws.services.iam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AccountResolver;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.services.iam.model.IamRole;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Supplies a role's trust policy to IAM policy evaluation as the role's resource-based policy, for
* the STS actions that name the role, together with the account that owns the role: the one in its
* ARN, also when the role does not exist or its trust policy cannot be read, so assuming another
* account's role is judged as cross-account.
*
* <p>The STS API Reference (AssumeRole) says "the role's trust policy acts as an IAM resource-based
* policy". A trust policy names no {@code Resource}, the role it is attached to being its resource,
* so each statement that names none is given the role's ARN. IAM actions on the role, such as
* {@code iam:GetRole} or {@code iam:PassRole}, are not governed by its trust policy and get nothing
* here.
*/
@ApplicationScoped
public class RoleTrustPolicyProvider implements ResourcePolicyProvider {
private static final Logger LOG = Logger.getLogger(RoleTrustPolicyProvider.class);
private static final String ROLE_PREFIX = "role/";
private static final String STS_ACTION_PREFIX = "sts:";
private final IamService iamService;
private final ObjectMapper objectMapper;
@Inject
public RoleTrustPolicyProvider(IamService iamService, ObjectMapper objectMapper) {
this.iamService = iamService;
this.objectMapper = objectMapper;
}
@Override
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String resourceArn) {
return List.of();
}
@Override
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String action, String resourceArn) {
if (action == null || !action.startsWith(STS_ACTION_PREFIX) || !AwsArnUtils.isArnFor(resourceArn, "iam")) {
return List.of();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
String target = arn.resource();
if (!AccountResolver.isAccountId(arn.accountId()) || !target.startsWith(ROLE_PREFIX)
|| target.length() == ROLE_PREFIX.length() || target.contains("*")) {
return List.of();
}
String roleName = target.substring(target.lastIndexOf('/') + 1);
String trustPolicy = iamService.findRole(arn.accountId(), roleName)
.filter(role -> IamService.roleArnMatches(resourceArn, role))
.map(IamRole::getAssumeRolePolicyDocument)
.flatMap(document -> attachedTo(document, resourceArn))
.orElse(null);
return List.of(new ResourcePolicy(trustPolicy, arn.accountId()));
}
/**
* The trust policy with the role's ARN as the {@code Resource} of every statement naming none, or
* empty when it cannot be read, which STS refuses whatever it says.
*/
private Optional<String> attachedTo(String trustPolicy, String roleArn) {
if (trustPolicy == null || trustPolicy.isBlank()) {
return Optional.empty();
}
JsonNode root;
try {
root = objectMapper.readTree(trustPolicy);
} catch (Exception e) {
LOG.debugv("Unreadable trust policy on {0}: {1}", roleArn, e.getMessage());
return Optional.empty();
}
if (!(root instanceof ObjectNode document) || !TrustPolicyStatements.wellFormed(root.path("Statement"))) {
return Optional.empty();
}
JsonNode statements = document.path("Statement");
List<JsonNode> statementList = new ArrayList<>();
if (statements.isArray()) {
statements.forEach(statementList::add);
} else {
statementList.add(statements);
}
ArrayNode attached = objectMapper.createArrayNode();
for (JsonNode statement : statementList) {
ObjectNode copy = ((ObjectNode) statement).deepCopy();
if (!copy.has("Resource") && !copy.has("NotResource")) {
copy.put("Resource", roleArn);
}
attached.add(copy);
}
ObjectNode result = document.deepCopy();
result.set("Statement", attached);
try {
return Optional.of(objectMapper.writeValueAsString(result));
} catch (JsonProcessingException e) {
LOG.debugv("Unwritable trust policy on {0}: {1}", roleArn, e.getMessage());
return Optional.empty();
}
}
}