-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support bearer tokens through environment for Bedrock #3266
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e2fb86f
Support bearer tokens through environment for Bedrock
mullermp a351ec9
Revert generated clients
mullermp feda251
Add changelogs
mullermp 0ecb370
Add metrics test cases
mullermp d452a72
More generic metrics tracking
mullermp 56f5935
Greatly improve sign testing and refactor signer
mullermp 5a9ab8d
Partially undo ambitious refactor
mullermp 4e0d4f4
Revert sigv4 signer gem changes
mullermp 16284a7
Merge branch 'version-3' into bearer-env
mullermp d54b216
Set minimum core version
mullermp 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
27 changes: 27 additions & 0 deletions
27
gems/aws-sdk-bedrock/lib/aws-sdk-bedrock/plugins/bearer_authorization.rb
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Aws::Bedrock | ||
module Plugins | ||
# @api private | ||
class BearerAuthorization < Seahorse::Client::Plugin | ||
def after_initialize(client) | ||
return unless (token = ENV['AWS_BEARER_TOKEN_BEDROCK']) | ||
|
||
token_provider = Aws::StaticTokenProvider.new(token) | ||
token_provider.metrics = ['BEARER_SERVICE_ENV_VARS'] | ||
client.config.token_provider ||= token_provider | ||
end | ||
|
||
class Handler < Seahorse::Client::Handler | ||
def call(context) | ||
# This also sets the preferred auth scheme even if the code token has precedence. | ||
context[:auth_scheme] = { 'name' => 'bearer' } if ENV['AWS_BEARER_TOKEN_BEDROCK'] | ||
@handler.call(context) | ||
end | ||
end | ||
|
||
# After endpoint/auth but before builders. | ||
handle(Handler, priority: 60) | ||
end | ||
end | ||
end |
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,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'spec_helper' | ||
|
||
module Aws | ||
module Bedrock | ||
describe Client do | ||
def metrics_from_user_agent_header(resp) | ||
header = resp.context.http_request.headers['User-Agent'] | ||
header.match(%r{ m/([A-Za-z0-9+-,]+)})[1].split(',') | ||
end | ||
|
||
it 'uses a bearer token from the environment' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
expect(client.config.token_provider.token.token).to eq('bedrock-token') | ||
resp = client.list_imported_models | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer bedrock-token') | ||
end | ||
|
||
it 'does not use a token for a different service' do | ||
ENV['AWS_BEARER_TOKEN_FOO'] = 'foo-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
expect(client.config.token_provider).to be_nil | ||
resp = client.list_imported_models | ||
expect(resp.context.http_request.headers['Authorization']).to_not eq('Bearer foo-token') | ||
end | ||
|
||
it 'still prefers bearer token when given an auth scheme preference' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
ENV['AWS_AUTH_SCHEME_PREFERENCE'] = 'sigv4,httpBearerAuth' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
resp = client.list_imported_models | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer bedrock-token') | ||
end | ||
|
||
it 'uses the token value from code over the environment token' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new( | ||
stub_responses: true, | ||
token_provider: Aws::StaticTokenProvider.new('explicit-code-token') | ||
) | ||
resp = client.list_imported_models | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer explicit-code-token') | ||
mullermp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
it 'sets a user agent metric' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
resp = client.list_imported_models | ||
metrics = metrics_from_user_agent_header(resp) | ||
expect(metrics).to include('3') | ||
end | ||
|
||
it 'does not set a user agent metric when using a token from code' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new( | ||
stub_responses: true, | ||
token_provider: Aws::StaticTokenProvider.new('explicit-code-token') | ||
) | ||
resp = client.list_imported_models | ||
metrics = metrics_from_user_agent_header(resp) | ||
expect(metrics).to_not include('3') | ||
end | ||
end | ||
end | ||
end |
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
27 changes: 27 additions & 0 deletions
27
gems/aws-sdk-bedrockruntime/lib/aws-sdk-bedrockruntime/plugins/bearer_authorization.rb
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Aws::BedrockRuntime | ||
module Plugins | ||
# @api private | ||
class BearerAuthorization < Seahorse::Client::Plugin | ||
def after_initialize(client) | ||
return unless (token = ENV['AWS_BEARER_TOKEN_BEDROCK']) | ||
|
||
token_provider = Aws::StaticTokenProvider.new(token) | ||
token_provider.metrics = ['BEARER_SERVICE_ENV_VARS'] | ||
client.config.token_provider ||= token_provider | ||
end | ||
|
||
class Handler < Seahorse::Client::Handler | ||
def call(context) | ||
# This also sets the preferred auth scheme even if the code token has precedence. | ||
context[:auth_scheme] = { 'name' => 'bearer' } if ENV['AWS_BEARER_TOKEN_BEDROCK'] | ||
@handler.call(context) | ||
end | ||
end | ||
|
||
# After endpoint/auth but before builders. | ||
handle(Handler, priority: 60) | ||
end | ||
end | ||
end |
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'spec_helper' | ||
|
||
module Aws | ||
module BedrockRuntime | ||
describe Client do | ||
def metrics_from_user_agent_header(resp) | ||
header = resp.context.http_request.headers['User-Agent'] | ||
header.match(%r{ m/([A-Za-z0-9+-,]+)})[1].split(',') | ||
end | ||
|
||
def invoke_model(client) | ||
stub = client.stub_data(:invoke_model, body: 'test') | ||
client.stub_responses(:invoke_model, stub) | ||
client.invoke_model(model_id: 'test') | ||
end | ||
|
||
it 'uses a bearer token from the environment' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
expect(client.config.token_provider.token.token).to eq('bedrock-token') | ||
resp = invoke_model(client) | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer bedrock-token') | ||
end | ||
|
||
it 'does not use a token for a different service' do | ||
ENV['AWS_BEARER_TOKEN_FOO'] = 'foo-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
expect(client.config.token_provider).to be_nil | ||
resp = invoke_model(client) | ||
expect(resp.context.http_request.headers['Authorization']).to_not eq('Bearer foo-token') | ||
end | ||
|
||
it 'still prefers bearer token when given an auth scheme preference' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
ENV['AWS_AUTH_SCHEME_PREFERENCE'] = 'sigv4,httpBearerAuth' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
resp = invoke_model(client) | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer bedrock-token') | ||
end | ||
|
||
it 'uses explicit config over the environment token' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new( | ||
stub_responses: true, | ||
token_provider: Aws::StaticTokenProvider.new('explicit-code-token') | ||
) | ||
resp = invoke_model(client) | ||
expect(resp.context.http_request.headers['Authorization']).to eq('Bearer explicit-code-token') | ||
end | ||
|
||
it 'sets a user agent metric' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new(stub_responses: true, token_provider: nil) | ||
resp = invoke_model(client) | ||
metrics = metrics_from_user_agent_header(resp) | ||
expect(metrics).to include('3') | ||
end | ||
|
||
it 'does not set a user agent metric when using a token from code' do | ||
ENV['AWS_BEARER_TOKEN_BEDROCK'] = 'bedrock-token' | ||
client = Client.new( | ||
stub_responses: true, | ||
token_provider: Aws::StaticTokenProvider.new('explicit-code-token') | ||
) | ||
resp = invoke_model(client) | ||
metrics = metrics_from_user_agent_header(resp) | ||
expect(metrics).to_not include('3') | ||
end | ||
end | ||
end | ||
end |
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
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.