mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(iam): enforce STS session policies (#1636)
* test(iam): expose STS session policy S3 deny gap * fix(iam): enforce STS session policies ## Summary STS session policies were recorded on temporary credentials but IAM enforcement evaluated only the caller's identity policies. As a result, an assumed role with broad role permissions could still perform an action even when the inline `AssumeRole` session policy should have denied or narrowed it. This changes enforcement to evaluate the full caller context, including identity policies, session policies, and permission boundaries. It also keeps temporary-credential lookup account-independent so assumed-role sessions can be resolved after account routing moves the request into the role account, and it uses the resolved request account when building resource ARNs for policy evaluation. ## Type of change - [x] Bug fix (`fix:`) ## AWS Compatibility Incorrect behavior: STS `AssumeRole` accepted the standard inline `Policy` parameter, but later requests signed with those temporary credentials did not have that session policy applied by IAM enforcement. The fix matches AWS policy evaluation semantics: explicit denies in session policies participate in the deny pass, and a present session policy must also explicitly allow the request for the role's permissions to be effective. This preserves the existing Floci bypass behavior for unknown local credentials and the `test` root credential. ## What changed - `IamEnforcementFilter` resolves a `CallerContext` and evaluates the complete IAM context instead of identity-policy documents only. - `IamService` resolves STS sessions across account namespaces for caller context, caller ARN, account routing, and temporary secret lookup. - Enforcement resource ARN construction uses the request account already resolved by `AccountContextFilter`, so assumed-role credentials evaluate account-scoped resources against the role account. - IAM and multi-account docs now describe session-policy intersection and temporary-credential resolution accurately. - Java SDK compatibility coverage now checks that an assumed-role session policy deny overrides a role allow. ## Checklist - [x] `./mvnw test` passes locally - attempted; only failures were unrelated LambdaReactiveSyncIntegrationTest container startup Broken pipe errors - [x] Selected Java SDK compatibility tests pass locally - in `compatibility-tests/sdk-test-java`: `mvn -q -Dtest=IamEnforcementTest,IamTest,StsTest,S3Test test` - [x] New or updated integration test added - [x] Commit messages follow Conventional Commits * fix(iam): tighten cross-account session lookup
This commit is contained in:
1 parent
3304184bd3
commit
4f4e7be90e
8 files changed
+372
-23
No files matched your search
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
|
||||
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
@@ -15,7 +16,6 @@ import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -32,8 +32,9 @@ import java.util.regex.Pattern;
|
||||
* <li>The action cannot be resolved (unknown mapping → permissive)</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Phase 1 evaluates identity-based policies only.
|
||||
* Resource-based policies (S3 bucket policy, Lambda resource policy, etc.) are Phase 2.
|
||||
* <p>Evaluates the caller's identity policies, optional session policy, and optional
|
||||
* permissions boundary. Resource-based policies (S3 bucket policy, Lambda resource
|
||||
* policy, etc.) are not yet supplied to this filter.
|
||||
*/
|
||||
@Provider
|
||||
@ApplicationScoped
|
||||
@@ -51,6 +52,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
private final IamPolicyEvaluator evaluator;
|
||||
private final IamActionRegistry actionRegistry;
|
||||
private final ResourceArnBuilder arnBuilder;
|
||||
private final RequestContext requestContext;
|
||||
|
||||
@Inject
|
||||
public IamEnforcementFilter(EmulatorConfig config,
|
||||
@@ -58,13 +60,15 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
IamService iamService,
|
||||
IamPolicyEvaluator evaluator,
|
||||
IamActionRegistry actionRegistry,
|
||||
ResourceArnBuilder arnBuilder) {
|
||||
ResourceArnBuilder arnBuilder,
|
||||
RequestContext requestContext) {
|
||||
this.config = config;
|
||||
this.accountResolver = accountResolver;
|
||||
this.iamService = iamService;
|
||||
this.evaluator = evaluator;
|
||||
this.actionRegistry = actionRegistry;
|
||||
this.arnBuilder = arnBuilder;
|
||||
this.requestContext = requestContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,16 +97,18 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
return; // unknown action → ALLOW (permissive)
|
||||
}
|
||||
|
||||
List<String> policies = iamService.resolveCallerPolicies(akid);
|
||||
if (policies == null) {
|
||||
CallerContext caller = iamService.resolveCallerContext(akid);
|
||||
if (caller == null) {
|
||||
return; // unknown access key → bypass (backward-compat)
|
||||
}
|
||||
|
||||
String region = config.defaultRegion();
|
||||
String accountId = accountResolver.resolve(auth);
|
||||
String region = requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
|
||||
String accountId = requestContext.getAccountId() == null
|
||||
? accountResolver.resolve(auth)
|
||||
: requestContext.getAccountId();
|
||||
String resource = arnBuilder.build(credentialScope, ctx, region, accountId);
|
||||
|
||||
Decision decision = evaluator.evaluate(policies, action, resource);
|
||||
Decision decision = evaluator.evaluate(caller, null, action, resource, null);
|
||||
if (decision == Decision.DENY) {
|
||||
LOG.infov("IAM enforcement DENY: akid={0} action={1} resource={2}", akid, action, resource);
|
||||
ctx.abortWith(accessDeniedResponse(action, credentialScope, ctx.getMediaType()));
|
||||
|
||||
@@ -48,6 +48,7 @@ public class IamService implements SessionAccountLookup {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(IamService.class);
|
||||
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
private static final String TEMPORARY_ACCESS_KEY_PREFIX = "ASIA";
|
||||
private static final String DEFAULT_DEPLOYER_USER = "floci-deployer";
|
||||
private static final String DEFAULT_DEPLOYER_ACCESS_KEY_ID = "floci";
|
||||
private static final String DEFAULT_DEPLOYER_SECRET_ACCESS_KEY = "floci";
|
||||
@@ -935,7 +936,7 @@ public class IamService implements SessionAccountLookup {
|
||||
if (fromAccessKey.isPresent()) {
|
||||
return fromAccessKey;
|
||||
}
|
||||
return sessions.get(accessKeyId).map(SessionCredential::getSecretAccessKey);
|
||||
return findSessionAnyAccount(accessKeyId).map(SessionCredential::getSecretAccessKey);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -989,9 +990,7 @@ public class IamService implements SessionAccountLookup {
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> resolveAccountId(String accessKeyId) {
|
||||
// STS issues only ASIA-prefixed temporary keys, so anything else cannot be a session.
|
||||
// Short-circuit here to keep the per-request hot path off the session-store scan below.
|
||||
if (accessKeyId == null || !accessKeyId.startsWith("ASIA")) {
|
||||
if (!isTemporaryAccessKey(accessKeyId)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<SessionCredential> sessionOpt = findSessionAnyAccount(accessKeyId);
|
||||
@@ -1015,6 +1014,9 @@ public class IamService implements SessionAccountLookup {
|
||||
* across all accounts; the access key's global uniqueness keeps the result unambiguous.
|
||||
*/
|
||||
private Optional<SessionCredential> findSessionAnyAccount(String accessKeyId) {
|
||||
if (!isTemporaryAccessKey(accessKeyId)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
return Optional.ofNullable(aware.scanAllAccountsAsMap().get(accessKeyId));
|
||||
}
|
||||
@@ -1037,12 +1039,13 @@ public class IamService implements SessionAccountLookup {
|
||||
return new CallerContext(identityPolicies, null, boundaryDoc);
|
||||
}
|
||||
|
||||
// Check assumed-role sessions
|
||||
Optional<SessionCredential> sessionOpt = sessions.get(accessKeyId);
|
||||
// Check assumed-role sessions. These can be stored under the account that minted the
|
||||
// session, while request routing for temporary credentials uses the role's account.
|
||||
Optional<SessionCredential> sessionOpt = findSessionForCallerContext(accessKeyId);
|
||||
if (sessionOpt.isPresent()) {
|
||||
SessionCredential session = sessionOpt.get();
|
||||
if (session.getExpiration() != null && session.getExpiration().isBefore(java.time.Instant.now())) {
|
||||
sessions.delete(accessKeyId);
|
||||
deleteSession(accessKeyId, session);
|
||||
return null; // expired — unknown key → bypass
|
||||
}
|
||||
|
||||
@@ -1058,6 +1061,20 @@ public class IamService implements SessionAccountLookup {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Optional<SessionCredential> findSessionForCallerContext(String accessKeyId) {
|
||||
if (accessKeyId == null || accessKeyId.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<SessionCredential> session = sessions.get(accessKeyId);
|
||||
if (session.isPresent()) {
|
||||
return session;
|
||||
}
|
||||
if (!isTemporaryAccessKey(accessKeyId)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return findSessionAnyAccount(accessKeyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all identity-based policy documents applicable to the caller identified
|
||||
* by {@code accessKeyId}.
|
||||
@@ -1083,11 +1100,11 @@ public class IamService implements SessionAccountLookup {
|
||||
return users.get(userName).map(IamUser::getArn);
|
||||
}
|
||||
|
||||
Optional<SessionCredential> sessionOpt = sessions.get(accessKeyId);
|
||||
Optional<SessionCredential> sessionOpt = findSessionForCallerContext(accessKeyId);
|
||||
if (sessionOpt.isPresent()) {
|
||||
SessionCredential session = sessionOpt.get();
|
||||
if (session.getExpiration() != null && session.getExpiration().isBefore(java.time.Instant.now())) {
|
||||
sessions.delete(accessKeyId);
|
||||
deleteSession(accessKeyId, session);
|
||||
return Optional.empty();
|
||||
}
|
||||
String roleArn = session.getRoleArn();
|
||||
@@ -1099,6 +1116,20 @@ public class IamService implements SessionAccountLookup {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static boolean isTemporaryAccessKey(String accessKeyId) {
|
||||
return accessKeyId != null && accessKeyId.startsWith(TEMPORARY_ACCESS_KEY_PREFIX);
|
||||
}
|
||||
|
||||
private void deleteSession(String accessKeyId, SessionCredential session) {
|
||||
String originAccountId = session.getOriginAccountId();
|
||||
if (originAccountId != null && !originAccountId.isBlank()
|
||||
&& sessions instanceof AccountAwareStorageBackend<SessionCredential> aware) {
|
||||
aware.deleteForAccount(originAccountId, accessKeyId);
|
||||
return;
|
||||
}
|
||||
sessions.delete(accessKeyId);
|
||||
}
|
||||
|
||||
public CallerContext resolvePrincipalContext(String principalArn) {
|
||||
if (principalArn == null || principalArn.isBlank()) {
|
||||
throw new AwsException("ValidationError", "PolicySourceArn is required.", 400);
|
||||
|
||||
Reference in new issue
Block a user