Class: Aws::AssumeRoleCredentials

Inherits:
Object
  • Object
show all
Includes:
CredentialProvider, RefreshingCredentials
Defined in:
lib/aws-sdk-core/assume_role_credentials.rb

Overview

An auto-refreshing credential provider that assumes a role via STS::Client#assume_role.

role_credentials = Aws::AssumeRoleCredentials.new(
  client: Aws::STS::Client.new(...),
  role_arn: "linked::account::arn",
  role_session_name: "session-name"
)
ec2 = Aws::EC2::Client.new(credentials: role_credentials)

If you omit ‘:client` option, a new STS::Client object will be constructed with additional options that were provided.

Constant Summary

Constants included from RefreshingCredentials

RefreshingCredentials::ASYNC_EXPIRATION_LENGTH, RefreshingCredentials::CLIENT_EXCLUDE_OPTIONS, RefreshingCredentials::SYNC_EXPIRATION_LENGTH

Instance Attribute Summary collapse

Attributes included from CredentialProvider

#credentials, #expiration

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RefreshingCredentials

#credentials, #refresh!

Methods included from CredentialProvider

#set?

Constructor Details

#initialize(options = {}) ⇒ AssumeRoleCredentials

Returns a new instance of AssumeRoleCredentials.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :role_arn (required, String)
  • :role_session_name (required, String)
  • :policy (String)
  • :duration_seconds (Integer)
  • :external_id (String)
  • :client (STS::Client)
  • before_refresh (Callable)

    Proc called before 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
    


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aws-sdk-core/assume_role_credentials.rb', line 41

def initialize(options = {})
  client_opts = {}
  @assume_role_params = {}
  options.each_pair do |key, value|
    if self.class.assume_role_options.include?(key)
      @assume_role_params[key] = value
    elsif !CLIENT_EXCLUDE_OPTIONS.include?(key)
      client_opts[key] = value
    end
  end
  @client = client_opts[:client] || STS::Client.new(client_opts)
  @async_refresh = true
  super
end

Instance Attribute Details

#assume_role_paramsHash (readonly)

Returns:

  • (Hash)


60
61
62
# File 'lib/aws-sdk-core/assume_role_credentials.rb', line 60

def assume_role_params
  @assume_role_params
end

#clientSTS::Client (readonly)

Returns:



57
58
59
# File 'lib/aws-sdk-core/assume_role_credentials.rb', line 57

def client
  @client
end

Class Method Details

.assume_role_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
# File 'lib/aws-sdk-core/assume_role_credentials.rb', line 84

def assume_role_options
  @aro ||= begin
    input = STS::Client.api.operation(:assume_role).input
    Set.new(input.shape.member_names)
  end
end