Skip to content

Commit 1f02a26

Browse files
authored
chore(middleware-sdk-s3): add status code 400 for S3 region redirects (#6572)
* chore(middleware-sdk-s3): add status code 400 for redirects * chore(middleware-sdk-s3): add error code check * chore(middleware-sdk-s3): re-add comment for PermanentRedirect
1 parent 398b45c commit 1f02a26

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

packages/middleware-sdk-s3/src/region-redirect-middleware.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ describe(regionRedirectMiddleware.name, () => {
1818
return null as any;
1919
};
2020

21+
const next400 = (arg: any) => {
22+
if (call === 0) {
23+
call++;
24+
throw Object.assign(new Error(), {
25+
name: "IllegalLocationConstraintException",
26+
$metadata: { httpStatusCode: 400 },
27+
$response: { headers: { "x-amz-bucket-region": redirectRegion } },
28+
});
29+
}
30+
return null as any;
31+
};
32+
2133
beforeEach(() => {
2234
call = 0;
2335
});
@@ -30,6 +42,14 @@ describe(regionRedirectMiddleware.name, () => {
3042
expect(context.__s3RegionRedirect).toEqual(redirectRegion);
3143
});
3244

45+
it("set S3 region redirect on context if receiving an error with status 400", async () => {
46+
const middleware = regionRedirectMiddleware({ region, followRegionRedirects: true });
47+
const context = {} as HandlerExecutionContext;
48+
const handler = middleware(next400, context);
49+
await handler({ input: null });
50+
expect(context.__s3RegionRedirect).toEqual(redirectRegion);
51+
});
52+
3353
it("does not follow the redirect when followRegionRedirects is false", async () => {
3454
const middleware = regionRedirectMiddleware({ region, followRegionRedirects: false });
3555
const context = {} as HandlerExecutionContext;

packages/middleware-sdk-s3/src/region-redirect-middleware.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,23 @@ export function regionRedirectMiddleware(clientConfig: PreviouslyResolved): Init
3535
try {
3636
return await next(args);
3737
} catch (err) {
38-
if (
39-
clientConfig.followRegionRedirects &&
40-
// 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
41-
err?.$metadata?.httpStatusCode === 301
42-
) {
43-
try {
44-
const actualRegion = err.$response.headers["x-amz-bucket-region"];
45-
context.logger?.debug(`Redirecting from ${await clientConfig.region()} to ${actualRegion}`);
46-
context.__s3RegionRedirect = actualRegion;
47-
} catch (e) {
48-
throw new Error("Region redirect failed: " + e);
38+
if (clientConfig.followRegionRedirects) {
39+
if (
40+
err?.$metadata?.httpStatusCode === 301 ||
41+
// 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
42+
(err?.$metadata?.httpStatusCode === 400 && err?.name === "IllegalLocationConstraintException")
43+
) {
44+
try {
45+
const actualRegion = err.$response.headers["x-amz-bucket-region"];
46+
context.logger?.debug(`Redirecting from ${await clientConfig.region()} to ${actualRegion}`);
47+
context.__s3RegionRedirect = actualRegion;
48+
} catch (e) {
49+
throw new Error("Region redirect failed: " + e);
50+
}
51+
return next(args);
4952
}
50-
return next(args);
51-
} else {
52-
throw err;
5353
}
54+
throw err;
5455
}
5556
};
5657
}

0 commit comments

Comments
 (0)