Sha256: 464639531335dee791a26a9879fd36abdbfd99fc9ae61999a2b0772576c04e2b

Contents?: true

Size: 1.31 KB

Versions: 54

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'thread'

module Aws

  # Base class used credential classes that can be refreshed. This
  # provides basic refresh logic in a thread-safe manner. 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

54 entries across 54 versions & 1 rubygems

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