added the aws signature on top of the aws-sigv4 plugin, with support for using aws-sdk-core ready-made config

This commit is contained in:
HoneyryderChuck 2021-01-30 23:11:50 +00:00
parent 755b895b08
commit 463dad46cb
6 changed files with 95 additions and 33 deletions

View File

@ -0,0 +1,41 @@
# frozen_string_literal: true
module HTTPX
module Plugins
#
# This plugin adds helper methods to apply AWS Sigv4 to AWS cloud requests.
#
module AWSAuthentication
class << self
def load_dependencies(klass)
require "aws-sdk-core"
klass.plugin(:aws_sigv4)
end
end
module InstanceMethods
#
# aws_authentication
# aws_authentication(credentials: Aws::Credentials.new('akid', 'secret'))
# aws_authentication()
#
def aws_authentication(options = nil)
s3_client = Aws::S3::Client.new
credentials = s3_client.config[:credentials]
aws_sigv4_authentication(
username: credentials.access_key_id,
password: credentials.secret_access_key,
security_token: credentials.session_token,
region: s3_client.config[:region],
provider_prefix: "aws",
header_provider_field: "amz",
**options
)
end
alias_method :aws_auth, :aws_authentication
end
end
register_plugin :aws_authentication, AWSAuthentication
end
end

View File

@ -8,6 +8,8 @@ module HTTPX
#
# This plugin adds AWS Sigv4 authentication.
#
# https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
#
# https://gitlab.com/honeyryderchuck/httpx/wikis/AWS-SigV4
#
module AWSSigV4
@ -152,9 +154,8 @@ module HTTPX
end.new.merge(options)
end
def load_dependencies(klass)
def load_dependencies(_klass)
require "openssl"
klass.plugin(:authentication)
end
end

View File

@ -1,7 +1,6 @@
# frozen_string_literal: true
require_relative "support/http_helpers"
require "aws-sdk-s3"
class HTTPXAwsSigv4Test < Minitest::Test
include ResponseHelpers
@ -90,36 +89,6 @@ class HTTPXAwsSigv4Test < Minitest::Test
"Signature=4a7d3e06d1950eb64a3daa1becaa8ba030d9099858516cb2fa4533fab4e8937d"
end
AWS_URI = ENV.fetch("AMZ_HOST", "aws:4566")
USERNAME = ENV.fetch("AWS_ACCESS_KEY_ID", "test")
PASSWORD = ENV.fetch("AWS_SECRET_ACCESS_KEY", "test")
def test_plugin_aws_sigv4_get_object
s3_client = Aws::S3::Client.new(
endpoint: "http://#{AWS_URI}",
force_path_style: true
# http_wire_trace: true,
# logger: Logger.new(STDERR)
)
s3_client.create_bucket(bucket: "test", acl: "private")
object = s3_client.put_object(bucket: "test", key: "testimage", body: "bucketz")
# now let's get it
# no_sig_response = HTTPX.get("http://#{AWS_URI}/test/testimage")
# verify_error_response(no_sig_response)
aws_req_headers = object.context.http_request.headers
response = sigv4_session(username: USERNAME, password: PASSWORD, unsigned_headers: %w[accept content-type content-length])
.with(headers: {
"user-agent" => aws_req_headers["user-agent"],
"expect" => "100-continue",
"x-amz-date" => aws_req_headers["x-amz-date"],
"content-md5" => OpenSSL::Digest.base64digest("MD5", "bucketz"),
})
.put("http://#{AWS_URI}/test/testimage", body: "bucketz")
verify_status(response, 200)
end
private
def sigv4_session(**options)

View File

@ -27,6 +27,7 @@ class HTTPTest < Minitest::Test
include Plugins::Expect
include Plugins::RateLimiter
include Plugins::Stream
include Plugins::AWSAuthentication
def test_verbose_log
log = StringIO.new

View File

@ -28,6 +28,7 @@ class HTTPSTest < Minitest::Test
include Plugins::RateLimiter
include Plugins::Persistent unless RUBY_ENGINE == "jruby" || RUBY_VERSION < "2.3"
include Plugins::Stream
include Plugins::AWSAuthentication
def test_connection_coalescing
coalesced_origin = "https://#{ENV["HTTPBIN_COALESCING_HOST"]}"

View File

@ -0,0 +1,49 @@
# frozen_string_literal: true
require "aws-sdk-s3"
module Requests
module Plugins
module AWSAuthentication
AWS_URI = ENV.fetch("AMZ_HOST", "aws:4566")
def test_plugin_aws_authentication_put_object
amz_uri = origin(AWS_URI)
s3_client = Aws::S3::Client.new(
endpoint: amz_uri,
force_path_style: true,
ssl_verify_peer: false,
# http_wire_trace: true,
# logger: Logger.new(STDERR)
)
s3_client.create_bucket(bucket: "test", acl: "private")
object = s3_client.put_object(bucket: "test", key: "testimage", body: "bucketz")
# now let's get it
# no_sig_response = HTTPX.get("http://#{AWS_URI}/test/testimage")
# verify_error_response(no_sig_response)
aws_req_headers = object.context.http_request.headers
response = aws_s3_session(unsigned_headers: %w[accept content-type content-length])
.with(ssl: { verify_mode: OpenSSL::SSL::VERIFY_NONE },
headers: {
"user-agent" => aws_req_headers["user-agent"],
# gotta fix localstack first
# "expect" => "100-continue",
"x-amz-date" => aws_req_headers["x-amz-date"],
"content-md5" => OpenSSL::Digest.base64digest("MD5", "bucketz"),
})
.put("#{amz_uri}/test/testimage", body: "bucketz")
verify_status(response, 200)
end
private
def aws_s3_session(**options)
HTTPX.plugin(:aws_authentication).aws_authentication(service: "s3", **options)
end
end
end
end