File: aws_sdk_authentication.rb

package info (click to toggle)
ruby-httpx 1.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,816 kB
  • sloc: ruby: 12,209; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 2,972 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # This plugin applies AWS Sigv4 to requests, using the AWS SDK credentials and configuration.
    #
    # It requires the "aws-sdk-core" gem.
    #
    module AwsSdkAuthentication
      # Mock configuration, to be used only when resolving credentials
      class Configuration
        attr_reader :profile

        def initialize(profile)
          @profile = profile
        end

        def respond_to_missing?(*)
          true
        end

        def method_missing(*); end
      end

      #
      # encapsulates access to an AWS SDK credentials store.
      #
      class Credentials
        def initialize(aws_credentials)
          @aws_credentials = aws_credentials
        end

        def username
          @aws_credentials.access_key_id
        end

        def password
          @aws_credentials.secret_access_key
        end

        def security_token
          @aws_credentials.session_token
        end
      end

      class << self
        def load_dependencies(_klass)
          require "aws-sdk-core"
        end

        def configure(klass)
          klass.plugin(:aws_sigv4)
        end

        def extra_options(options)
          options.merge(max_concurrent_requests: 1)
        end

        def credentials(profile)
          mock_configuration = Configuration.new(profile)
          Credentials.new(Aws::CredentialProviderChain.new(mock_configuration).resolve)
        end

        def region(profile)
          # https://github.com/aws/aws-sdk-ruby/blob/version-3/gems/aws-sdk-core/lib/aws-sdk-core/plugins/regional_endpoint.rb#L62
          keys = %w[AWS_REGION AMAZON_REGION AWS_DEFAULT_REGION]
          env_region = ENV.values_at(*keys).compact.first
          env_region = nil if env_region == ""
          cfg_region = Aws.shared_config.region(profile: profile)
          env_region || cfg_region
        end
      end

      # adds support for the following options:
      #
      # :aws_profile :: AWS account profile to retrieve credentials from.
      module OptionsMethods
        private

        def option_aws_profile(value)
          String(value)
        end
      end

      module InstanceMethods
        #
        # aws_authentication
        # aws_authentication(credentials: Aws::Credentials.new('akid', 'secret'))
        # aws_authentication()
        #
        def aws_sdk_authentication(
          credentials: AwsSdkAuthentication.credentials(@options.aws_profile),
          region: AwsSdkAuthentication.region(@options.aws_profile),
          **options
        )
          aws_sigv4_authentication(
            credentials: credentials,
            region: region,
            provider_prefix: "aws",
            header_provider_field: "amz",
            **options
          )
        end
        alias_method :aws_auth, :aws_sdk_authentication
      end
    end
    register_plugin :aws_sdk_authentication, AwsSdkAuthentication
  end
end