From 8d893a29e750ef694970fcc9ad5f878ddcd4e814 Mon Sep 17 00:00:00 2001 From: Anwesha Naskar Date: Wed, 10 Oct 2018 11:54:52 -0400 Subject: [PATCH 1/4] feat(index): adds new function to append cloud functions service bind credentials --- index.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/index.js b/index.js index 39f1632..6393ecc 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 addCredentialsFromServiceBind = function(theParams, service, serviceAltName) { + if (Object.keys(theParams).length === 0) { + return theParams; + } + let bxCreds = {}; + if (theParams.__bx_creds && theParams.__bx_creds[service]) { + // If user has IAM instance of service + bxCreds = theParams.__bx_creds[service]; + } else if (theParams.__bx_creds && theParams.__bx_creds[serviceAltName]) { + // If user has no IAM instance of service, check for CF instances + bxCreds = theParams.__bx_creds[serviceAltName]; + } + const _params = Object.assign({}, bxCreds, theParams); + 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, + addCredentialsFromServiceBind, }; From 85bed09e94152ab8cddf3d592ce4901446e2c666 Mon Sep 17 00:00:00 2001 From: Anwesha Naskar Date: Thu, 11 Oct 2018 18:03:03 -0400 Subject: [PATCH 2/4] refactor(index): renames methods and parameters for service bind --- index.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 6393ecc..2412754 100644 --- a/index.js +++ b/index.js @@ -82,19 +82,19 @@ const getCredentialsFromLocalConfig = function(serviceLabel, credentials) { * @param {string} serviceAltName - alternate name of service used for cloud foundry instances * @return {Object} - returns parameters modified to include credentials from service bind */ -const addCredentialsFromServiceBind = function(theParams, service, serviceAltName) { - if (Object.keys(theParams).length === 0) { - return theParams; +const getCredentialsFromServiceBind = function(params, serviceName, serviceAltName) { + if (Object.keys(params).length === 0) { + return params; } let bxCreds = {}; - if (theParams.__bx_creds && theParams.__bx_creds[service]) { + if (params.__bx_creds && params.__bx_creds[serviceName]) { // If user has IAM instance of service - bxCreds = theParams.__bx_creds[service]; - } else if (theParams.__bx_creds && theParams.__bx_creds[serviceAltName]) { + 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 = theParams.__bx_creds[serviceAltName]; + bxCreds = params.__bx_creds[serviceAltName]; } - const _params = Object.assign({}, bxCreds, theParams); + const _params = Object.assign({}, bxCreds, params); if (_params.apikey) { _params.iam_apikey = _params.apikey; delete _params.apikey; @@ -127,5 +127,5 @@ module.exports = { getCredentials, getCredentialsFromLocalConfig, getCredentialsForStarter, - addCredentialsFromServiceBind, + getCredentialsFromServiceBind, }; From ea39da6c61d0e6517262120b581c6160e8ac037b Mon Sep 17 00:00:00 2001 From: Anwesha Naskar Date: Fri, 12 Oct 2018 11:24:54 -0400 Subject: [PATCH 3/4] test(tests): adds tests for service bind --- package.json | 3 ++- test/test.parse.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) 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..fa59646 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', () => { + const params = { text: 'hello', __bx_creds: {conversation: credentials}}; + const _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', () => { + const params = { text: 'hello', __bx_creds: {conversation: credentials}}; + const _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', () => { + const params = { text: 'hello', __bx_creds: {conversationAltName: credentials,}}; + const _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', () => { + const params = { text: 'hello', __bx_creds: {assistant: credentials,}}; + const _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation', 'conversationAltName'); + assert.deepEqual(_params, extend({}, {text: 'hello'})); + }); + + it('should modify apikey to iam_apikey', () => { + const params = { text: 'hello', __bx_creds: {assistant: {apikey: ''},}}; + const _params = vcapServices.getCredentialsFromServiceBind(params, 'assistant'); + assert.deepEqual(_params, {text: 'hello', iam_apikey: ''}); + }); + +}); From 0657d9b39455f838ea20ca26bf34520863108983 Mon Sep 17 00:00:00 2001 From: Anwesha Naskar Date: Fri, 12 Oct 2018 11:42:21 -0400 Subject: [PATCH 4/4] refactor(tests): refactors tests to not use es6 features --- test/test.parse.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/test.parse.js b/test/test.parse.js index fa59646..27a90f5 100644 --- a/test/test.parse.js +++ b/test/test.parse.js @@ -209,33 +209,33 @@ describe('cloud functions credentials bind', function() { 'password': '', 'username': '', }; - it('should succeed with __bx_creds as credential source', () => { - const params = { text: 'hello', __bx_creds: {conversation: credentials}}; - const _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation'); + 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', () => { - const params = { text: 'hello', __bx_creds: {conversation: credentials}}; - const _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation'); + 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', () => { - const params = { text: 'hello', __bx_creds: {conversationAltName: credentials,}}; - const _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation', 'conversationAltName'); + 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', () => { - const params = { text: 'hello', __bx_creds: {assistant: credentials,}}; - const _params = vcapServices.getCredentialsFromServiceBind(params, 'conversation', 'conversationAltName'); + 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', () => { - const params = { text: 'hello', __bx_creds: {assistant: {apikey: ''},}}; - const _params = vcapServices.getCredentialsFromServiceBind(params, 'assistant'); + 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: ''}); });