-
Notifications
You must be signed in to change notification settings - Fork 426
fix: reduce AWS STS inline policy size by merging read+write actions #4228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yushesp
wants to merge
1
commit into
apache:main
Choose a base branch
from
yushesp:fix/sts-policy-size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−39
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| import java.net.URI; | ||
| import java.util.EnumSet; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
@@ -51,6 +52,7 @@ | |
| import software.amazon.awssdk.services.sts.StsClient; | ||
| import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; | ||
| import software.amazon.awssdk.services.sts.model.AssumeRoleResponse; | ||
| import software.amazon.awssdk.services.sts.model.PackedPolicyTooLargeException; | ||
| import software.amazon.awssdk.services.sts.model.Tag; | ||
|
|
||
| /** Credential vendor that supports generating */ | ||
|
|
@@ -151,7 +153,17 @@ public StorageAccessConfig getSubscopedCreds( | |
| StsClient stsClient = | ||
| stsClientProvider.stsClient(StsDestination.of(storageConfig.getStsEndpointUri(), region)); | ||
|
|
||
| AssumeRoleResponse response = stsClient.assumeRole(request.build()); | ||
| AssumeRoleResponse response; | ||
| try { | ||
| response = stsClient.assumeRole(request.build()); | ||
| } catch (PackedPolicyTooLargeException e) { | ||
| throw new RuntimeException( | ||
| "AWS STS rejected the session policy: it exceeds the 2048-character packed-policy" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was confused when I saw the corresponding But it's actually correct. I was misleaded by the comment and naming. |
||
| + " limit. This commonly happens with deeply nested namespace hierarchies or" | ||
| + " long namespace names. Consider shortening the namespace path or splitting" | ||
| + " the hierarchy.", | ||
| e); | ||
| } | ||
| accessConfig.put(StorageAccessProperty.AWS_KEY_ID, response.credentials().accessKeyId()); | ||
| accessConfig.put( | ||
| StorageAccessProperty.AWS_SECRET_KEY, response.credentials().secretAccessKey()); | ||
|
|
@@ -208,11 +220,16 @@ private boolean shouldUseKms(AwsStorageConfigurationInfo storageConfig) { | |
| } | ||
|
|
||
| /** | ||
| * generate an IamPolicy from the input readLocations and writeLocations, optionally with list | ||
| * Generate an IamPolicy from the input readLocations and writeLocations, optionally with list | ||
| * support. Credentials will be scoped to exactly the resources provided. If read and write | ||
| * locations are empty, a non-empty policy will be generated that grants GetObject and optionally | ||
| * ListBucket privileges with no resources. This prevents us from sending an empty policy to AWS | ||
| * and just assuming the role with full privileges. | ||
| * | ||
| * <p>Write locations are emitted in a single combined statement that grants both read and write | ||
| * actions, rather than in a separate write-only statement that duplicates the resource ARN | ||
| * already listed in the read statement. This keeps the packed inline policy within AWS STS's | ||
| * 2048-character limit for deeply nested namespace hierarchies. | ||
| */ | ||
| private IamPolicy policyString( | ||
| AwsStorageConfigurationInfo storageConfigurationInfo, | ||
|
|
@@ -221,25 +238,24 @@ private IamPolicy policyString( | |
| Set<String> writeLocations, | ||
| String region) { | ||
| IamPolicy.Builder policyBuilder = IamPolicy.builder(); | ||
| IamStatement.Builder allowGetObjectStatementBuilder = | ||
| IamStatement.builder() | ||
| .effect(IamEffect.ALLOW) | ||
| .addAction("s3:GetObject") | ||
| .addAction("s3:GetObjectVersion"); | ||
| Map<String, IamStatement.Builder> bucketListStatementBuilder = new HashMap<>(); | ||
| Map<String, IamStatement.Builder> bucketGetLocationStatementBuilder = new HashMap<>(); | ||
|
|
||
| String arnPrefix = arnPrefixForPartition(storageConfigurationInfo.getAwsPartition()); | ||
| String currentKmsKey = storageConfigurationInfo.getCurrentKmsKey(); | ||
| List<String> allowedKmsKeys = storageConfigurationInfo.getAllowedKmsKeys(); | ||
|
|
||
| // Locations readable but not writable receive only read actions; everything in writeLocations | ||
| // receives read and write actions from a single combined statement below. Splitting this way | ||
| // avoids listing every write-location ARN twice. | ||
| Set<String> readOnlyLocations = new HashSet<>(readLocations); | ||
| readOnlyLocations.removeAll(writeLocations); | ||
| boolean canWrite = !writeLocations.isEmpty(); | ||
|
|
||
| Map<String, IamStatement.Builder> bucketListStatementBuilder = new HashMap<>(); | ||
| Map<String, IamStatement.Builder> bucketGetLocationStatementBuilder = new HashMap<>(); | ||
| Stream.concat(readLocations.stream(), writeLocations.stream()) | ||
| .distinct() | ||
| .forEach( | ||
| location -> { | ||
| URI uri = URI.create(location); | ||
| allowGetObjectStatementBuilder.addResource( | ||
| IamResource.create( | ||
| arnPrefix + StorageUtil.concatFilePrefixes(parseS3Path(uri), "*", "/"))); | ||
| final var bucket = arnPrefix + StorageUtil.getBucket(uri); | ||
| if (allowList) { | ||
| bucketListStatementBuilder | ||
|
|
@@ -264,21 +280,22 @@ private IamPolicy policyString( | |
| .addResource(key)); | ||
| }); | ||
|
|
||
| boolean canWrite = !writeLocations.isEmpty(); | ||
| if (canWrite) { | ||
| IamStatement.Builder allowPutObjectStatementBuilder = | ||
| IamStatement.Builder allowReadWriteStatementBuilder = | ||
| IamStatement.builder() | ||
| .effect(IamEffect.ALLOW) | ||
| .addAction("s3:GetObject") | ||
| .addAction("s3:GetObjectVersion") | ||
| .addAction("s3:PutObject") | ||
| .addAction("s3:DeleteObject"); | ||
| writeLocations.forEach( | ||
| location -> { | ||
| URI uri = URI.create(location); | ||
| allowPutObjectStatementBuilder.addResource( | ||
| allowReadWriteStatementBuilder.addResource( | ||
| IamResource.create( | ||
| arnPrefix + StorageUtil.concatFilePrefixes(parseS3Path(uri), "*", "/"))); | ||
| }); | ||
| policyBuilder.addStatement(allowPutObjectStatementBuilder.build()); | ||
| policyBuilder.addStatement(allowReadWriteStatementBuilder.build()); | ||
| } | ||
| if (shouldUseKms(storageConfigurationInfo)) { | ||
| addKmsKeyPolicy( | ||
|
|
@@ -302,7 +319,26 @@ private IamPolicy policyString( | |
| bucketGetLocationStatementBuilder | ||
| .values() | ||
| .forEach(statementBuilder -> policyBuilder.addStatement(statementBuilder.build())); | ||
| return policyBuilder.addStatement(allowGetObjectStatementBuilder.build()).build(); | ||
|
|
||
| // Emit a read-only statement when there are read-only locations, or as a fallback with no | ||
| // resources when no combined read+write statement was emitted — ensuring the inline policy is | ||
| // always non-empty. | ||
| if (!readOnlyLocations.isEmpty() || !canWrite) { | ||
| IamStatement.Builder allowGetObjectStatementBuilder = | ||
| IamStatement.builder() | ||
| .effect(IamEffect.ALLOW) | ||
| .addAction("s3:GetObject") | ||
| .addAction("s3:GetObjectVersion"); | ||
| readOnlyLocations.forEach( | ||
| location -> { | ||
| URI uri = URI.create(location); | ||
| allowGetObjectStatementBuilder.addResource( | ||
| IamResource.create( | ||
| arnPrefix + StorageUtil.concatFilePrefixes(parseS3Path(uri), "*", "/"))); | ||
| }); | ||
| policyBuilder.addStatement(allowGetObjectStatementBuilder.build()); | ||
| } | ||
| return policyBuilder.build(); | ||
| } | ||
|
|
||
| private static void addKmsKeyPolicy( | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I would expect some more specific than
RuntimeExceptionhere but as the rest of the storage layer is also usingRuntimeException, it's consistent and make sense 😄