Sha256: 39bd0430d544dc6dc84c9b98a3409836d126e1c888138a808b64ec614d2ee125

Contents?: true

Size: 1.82 KB

Versions: 54

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

require 'set'

module Aws

  # An auto-refreshing credential provider that works by assuming
  # a role via {Aws::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.
  class AssumeRoleCredentials

    include CredentialProvider
    include RefreshingCredentials

    # @option options [required, String] :role_arn
    # @option options [required, String] :role_session_name
    # @option options [String] :policy
    # @option options [Integer] :duration_seconds
    # @option options [String] :external_id
    # @option options [STS::Client] :client
    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
        else
          client_opts[key] = value
        end
      end
      @client = client_opts[:client] || STS::Client.new(client_opts)
      super
    end

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

    private

    def refresh
      c = @client.assume_role(@assume_role_params).credentials
      @credentials = Credentials.new(
        c.access_key_id,
        c.secret_access_key,
        c.session_token
      )
      @expiration = c.expiration
    end

    class << self

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

    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
aws-sdk-core-3.126.1 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.126.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.6 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.5 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.4 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.3 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.2 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.1 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.125.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.124.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.123.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.122.1 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.122.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.6 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.5 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.3 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.2 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.1 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.121.0 lib/aws-sdk-core/assume_role_credentials.rb
aws-sdk-core-3.120.0 lib/aws-sdk-core/assume_role_credentials.rb