mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
7bac1e356a
commit
16704ff32b
13 files changed
+721
-75
No files matched your search
+28
-13
@@ -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;
|
||||
|
||||
@@ -6,177 +6,625 @@
|
||||
"note": "make s3control-tag-condition-keys-check re-derives members from botocore; make s3control-tag-condition-keys-verify checks actions against AWS."
|
||||
},
|
||||
"actions": {
|
||||
"s3:AbortMultipartUpload": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:AssociateAccessGrantsIdentityCenter": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:BypassGovernanceRetention": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateAccessGrant": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateAccessGrantsInstance": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateAccessGrantsLocation": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateAccessPoint": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateAccessPointForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateBucket": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateBucketMetadataTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateJob": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateMultiRegionAccessPoint": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:CreateStorageLensGroup": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessGrant": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessGrantsInstance": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessGrantsInstanceResourcePolicy": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessGrantsLocation": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessPoint": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessPointForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessPointPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteAccessPointPolicyForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteBucket": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteBucketMetadataTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteBucketPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteBucketWebsite": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteJobTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteMultiRegionAccessPoint": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObject": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObjectAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObjectTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObjectVersion": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObjectVersionAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteObjectVersionTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteStorageLensConfiguration": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteStorageLensConfigurationTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DeleteStorageLensGroup": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DescribeJob": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DescribeMultiRegionAccessPointOperation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:DissociateAccessGrantsIdentityCenter": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccelerateConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessGrant": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessGrantsInstance": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessGrantsInstanceForPrefix": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessGrantsInstanceResourcePolicy": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessGrantsLocation": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPoint": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointConfigurationForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointPolicyForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointPolicyStatus": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccessPointPolicyStatusForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAccountPublicAccessBlock": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetAnalyticsConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketAbac": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketCORS": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketLocation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketLogging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketMetadataTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketNotification": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketObjectLockConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketOwnershipControls": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketPolicyStatus": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketPublicAccessBlock": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketRequestPayment": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketVersioning": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetBucketWebsite": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetDataAccess": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetEncryptionConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetIntelligentTieringConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetInventoryConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetJobTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetLifecycleConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetMetricsConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetMultiRegionAccessPoint": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetMultiRegionAccessPointPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetMultiRegionAccessPointPolicyStatus": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetMultiRegionAccessPointRoutes": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObject": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectAttributes": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectLegalHold": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectRetention": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectTorrent": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersion": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionAnnotationForReplication": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionAttributes": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionForReplication": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetObjectVersionTorrent": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetReplicationConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetStorageLensConfiguration": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetStorageLensConfigurationTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetStorageLensDashboard": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:GetStorageLensGroup": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:InitiateReplication": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAccessGrants": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAccessGrantsInstances": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAccessGrantsLocations": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAccessPoints": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAccessPointsForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListAllMyBuckets": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListBucket": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListBucketMultipartUploads": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListBucketVersions": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListCallerAccessGrants": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListJobs": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListMultiRegionAccessPoints": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListMultipartUploadParts": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListObjectAnnotations": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListObjectVersionAnnotations": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListStorageLensConfigurations": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListStorageLensGroups": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ListTagsForResource": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ObjectOwnerOverrideToBucketOwner": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PauseReplication": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccelerateConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccessGrantsInstanceResourcePolicy": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccessPointConfigurationForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccessPointPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccessPointPolicyForObjectLambda": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAccountPublicAccessBlock": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutAnalyticsConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketAbac": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketCORS": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketLogging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketNotification": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketObjectLockConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketOwnershipControls": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketPublicAccessBlock": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketRequestPayment": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketVersioning": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutBucketWebsite": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutEncryptionConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutIntelligentTieringConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutInventoryConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutJobTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutLifecycleConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutMetricsConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutMultiRegionAccessPointPolicy": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObject": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectLegalHold": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectRetention": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectVersionAcl": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectVersionAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutObjectVersionTagging": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutReplicationConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutStorageLensConfiguration": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:PutStorageLensConfigurationTagging": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ReplicateDelete": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ReplicateObject": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ReplicateObjectAnnotation": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:ReplicateTags": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:RestoreObject": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:SubmitMultiRegionAccessPointRoutes": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:TagResource": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UntagResource": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateAccessGrantsLocation": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateBucketMetadataAnnotationTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateBucketMetadataInventoryTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateBucketMetadataJournalTableConfiguration": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateJobPriority": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateJobStatus": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateObjectEncryption": [
|
||||
"s3:ResourceAccount"
|
||||
],
|
||||
"s3:UpdateStorageLensGroup": [
|
||||
"aws:RequestTag/${TagKey}",
|
||||
"aws:TagKeys"
|
||||
"aws:TagKeys",
|
||||
"s3:ResourceAccount"
|
||||
]
|
||||
},
|
||||
"members": {
|
||||
|
||||
+26
-5
@@ -27,6 +27,8 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -1132,6 +1134,7 @@ class IamConditionContextResolverTest {
|
||||
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
|
||||
assertEquals(List.of("eu-west-2"), conditions.get("aws:RequestedRegion"));
|
||||
assertEquals(List.of("false"), conditions.get("aws:PrincipalIsAWSService"));
|
||||
assertFalse(conditions.containsKey("s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1145,16 +1148,19 @@ class IamConditionContextResolverTest {
|
||||
assertFalse(conditions.containsKey("aws:SecureTransport"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceAccountIsTheBucketOwnerWhenAnS3ArnCarriesNoAccount() {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"arn:aws:s3:::partner-bucket", "arn:aws:s3:::partner-bucket/report.csv",
|
||||
"arn:aws-cn:s3:::partner-bucket/report.csv", "arn:aws-us-gov:s3:::partner-bucket"})
|
||||
void resourceAccountIsTheBucketOwnerWhenAnS3ArnCarriesNoAccount(String resourceArn) {
|
||||
// S3 bucket and object ARNs have an empty account segment by design, so the owner has to
|
||||
// come from service state. Falling back to the caller would make a cross-account bucket
|
||||
// look like the caller's own.
|
||||
Map<String, List<String>> conditions = IamConditionContextResolver.withGlobalContext(
|
||||
null, "arn:aws:s3:::partner-bucket/report.csv", "eu-west-2",
|
||||
"000000000000", "111111111111");
|
||||
null, resourceArn, "eu-west-2",
|
||||
"000000000000", "111111111111", "s3:GetObject");
|
||||
|
||||
assertEquals(List.of("111111111111"), conditions.get("aws:ResourceAccount"));
|
||||
assertEquals(List.of("111111111111"), conditions.get("s3:ResourceAccount"));
|
||||
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
|
||||
}
|
||||
|
||||
@@ -1165,6 +1171,7 @@ class IamConditionContextResolverTest {
|
||||
|
||||
assertFalse(conditions.containsKey("aws:ResourceAccount"),
|
||||
"guessing the owner would authorize cross-account access a Condition meant to refuse");
|
||||
assertFalse(conditions.containsKey("s3:ResourceAccount"));
|
||||
assertEquals(List.of("000000000000"), conditions.get("aws:PrincipalAccount"));
|
||||
}
|
||||
|
||||
@@ -1203,11 +1210,25 @@ class IamConditionContextResolverTest {
|
||||
@Test
|
||||
void aServiceCallWithNoSourceLeavesTheSourceKeysOut() {
|
||||
Map<String, List<String>> conditions = IamConditionContextResolver.serviceCallContext(
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null);
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null, "s3:PutObject");
|
||||
|
||||
assertFalse(conditions.containsKey("aws:SourceArn"));
|
||||
assertFalse(conditions.containsKey("aws:SourceAccount"));
|
||||
assertEquals(List.of("111122223333"), conditions.get("aws:ResourceAccount"));
|
||||
assertEquals(List.of("111122223333"), conditions.get("s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"s3:AllowVendedLogDeliveryForResource", "s3:PutAccessPointPublicAccessBlock", "s3:UnknownAction", "sns:Publish"})
|
||||
void resourceAccountKeyIsAbsentForActionsNotListingIt(String action) {
|
||||
Map<String, List<String>> signed = IamConditionContextResolver.withGlobalContext(
|
||||
null, "arn:aws:s3:::bucket", "us-east-1", "222233334444", "111122223333", action);
|
||||
Map<String, List<String>> service = IamConditionContextResolver.serviceCallContext(
|
||||
"ses.amazonaws.com", "arn:aws:s3:::bucket", "111122223333", null, null, action);
|
||||
for (Map<String, List<String>> context : List.of(signed, service)) {
|
||||
assertEquals(List.of("111122223333"), context.get("aws:ResourceAccount"));
|
||||
assertFalse(context.containsKey("s3:ResourceAccount"));
|
||||
}
|
||||
}
|
||||
|
||||
private Decision decisionForEveryTarget(String policy, String action, ContainerRequestContext request) {
|
||||
|
||||
@@ -2486,7 +2486,7 @@ class IamEnforcementFilterTest {
|
||||
.thenReturn(conditions);
|
||||
|
||||
Map<String, List<String>> expected = IamConditionContextResolver.withGlobalContext(
|
||||
conditions, "arn:aws:s3:::partner-bucket", "us-east-1", "222233334444", "111111111111");
|
||||
conditions, "arn:aws:s3:::partner-bucket", "us-east-1", "222233334444", "111111111111", "s3:ListBucket");
|
||||
assertEquals(List.of("111111111111"), expected.get("aws:ResourceAccount"));
|
||||
|
||||
when(evaluator.evaluateResourcePolicyFor(any(), any(), anyString(), anyString(), any()))
|
||||
|
||||
+15
-1
@@ -128,7 +128,7 @@ class ServiceCallAuthorizerTest {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"s3:GetObjectVersion","Resource":"%s",
|
||||
"Condition":{"StringEquals":{"s3:VersionId":"v2"}}}]}""".formatted(object);
|
||||
"Condition":{"StringEquals":{"s3:VersionId":"v2","s3:ResourceAccount":"%s"}}}]}""".formatted(object, OWNER);
|
||||
providers.add((scope, arn) -> object.equals(arn)
|
||||
? List.of(new ResourcePolicyProvider.ResourcePolicy(policy, OWNER)) : List.of());
|
||||
|
||||
@@ -139,6 +139,20 @@ class ServiceCallAuthorizerTest {
|
||||
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObjectVersion", object, null, OWNER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServiceCallOmitsResourceAccountWhenItsActionDoesNotListTheKey() {
|
||||
String resource = "arn:aws:s3:us-east-1:" + OWNER + ":accesspoint/reports";
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"s3:*","Resource":"*",
|
||||
"Condition":{"Null":{"s3:ResourceAccount":"true"}}}]}""";
|
||||
providers.add((scope, arn) -> List.of(new ResourcePolicyProvider.ResourcePolicy(policy, OWNER)));
|
||||
|
||||
assertEquals(Decision.ALLOW,
|
||||
authorizer.authorize("lambda", "s3:PutAccessPointPublicAccessBlock", resource, null, OWNER));
|
||||
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObject", resource, null, OWNER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDenyInTheTopicPolicyWins() {
|
||||
topicPolicy("""
|
||||
|
||||
+12
@@ -8,7 +8,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3ControlTagConditionKeysTest {
|
||||
|
||||
@@ -17,6 +19,16 @@ class S3ControlTagConditionKeysTest {
|
||||
+ "<Tag><Key>Env</Key><Value>dev</Value></Tag>"
|
||||
+ "</Tags></TagResourceRequest>";
|
||||
|
||||
@Test
|
||||
void resourceAccountCoverageComesFromTheActionTable() {
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:GetObject", "s3:ResourceAccount"));
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:PutObject", "s3:ResourceAccount"));
|
||||
assertTrue(S3ControlTagConditionKeys.takes("s3:ListBucket", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes("s3:PutAccessPointPublicAccessBlock", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes("s3:AllowVendedLogDeliveryForResource", "s3:ResourceAccount"));
|
||||
assertFalse(S3ControlTagConditionKeys.takes(null, "s3:ResourceAccount"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceTakesEachTagOfItsBodyAndTheirKeys() {
|
||||
assertEquals(Map.of("aws:RequestTag/Owner", List.of("team"), "aws:RequestTag/Env", List.of("dev"),
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.github.hectorvent.floci.testing.S3IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testutil.S3RequestSigner;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.Credentials;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(S3IamEnforcementProfile.class)
|
||||
class S3ResourceAccountConditionIntegrationTest {
|
||||
private static final String OWNER = "111122223333";
|
||||
private static final String CALLER = "222233334444";
|
||||
private static final String ALLOW_S3 = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:*","Resource":"*"}]}
|
||||
""";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aws:ResourceAccount", "s3:ResourceAccount"})
|
||||
void sessionPolicyUsesBucketOwnerAndStillRestrictsPrefixAndHonorsDeny(String conditionKey) {
|
||||
String suffix = UUID.randomUUID().toString();
|
||||
String bucket = "resource-account-" + suffix;
|
||||
String user = "resource-account-" + suffix;
|
||||
String role = "resource-account-" + suffix;
|
||||
UserCredentials ownerKeys = EnforcementFixtures.createUser(
|
||||
cleanup, endpoint.getPort(), auth(OWNER, "iam"), user);
|
||||
EnforcementFixtures.putUserPolicy(endpoint.getPort(), auth(OWNER, "iam"), user, "s3", ALLOW_S3);
|
||||
cleanup.register(() -> EnforcementFixtures.removeRole(endpoint.getPort(), auth(CALLER, "iam"), role));
|
||||
cleanup.register(() -> EnforcementFixtures.removeBucket(endpoint.getPort(),
|
||||
S3RequestSigner.signedAs(ownerKeys.accessKeyId(), ownerKeys.secretAccessKey()), bucket));
|
||||
try (IamClient iam = AwsTestClients.create(IamClient.builder(), CALLER, Region.US_EAST_1);
|
||||
StsClient sts = AwsTestClients.create(StsClient.builder(), CALLER, Region.US_EAST_1);
|
||||
S3Client owner = client(AwsBasicCredentials.create(ownerKeys.accessKeyId(), ownerKeys.secretAccessKey()))) {
|
||||
String roleArn = iam.createRole(request -> request.roleName(role).assumeRolePolicyDocument("""
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
|
||||
"Principal":{"AWS":"arn:aws:iam::%s:root"}}]}
|
||||
""".formatted(CALLER))).role().arn();
|
||||
iam.putRolePolicy(request -> request.roleName(role).policyName("s3").policyDocument(ALLOW_S3));
|
||||
owner.createBucket(request -> request.bucket(bucket));
|
||||
String grant = """
|
||||
{"Effect":"Allow","Principal":{"AWS":"%s"},"Action":"s3:*",
|
||||
"Resource":["arn:aws:s3:::%s","arn:aws:s3:::%s/*"]}
|
||||
""".formatted(roleArn, bucket, bucket);
|
||||
owner.putBucketPolicy(request -> request.bucket(bucket).policy(
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" + grant + "]}"));
|
||||
try (S3Client matching = session(sts, roleArn, bucket, conditionKey, OWNER);
|
||||
S3Client wrongOwner = session(sts, roleArn, bucket, conditionKey, CALLER)) {
|
||||
matching.putObject(request -> request.bucket(bucket).key("allowed/index-0"),
|
||||
RequestBody.fromString("snapshot-data"));
|
||||
assertEquals("snapshot-data", matching.getObjectAsBytes(
|
||||
request -> request.bucket(bucket).key("allowed/index-0")).asUtf8String());
|
||||
assertEquals(1, matching.listObjectsV2(request -> request.bucket(bucket).prefix("allowed/")).keyCount());
|
||||
denied(() -> matching.listObjectsV2(request -> request.bucket(bucket).prefix("other/")));
|
||||
denied(() -> wrongOwner.listObjectsV2(request -> request.bucket(bucket).prefix("allowed/")));
|
||||
denied(() -> wrongOwner.getObjectAsBytes(request -> request.bucket(bucket).key("allowed/index-0")));
|
||||
|
||||
String deny = """
|
||||
{"Effect":"Deny","Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"StringEquals":{"%s":"%s"}}}
|
||||
""".formatted(bucket, conditionKey, OWNER);
|
||||
owner.putBucketPolicy(request -> request.bucket(bucket).policy(
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[" + grant + "," + deny + "]}"));
|
||||
denied(() -> matching.getObjectAsBytes(request -> request.bucket(bucket).key("allowed/index-0")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private S3Client session(StsClient sts, String roleArn, String bucket, String conditionKey, String owner) {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListBucket","Resource":"arn:aws:s3:::%1$s",
|
||||
"Condition":{"StringLike":{"s3:prefix":"allowed/*","%2$s":"%3$s"}}},
|
||||
{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject"],"Resource":"arn:aws:s3:::%1$s/allowed/*",
|
||||
"Condition":{"StringEquals":{"%2$s":"%3$s"}}}]}
|
||||
""".formatted(bucket, conditionKey, owner);
|
||||
Credentials credentials = sts.assumeRole(request -> request.roleArn(roleArn)
|
||||
.roleSessionName("snapshot-" + owner).policy(policy)).credentials();
|
||||
return client(AwsSessionCredentials.create(
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), credentials.sessionToken()));
|
||||
}
|
||||
|
||||
private S3Client client(AwsCredentials credentials) {
|
||||
return S3Client.builder().endpointOverride(endpoint).region(Region.US_EAST_1).forcePathStyle(true)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(credentials)).build();
|
||||
}
|
||||
|
||||
private static void denied(Runnable operation) {
|
||||
S3Exception error = assertThrows(S3Exception.class, operation::run);
|
||||
assertEquals(403, error.statusCode());
|
||||
assertEquals("AccessDenied", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ when its action takes them. The table comes from two sources, each answering wha
|
||||
- botocore's `s3control` model (Apache-2.0, https://github.com/boto/botocore, the pinned release),
|
||||
which gives each request's members but not which of them AWS checks.
|
||||
|
||||
`actions` holds every `s3` action that takes either key. `members` holds, for every S3 Control
|
||||
`actions` holds every `s3` action that takes either tag key or `s3:ResourceAccount`.
|
||||
The latter is populated from the resolved resource owner, not a request member. `members` holds, for every S3 Control
|
||||
operation, as `s3:<Operation>`, the request members that feed each key: a top-level body member
|
||||
holding tags, found as `regen_iam_tag_on_create.py` finds them (a list of key and value structures,
|
||||
named by its list element, or a map of strings), feeds both keys, and a top-level query string list
|
||||
@@ -60,11 +61,11 @@ BODY_LOCATIONS = (None, "")
|
||||
|
||||
|
||||
def action_keys(reference: dict) -> dict[str, list[str]]:
|
||||
"""Every action of the namespace that takes a request tag key, with the ones it takes."""
|
||||
"""Every action that takes a request tag or resource-account key, with the keys it takes."""
|
||||
resource_keys = _resource_keys([reference])
|
||||
found = {}
|
||||
for action in sorted(reference["Actions"], key=lambda entry: entry["Name"]):
|
||||
keys = REQUEST_TAG_KEYS & _condition_keys(NAMESPACE, action, resource_keys)
|
||||
keys = (REQUEST_TAG_KEYS | {"s3:ResourceAccount"}) & _condition_keys(NAMESPACE, action, resource_keys)
|
||||
if keys:
|
||||
found[f"{NAMESPACE}:{action['Name']}"] = sorted(keys)
|
||||
return found
|
||||
|
||||
@@ -8,11 +8,11 @@ from regen_s3control_tag_condition_keys import OUTPUT, action_keys, request_memb
|
||||
|
||||
|
||||
def test_action_and_resource_keys_are_combined_and_filtered():
|
||||
reference = {"Name": "s3", "Resources": [{"Name": "job", "ConditionKeys": ["aws:RequestTag/${TagKey}", "s3:JobSuspendedCause"]}],
|
||||
reference = {"Name": "s3", "Resources": [{"Name": "job", "ConditionKeys": ["aws:RequestTag/${TagKey}", "s3:JobSuspendedCause", "s3:ResourceAccount"]}],
|
||||
"Actions": [{"Name": "TagResource", "ActionConditionKeys": ["aws:TagKeys", "s3:authType"]},
|
||||
{"Name": "PutJobTagging", "Resources": [{"Name": "job"}]},
|
||||
{"Name": "ListAccessPoints", "ActionConditionKeys": ["s3:authType"]}]}
|
||||
assert action_keys(reference) == {"s3:PutJobTagging": ["aws:RequestTag/${TagKey}"],
|
||||
assert action_keys(reference) == {"s3:PutJobTagging": ["aws:RequestTag/${TagKey}", "s3:ResourceAccount"],
|
||||
"s3:TagResource": ["aws:TagKeys"]}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ def test_body_tags_feed_both_keys_and_query_tag_keys_feed_tag_keys():
|
||||
def test_the_vendored_table_reads_tag_resource_and_untag_resource():
|
||||
document = json.loads(OUTPUT.read_text())
|
||||
assert validate(document) == []
|
||||
assert document["actions"]["s3:TagResource"] == ["aws:RequestTag/${TagKey}", "aws:TagKeys"]
|
||||
assert document["actions"]["s3:TagResource"] == ["aws:RequestTag/${TagKey}", "aws:TagKeys", "s3:ResourceAccount"]
|
||||
assert "aws:TagKeys" in document["actions"]["s3:UntagResource"]
|
||||
assert document["members"]["s3:TagResource"]["aws:RequestTag/${TagKey}"] == [
|
||||
{"member": "Tags[Tag]", "key": "Key", "value": "Value"}]
|
||||
@@ -85,3 +85,13 @@ def run_aggregate_gate(tmp_path, changed, fail):
|
||||
["make", "--no-print-directory", "iam-data-verify", "CHANGED=" + changed,
|
||||
"MAKE=" + str(recursive_make)],
|
||||
cwd=OUTPUT.parents[4], capture_output=True, text=True, check=False)
|
||||
|
||||
|
||||
def test_resource_account_only_action_and_resource_coverage():
|
||||
reference = {"Name": "s3", "Resources": [
|
||||
{"Name": "object", "ConditionKeys": ["s3:ResourceAccount"]}], "Actions": [
|
||||
{"Name": "GetObject", "Resources": [{"Name": "object"}]},
|
||||
{"Name": "ListBucket", "ActionConditionKeys": ["s3:ResourceAccount"]},
|
||||
{"Name": "PutAccessPointPublicAccessBlock"}]}
|
||||
assert action_keys(reference) == {
|
||||
"s3:GetObject": ["s3:ResourceAccount"], "s3:ListBucket": ["s3:ResourceAccount"]}
|
||||
Reference in new issue
Block a user