Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit f3839b4

Browse files
committed
Retry bad IAM token requests.
1 parent 0148fff commit f3839b4

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

CHANGES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# 4.1.2 (2019-07-29)
2-
- [FIXED] Plugins can now be loaded from outside of the 'plugins/' directory
1+
# UNRELEASED
2+
- [FIXED] Allow plugins to be loaded from outside the 'plugins/' directory.
3+
- [FIXED] Retry bad IAM token requests.
34

45
# 4.1.1 (2019-06-17)
56
- [FIXED] Remove unnecessary `npm-cli-login` dependency.

plugins/iamauth.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ class IAMPlugin extends BasePlugin {
153153
callback(new Error('Invalid response from IAM token service'));
154154
}
155155
} else {
156-
self.shouldApplyIAMAuth = false;
157-
callback(new Error('Failed to access token'));
156+
if (response.statusCode < 500 && response.statusCode !== 429) {
157+
self.shouldApplyIAMAuth = false; // no retry
158+
}
159+
callback(new Error(`Failed to acquire access token. Status code: ${response.statusCode}`));
158160
}
159161
});
160162
},
@@ -175,8 +177,10 @@ class IAMPlugin extends BasePlugin {
175177
debug('Successfully renewed IAM session.');
176178
callback();
177179
} else {
178-
self.shouldApplyIAMAuth = false;
179-
callback(new Error('Failed to exchange IAM token with Cloudant'));
180+
if (response.statusCode < 500) {
181+
self.shouldApplyIAMAuth = false; // no retry
182+
}
183+
callback(new Error(`Failed to exchange IAM token with Cloudant. Status code: ${response.statusCode}`));
180184
}
181185
});
182186
}

test/plugins/iamauth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ describe('#db IAMAuth Plugin', function() {
353353
'response_type': 'cloud_iam',
354354
'apikey': IAM_API_KEY
355355
})
356+
.times(3)
356357
.reply(500, 'Internal Error 500\nThe server encountered an unexpected condition which prevented it from fulfilling the request.');
357358

358359
var cloudantMocks = nock(SERVER)
@@ -418,10 +419,12 @@ describe('#db IAMAuth Plugin', function() {
418419
'response_type': 'cloud_iam',
419420
'apikey': IAM_API_KEY
420421
})
422+
.times(3)
421423
.reply(200, MOCK_IAM_TOKEN_RESPONSE);
422424

423425
var cloudantMocks = nock(SERVER)
424426
.post('/_iam_session', {access_token: MOCK_ACCESS_TOKEN})
427+
.times(3)
425428
.reply(500, {error: 'internal_server_error', reason: 'Internal Server Error'})
426429
.get(DBNAME)
427430
.reply(401, {error: 'unauthorized', reason: 'Unauthorized'});

0 commit comments

Comments
 (0)