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

feat: Bearer token auth #1637

Merged
merged 17 commits into from
Jul 25, 2024
Merged

feat: Bearer token auth #1637

merged 17 commits into from
Jul 25, 2024

Conversation

sichanyoo
Copy link
Contributor

@sichanyoo sichanyoo commented Jul 17, 2024

Companion PR: smithy-lang/smithy-swift#786

Issue #

#1083

Description of changes

Refer to comments on github diff

Manual testing steps

To test this locally, first set up your IAM Identity Center (doc link) & AWS CodeCatalyst (doc link).
Then, configure your config file to contain the following:

[default]
region=us-west-2
output=json
sso_session = codecatalyst

[profile codecatalyst]
region = us-west-2
sso_session = codecatalyst

[sso-session codecatalyst]
sso_region = us-east-1
sso_start_url = https://view.awsapps.com/start
sso_registration_scopes = codecatalyst:read_write

Then, run

aws sso login --profile=codecatalyst

on the terminal. This will open a browser tab for you to authorize your AWS account to be accessed by CodeCatalyst.
Successful previous step creates a token file at path ~/.aws/sso/cache/<sha-1-hash-of-sso-sesion-name>.json.
In CLITool, use CodeCatalyst::VerifySession API by simply doing the following:

func run() async throws {
        let client = try CodeCatalystClient(region: "us-west-2")
        let id = try await client.verifySession(input: VerifySessionInput()).identity
        print(id ?? "#### NO_ID_RETRIEVED")
}

Observe that the server accepts bearer auth and returns the ID successfully.

New/existing dependencies impact assessment, if applicable

Conventional Commits

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor Author

@sichanyoo sichanyoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments to help reviewers.

@@ -0,0 +1,35 @@
//
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much like the default AWS credential identity resolver chain. Unlike it tho, this only has one resolver atm.

@@ -0,0 +1,94 @@
//
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This token identity resolver fetches the access token from cached SSO token file.

@@ -18,8 +18,11 @@ import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored to remove logic for getting auth schemes supported by the model and move it into smithy-swift. Refer to comment: https://github.com/smithy-lang/smithy-swift/pull/786/files#r1681867714

Comment on lines 65 to 79
override fun addAdditionalSchemes(writer: SwiftWriter, authSchemeList: Array<String>): Array<String> {
val effectiveAuthSchemes = ServiceIndex(ctx.model).getEffectiveAuthSchemes(ctx.service)

val sdkId = AuthSchemeResolverGenerator.getSdkId(ctx)
val servicesUsingSigV4A = arrayOf("S3", "EventBridge", "CloudFrontKeyValueStore")
val sdkId = AuthSchemeResolverGenerator.getSdkId(ctx)
val servicesUsingSigV4A = arrayOf("S3", "EventBridge", "CloudFrontKeyValueStore")
var updatedAuthSchemeList = authSchemeList

if (effectiveAuthSchemes.contains(SigV4Trait.ID)) {
authSchemeList += writer.format("\$N()", AWSSDKHTTPAuthTypes.SigV4AuthScheme)
}
if (effectiveAuthSchemes.contains(SigV4ATrait.ID) || servicesUsingSigV4A.contains(sdkId)) {
authSchemeList += writer.format("\$N()", AWSSDKHTTPAuthTypes.SigV4AAuthScheme)
}
return "[${authSchemeList.joinToString(", ")}]"
if (effectiveAuthSchemes.contains(SigV4Trait.ID)) {
updatedAuthSchemeList += writer.format("\$N()", AWSSDKHTTPAuthTypes.SigV4AuthScheme)
}
if (effectiveAuthSchemes.contains(SigV4ATrait.ID) || servicesUsingSigV4A.contains(sdkId)) {
updatedAuthSchemeList += writer.format("\$N()", AWSSDKHTTPAuthTypes.SigV4AAuthScheme)
}

return updatedAuthSchemeList
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handles detecting and adding the SigV4 / SigV4A auth schemes to list of auth schemes supported by the service.

Comment on lines +52 to +58
"bearerTokenIdentityResolver" -> {
ConfigProperty(
"bearerTokenIdentityResolver",
SmithyIdentityTypes.BearerTokenIdentityResolver.toGeneric(),
{ it.format("\$N()", AWSSDKIdentityTypes.DefaultBearerTokenIdentityResolverChain) },
true
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value for bearerTokenIdentityResolver field of the client config was StaticBearerTokenIdentityResolver in smithy-swift, but over here in aws-sdk-swift, it has to be the default chain instead. Hence this override.

Comment on lines -133 to -138
private val authSchemesDefaultProvider = DefaultProvider(
{ getModeledAuthSchemesSupportedBySDK(ctx, it) },
isThrowable = false,
isAsync = false
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to AuthUtils in smithy-swift.

@@ -28,16 +28,20 @@ class AuthSchemePlugin(private val serviceConfig: ServiceConfig) : Plugin {
writer.write("private var authSchemes: \$N", SmithyHTTPAuthAPITypes.AuthSchemes.toOptional())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file are just adding bearerTokenIdentityResolver field as one of the new fields configurable by the auth scheme plugin.

@@ -1,7 +1,8 @@
package software.amazon.smithy.aws.swift.codegen.plugins

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file are just adding bearerTokenIdentityResolver field as one of the new fields configurable by the auth scheme plugin.

@@ -18,6 +18,7 @@ object AWSSDKIdentityTypes {
val AWSEndpointsRequestContext = runtimeSymbol("AWSEndpointsRequestContext")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new runtime type, but in codegen side.

@sichanyoo sichanyoo merged commit a37a5da into main Jul 25, 2024
29 checks passed
@sichanyoo sichanyoo deleted the feat/bearer-token-auth branch July 25, 2024 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants