Skip to content

Commit

Permalink
Merge pull request #21 from anweshan/master
Browse files Browse the repository at this point in the history
feat(index): adds new function to append cloud functions service bind credentials
  • Loading branch information
germanattanasio authored Oct 12, 2018
2 parents 79ea0c1 + 0657d9b commit 96b7d1a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -98,4 +127,5 @@ module.exports = {
getCredentials,
getCredentialsFromLocalConfig,
getCredentialsForStarter,
getCredentialsFromServiceBind,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
39 changes: 39 additions & 0 deletions test/test.parse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var assert = require('assert');
var extend = require('extend');
var vcapServices = require('../index');

function assertEmptyObject(expected, actual) {
Expand Down Expand Up @@ -201,3 +202,41 @@ describe('credentials file and Kube', function() {
assertEmptyObject({}, vcapServices.getCredentialsForStarter(undefined));
});
});

describe('cloud functions credentials bind', function() {

var credentials = {
'password': '<password>',
'username': '<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: '<api-key>'},}};
var _params = vcapServices.getCredentialsFromServiceBind(params, 'assistant');
assert.deepEqual(_params, {text: 'hello', iam_apikey: '<api-key>'});
});

});

0 comments on commit 96b7d1a

Please sign in to comment.