lib/httpx/plugins/aws_sdk_authentication.rb in httpx-0.17.0 vs lib/httpx/plugins/aws_sdk_authentication.rb in httpx-0.18.0

- old
+ new

@@ -6,10 +6,27 @@ # 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(*) + nil + end + end + # # encapsulates access to an AWS SDK credentials store. # class Credentials def initialize(aws_credentials) @@ -28,46 +45,53 @@ @aws_credentials.session_token end end class << self - attr_reader :credentials, :region - def load_dependencies(_klass) require "aws-sdk-core" - - client = Class.new(Seahorse::Client::Base) do - @identifier = :httpx - set_api(Aws::S3::ClientApi::API) - add_plugin(Aws::Plugins::CredentialsConfiguration) - add_plugin(Aws::Plugins::RegionalEndpoint) - class << self - attr_reader :identifier - end - end.new - - @credentials = Credentials.new(client.config[:credentials]) - @region = client.config[:region] 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 + module OptionsMethods + 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(**options) - credentials = AwsSdkAuthentication.credentials - region = AwsSdkAuthentication.region + 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",