Skip to content

Commit

Permalink
Fixed the invocation of refresh! method on SharedCredentials (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashiqueps authored Apr 12, 2022
1 parent 3e79476 commit 2c58c2a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Don't call `refresh!` on non-refreshable `Credentials` when retrying errors (#2685).

3.130.0 (2022-03-11)
------------------

Expand Down
14 changes: 12 additions & 2 deletions gems/aws-sdk-core/lib/aws-sdk-core/plugins/retry_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,17 @@ def exponential_backoff(retries)

def retry_request(context, error)
context.retries += 1
context.config.credentials.refresh! if error.expired_credentials?
context.config.credentials.refresh! if refresh_credentials?(context, error)
context.http_request.body.rewind
context.http_response.reset
call(context)
end

def refresh_credentials?(context, error)
error.expired_credentials? &&
context.config.credentials.respond_to?(:refresh!)
end

def add_retry_headers(context)
request_pairs = {
'attempt' => context.retries,
Expand Down Expand Up @@ -383,7 +388,7 @@ def retry_if_possible(response, error_inspector)
def retry_request(context, error)
delay_retry(context)
context.retries += 1
context.config.credentials.refresh! if error.expired_credentials?
context.config.credentials.refresh! if refresh_credentials?(context, error)
context.http_request.body.rewind
context.http_response.reset
call(context)
Expand All @@ -399,6 +404,11 @@ def should_retry?(context, error)
response_truncatable?(context)
end

def refresh_credentials?(context, error)
error.expired_credentials? &&
context.config.credentials.respond_to?(:refresh!)
end

def retry_limit(context)
context.config.retry_limit
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../../spec_helper'
require_relative '../../retry_errors_helper'

module Aws
module Plugins
Expand Down Expand Up @@ -207,12 +208,20 @@ module Plugins
end

it 'retries if creds expire and are refreshable' do
# Note: this adds the refresh! method to credentials
expect(credentials).to receive(:refresh!).exactly(3).times
resp.error = RetryErrorsSvc::Errors::AuthFailure.new(nil, nil)
handle { |_context| resp }
expect(resp.context.retries).to eq(3)
end

it 'does not call refresh! when error is expired credentials and clock skew' do
resp.error = RetryErrorsSvc::Errors::RequestExpired.new(nil, nil)
resp.context.http_response.headers['date'] = (Time.now + 10*60).iso8601
handle { |_context| resp }
expect(resp.context.retries).to eq(1)
end

it 'retries if endpoint discovery error is detected' do
config.api.endpoint_operation = :describe_endpoints
DescribeEndpointsRequest = Seahorse::Model::Shapes::StructureShape.new(
Expand Down

0 comments on commit 2c58c2a

Please sign in to comment.