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

Add a before_refresh callback to AssumeRoleCredentials #2663

Merged
merged 5 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Unreleased Changes
------------------

* Issue - Add a before_refresh callback to AssumeRoleCredentials (#2529).
* Issue - Raise a `NoSuchProfileError` when config and credentials files don't exist.

3.126.1 (2022-02-14)
Expand Down
18 changes: 18 additions & 0 deletions gems/aws-sdk-core/lib/aws-sdk-core/assume_role_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class AssumeRoleCredentials
# @option options [Integer] :duration_seconds
# @option options [String] :external_id
# @option options [STS::Client] :client
# @option options [Callable] before_refresh Proc called before
mullermp marked this conversation as resolved.
Show resolved Hide resolved
# credentials are refreshed. Useful for updating tokens.
# `before_refresh` is called when AWS credentials are
# required and need to be refreshed. Tokens can be refreshed using
# the following example:
#
# before_refresh = Proc.new do |assume_role_credentials| do
# assume_role_credentials.assume_role_params['token_code'] = update_token
# end
#
def initialize(options = {})
client_opts = {}
@assume_role_params = {}
Expand All @@ -39,15 +49,23 @@ def initialize(options = {})
end
end
@client = client_opts[:client] || STS::Client.new(client_opts)
@before_refresh = options.delete(:before_refresh)

super
end

# @return [STS::Client]
attr_reader :client

# @return [Hash]
attr_reader :assume_role_params

private

def refresh

@before_refresh.call(self) if @before_refresh

c = @client.assume_role(@assume_role_params).credentials
@credentials = Credentials.new(
c.access_key_id,
Expand Down
16 changes: 16 additions & 0 deletions gems/aws-sdk-core/spec/aws/assume_role_credentials_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,21 @@ module Aws
c.credentials
end

it 'calls before_refresh with self' do
before_refresh_called = false
before_refresh = proc do |cred_provider|
before_refresh_called = true
expect(cred_provider).to be_instance_of(AssumeRoleCredentials)
end

AssumeRoleCredentials.new(
role_arn: 'arn',
role_session_name: 'session',
before_refresh: before_refresh
)

expect(before_refresh_called).to be(true)
end

end
end