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

Commit 66810db

Browse files
committed
Add new setIamApiKey function.
1 parent 9dddca3 commit 66810db

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# UNRELEASED
2+
- [NEW] Added option to set new IAM API key.
23
- [FIXED] Allow plugins to be loaded from outside the 'plugins/' directory.
34
- [FIXED] Retry bad IAM token requests.
45

lib/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ class CloudantClient {
233233

234234
// public
235235

236+
/**
237+
* Get a client plugin instance.
238+
*
239+
* @param {string} pluginId
240+
*/
241+
getPlugin(pluginId) {
242+
return this._plugins[this._pluginIds.indexOf(pluginId)];
243+
}
244+
236245
/**
237246
* Perform a request using this Cloudant client.
238247
*

plugins/iamauth.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class IAMPlugin extends BasePlugin {
3737

3838
super(client, cfg);
3939

40-
this.iamApiKey = null;
40+
this.currentIamApiKey = null;
4141
this.baseUrl = cfg.baseUrl || null;
42-
this.cookieJar = request.jar();
42+
this.cookieJar = null;
4343
this.tokenUrl = cfg.iamTokenUrl || 'https://iam.cloud.ibm.com/identity/token';
4444
this.shouldApplyIAMAuth = true;
4545
this.refreshRequired = true;
@@ -48,8 +48,10 @@ class IAMPlugin extends BasePlugin {
4848
onRequest(state, req, callback) {
4949
var self = this;
5050

51-
if (self._cfg.iamApiKey !== self.iamApiKey) {
52-
debug('New credentials identified. Renewing session cookie...');
51+
if (self._cfg.iamApiKey !== self.currentIamApiKey) {
52+
debug('New IAM API key identified.');
53+
this.cookieJar = request.jar(); // new jar
54+
self.currentIamApiKey = self._cfg.iamApiKey;
5355
self.shouldApplyIAMAuth = self.refreshRequired = true;
5456
}
5557

@@ -177,7 +179,6 @@ class IAMPlugin extends BasePlugin {
177179
if (error) {
178180
callback(error);
179181
} else if (response.statusCode === 200) {
180-
self.iamApiKey = cfg.iamApiKey;
181182
self.refreshRequired = false;
182183
debug('Successfully renewed IAM session.');
183184
callback();
@@ -196,6 +197,11 @@ class IAMPlugin extends BasePlugin {
196197
});
197198
});
198199
}
200+
201+
setIamApiKey(iamApiKey) {
202+
debug('Setting new IAM API key.');
203+
this._cfg.iamApiKey = iamApiKey;
204+
}
199205
}
200206

201207
IAMPlugin.id = 'iamauth';

test/plugins/iamauth.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,58 @@ describe('#db IAMAuth Plugin', function() {
604604
done();
605605
});
606606
});
607+
608+
it('supports changing the IAM API key', function(done) {
609+
if (process.env.NOCK_OFF) {
610+
this.skip();
611+
}
612+
613+
var iamMocks = nock(TOKEN_SERVER)
614+
.post('/identity/token', {
615+
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
616+
'response_type': 'cloud_iam',
617+
'apikey': 'bad_key'
618+
})
619+
.reply(400, {
620+
errorCode: 'BXNIM0415E',
621+
errorMessage: 'Provided API key could not be found'
622+
})
623+
.post('/identity/token', {
624+
'grant_type': 'urn:ibm:params:oauth:grant-type:apikey',
625+
'response_type': 'cloud_iam',
626+
'apikey': IAM_API_KEY
627+
})
628+
.reply(200, MOCK_IAM_TOKEN_RESPONSE);
629+
630+
var cloudantMocks = nock(SERVER)
631+
.get(DBNAME)
632+
.reply(401, {error: 'unauthorized', reason: 'Unauthorized'})
633+
.post('/_iam_session', {access_token: MOCK_ACCESS_TOKEN})
634+
.reply(200, {ok: true}, MOCK_SET_IAM_SESSION_HEADER)
635+
.get(DBNAME)
636+
.reply(200, {doc_count: 0});
637+
638+
var cloudantClient = new Client({ plugins: { iamauth: { iamApiKey: 'bad_key' } } });
639+
var req = { url: SERVER + DBNAME, method: 'GET' };
640+
cloudantClient.request(req, function(err, resp, data) {
641+
assert.equal(err, null);
642+
assert.equal(resp.request.headers.cookie, null);
643+
assert.equal(resp.statusCode, 401);
644+
assert.ok(data.indexOf('"error":"unauthorized"') > -1);
645+
646+
// update IAM API key
647+
cloudantClient.getPlugin('iamauth').setIamApiKey(IAM_API_KEY);
648+
649+
cloudantClient.request(req, function(err, resp, data) {
650+
assert.equal(err, null);
651+
assert.equal(resp.request.headers.cookie, MOCK_IAM_SESSION);
652+
assert.equal(resp.statusCode, 200);
653+
assert.ok(data.indexOf('"doc_count":0') > -1);
654+
655+
iamMocks.done();
656+
cloudantMocks.done();
657+
done();
658+
});
659+
});
660+
});
607661
});

0 commit comments

Comments
 (0)