Repository navigation
Add support of AWS profile for Bedrock #1190
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
steipete
merged 12 commits into
steipete:main
from
oleksandr-soldatov:feat/bedrock-aws-profile-auth
May 28, 2026
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
87f5fa1
Add support of AWS profile for Bedrock
oleksandr-soldatov c995693
fix review comments
oleksandr-soldatov 5904253
fix review comments
oleksandr-soldatov 760b9d5
fix usage history
oleksandr-soldatov 6691cc3
scrub keys from logs
oleksandr-soldatov cb0368c
preserve bedrock auth
oleksandr-soldatov 499958c
Merge remote-tracking branch 'origin/main' into feat/bedrock-aws-prof…
steipete 09cd428
docs: note Bedrock profile auth
steipete 3d68c72
fix: preserve Bedrock profile environment credentials
steipete 173d481
fix: unset AWS_PROFILE for Bedrock profile export
steipete ef7f70d
fix: preserve merged Bedrock static credentials
steipete db1442c
fix: honor env Bedrock profile mode
steipete 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
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
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
87 changes: 87 additions & 0 deletions
87
Sources/CodexBarCore/Providers/Bedrock/BedrockProfileCredentialProvider.swift
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,87 @@ | ||
| import Foundation | ||
|
|
||
| /// Resolves AWS credentials for a named profile by shelling out to the AWS CLI. | ||
| /// | ||
| /// Uses `aws configure export-credentials`, which transparently resolves static | ||
| /// credentials, SSO sessions, assume-role chains, and `credential_process` profiles. | ||
| /// The runner is injected so tests never invoke a real `aws` binary. | ||
| struct BedrockProfileCredentialProvider: Sendable { | ||
| typealias Runner = @Sendable ( | ||
| _ arguments: [String], | ||
| _ environment: [String: String]) async throws -> SubprocessResult | ||
|
|
||
| let awsBinaryPath: String | ||
| let run: Runner | ||
|
|
||
| /// Production provider that drives the real `aws` binary via `SubprocessRunner`. | ||
| static func live(awsBinaryPath: String) -> BedrockProfileCredentialProvider { | ||
| BedrockProfileCredentialProvider(awsBinaryPath: awsBinaryPath) { arguments, environment in | ||
| try await SubprocessRunner.run( | ||
| binary: awsBinaryPath, | ||
| arguments: arguments, | ||
| environment: environment, | ||
| timeout: 20, | ||
| label: "aws-bedrock-credentials") | ||
| } | ||
| } | ||
|
|
||
| func exportCredentials( | ||
| profile: String, | ||
| environment: [String: String] = [:]) async throws -> BedrockAWSSigner.Credentials | ||
| { | ||
| let result: SubprocessResult | ||
| do { | ||
| result = try await self.run( | ||
| ["configure", "export-credentials", "--profile", profile, "--format", "process"], | ||
| environment) | ||
| } catch let SubprocessRunnerError.nonZeroExit(_, stderr) { | ||
| throw Self.mapExportError(stderr: stderr, profile: profile) | ||
| } | ||
|
|
||
| guard let data = result.stdout.data(using: .utf8), | ||
| let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any], | ||
| let accessKeyID = Self.nonEmpty(json["AccessKeyId"] as? String), | ||
| let secretAccessKey = Self.nonEmpty(json["SecretAccessKey"] as? String) | ||
| else { | ||
| throw BedrockUsageError.parseFailed("Could not parse AWS CLI export-credentials output") | ||
| } | ||
|
|
||
| return BedrockAWSSigner.Credentials( | ||
| accessKeyID: accessKeyID, | ||
| secretAccessKey: secretAccessKey, | ||
| sessionToken: Self.nonEmpty(json["SessionToken"] as? String)) | ||
| } | ||
|
|
||
| /// Returns the profile's configured region, or `nil` when unset. | ||
| /// `aws configure get region` exits non-zero when the value is not configured, | ||
| /// which is a normal case rather than an error. | ||
| func resolveRegion( | ||
| profile: String, | ||
| environment: [String: String] = [:]) async throws -> String? | ||
| { | ||
| do { | ||
| let result = try await self.run( | ||
| ["configure", "get", "region", "--profile", profile], | ||
| environment) | ||
| return Self.nonEmpty(result.stdout) | ||
| } catch SubprocessRunnerError.nonZeroExit { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| static func mapExportError(stderr: String, profile: String) -> BedrockUsageError { | ||
| let lower = stderr.lowercased() | ||
| if lower.contains("sso login") || lower.contains("expired") || lower.contains("token has expired") { | ||
| return .profileSessionExpired(profile) | ||
| } | ||
| let trimmed = stderr.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| return .apiError(trimmed.isEmpty ? "AWS CLI failed to export credentials" : trimmed) | ||
| } | ||
|
|
||
| private static func nonEmpty(_ raw: String?) -> String? { | ||
| guard let value = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.