generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 2
[auth] Add support for Bedrock API Keys #30
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8179f69
add support for Bedrock API Key
sebsto 8358b4e
add documentation for authentication
sebsto 64a07b5
add a test on AWS_BEARER_TOKEN_BEDROCK
sebsto c312031
add example
sebsto 3d76d6b
swift-format
sebsto 835cb2e
add license header
sebsto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,13 @@ import AwsCommonRuntimeKit | |||||||||||
| import Foundation | ||||||||||||
| import Logging | ||||||||||||
|
|
||||||||||||
| // for setenv and unsetenv functions | ||||||||||||
| #if os(Linux) | ||||||||||||
| import Glibc | ||||||||||||
| #else | ||||||||||||
| import Darwin.C | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| public struct BedrockService: Sendable { | ||||||||||||
| package let region: Region | ||||||||||||
| package let logger: Logging.Logger | ||||||||||||
|
|
@@ -106,21 +113,18 @@ public struct BedrockService: Sendable { | |||||||||||
| /// - authentication: The authentication type to use | ||||||||||||
| /// - Returns: Configured BedrockClientProtocol instance | ||||||||||||
| /// - Throws: Error if client creation fails | ||||||||||||
| static private func createBedrockClient( | ||||||||||||
| internal static func createBedrockClient( | ||||||||||||
| region: Region, | ||||||||||||
| authentication: BedrockAuthentication, | ||||||||||||
| logger: Logging.Logger | ||||||||||||
| ) async throws | ||||||||||||
| -> BedrockClientProtocol | ||||||||||||
| -> BedrockClient | ||||||||||||
| { | ||||||||||||
| let config = try await BedrockClient.BedrockClientConfiguration( | ||||||||||||
| region: region.rawValue | ||||||||||||
| ) | ||||||||||||
| if let awsCredentialIdentityResolver = try? await authentication.getAWSCredentialIdentityResolver( | ||||||||||||
| let config: BedrockClient.BedrockClientConfiguration = try await prepareConfig( | ||||||||||||
| region: region, | ||||||||||||
| authentication: authentication, | ||||||||||||
| logger: logger | ||||||||||||
| ) { | ||||||||||||
| config.awsCredentialIdentityResolver = awsCredentialIdentityResolver | ||||||||||||
| } | ||||||||||||
| ) | ||||||||||||
| return BedrockClient(config: config) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -130,24 +134,55 @@ public struct BedrockService: Sendable { | |||||||||||
| /// - authentication: The authentication type to use | ||||||||||||
| /// - Returns: Configured BedrockRuntimeClientProtocol instance | ||||||||||||
| /// - Throws: Error if client creation fails | ||||||||||||
| static private func createBedrockRuntimeClient( | ||||||||||||
| internal static func createBedrockRuntimeClient( | ||||||||||||
| region: Region, | ||||||||||||
| authentication: BedrockAuthentication, | ||||||||||||
| logger: Logging.Logger | ||||||||||||
| ) | ||||||||||||
| async throws | ||||||||||||
| -> BedrockRuntimeClientProtocol | ||||||||||||
| -> BedrockRuntimeClient | ||||||||||||
| { | ||||||||||||
| let config = | ||||||||||||
| try await BedrockRuntimeClient.BedrockRuntimeClientConfiguration( | ||||||||||||
| region: region.rawValue | ||||||||||||
| ) | ||||||||||||
| let config: BedrockRuntimeClient.BedrockRuntimeClientConfiguration = try await prepareConfig( | ||||||||||||
| region: region, | ||||||||||||
| authentication: authentication, | ||||||||||||
| logger: logger | ||||||||||||
| ) | ||||||||||||
| return BedrockRuntimeClient(config: config) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// Generic function to create client configuration and avoid duplication code. | ||||||||||||
| internal static func prepareConfig<C: BedrockConfigProtocol>( | ||||||||||||
| region: Region, | ||||||||||||
| authentication: BedrockAuthentication, | ||||||||||||
| logger: Logging.Logger | ||||||||||||
| ) async throws -> C { | ||||||||||||
| var config: C = try await .init() | ||||||||||||
|
|
||||||||||||
| config.region = region.rawValue | ||||||||||||
|
|
||||||||||||
| // support profile, SSO, web identity and static authentication | ||||||||||||
| if let awsCredentialIdentityResolver = try? await authentication.getAWSCredentialIdentityResolver( | ||||||||||||
| logger: logger | ||||||||||||
| ) { | ||||||||||||
| config.awsCredentialIdentityResolver = awsCredentialIdentityResolver | ||||||||||||
| } | ||||||||||||
| return BedrockRuntimeClient(config: config) | ||||||||||||
|
|
||||||||||||
| // support API keys | ||||||||||||
| if case .apiKey(let key) = authentication { | ||||||||||||
| config.httpClientConfiguration.defaultHeaders.add( | ||||||||||||
| name: "Authorization", | ||||||||||||
| value: "Bearer \(key)" | ||||||||||||
| ) | ||||||||||||
| logger.trace("Using API Key for authentication") | ||||||||||||
| } else { | ||||||||||||
| logger.trace("Using AWS credentials for authentication") | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| //We uncheck AWS_BEARER_TOKEN_BEDROCK to avoid conflict with future AWS SDK version | ||||||||||||
| //see https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started-api-keys.html | ||||||||||||
| unsetenv("AWS_BEARER_TOKEN_BEDROCK") | ||||||||||||
|
||||||||||||
| unsetenv("AWS_BEARER_TOKEN_BEDROCK") | |
| // Temporarily remove AWS_BEARER_TOKEN_BEDROCK from the environment for this configuration | |
| var environment = ProcessInfo.processInfo.environment | |
| environment["AWS_BEARER_TOKEN_BEDROCK"] = nil | |
| // Pass the modified environment to the relevant configuration or process if needed |
This file contains hidden or 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,30 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift Bedrock Library open source project | ||
| // | ||
| // Copyright (c) 2025 Amazon.com, Inc. or its affiliates | ||
| // and the Swift Bedrock Library project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift Bedrock Library project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import AWSBedrock | ||
| import AWSBedrockRuntime | ||
| import ClientRuntime | ||
| import SmithyIdentity | ||
|
|
||
| protocol BedrockConfigProtocol { | ||
| init() async throws | ||
| var awsCredentialIdentityResolver: any SmithyIdentity.AWSCredentialIdentityResolver { get set } | ||
| var httpClientConfiguration: ClientRuntime.HttpClientConfiguration { get set } | ||
| var region: String? { get set } | ||
| } | ||
| extension BedrockClient.BedrockClientConfiguration: @retroactive @unchecked Sendable, BedrockConfigProtocol {} | ||
| extension BedrockRuntimeClient.BedrockRuntimeClientConfiguration: @retroactive @unchecked Sendable, | ||
| BedrockConfigProtocol | ||
| {} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The
prepareConfigfunction bundles initialization, credential resolver setup, API-key header injection, and environment cleanup. Consider splitting these responsibilities into focused helper methods or adding detailed comments to clarify each step and improve readability and testability.