From 6c53a93bad3b0f57dec74c85309ccd868ac0c481 Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Tue, 10 Oct 2023 11:44:36 -0700 Subject: [PATCH] feat(experimentalIdentityAndAuth): add different `httpAuthSchemeMiddleware` plugins depending on `@endpointRuleSet` (#1004) --- .changeset/plenty-cycles-boil.md | 5 +++ .../package.json | 1 + .../getHttpAuthSchemeEndpointRuleSetPlugin.ts | 33 +++++++++++++++++++ .../getHttpAuthSchemePlugin.ts | 4 +-- .../src/middleware-http-auth-scheme/index.ts | 1 + .../AddHttpAuthSchemeMiddleware.java | 14 ++++++-- yarn.lock | 1 + 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 .changeset/plenty-cycles-boil.md create mode 100644 packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemeEndpointRuleSetPlugin.ts diff --git a/.changeset/plenty-cycles-boil.md b/.changeset/plenty-cycles-boil.md new file mode 100644 index 00000000000..55418677b5a --- /dev/null +++ b/.changeset/plenty-cycles-boil.md @@ -0,0 +1,5 @@ +--- +"@smithy/experimental-identity-and-auth": patch +--- + +Add different `httpAuthSchemeMiddleware` plugins depending on `@endpointRuleSet` diff --git a/packages/experimental-identity-and-auth/package.json b/packages/experimental-identity-and-auth/package.json index 25b8c8efa8f..8eae41f21fc 100644 --- a/packages/experimental-identity-and-auth/package.json +++ b/packages/experimental-identity-and-auth/package.json @@ -26,6 +26,7 @@ "dependencies": { "@smithy/middleware-endpoint": "workspace:^", "@smithy/middleware-retry": "workspace:^", + "@smithy/middleware-serde": "workspace:^", "@smithy/protocol-http": "workspace:^", "@smithy/signature-v4": "workspace:^", "@smithy/types": "workspace:^", diff --git a/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemeEndpointRuleSetPlugin.ts b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemeEndpointRuleSetPlugin.ts new file mode 100644 index 00000000000..a031255f804 --- /dev/null +++ b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemeEndpointRuleSetPlugin.ts @@ -0,0 +1,33 @@ +import { endpointMiddlewareOptions } from "@smithy/middleware-endpoint"; +import { HandlerExecutionContext, Pluggable, RelativeMiddlewareOptions, SerializeHandlerOptions } from "@smithy/types"; + +import { HttpAuthSchemeParameters } from "../HttpAuthSchemeProvider"; +import { httpAuthSchemeMiddleware, PreviouslyResolved } from "./httpAuthSchemeMiddleware"; + +/** + * @internal + */ +export const httpAuthSchemeEndpointRuleSetMiddlewareOptions: SerializeHandlerOptions & RelativeMiddlewareOptions = { + step: "serialize", + tags: ["HTTP_AUTH_SCHEME"], + name: "httpAuthSchemeMiddleware", + override: true, + relation: "before", + toMiddleware: endpointMiddlewareOptions.name!, +}; + +/** + * @internal + */ +export const getHttpAuthSchemeEndpointRuleSetPlugin = < + TConfig extends object, + TContext extends HandlerExecutionContext, + TParameters extends HttpAuthSchemeParameters, + TInput extends object +>( + config: TConfig & PreviouslyResolved +): Pluggable => ({ + applyToStack: (clientStack) => { + clientStack.addRelativeTo(httpAuthSchemeMiddleware(config), httpAuthSchemeEndpointRuleSetMiddlewareOptions); + }, +}); diff --git a/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemePlugin.ts b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemePlugin.ts index f2deef92d68..589bb41c564 100644 --- a/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemePlugin.ts +++ b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/getHttpAuthSchemePlugin.ts @@ -1,4 +1,4 @@ -import { endpointMiddlewareOptions } from "@smithy/middleware-endpoint"; +import { serializerMiddlewareOption } from "@smithy/middleware-serde"; import { HandlerExecutionContext, Pluggable, RelativeMiddlewareOptions, SerializeHandlerOptions } from "@smithy/types"; import { HttpAuthSchemeParameters } from "../HttpAuthSchemeProvider"; @@ -13,7 +13,7 @@ export const httpAuthSchemeMiddlewareOptions: SerializeHandlerOptions & Relative name: "httpAuthSchemeMiddleware", override: true, relation: "before", - toMiddleware: endpointMiddlewareOptions.name!, + toMiddleware: serializerMiddlewareOption.name!, }; /** diff --git a/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/index.ts b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/index.ts index 3e570d934d0..5042e7dc7b4 100644 --- a/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/index.ts +++ b/packages/experimental-identity-and-auth/src/middleware-http-auth-scheme/index.ts @@ -1,2 +1,3 @@ export * from "./httpAuthSchemeMiddleware"; +export * from "./getHttpAuthSchemeEndpointRuleSetPlugin"; export * from "./getHttpAuthSchemePlugin"; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java index e98df36be1d..e8df6cd2d49 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemeMiddleware.java @@ -5,8 +5,6 @@ package software.amazon.smithy.typescript.codegen.auth.http.integration; -import static software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention.HAS_MIDDLEWARE; - import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,6 +15,7 @@ import software.amazon.smithy.model.knowledge.ServiceIndex; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.ShapeId; +import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; import software.amazon.smithy.typescript.codegen.CodegenUtils; import software.amazon.smithy.typescript.codegen.ConfigField; import software.amazon.smithy.typescript.codegen.TypeScriptCodegenContext; @@ -27,6 +26,7 @@ import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention; import software.amazon.smithy.utils.SmithyInternalApi; /** @@ -46,10 +46,18 @@ public boolean matchesSettings(TypeScriptSettings settings) { public List getClientPlugins() { return List.of( RuntimeClientPlugin.builder() + .servicePredicate((m, s) -> s.hasTrait(EndpointRuleSetTrait.ID)) + .withConventions( + TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH.dependency, + "HttpAuthSchemeEndpointRuleSet", + Convention.HAS_MIDDLEWARE) + .build(), + RuntimeClientPlugin.builder() + .servicePredicate((m, s) -> !s.hasTrait(EndpointRuleSetTrait.ID)) .withConventions( TypeScriptDependency.EXPERIMENTAL_IDENTITY_AND_AUTH.dependency, "HttpAuthScheme", - HAS_MIDDLEWARE) + Convention.HAS_MIDDLEWARE) .build(), RuntimeClientPlugin.builder() .inputConfig(Symbol.builder() diff --git a/yarn.lock b/yarn.lock index 801a937afea..8d7b1df6371 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1871,6 +1871,7 @@ __metadata: dependencies: "@smithy/middleware-endpoint": "workspace:^" "@smithy/middleware-retry": "workspace:^" + "@smithy/middleware-serde": "workspace:^" "@smithy/protocol-http": "workspace:^" "@smithy/signature-v4": "workspace:^" "@smithy/types": "workspace:^"