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.552 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.551 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.550 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.549 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.548 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.547 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.546 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.545 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.544 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.543 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.542 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.541 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.540 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.539 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.538 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.537 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.536 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.535 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.534 lib/aws-sdk-core/refreshing_credentials.rb
aws-sdk-core-2.11.533 lib/aws-sdk-core/refreshing_credentials.rb