-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client-s3-control): add prefix dedupe middleware (#4286)
- Loading branch information
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
17 changes: 17 additions & 0 deletions
17
...ges/middleware-sdk-s3-control/src/host-prefix-deduplication/deduplicateHostPrefix.spec.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { deduplicateHostPrefix } from "./deduplicateHostPrefix"; | ||
|
||
describe(deduplicateHostPrefix.name, () => { | ||
it("should deduplicate host name prefixes", () => { | ||
expect(deduplicateHostPrefix("a.a.host.com")).toEqual("a.host.com"); | ||
expect(deduplicateHostPrefix("1234567890.1234567890.host.com")).toEqual("1234567890.host.com"); | ||
expect(deduplicateHostPrefix("abcdefgh.abcdefgh.host.com")).toEqual("abcdefgh.host.com"); | ||
}); | ||
|
||
it("should do nothing if no duplication exists in the first two positions", () => { | ||
expect(deduplicateHostPrefix("b.a.host.com")).toEqual("b.a.host.com"); | ||
expect(deduplicateHostPrefix("0123456789.1234567890.host.com")).toEqual("0123456789.1234567890.host.com"); | ||
expect(deduplicateHostPrefix("zabcdefg.abcdefgh.host.com")).toEqual("zabcdefg.abcdefgh.host.com"); | ||
|
||
expect(deduplicateHostPrefix("12345.abcdefgh.12345.12345.host.com")).toEqual("12345.abcdefgh.12345.12345.host.com"); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/middleware-sdk-s3-control/src/host-prefix-deduplication/deduplicateHostPrefix.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @example | ||
* 12345.12345.____.com should become 12345.____.com. | ||
*/ | ||
export const deduplicateHostPrefix = (hostname: string): string => { | ||
const [prefix1, prefix2, ...rest] = hostname.split("."); | ||
if (prefix1 === prefix2) { | ||
return [prefix1, ...rest].join("."); | ||
} | ||
return hostname; | ||
}; |
50 changes: 50 additions & 0 deletions
50
...dleware-sdk-s3-control/src/host-prefix-deduplication/hostPrefixDeduplicationMiddleware.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
HandlerExecutionContext, | ||
HttpRequest, | ||
Pluggable, | ||
RelativeMiddlewareOptions, | ||
SerializeHandler, | ||
SerializeHandlerArguments, | ||
SerializeHandlerOutput, | ||
SerializeMiddleware, | ||
} from "@aws-sdk/types"; | ||
|
||
import { deduplicateHostPrefix } from "./deduplicateHostPrefix"; | ||
|
||
/** | ||
* @internal | ||
* This customization handles an edge case where | ||
* a hostprefix may be duplicated in the endpoint ruleset resolution | ||
* and hostPrefix serialization via the pre-endpoints 2.0 trait, | ||
* and which cannot be reconciled automatically. | ||
*/ | ||
export const hostPrefixDeduplicationMiddleware = (): SerializeMiddleware<any, any> => { | ||
return (next: SerializeHandler<any, any>, context: HandlerExecutionContext): SerializeHandler<any, any> => | ||
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<any>> => { | ||
const httpRequest: HttpRequest = (args.request ?? {}) as HttpRequest; | ||
if (httpRequest?.hostname) { | ||
httpRequest.hostname = deduplicateHostPrefix(httpRequest.hostname); | ||
} | ||
return next(args); | ||
}; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const hostPrefixDeduplicationMiddlewareOptions: RelativeMiddlewareOptions = { | ||
tags: ["HOST_PREFIX_DEDUPLICATION", "ENDPOINT_V2", "ENDPOINT"], | ||
toMiddleware: "serializerMiddleware", | ||
relation: "after", | ||
name: "hostPrefixDeduplicationMiddleware", | ||
override: true, | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const getHostPrefixDeduplicationPlugin = <T>(config: T): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.addRelativeTo(hostPrefixDeduplicationMiddleware(), hostPrefixDeduplicationMiddlewareOptions); | ||
}, | ||
}); |
This file contains 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