1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[aws|storage] redirect fixes to match latest and v4 sigs

This commit is contained in:
geemus 2014-11-19 10:39:28 -06:00
parent 0f77e9a766
commit 5c9542d2ce
2 changed files with 22 additions and 15 deletions

View file

@ -5,7 +5,7 @@ module Fog
module AWS
class SignatureV4
ALGORITHM = 'AWS4-HMAC-SHA256'
def initialize(aws_access_key_id, secret_key, region,service)
def initialize(aws_access_key_id, secret_key, region, service)
@region = region
@service = service
@aws_access_key_id = aws_access_key_id
@ -43,7 +43,7 @@ module Fog
#{body_sha || Digest::SHA256.hexdigest(params[:body] || '')}
DATA
canonical_request.chop!
string_to_sign = <<-DATA
#{ALGORITHM}
#{date.to_iso8601_basic}
@ -52,7 +52,7 @@ DATA
DATA
string_to_sign.chop!
signature = derived_hmac(date).sign(string_to_sign)
{

View file

@ -519,24 +519,31 @@ module Fog
def _request(scheme, host, port, params, original_params, &block)
connection(scheme, host, port).request(params, &block)
rescue Excon::Errors::TemporaryRedirect => error
rescue Excon::Errors::MovedPermanently, Excon::Errors::TemporaryRedirect => error
headers = (error.response.is_a?(Hash) ? error.response[:headers] : error.response.headers)
uri = URI.parse(headers['Location'])
Fog::Logger.warning("fog: followed redirect to #{uri.host}, connecting to the matching region will be more performant")
region = case uri.host
host = if headers.has_key?('Location')
URI.parse(headers['Location']).host
else
body = error.response.is_a?(Hash) ? error.response[:body] : error.response.body
%r{<Endpoint>([^<]*)</Endpoint>}.match(body).captures.first
end
Fog::Logger.warning("fog: followed redirect to #{host}, connecting to the matching region will be more performant")
region = case host
when 's3.amazonaws.com'
DEFAULT_REGION
else
%r{s3-([^\.]*).amazonaws.com}.match(uri.host).captures.first
%r{s3-([^\.]*).amazonaws.com}.match(host).captures.first
end
Fog::XML::Connection.new(
"#{uri.scheme}://#{uri.host}:#{uri.port}",
false,
@connection_options
).request(
original_params.merge(:region => region),
&block
original_signer = @signer
@signer = Fog::AWS::SignatureV4.new( @aws_access_key_id, @aws_secret_access_key, region, 's3')
params[:headers].delete('Authorization')
response = request(
original_params.merge(
:host => host
), &block
)
@signer = original_signer
response
end
# See http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html