-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(experimentalIdentityAndAuth): customize
@aws.auth#sigv4
identi…
…ty providers for the AWS SDK (#5179) * feat(experimentalIdentityAndAuth): customize `@aws.auth#sigv4` identity providers for the AWS SDK Register `AwsCustomizeSigv4AuthPlugin` integration to customize `@aws.auth#sigv4` to use: - Browser: a function that throws an error saying `credentials` is missing - Node.js: `decorateDefaultCredentialProvider(credentialDefaultProvider)` from `@aws-sdk/credential-provider-node` and `aws-sdk/client-sts`. * feat(experimentalIdentityAndAuth): copy smithy-typescript generic auth tests
- Loading branch information
Steven Yuan
authored
Sep 8, 2023
1 parent
d058d78
commit 53ef8f9
Showing
6 changed files
with
209 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
$version: "2.0" | ||
|
||
namespace example.weather | ||
|
||
use aws.auth#sigv4 | ||
|
||
@authDefinition | ||
@trait | ||
structure customAuth {} | ||
|
||
@trait | ||
@protocolDefinition | ||
structure fakeProtocol {} | ||
|
||
@fakeProtocol | ||
@httpApiKeyAuth(name: "X-Api-Key", in: "header") | ||
@httpBearerAuth | ||
@sigv4(name: "weather") | ||
@customAuth | ||
@auth([sigv4]) | ||
service Weather { | ||
version: "2006-03-01" | ||
operations: [ | ||
// experimentalIdentityAndAuth | ||
OnlyHttpApiKeyAuth | ||
OnlyHttpApiKeyAuthOptional | ||
OnlyHttpBearerAuth | ||
OnlyHttpBearerAuthOptional | ||
OnlyHttpApiKeyAndBearerAuth | ||
OnlyHttpApiKeyAndBearerAuthReversed | ||
OnlySigv4Auth | ||
OnlySigv4AuthOptional | ||
OnlyCustomAuth | ||
OnlyCustomAuthOptional | ||
SameAsService | ||
] | ||
} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAuth") | ||
@auth([httpApiKeyAuth]) | ||
operation OnlyHttpApiKeyAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpBearerAuth") | ||
@auth([httpBearerAuth]) | ||
operation OnlyHttpBearerAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlySigv4Auth") | ||
@auth([sigv4]) | ||
operation OnlySigv4Auth {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAndBearerAuth") | ||
@auth([httpApiKeyAuth, httpBearerAuth]) | ||
operation OnlyHttpApiKeyAndBearerAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAndBearerAuthReversed") | ||
@auth([httpBearerAuth, httpApiKeyAuth]) | ||
operation OnlyHttpApiKeyAndBearerAuthReversed {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpApiKeyAuthOptional") | ||
@auth([httpApiKeyAuth]) | ||
@optionalAuth | ||
operation OnlyHttpApiKeyAuthOptional {} | ||
|
||
@http(method: "GET", uri: "/OnlyHttpBearerAuthOptional") | ||
@auth([httpBearerAuth]) | ||
@optionalAuth | ||
operation OnlyHttpBearerAuthOptional {} | ||
|
||
@http(method: "GET", uri: "/OnlySigv4AuthOptional") | ||
@auth([sigv4]) | ||
@optionalAuth | ||
operation OnlySigv4AuthOptional {} | ||
|
||
@http(method: "GET", uri: "/OnlyCustomAuth") | ||
@auth([customAuth]) | ||
operation OnlyCustomAuth {} | ||
|
||
@http(method: "GET", uri: "/OnlyCustomAuthOptional") | ||
@auth([customAuth]) | ||
@optionalAuth | ||
operation OnlyCustomAuthOptional {} | ||
|
||
@http(method: "GET", uri: "/SameAsService") | ||
operation SameAsService {} |
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
63 changes: 63 additions & 0 deletions
63
...azon/smithy/aws/typescript/codegen/auth/http/integration/AwsCustomizeSigv4AuthPlugin.java
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,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.aws.typescript.codegen.auth.http.integration; | ||
|
||
import java.util.List; | ||
import software.amazon.smithy.aws.traits.auth.SigV4Trait; | ||
import software.amazon.smithy.aws.typescript.codegen.AwsDependency; | ||
import software.amazon.smithy.typescript.codegen.LanguageTarget; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptSettings; | ||
import software.amazon.smithy.typescript.codegen.auth.http.HttpAuthScheme; | ||
import software.amazon.smithy.typescript.codegen.auth.http.SupportedHttpAuthSchemesIndex; | ||
import software.amazon.smithy.typescript.codegen.auth.http.integration.AddSigV4AuthPlugin; | ||
import software.amazon.smithy.typescript.codegen.auth.http.integration.HttpAuthTypeScriptIntegration; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Customize @aws.auth#sigv4 for AWS SDKs. | ||
* | ||
* This is the experimental behavior for `experimentalIdentityAndAuth`. | ||
*/ | ||
@SmithyInternalApi | ||
public class AwsCustomizeSigv4AuthPlugin implements HttpAuthTypeScriptIntegration { | ||
|
||
/** | ||
* Integration should only be used if `experimentalIdentityAndAuth` flag is true. | ||
*/ | ||
@Override | ||
public boolean matchesSettings(TypeScriptSettings settings) { | ||
return settings.getExperimentalIdentityAndAuth(); | ||
} | ||
|
||
/** | ||
* Run after default AddSigV4AuthPlugin. | ||
*/ | ||
@Override | ||
public List<String> runAfter() { | ||
return List.of(AddSigV4AuthPlugin.class.getCanonicalName()); | ||
} | ||
|
||
@Override | ||
public void customizeSupportedHttpAuthSchemes(SupportedHttpAuthSchemesIndex supportedHttpAuthSchemesIndex) { | ||
HttpAuthScheme authScheme = supportedHttpAuthSchemesIndex.getHttpAuthScheme(SigV4Trait.ID).toBuilder() | ||
// Current behavior of unconfigured `credentials` is to throw an error. | ||
// This may need to be customized if a service is released with multiple auth schemes. | ||
.putDefaultIdentityProvider(LanguageTarget.BROWSER, w -> | ||
w.write("async () => { throw new Error(\"`credentials` is missing\"); }")) | ||
// Use `@aws-sdk/credential-provider-node` with `@aws-sdk/client-sts` as the | ||
// default identity provider chain for Node.js | ||
.putDefaultIdentityProvider(LanguageTarget.NODE, w -> { | ||
w.addDependency(AwsDependency.STS_CLIENT); | ||
w.addImport("decorateDefaultCredentialProvider", null, AwsDependency.STS_CLIENT); | ||
w.addDependency(AwsDependency.CREDENTIAL_PROVIDER_NODE); | ||
w.addImport("defaultProvider", "credentialDefaultProvider", | ||
AwsDependency.CREDENTIAL_PROVIDER_NODE); | ||
w.write("decorateDefaultCredentialProvider(credentialDefaultProvider)"); | ||
}) | ||
.build(); | ||
supportedHttpAuthSchemesIndex.putHttpAuthScheme(authScheme.getSchemeId(), authScheme); | ||
} | ||
} |
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