Skip to content

fix(s3): handle 301 redirect in bucket region detection#2000

Merged
frewilhelm merged 4 commits into
open-component-model:mainfrom
jakobmoellerdev:fix/s3-301-redirect-region-detection
Jun 12, 2026
Merged

fix(s3): handle 301 redirect in bucket region detection#2000
frewilhelm merged 4 commits into
open-component-model:mainfrom
jakobmoellerdev:fix/s3-301-redirect-region-detection

Conversation

@jakobmoellerdev

Copy link
Copy Markdown
Member

Summary

  • ocm download resources fails when an S3 bucket lives in a different region than the default hint (us-west-1)
  • AWS S3 returns HTTP 301 on HeadBucket to signal the correct region via X-Amz-Bucket-Region response header
  • AWS SDK v2 does not follow 301 redirects (only 307/308); the 301 is surfaced as a *smithyhttp.ResponseError
  • manager.GetBucketRegion's deserializeBucketRegion middleware only reads the header on non-error responses, so the 301 propagated as a fatal error

Error seen by users:

failed to find bucket region: operation error S3: HeadBucket,
https response error StatusCode: 301 ... api error MovedPermanently: Moved Permanently

Fix: after GetBucketRegion returns an error, inspect it as a *smithyhttp.ResponseError. If it is a 301 with X-Amz-Bucket-Region header present, extract the region and continue.

Reported in: #1999

Test plan

  • Unit tests cover: 301 with region header (extracts region), 301 without header (propagates error), 403 (ignored), plain network error (propagates)
  • go test ./api/utils/accessio/downloader/s3/... passes
  • go build ./... passes
  • Manual verification: ocm --verbose download resources -r <component-with-s3-resource-in-non-default-region>

AWS SDK v2 does not follow 301 redirects (only 307/308). S3 returns 301
on HeadBucket when the bucket is in a different region, signaling the
correct region via the X-Amz-Bucket-Region response header. The SDK's
GetBucketRegion middleware only reads that header on non-error responses,
so the 301 was propagated as a fatal error.

After GetBucketRegion returns an error, inspect it as a
*smithyhttp.ResponseError. If it is a 301 with the X-Amz-Bucket-Region
header present, extract the region and continue instead of failing.

Fixes: unable to read access: failed to find bucket region:
  operation error S3: HeadBucket, StatusCode: 301, MovedPermanently
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
@jakobmoellerdev jakobmoellerdev force-pushed the fix/s3-301-redirect-region-detection branch from 6b582b6 to bb06642 Compare June 11, 2026 08:34
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
@github-actions github-actions Bot added kind/dependency dependency update, etc. size/m Medium and removed size/s Small labels Jun 12, 2026
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
@frewilhelm frewilhelm marked this pull request as ready for review June 12, 2026 11:57
@frewilhelm frewilhelm requested a review from a team as a code owner June 12, 2026 11:57
@frewilhelm frewilhelm merged commit c87a0c3 into open-component-model:main Jun 12, 2026
20 checks passed
frewilhelm added a commit that referenced this pull request Jun 12, 2026
<!-- markdownlint-disable MD041 -->
#### What this PR does / why we need it

Cherry-pick of #1994 and #2000 from main to releases/0.44

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jakob Möller <jakob.moeller@sap.com>
matthiasbruns pushed a commit to knp-sap/ocm that referenced this pull request Jun 15, 2026
…nt-model#2000)

## Summary

- `ocm download resources` fails when an S3 bucket lives in a different
region than the default hint (`us-west-1`)
- AWS S3 returns HTTP 301 on `HeadBucket` to signal the correct region
via `X-Amz-Bucket-Region` response header
- AWS SDK v2 does not follow 301 redirects (only 307/308); the 301 is
surfaced as a `*smithyhttp.ResponseError`
- `manager.GetBucketRegion`'s `deserializeBucketRegion` middleware only
reads the header on non-error responses, so the 301 propagated as a
fatal error

**Error seen by users:**
```
failed to find bucket region: operation error S3: HeadBucket,
https response error StatusCode: 301 ... api error MovedPermanently: Moved Permanently
```

**Fix:** after `GetBucketRegion` returns an error, inspect it as a
`*smithyhttp.ResponseError`. If it is a 301 with `X-Amz-Bucket-Region`
header present, extract the region and continue.

Reported in: open-component-model#1999

## Test plan

- [ ] Unit tests cover: 301 with region header (extracts region), 301
without header (propagates error), 403 (ignored), plain network error
(propagates)
- [ ] `go test ./api/utils/accessio/downloader/s3/...` passes
- [ ] `go build ./...` passes
- [ ] Manual verification: `ocm --verbose download resources -r
<component-with-s3-resource-in-non-default-region>`

---------

Signed-off-by: Jakob Möller <contact@jakob-moeller.com>
Signed-off-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
Co-authored-by: Frederic Wilhelm <frederic.wilhelm@sap.com>
Signed-off-by: Matthias Bruns <git@matthiasbruns.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bugfix Bug kind/dependency dependency update, etc. size/m Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants