Skip to content

Commit

Permalink
Added support for non-standard ports with sigv4.
Browse files Browse the repository at this point in the history
Fixes #883.
  • Loading branch information
trevorrowe committed Jul 29, 2015
1 parent 6318995 commit 4b501ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Unreleased Changes
------------------

* Feature - Signature Version 4 - Added support for signing requests against
hosts that do not use the standard port for the given HTTP scheme. This
makes it possible to use the signer against test endpoints.

See [related GitHub issue #883](https://github.com/aws/aws-sdk-ruby/issues/883).

2.1.10 (2015-07-29)
------------------

Expand Down
17 changes: 15 additions & 2 deletions aws-sdk-core/lib/aws-sdk-core/signers/v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def sign(req)
datetime = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
body_digest = req.headers['X-Amz-Content-Sha256'] || hexdigest(req.body)
req.headers['X-Amz-Date'] = datetime
req.headers['Host'] = req.endpoint.host
req.headers['Host'] = host(req.endpoint)
req.headers['X-Amz-Security-Token'] = @credentials.session_token if
@credentials.session_token
req.headers['X-Amz-Content-Sha256'] ||= body_digest
Expand All @@ -51,7 +51,7 @@ def presigned_url(request, options = {})
now = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
body_digest = options[:body_digest] || hexdigest(request.body)

request.headers['Host'] = request.endpoint.host
request.headers['Host'] = host(request.endpoint)
request.headers.delete('User-Agent')

params = Aws::Query::ParamList.new
Expand Down Expand Up @@ -179,6 +179,19 @@ def canonical_header_value(value)
value.match(/^".*"$/) ? value : value.gsub(/\s+/, ' ').strip
end

def host(uri)
if standard_port?(uri)
uri.host
else
"#{uri.host}:#{uri.port}"
end
end

def standard_port?(uri)
(uri.scheme == 'http' && uri.port == 80) ||
(uri.scheme == 'https' && uri.port == 443)
end

def hexdigest(value)
digest = OpenSSL::Digest::SHA256.new
if value.respond_to?(:read)
Expand Down
9 changes: 9 additions & 0 deletions aws-sdk-core/spec/aws/signers/v4_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ module Signers
expect(sign.headers['Host']).to eq('domain.com')
end

it "includes port in host when non stadard" do
endpoint.scheme = 'http'
endpoint.port = 3000
expect(sign.headers['Host']).to eq('domain.com:3000')
endpoint.scheme = 'https'
endpoint.port = 3000
expect(sign.headers['Host']).to eq('domain.com:3000')
end

it "populates the 'X-Amz-Date' header" do
datetime = '20120102:10:11:12Z'
expect(utc).to receive(:strftime).with("%Y%m%dT%H%M%SZ") { datetime }
Expand Down

0 comments on commit 4b501ca

Please sign in to comment.