Sha256: 1d3724317b1ca47eac506deccbb213d172cc7ad46c0c48ad653fa7ba34df1c0a

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 KB

Contents

require 'thread'

module Aws

  # Base class used credential classes that can be refreshed. This
  # provides basic refresh logic in a thread-safe manor. Classes mixing in
  # this module are expected to implement a #refresh method that populates
  # the following instance variables:
  #
  # * `@access_key_id`
  # * `@secret_access_key`
  # * `@session_token`
  # * `@expiration`
  #
  # @api private
  module RefreshingCredentials

    def initialize(options = {})
      @mutex = Mutex.new
      refresh
    end

    # @return [String,nil]
    def access_key_id
      refresh_if_near_expiration
      @access_key_id
    end

    # @return [String,nil]
    def secret_access_key
      refresh_if_near_expiration
      @secret_access_key
    end

    # @return [String,nil]
    def session_token
      refresh_if_near_expiration
      @session_token
    end

    # @return [Time,nil]
    def expiration
      refresh_if_near_expiration
      @expiration
    end

    # Refresh credentials.
    # @return [void]
    def refresh!
      @mutex.synchronize { refresh }
    end

    private

    # Refreshes instance metadata credentials if they are within
    # 5 minutes of expiration.
    def refresh_if_near_expiration
      if near_expiration?
        @mutex.synchronize do
          refresh if near_expiration?
        end
      end
    end

    def near_expiration?
      if @expiration
        # are we within 5 minutes of expiration?
        (Time.now.to_i + 5 * 60) > @expiration.to_i
      end
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
aws-sdk-core-2.0.18 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.17 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.16 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.15 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.14 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.13 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.12 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.11 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.10 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.9 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.8 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.7 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.6 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.5 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.0.4 lib/aws-sdk-core/refreshing_credentials.rb