From e5d0cddf2c46d59d36f5c6e2234a0a46fa03c21b Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Mon, 3 Nov 2014 15:50:41 +0000 Subject: [PATCH] [AWS|SNS] Use Signature v4 --- lib/fog/aws/sns.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/fog/aws/sns.rb b/lib/fog/aws/sns.rb index 76cf6df26..70af5a79e 100644 --- a/lib/fog/aws/sns.rb +++ b/lib/fog/aws/sns.rb @@ -49,12 +49,12 @@ module Fog # * SNS object with connection to AWS. def initialize(options={}) @use_iam_profile = options[:use_iam_profile] - setup_credentials(options) @connection_options = options[:connection_options] || {} @instrumentor = options[:instrumentor] @instrumentor_name = options[:instrumentor_name] || 'fog.aws.sns' options[:region] ||= 'us-east-1' + @region = options[:region] @host = options[:host] || "sns.#{options[:region]}.amazonaws.com" @path = options[:path] || '/' @@ -62,6 +62,8 @@ module Fog @port = options[:port] || 443 @scheme = options[:scheme] || 'https' @connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options) + + setup_credentials(options) end def reload @@ -76,7 +78,7 @@ module Fog @aws_session_token = options[:aws_session_token] @aws_credentials_expire_at = options[:aws_credentials_expire_at] - @hmac = Fog::HMAC.new('sha256', @aws_secret_access_key) + @signer = Fog::AWS::SignatureV4.new( @aws_access_key_id, @aws_secret_access_key, @region, 'sns') end def request(params) @@ -85,12 +87,13 @@ module Fog idempotent = params.delete(:idempotent) parser = params.delete(:parser) - body = AWS.signed_params( + body, headers = AWS.signed_params_v4( params, + { 'Content-Type' => 'application/x-www-form-urlencoded' }, { - :aws_access_key_id => @aws_access_key_id, + :method => 'POST', :aws_session_token => @aws_session_token, - :hmac => @hmac, + :signer => @signer, :host => @host, :path => @path, :port => @port @@ -99,19 +102,19 @@ module Fog if @instrumentor @instrumentor.instrument("#{@instrumentor_name}.request", params) do - _request(body, idempotent, parser) + _request(body, headers, idempotent, parser) end else - _request(body, idempotent, parser) + _request(body, headers, idempotent, parser) end end - def _request(body, idempotent, parser) + def _request(body, headers, idempotent, parser) @connection.request({ :body => body, :expects => 200, :idempotent => idempotent, - :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, + :headers => headers, :method => 'POST', :parser => parser })