Skip to content

Commit 4b0871c

Browse files
authored
Update SES to use AWS SigV4 (#727)
1 parent b221473 commit 4b0871c

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

lib/fog/aws/ses.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,21 @@ class Real
4848
def initialize(options={})
4949

5050
@use_iam_profile = options[:use_iam_profile]
51-
setup_credentials(options)
5251

5352
@instrumentor = options[:instrumentor]
5453
@instrumentor_name = options[:instrumentor_name] || 'fog.aws.ses'
5554
@connection_options = options[:connection_options] || {}
5655
options[:region] ||= 'us-east-1'
56+
@region = options[:region]
57+
5758
@host = options[:host] || "email.#{options[:region]}.amazonaws.com"
5859
@path = options[:path] || '/'
5960
@persistent = options[:persistent] || false
6061
@port = options[:port] || 443
6162
@scheme = options[:scheme] || 'https'
6263
@connection = Fog::XML::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
64+
65+
setup_credentials(options)
6366
end
6467

6568
def reload
@@ -74,7 +77,7 @@ def setup_credentials(options)
7477
@aws_session_token = options[:aws_session_token]
7578
@aws_credentials_expire_at = options[:aws_credentials_expire_at]
7679

77-
@hmac = Fog::HMAC.new('sha256', @aws_secret_access_key)
80+
@signer = Fog::AWS::SignatureV4.new(@aws_access_key_id, @aws_secret_access_key, @region, 'ses')
7881
end
7982

8083
def request(params)
@@ -87,20 +90,20 @@ def request(params)
8790
'Content-Type' => 'application/x-www-form-urlencoded',
8891
'Date' => Fog::Time.now.to_date_header,
8992
}
90-
headers['x-amz-security-token'] = @aws_session_token if @aws_session_token
91-
#AWS3-HTTPS AWSAccessKeyId=<Your AWS Access Key ID>, Algorithm=HmacSHA256, Signature=<Signature>
92-
headers['X-Amzn-Authorization'] = 'AWS3-HTTPS '
93-
headers['X-Amzn-Authorization'] << 'AWSAccessKeyId=' << @aws_access_key_id
94-
headers['X-Amzn-Authorization'] << ', Algorithm=HmacSHA256'
95-
headers['X-Amzn-Authorization'] << ', Signature=' << Base64.encode64(@hmac.sign(headers['Date'])).chomp!
96-
97-
body = ''
98-
for key in params.keys.sort
99-
unless (value = params[key]).nil?
100-
body << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')}&"
101-
end
102-
end
103-
body.chop! # remove trailing '&'
93+
94+
body, headers = AWS.signed_params_v4(
95+
params,
96+
{ 'Content-Type' => 'application/x-www-form-urlencoded' },
97+
{
98+
:method => 'POST',
99+
:aws_session_token => @aws_session_token,
100+
:signer => @signer,
101+
:host => @host,
102+
:path => @path,
103+
:port => @port,
104+
:version => '2010-12-01'
105+
}
106+
)
104107

105108
if @instrumentor
106109
@instrumentor.instrument("#{@instrumentor_name}.request", params) do

0 commit comments

Comments
 (0)