fix(s3): resolve ResourceAccount conditions from the bucket owner (#7372)

* fix(s3): resolve ResourceAccount conditions from the bucket owner

* fix(s3): generate ResourceAccount action coverage

Read s3:ResourceAccount support from the Service Reference action table and carry each evaluated action into shared signed-request and service-call context. Preserve resolved bucket ownership, existing request tag coverage and SDK session-policy regressions. Reuse AwsTestClients for IAM and STS fixtures.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-02 23:52:33 -04:00
1 parent 7bac1e356a
commit 16704ff32b
13 files changed
+721 -75

No files matched your search

@@ -265,19 +265,16 @@ public class IamConditionContextResolver {
public static Map<String, List<String>> withGlobalContext(Map<String, List<String>> serviceContext,
String resourceArn, String region,
String accountId, String resourceOwnerAccountId) {
return withGlobalContext(serviceContext, resourceArn, region, accountId, resourceOwnerAccountId, null);
}
public static Map<String, List<String>> withGlobalContext(Map<String, List<String>> serviceContext,
String resourceArn, String region,
String accountId, String resourceOwnerAccountId, String action) {
Map<String, List<String>> conditions = serviceContext == null
? new LinkedHashMap<>()
: new LinkedHashMap<>(serviceContext);
// aws:ResourceAccount is the account that OWNS the resource, which is not always the
// caller's. Some ARNs carry it; S3 bucket and object ARNs deliberately do not, and there
// the owner has to come from service state. When neither source knows it, the key is left
// out rather than guessed: an absent key fails a Condition that tests it, whereas a wrong
// one would silently authorize cross-account access the policy meant to refuse.
String resourceAccount = AwsArnUtils.accountOrDefault(resourceArn, null);
if (resourceAccount == null || resourceAccount.isBlank()) {
resourceAccount = resourceOwnerAccountId;
}
putIfPresent(conditions, "aws:ResourceAccount", resourceAccount);
putResourceAccount(conditions, resourceArn, resourceOwnerAccountId, action);
putIfPresent(conditions, "aws:PrincipalAccount", accountId);
putIfPresent(conditions, "aws:RequestedRegion", region);
conditions.putIfAbsent("aws:ViaAWSService", List.of("false"));
@@ -286,6 +283,20 @@ public class IamConditionContextResolver {
return conditions;
}
private static void putResourceAccount(Map<String, List<String>> conditions, String resourceArn,
String resourceOwnerAccountId, String action) {
// S3 ARNs omit the owner account. Both condition keys must use the stored bucket owner,
// never the caller; leave them absent when neither the ARN nor service state knows it.
String account = AwsArnUtils.accountOrDefault(resourceArn, null);
if (account == null || account.isBlank()) {
account = resourceOwnerAccountId;
}
putIfPresent(conditions, "aws:ResourceAccount", account);
if (S3ControlTagConditionKeys.takes(action, "s3:ResourceAccount")) {
putIfPresent(conditions, "s3:ResourceAccount", account);
}
}
/**
* The request context of a call an AWS service makes on its own behalf, such as SES publishing to
* a topic, from the IAM User Guide's global condition keys: {@code aws:PrincipalIsAWSService} is
@@ -301,6 +312,12 @@ public class IamConditionContextResolver {
public static Map<String, List<String>> serviceCallContext(String servicePrincipal, String resourceArn,
String resourceOwnerAccountId,
String sourceArn, String sourceAccount) {
return serviceCallContext(servicePrincipal, resourceArn, resourceOwnerAccountId, sourceArn, sourceAccount, null);
}
public static Map<String, List<String>> serviceCallContext(String servicePrincipal, String resourceArn,
String resourceOwnerAccountId,
String sourceArn, String sourceAccount, String action) {
Map<String, List<String>> conditions = new LinkedHashMap<>();
conditions.put("aws:PrincipalIsAWSService", List.of("true"));
conditions.put("aws:ViaAWSService", List.of("false"));
@@ -312,9 +329,7 @@ public class IamConditionContextResolver {
&& AwsArnUtils.isArnFor(resourceArn, "sns")) {
putIfPresent(conditions, "aws:SourceOwner", sourceAccount);
}
String resourceAccount = AwsArnUtils.accountOrDefault(resourceArn, null);
putIfPresent(conditions, "aws:ResourceAccount",
resourceAccount == null || resourceAccount.isBlank() ? resourceOwnerAccountId : resourceAccount);
putResourceAccount(conditions, resourceArn, resourceOwnerAccountId, action);
putIfPresent(conditions, "aws:RequestedRegion", AwsArnUtils.regionOrDefault(resourceArn, null));
return conditions;
}
@@ -1294,7 +1294,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
for (Map<String, List<String>> targetContext : resourceContexts) {
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
targetContext, resource, region, accountId, resourceOwnerAccountId);
targetContext, resource, region, accountId, resourceOwnerAccountId, action);
ResourcePolicyDecision resourcePolicyDecision = evaluator.evaluateResourcePolicyFor(
effectiveResourcePolicies, principal, action, resource, effectiveContext);
Decision decision = evaluator.evaluateResolvedResourcePolicy(
@@ -1573,7 +1573,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
String region = requestContext.getRegion() == null
? config.defaultRegion() : requestContext.getRegion();
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
conditionContext, resource, region, accountId, effectiveOwnerAccountId);
conditionContext, resource, region, accountId, effectiveOwnerAccountId, action);
if (effectiveDecision == null) {
effectiveDecision = evaluator.evaluateResourcePolicyFor(
policyDocs.isEmpty() ? null : policyDocs,
@@ -83,7 +83,7 @@ public class ServiceCallAuthorizer {
.filter(document -> document != null && !document.isBlank())
.toList();
Map<String, List<String>> context = IamConditionContextResolver.serviceCallContext(
servicePrincipal, resourceArn, ownerAccountId, sourceArn, sourceAccount);
servicePrincipal, resourceArn, ownerAccountId, sourceArn, sourceAccount, action);
context.putAll(requestKeys);
Decision decision = evaluator.evaluateServicePrincipal(
documents.isEmpty() ? null : documents, servicePrincipal, action, resourceArn, context);
@@ -106,7 +106,7 @@ public class ServiceRoleAuthorizer {
String resourceAccount = resource != null ? resource.accountId() : accountId;
Map<String, List<String>> context = IamConditionContextResolver.withGlobalContext(
Map.of("aws:PrincipalArn", List.of(roleArn)),
permission.resourceArn(), region, accountId, resourceAccount);
permission.resourceArn(), region, accountId, resourceAccount, permission.action());
return evaluator.evaluate(caller, List.of(), permission.action(), permission.resourceArn(), context);
}
}
@@ -17,7 +17,8 @@ import java.util.function.Supplier;
* {@code aws:TagKeys}, read from the table {@code tools/aws/regen_s3control_tag_condition_keys.py}
* generates: the actions the Service Reference gives either key, and the request members botocore's
* {@code s3control} model carries them in. S3 Control's routes are checked as the action of their
* operation's name, so an action names its operation's members.
* operation's name, so an action names its operation's members. The table also records
* {@code s3:ResourceAccount} for S3 and S3 Control authorization, supplied from the resolved owner.
*/
public final class S3ControlTagConditionKeys {
@@ -66,7 +67,8 @@ public final class S3ControlTagConditionKeys {
return conditions.isEmpty() ? null : conditions;
}
private static boolean takes(String action, String key) {
/** Whether the Service Reference lists the condition key for this S3 action. */
public static boolean takes(String action, String key) {
for (JsonNode taken : TABLE.path("actions").path(action == null ? "" : action)) {
if (key.equals(taken.asText())) {
return true;