diff --git a/index.js b/index.js index 39f1632..2412754 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,35 @@ const getCredentialsFromLocalConfig = function(serviceLabel, credentials) { return creds; } +/** +* Helper function used to add credentials bound to cloud functions using wsk service bind +* +* @param {Object} theParams - parameters sent to service +* @param {string} service - name of service in bluemix used to retrieve credentials, used for IAM instances +* @param {string} serviceAltName - alternate name of service used for cloud foundry instances +* @return {Object} - returns parameters modified to include credentials from service bind +*/ +const getCredentialsFromServiceBind = function(params, serviceName, serviceAltName) { + if (Object.keys(params).length === 0) { + return params; + } + let bxCreds = {}; + if (params.__bx_creds && params.__bx_creds[serviceName]) { + // If user has IAM instance of service + bxCreds = params.__bx_creds[serviceName]; + } else if (params.__bx_creds && params.__bx_creds[serviceAltName]) { + // If user has no IAM instance of service, check for CF instances + bxCreds = params.__bx_creds[serviceAltName]; + } + const _params = Object.assign({}, bxCreds, params); + if (_params.apikey) { + _params.iam_apikey = _params.apikey; + delete _params.apikey; + } + delete _params.__bx_creds; + return _params; +} + /** * Returns all the credentials that match the service label from env variables * @@ -98,4 +127,5 @@ module.exports = { getCredentials, getCredentialsFromLocalConfig, getCredentialsForStarter, + getCredentialsFromServiceBind, }; diff --git a/package.json b/package.json index 50b7bb8..752e1fd 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "mocha": "~3.2.0", "coveralls": "~2.12.0", "istanbul": "~0.4.5", - "jshint": "~2.9.4" + "jshint": "~2.9.4", + "extend": "^3.0.1" }, "dependencies": {}, "scripts": { diff --git a/test/test.parse.js b/test/test.parse.js index 6dc2b84..27a90f5 100644 --- a/test/test.parse.js +++ b/test/test.parse.js @@ -1,6 +1,7 @@ 'use strict'; var assert = require('assert'); +var extend = require('extend'); var vcapServices = require('../index'); function assertEmptyObject(expected, actual) { @@ -201,3 +202,41 @@ describe('credentials file and Kube', function() { assertEmptyObject({}, vcapServices.getCredentialsForStarter(undefined)); }); }); + +describe('cloud functions credentials bind', function() { + + var credentials = { + 'password': '', + 'username': '', + }; + it('should succeed with __bx_creds as credential source', function(){ + var params = { text: 'hello', __bx_creds: {conversation: credentials}}; + var _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation'); + assert.deepEqual(_params, extend({}, {text: 'hello'}, credentials)); + }); + + it('should succeed with __bx_creds as credential source with an alternate name', function() { + var params = { text: 'hello', __bx_creds: {conversation: credentials}}; + var _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation'); + assert.deepEqual(_params, extend({}, {text: 'hello'}, credentials)); + }); + + it('should succeed with __bx_creds as credential source with an alternate name', function() { + var params = { text: 'hello', __bx_creds: {conversationAltName: credentials,}}; + var _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation', 'conversationAltName'); + assert.deepEqual(_params, extend({}, {text: 'hello'}, credentials)); + }); + + it('should not modify params with __bx_creds as credential source with a different name', function() { + var params = { text: 'hello', __bx_creds: {assistant: credentials,}}; + var _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation', 'conversationAltName'); + assert.deepEqual(_params, extend({}, {text: 'hello'})); + }); + + it('should modify apikey to iam_apikey', function() { + var params = { text: 'hello', __bx_creds: {assistant: {apikey: ''},}}; + var _params = vcapServices.getCredentialsFromServiceBind(params, 'assistant'); + assert.deepEqual(_params, {text: 'hello', iam_apikey: ''}); + }); + +});