From f2bca6819daa5f6453bbfc371ef9bde94a0f5899 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Tue, 18 Dec 2018 12:41:25 +0530 Subject: [PATCH 01/11] MT first cut --- lib/handler.js | 15 ++++++++------- lib/nlu/utils.js | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 1df0ccb..0de35ad 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -30,17 +30,18 @@ let Handler = function () { /** * Initializes the handler, sets up the connection to wcs */ -Handler.prototype.initialize = function () { - if(WaEnvUtils.isValidWaEnvVars()) { +Handler.prototype.initialize = function (creds) { + //check if credentials are coming from the handler initialize call from the index.js of the skill + if(creds){ + this.wcsCredentials = WaEnvUtils.setupWCSCredentialsfromVCAP(creds); + let credentials = this.wcsCredentials.credentials; + setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) + }else if(WaEnvUtils.isValidWaEnvVars()) { this.wcsCredentials = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName)); this.wcsCredentials = JSON.parse(rawJson); - } - if (this.wcsCredentials) { - let credentials = this.wcsCredentials.credentials; - setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) - } + } }; /** diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js index 10ef8d4..00297c3 100644 --- a/lib/nlu/utils.js +++ b/lib/nlu/utils.js @@ -55,8 +55,46 @@ let setupWCSCredentialsFromEnv = function () { return options; }; +/** + * Sets up the handler's wcs credentials from VCAP credentials read from the skill + * @param handler + */ + +let setupWCSCredentialsfromVCAP = function (credentials) { + + let options = { + "workspace": { + [process.env.WCS_WORKSPACE_LANGUAGE]: { + "name": credentials.workspace_name, + "workspace_id": credentials.workspace_id + } + } + }; + // new way of wa authentication - with iam api-key + if(credentials.apikey) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION_DATE, + "iam_apikey": credentials.apikey + } + } + // deprecated way of wa authentication + else if(process.env.WCS_PASSWORD && process.env.WCS_USERNAME) { + options.credentials = { + "url": process.env.WCS_URL, + "version": process.env.WCS_VERSION, + "version_date": process.env.WCS_VERSION_DATE, + "password": process.env.WCS_PASSWORD, + "username": process.env.WCS_USERNAME + } + } + return options; + +}; + module.exports = { isValidWaEnvVars: isValidWaEnvVars, - setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv + setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv, + setupWCSCredentialsfromVCAP: setupWCSCredentialsfromVCAP }; \ No newline at end of file From b97cd26df704ca3aa310feb08cc6d75d6ad1f565 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Tue, 18 Dec 2018 15:22:21 +0530 Subject: [PATCH 02/11] updated factory and package --- lib/nlu/factory.js | 11 +++++++---- package.json | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 6f7aba7..ccb0484 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -97,12 +97,12 @@ class Factory { } - static getNLUs(manifest) { + static getNLUs(manifest, credentials) { return new Promise(function (resolve, reject) { let promises = []; let types = manifest.nlu; types.forEach(function (type) { - promises.push(getNLU(type, manifest.name)); + promises.push(getNLU(type, manifest.name, credentials)); }); Promise.all(promises.map(reflect)).then(function (results) { @@ -125,10 +125,13 @@ class Factory { module.exports = Factory; -function getNLU(type, name) { +function getNLU(type, name, credentials) { return new Promise(function (resolve, reject) { let nlu; - if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { + if(credentials){ + nlu = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); + } + else if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { nlu = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { nlu = require(`${resPath}/nlu/${type}`); diff --git a/package.json b/package.json index 2d2a576..3bc4eca 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skill-sdk-nodejs", - "version": "0.0.18", + "version": "0.0.19", "description": "This is the SDK to be used when developing a skill for Watson Assistant Solutions using Node.js", "main": "index.js", "scripts": { From 9b8d4a2914d3feada92ddacb6bd87d9b370fb6b6 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Tue, 18 Dec 2018 15:23:48 +0530 Subject: [PATCH 03/11] utils updated to take credentials --- lib/nlu/utils.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js index 00297c3..26a7bfb 100644 --- a/lib/nlu/utils.js +++ b/lib/nlu/utils.js @@ -73,8 +73,8 @@ let setupWCSCredentialsfromVCAP = function (credentials) { // new way of wa authentication - with iam api-key if(credentials.apikey) { options.credentials = { - "url": process.env.WCS_URL, - "version": process.env.WCS_VERSION_DATE, + "url": credentials.url, + "version": credentials.version_date, "iam_apikey": credentials.apikey } } From c9addc111301267ef6003e5eb621f18fb92ff1ae Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 13:58:03 +0530 Subject: [PATCH 04/11] added comments and added additional checks --- lib/handler.js | 17 ++++++++++------- lib/nlu/factory.js | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/handler.js b/lib/handler.js index 0de35ad..988712b 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -29,19 +29,22 @@ let Handler = function () { /** * Initializes the handler, sets up the connection to wcs + * @params credentials which includes apikey, workspace_id, workspace_name, iam_apikey description, iam_apikey, workspace url, version, version_date */ -Handler.prototype.initialize = function (creds) { - //check if credentials are coming from the handler initialize call from the index.js of the skill - if(creds){ +Handler.prototype.initialize = function (credentials) { + //check if credentials are coming from the handler initialize call and also valid from the index.js of the skill + if((credentials && WaEnvUtils.isValidWaCredentials(credentials))){ this.wcsCredentials = WaEnvUtils.setupWCSCredentialsfromVCAP(creds); - let credentials = this.wcsCredentials.credentials; - setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) - }else if(WaEnvUtils.isValidWaEnvVars()) { + } else if(WaEnvUtils.isValidWaEnvVars()) { this.wcsCredentials = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { let rawJson = fs.readFileSync(path.join(process.env.skillSDKResDir, nluFolder, wcsFileName)); this.wcsCredentials = JSON.parse(rawJson); - } + } + if(this.wcsCredentials){ + let credentials = this.wcsCredentials.credentials; + setupWcs(this, credentials.url, credentials.username, credentials.password, credentials.version_date, credentials.version, credentials.iam_apikey) + } }; /** diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index ccb0484..408b8e6 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -96,7 +96,7 @@ class Factory { }); } - +//pass the credentials sent from the skill to getNLU's static getNLUs(manifest, credentials) { return new Promise(function (resolve, reject) { let promises = []; @@ -124,11 +124,11 @@ class Factory { module.exports = Factory; - +//pass the credentials from the skill function getNLU(type, name, credentials) { return new Promise(function (resolve, reject) { let nlu; - if(credentials){ + if((credentials && WaEnvUtils.isValidWaCredentials(credentials))){ nlu = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); } else if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') { From 5982b9bc1e77242659fb7ea14cbb2cbba0a1d6d5 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 14:12:36 +0530 Subject: [PATCH 05/11] added tenantID as an optional parameter in swagger converse and evaluate --- lib/server/api/swagger/swagger.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/server/api/swagger/swagger.yaml b/lib/server/api/swagger/swagger.yaml index d7047ba..6b02720 100644 --- a/lib/server/api/swagger/swagger.yaml +++ b/lib/server/api/swagger/swagger.yaml @@ -210,6 +210,10 @@ definitions: type: string example: 'en-US' description: language + tenantID: + type: string + example: '36 characters long' + description: the tenantID in a Multi Tenant Environment text: type: string example: 'Hello' @@ -344,6 +348,10 @@ definitions: type: string example: 'en-US' description: language + tenantID: + type: string + example: '36 characters long' + description: the tenantID in a Multi Tenant Environment text: type: string example: 'Hello' From 37740dc4ccf9110421c95657fd44082f2f6d63ce Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 14:19:55 +0530 Subject: [PATCH 06/11] reverted the tenantID inclusion in swagger for backward compatibility --- lib/server/api/swagger/swagger.yaml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/server/api/swagger/swagger.yaml b/lib/server/api/swagger/swagger.yaml index 6b02720..d7047ba 100644 --- a/lib/server/api/swagger/swagger.yaml +++ b/lib/server/api/swagger/swagger.yaml @@ -210,10 +210,6 @@ definitions: type: string example: 'en-US' description: language - tenantID: - type: string - example: '36 characters long' - description: the tenantID in a Multi Tenant Environment text: type: string example: 'Hello' @@ -348,10 +344,6 @@ definitions: type: string example: 'en-US' description: language - tenantID: - type: string - example: '36 characters long' - description: the tenantID in a Multi Tenant Environment text: type: string example: 'Hello' From aa0fb5cb0d2107dc162be13173666c4b761776a1 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 15:09:53 +0530 Subject: [PATCH 07/11] added additional checks --- lib/nlu/utils.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js index 26a7bfb..c177178 100644 --- a/lib/nlu/utils.js +++ b/lib/nlu/utils.js @@ -19,6 +19,17 @@ let isValidWaEnvVars = function() { process.env.WCS_WORKSPACE_LANGUAGE)); }; +/** + * check that all the WA credentials sent from the skill are valid + * @returns {string} + */ + +let isValidWaCredentials = function(credentials) { + return ((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE + && process.env.WCS_VERSION) || + (credentials.apikey && process.env.WCS_WORKSPACE_LANGUAGE && credentials.workspace_name && credentials.workspace_name && credentials.version_date && credentials.url)); +}; + /** * Sets up the handler's wcs credentials from environment variables @@ -95,6 +106,7 @@ let setupWCSCredentialsfromVCAP = function (credentials) { module.exports = { isValidWaEnvVars: isValidWaEnvVars, + isValidWaCredentials: isValidWaCredentials, setupWCSCredentialsFromEnv: setupWCSCredentialsFromEnv, setupWCSCredentialsfromVCAP: setupWCSCredentialsfromVCAP }; \ No newline at end of file From da60b6ae4c1e9c92102159658de37fce7ea5346b Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 15:12:52 +0530 Subject: [PATCH 08/11] typo fix --- lib/nlu/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nlu/utils.js b/lib/nlu/utils.js index c177178..c19be8c 100644 --- a/lib/nlu/utils.js +++ b/lib/nlu/utils.js @@ -27,7 +27,7 @@ let isValidWaEnvVars = function() { let isValidWaCredentials = function(credentials) { return ((process.env.WCS_USERNAME && process.env.WCS_URL && process.env.WCS_PASSWORD && process.env.WCS_VERSION_DATE && process.env.WCS_VERSION) || - (credentials.apikey && process.env.WCS_WORKSPACE_LANGUAGE && credentials.workspace_name && credentials.workspace_name && credentials.version_date && credentials.url)); + (credentials.apikey && process.env.WCS_WORKSPACE_LANGUAGE && credentials.workspace_name && credentials.workspace_name && credentials.url)); }; From d51c7ed9e2d35dcf33bd2a6294e99e348fa02a90 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Thu, 20 Dec 2018 17:38:41 +0530 Subject: [PATCH 09/11] typo fix --- lib/handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/handler.js b/lib/handler.js index 988712b..16e3d1d 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -34,7 +34,7 @@ let Handler = function () { Handler.prototype.initialize = function (credentials) { //check if credentials are coming from the handler initialize call and also valid from the index.js of the skill if((credentials && WaEnvUtils.isValidWaCredentials(credentials))){ - this.wcsCredentials = WaEnvUtils.setupWCSCredentialsfromVCAP(creds); + this.wcsCredentials = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); } else if(WaEnvUtils.isValidWaEnvVars()) { this.wcsCredentials = WaEnvUtils.setupWCSCredentialsFromEnv(); } else { From 2a5ea0443fdfbbc3a375e2e7a218b7a9aa2a71f2 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Fri, 21 Dec 2018 11:03:21 +0530 Subject: [PATCH 10/11] fix for WA rate limit to load workspace --- lib/handler.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/handler.js b/lib/handler.js index 16e3d1d..9d9716d 100644 --- a/lib/handler.js +++ b/lib/handler.js @@ -343,7 +343,8 @@ let setupWcs = function (handler, wcsUrl, wcsUsername, wcsPassword, versionDate, options = { version: version, iam_apikey: apiKey, - url: wcsUrl + url: wcsUrl, + export: false } } else { From 7731befc192bbd7cf27117ed1d691a3324c57564 Mon Sep 17 00:00:00 2001 From: Ashok Yalamanchili Date: Wed, 2 Jan 2019 15:49:50 +0530 Subject: [PATCH 11/11] fix for comment related to nlu type check --- lib/nlu/factory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nlu/factory.js b/lib/nlu/factory.js index 408b8e6..6f5065b 100644 --- a/lib/nlu/factory.js +++ b/lib/nlu/factory.js @@ -128,7 +128,7 @@ module.exports = Factory; function getNLU(type, name, credentials) { return new Promise(function (resolve, reject) { let nlu; - if((credentials && WaEnvUtils.isValidWaCredentials(credentials))){ + if(credentials && WaEnvUtils.isValidWaCredentials(credentials) && type === 'wcs'){ nlu = WaEnvUtils.setupWCSCredentialsfromVCAP(credentials); } else if(WaEnvUtils.isValidWaEnvVars() && type === 'wcs') {