Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ describe(regionRedirectMiddleware.name, () => {
return null as any;
};

const nextHeadBucket400 = (arg: any) => {
if (call === 0) {
call++;
throw Object.assign(new Error(), {
$metadata: { httpStatusCode: 400 },
$response: { headers: { "x-amz-bucket-region": redirectRegion } },
});
}
return null as any;
};

beforeEach(() => {
call = 0;
});
Expand All @@ -51,6 +62,14 @@ describe(regionRedirectMiddleware.name, () => {
expect(context.__s3RegionRedirect).toEqual(redirectRegion);
});

it("set S3 region redirect on context for HeadBucket with 400 status and x-amz-bucket-region header", async () => {
const middleware = regionRedirectMiddleware({ region, followRegionRedirects: true });
const context = { commandName: "HeadBucketCommand" } as HandlerExecutionContext;
const handler = middleware(nextHeadBucket400, context);
await handler({ input: null });
expect(context.__s3RegionRedirect).toEqual(redirectRegion);
});

it("does not follow the redirect when followRegionRedirects is false", async () => {
const middleware = regionRedirectMiddleware({ region, followRegionRedirects: false });
const context = {} as HandlerExecutionContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export function regionRedirectMiddleware(clientConfig: PreviouslyResolved): Init
if (
err?.$metadata?.httpStatusCode === 301 ||
// err.name === "PermanentRedirect" && --> removing the error name check, as that allows for HEAD operations (which have the 301 status code, but not the same error name) to be covered for region redirection as well
(err?.$metadata?.httpStatusCode === 400 && err?.name === "IllegalLocationConstraintException")
(err?.$metadata?.httpStatusCode === 400 && err?.name === "IllegalLocationConstraintException") ||
(err?.$metadata?.httpStatusCode === 400 &&
context.commandName === "HeadBucketCommand" &&
err?.$response?.headers?.["x-amz-bucket-region"])
) {
Copy link
Member

Choose a reason for hiding this comment

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

Simplify this using variable names and combining the two 400 checks?

const statusCode = err?.$metadata?.httpStatusCode;
const isHeadBucket = context.commandName === "HeadBucketCommand";
const hasBucketRegionHeader = err?.$response?.headers?.["x-amz-bucket-region"];

if (
  statusCode === 301 ||
  (statusCode === 400 && (
    err?.name === "IllegalLocationConstraintException" ||
    (isHeadBucket && hasBucketRegionHeader)
  ))
)

try {
const actualRegion = err.$response.headers["x-amz-bucket-region"];
Copy link
Contributor

Choose a reason for hiding this comment

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

reuse the value from hasBucketRegionHeader. Since it isn't a boolean it should simply be called bucketRegionHeader.

Expand Down
Loading