Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client-s3-control): add prefix dedupe middleware #4286

Merged
merged 3 commits into from Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions clients/client-s3-control/src/S3ControlClient.ts
Expand Up @@ -12,6 +12,7 @@ import { getLoggerPlugin } from "@aws-sdk/middleware-logger";
import { getRecursionDetectionPlugin } from "@aws-sdk/middleware-recursion-detection";
import { getRetryPlugin, resolveRetryConfig, RetryInputConfig, RetryResolvedConfig } from "@aws-sdk/middleware-retry";
import {
getHostPrefixDeduplicationPlugin,
resolveS3ControlConfig,
S3ControlInputConfig,
S3ControlResolvedConfig,
Expand Down Expand Up @@ -569,6 +570,7 @@ export class S3ControlClient extends __Client<
this.middlewareStack.use(getLoggerPlugin(this.config));
this.middlewareStack.use(getRecursionDetectionPlugin(this.config));
this.middlewareStack.use(getAwsAuthPlugin(this.config));
this.middlewareStack.use(getHostPrefixDeduplicationPlugin(this.config));
this.middlewareStack.use(getUserAgentPlugin(this.config));
}

Expand Down
Expand Up @@ -54,6 +54,11 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.withConventions(AwsDependency.S3_CONTROL_MIDDLEWARE.dependency, "S3Control", HAS_CONFIG)
.servicePredicate((m, s) -> isS3Control(s))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.S3_CONTROL_MIDDLEWARE.dependency,
"HostPrefixDeduplication", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> isS3Control(s))
.build(),
RuntimeClientPlugin.builder()
.withConventions(
AwsDependency.S3_CONTROL_MIDDLEWARE.dependency,
Expand Down
@@ -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");
});
});
@@ -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;
};
@@ -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);
},
});
1 change: 1 addition & 0 deletions packages/middleware-sdk-s3-control/src/index.ts
Expand Up @@ -6,4 +6,5 @@ export {
updateArnablesRequestMiddlewareOptions,
getProcessArnablesPlugin,
} from "./process-arnables-plugin";
export * from "./host-prefix-deduplication/hostPrefixDeduplicationMiddleware";
export * from "./redirect-from-postid";