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.632 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.631 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.630 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.629 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.628 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.627 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.626 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.625 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.624 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.623 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.622 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.621 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.620 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.619 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.618 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.617 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.616 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.615 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.614 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.613 lib/aws-sdk-core/refreshing_credentials.rb