Skip to content

[Server] Read S3 bucket names that are not host names in session policies and region discovery - #2264

Open
yili-db wants to merge 5 commits into
unitycatalog:mainfrom
yili-db:fix/policy_bucket_authority
Open

yili-db wants to merge 5 commits into
unitycatalog:mainfrom
yili-db:fix/policy_bucket_authority

Conversation

@yili-db

@yili-db yili-db commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Session policies on vended S3 credentials, and the region discovery of AWS S3 buckets, used the wrong bucket for valid bucket names that are not valid URI host names.

AwsPolicyGenerator grouped a credential's locations by URI::getHost. Java's URI parser returns null for a host such as data.2024 (its last label starts with a digit) or my_bucket (an underscore), although both are valid S3 bucket names. The policy then scoped access to arn:aws:s3:::null/..., so the vended credential could not reach the table.

  • NormalizedURL.getS3Bucket reads the bucket from the raw authority of an s3:// URL and takes only bucket-name characters (letters, digits, '.', '_' and '-', starting with a letter or digit); anything else, such as *, prod-*, a % escape, user info, a port, no bucket or another scheme, is INVALID_ARGUMENT, so it never reaches a session policy, where arn:aws:s3:::* would name every bucket
  • NormalizedURL.isS3Path, which checks the URLs of s3_compatible credentials' locations, uses the same rule, so a location is refused at creation for what vending would refuse later
  • AwsPolicyGenerator.getBucketToPathsMap groups by NormalizedURL::getS3Bucket
  • FileOperationsImpl reads the bucket with getS3Bucket too, on the URL as written, before the configured-region lookup and the shared S3BucketRegions lookup (getStorageBase() decodes escapes, so s3://%62ucket/t became s3://bucket)
  • StorageCredentialVendorImpl passes getS3Bucket to the S3-compatible region lookup, instead of URI::getAuthority
  • ExternalLocationRepository reads the bucket with getS3Bucket for the virtual-hosted bucket-name check of S3-compatible locations
  • FileOperationsTest.testGetFileIOConfigS3DiscoversRegionWhenNotConfigured covers the same three bucket names; it fails without the change
  • FileOperationsTest.testGetFileIOConfigS3RefusesAnInvalidBucketWithoutDiscovery: s3://%62ucket/t, s3://*/t and s3://user@bucket/t fail with INVALID_ARGUMENT and send no HeadBucket
  • NormalizedURLTest: getS3Bucket and isS3Path for valid names, and for no bucket, a bad first character, a wildcard, an escape, user info, a port and other schemes
  • AwsPolicyGeneratorTest.testPolicyLeavesOrdinaryPathUnchanged checks the exact object and bucket resources and s3:prefix for my-bucket, data.2024 and my_bucket, and that s3://*/p, s3://prod-*/p, s3://%2A/p, s3://%62ucket/p, s3://user@bucket/p and s3://bucket:80/p are refused, each next to a valid location, with the URL in the message; it fails without the change

Follow-up from the review of #2172. It applies to AWS S3 and S3-compatible storage alike.

Tests: build/sbt "server/testOnly *AwsPolicyGeneratorTest *FileOperationsTest *NormalizedURLTest", and the server suite on H2 (dev/run-tests.py server/test).

Compatibility: no API or configuration change; session policies for bucket names that are valid host names are unchanged. One behavior change: an external location whose URL has user info or a port (s3://user@bucket/p, s3://bucket:80/p), or whose bucket is not a valid bucket name, now fails credential vending and Iceberg region lookup with INVALID_ARGUMENT, naming the URL. Before, URI::getHost dropped the user info and port, so such locations worked for AWS. Neither form is a valid S3 location. To fix one, update the external location's URL to s3://<bucket>/<path>. Locations of an s3_compatible_access_key credential with such URLs are now refused when they are created or updated.

This pull request and its description were written by Isaac.

@yili-db

yili-db commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db yili-db changed the title [Server] Name the bucket in session policies for bucket names that are not host names [Server] Read S3 bucket names that are not host names in session policies and region discovery Nov 17, 2026
@yili-db

yili-db commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db

yili-db commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@timothyw553 timothyw553 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core fix looks good. i left a few comments around keeping bucket validation consistent across callers, validating before getStorageBase(), test isolation, and calling out the compatibility change.

* @return the bucket name
* @throws BaseException {@code INVALID_ARGUMENT} if the authority is not a valid bucket name
*/
public String getS3Bucket() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to my understanding, isS3Path() and StorageCredentialVendorImpl (path.toUri().getAuthority()) still use different bucket rules than getS3Bucket(), and getS3Bucket() itself doesn't check for an s3:// scheme (gs://b/p returns "b"). should we share one scheme-aware check between isS3Path() and getS3Bucket(), and switch the S3-compatible lookup in StorageCredentialVendorImpl to path.getS3Bucket()?

- s3BucketRegions.getS3CompatibleRegion(endpoint, path.toUri().getAuthority())
+ s3BucketRegions.getS3CompatibleRegion(endpoint, path.getS3Bucket())

*/
private String discoverRegion(NormalizedURL storageBase) {
String bucket = storageBase.toUri().getHost();
String bucket = storageBase.getS3Bucket();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.getStorageBase() rebuilds the URL using uri.getAuthority(), which decodes escapes (s3://%62ucket/t becomes s3://bucket) and turns an empty authority (s3:///p) into s3://null before discoverRegion calls getS3Bucket(). should we validate path.getS3Bucket() before getRegion(path.getStorageBase()) (and add a test checking no HeadBucket call is made)?

 } else {
+  path.getS3Bucket();
   config.put(AwsClientProperties.CLIENT_REGION, getRegion(path.getStorageBase()));
 }

URI::getHost,
uri -> new LinkedList<>(List.of(uri.getPath())),
// Only bucket-name characters, so a "*" never widens the policy's ARNs.
NormalizedURL::getS3Bucket,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously URI::getHost extracted "bucket" from s3://user@bucket/p and s3://bucket:80/p, and AWS IAM role location creation doesn't run isS3Path(), so existing locations in those forms will now fail with INVALID_ARGUMENT when vending credentials. assuming rejecting those is intentional, could we call out this compatibility change and how to update existing locations in the PR description?

assertThatThrownBy(
() ->
AwsPolicyGenerator.generatePolicy(
Set.of(SELECT), List.of(NormalizedURL.from(location))))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: calling NormalizedURL.from(location) inside assertThatThrownBy means this test would still pass if URL construction threw instead of generatePolicy. could we construct the URLs outside the lambda (maybe also mixing a valid and invalid location and asserting the message), plus add direct getS3Bucket() cases in NormalizedURLTest for missing authorities, invalid leading characters, and non-s3:// schemes?

+ NormalizedURL invalid = NormalizedURL.from(location);
+ NormalizedURL valid = NormalizedURL.from("s3://valid/p");
  assertThatThrownBy(
      ()
          -> AwsPolicyGenerator.generatePolicy(
-             Set.of(SELECT), List.of(NormalizedURL.from(location))))
+             Set.of(SELECT), List.of(valid, invalid)))

@yili-db
yili-db force-pushed the fix/policy_bucket_authority branch from f36f609 to b7f556e Compare November 20, 2026 00:50
@yili-db

yili-db commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@yili-db
yili-db requested a review from timothyw553 November 20, 2026 00:51
@yili-db
yili-db force-pushed the fix/policy_bucket_authority branch 2 times, most recently from 6fe072c to 5e04f65 Compare December 2, 2026 17:35
yili-db added a commit that referenced this pull request Dec 2, 2026
…atible storage (#2267)

**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [x] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

**Description of changes**

The server looked up S3 bucket regions in two places with two sets of
rules: `FileOperationsImpl` for AWS buckets (the Iceberg
`client.region`) and `S3BucketRegions` for S3-compatible storage (the
vended signing region). Both send an anonymous HeadBucket, but only
`S3BucketRegions` bounds its cache, rechecks regions, backs off after
failures and runs one lookup per bucket. This shares that lookup,
keeping the two fallback rules explicit.

- `S3BucketRegions.getAwsRegion(bucket)` serves AWS buckets with the
same lookup as `getS3CompatibleRegion`:
- a reported region is cached per bucket, at most 10,000 buckets, and
asked again after an hour (the AWS map was unbounded and never
rechecked)
- a failed recheck keeps the reported region, and the bucket is asked
again only after the 30-second backoff
  - one lookup runs per bucket at a time
- an AWS bucket in backoff whose reported region was evicted is asked
again, never given the S3-compatible default (the two caches evict
apart)
- a failure to build the lookup client is handled as a failed request:
as before on S3-compatible storage; for an AWS bucket it is now a
retriable `INTERNAL` error instead of an unhandled exception
  - each request is capped at 10 seconds
- AWS lookups address the bucket virtual-hosted, the SDK default as
before; S3-compatible lookups keep the location's addressing style
(`path_style_access`)
- a 404 drops the reported region on S3-compatible storage, where the
bucket may be created again with another region; an AWS bucket keeps it,
as for any failed recheck (noted in the class javadoc)
- Fallbacks stay explicit and unchanged:
- S3-compatible storage without a known region uses `us-east-1` and asks
again later
- an AWS bucket without a known region fails with `FAILED_PRECONDITION`
(no region, such as no such bucket) or `INTERNAL` (transient), with the
same messages as before, and is asked again on the next request
- `FileOperationsImpl` keeps only the regions configured with
`s3.bucketPath.N`/`s3.region.N` and asks `S3BucketRegions` for the rest
- Tests:
-
`S3BucketRegionsTest.testAwsBucketKeepsItsReportedRegionOnAFailedRecheck`,
parameterized over a recheck that fails with 503, 404, 403 without a
region, 200 without a region, or a malformed region: the region is kept,
the 30-second backoff holds, and an eviction during it fails the lookup
(through a test-only `evictReportedRegion`)
- `testAwsBucketWithoutAKnownRegionHasNoDefault`: no default for an AWS
bucket
- `testConcurrentRequestsShareOneLookup`, for a success and for a
failing AWS lookup that a second caller waits on
- `testReportedRegionIsUsedAndCachedPerStorageAndBucket`: two AWS
buckets on one instance each get their own region, apart from the same
name on S3-compatible storage, and the lookup client has the 10-second
timeout
- `FileOperationsTest` discovers the region of `my_bucket`, which is not
a host name; the other region cases pass unchanged (their mock builder
now also stubs `overrideConfiguration`, for the timeout)

Follow-up from the review of #2172. #2264 fixes how both places read a
bucket name that is not a valid host name (`URI::getAuthority`); this PR
already reads it that way, so whichever merges second needs a small
rebase.

A configured region for S3-compatible storage, to avoid the lookup
entirely, is left for a separate proposal.

Tests: `build/sbt "server/testOnly *S3BucketRegionsTest
*FileOperationsTest"`, and the server suite on H2 (`dev/run-tests.py
server/test`).

Compatibility: no API or configuration change. An AWS bucket's region is
now asked again after an hour instead of cached for the server's
lifetime, and a recheck that fails keeps the earlier region. A malformed
`x-amz-bucket-region` header from AWS is now refused like a missing one
(`FAILED_PRECONDITION`, unless a region was reported before), as it
already was for S3-compatible storage.

This pull request and its description were written by Isaac.

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
yili-db and others added 5 commits December 2, 2026 18:00
…e not host names

- `AwsPolicyGenerator` grouped locations by `URI::getHost`, which is null for valid bucket names such as `data.2024` or `my_bucket`, so policies named `arn:aws:s3:::null/...`
- Group by `URI::getAuthority` instead
- `testPolicyLeavesOrdinaryPathUnchanged` checks the exact resources for such buckets

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
- `FileOperationsImpl.discoverRegion` read the bucket with `URI::getHost`, which is null for `data.2024` or `my_bucket`, so it asked HeadBucket about no bucket
- Read it with `URI::getAuthority`
- `testGetFileIOConfigS3DiscoversRegionWhenNotConfigured` covers such buckets

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
- `URI::getAuthority` passed any authority text into the policy ARNs unescaped, so `s3://*/p` named every bucket (`arn:aws:s3:::*`), and `%62ucket` decoded to another name
- `NormalizedURL.getS3Bucket` reads the raw authority and takes only letters, digits, '.', '_' and '-'; anything else is `INVALID_ARGUMENT`
- `AwsPolicyGenerator` and `FileOperationsImpl.discoverRegion` both use it
- `testPolicyLeavesOrdinaryPathUnchanged` refuses a wildcard, a partial wildcard, escapes, user info and a port

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
- `NormalizedURL.getS3Bucket` requires an `s3://` URL, and `isS3Path` shares its rule
- `FileOperationsImpl` checks the bucket on the URL as written, before `getStorageBase()` decodes escapes
- `StorageCredentialVendorImpl` passes `getS3Bucket` to the S3-compatible region lookup
- Tests:
  - `NormalizedURLTest`: valid and refused bucket forms
  - `FileOperationsTest`: an invalid bucket sends no HeadBucket
  - `AwsPolicyGeneratorTest`: URLs built outside the assertion, next to a valid location

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
- `ExternalLocationRepository.validateS3CompatibleSettings` reads the bucket with `NormalizedURL.getS3Bucket`, as the policy and region paths do
- `BaseExternalLocationCRUDTest`: `s3://-bucket` is not an `s3://` path with a bucket, so it moves from the virtual-hosted cases to the path cases

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Isaac <[email protected]>
@yili-db
yili-db force-pushed the fix/policy_bucket_authority branch from 5e04f65 to d982039 Compare December 2, 2026 18:05

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants