Sha256: f30ddcabe978eb045e3f59b55a84c0629fb3ea6075a39bd311452107381d2268

Contents?: true

Size: 1.28 KB

Versions: 1076

Compression:

Stored size: 1.28 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 [Credentials]
    def credentials
      refresh_if_near_expiration
      @credentials
    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
      else
        true
      end
    end

  end
end

Version data entries

1,076 entries across 1,076 versions & 2 rubygems

Version Path
aws-sdk-core-2.11.592 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.591 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.590 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.589 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.588 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.587 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.586 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.585 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.584 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.583 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.582 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.581 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.580 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.579 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.578 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.577 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.576 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.575 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.574 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.573 lib/aws-sdk-core/refreshing_credentials.rb