File: express_credentials_provider.rb

package info (click to toggle)
ruby-aws-sdk-s3 1.170.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,740 kB
  • sloc: ruby: 16,388; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,609 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

module Aws
  module S3
    # @api private
    def self.express_credentials_cache
      @express_credentials_cache ||= LRUCache.new(max_entries: 100)
    end

    # Returns Credentials class for S3 Express. Accepts CreateSession
    # params as options. See {Client#create_session} for details.
    class ExpressCredentialsProvider
      # @param [Hash] options
      # @option options [Client] :client The S3 client used to create the
      #  session.
      # @option options [String] :session_mode (see: {Client#create_session})
      # @option options [Boolean] :caching (true) When true, credentials will
      #  be cached.
      # @option options [Callable] :before_refresh Proc called before
      #   credentials are refreshed.
      def initialize(options = {})
        @client = options.delete(:client)
        @caching = options.delete(:caching) != false
        @options = options
        return unless @caching

        @cache = Aws::S3.express_credentials_cache
      end

      def express_credentials_for(bucket)
        if @caching
          cached_credentials_for(bucket)
        else
          new_credentials_for(bucket)
        end
      end

      attr_accessor :client

      private

      def cached_credentials_for(bucket)
        if @cache.key?(bucket)
          @cache[bucket]
        else
          @cache[bucket] = new_credentials_for(bucket)
        end
      end

      def new_credentials_for(bucket)
        ExpressCredentials.new(
          bucket: bucket,
          client: @client,
          **@options
        )
      end
    end
  end
end