Skip to content

Commit

Permalink
Merge pull request #416 from chughts/iamfix
Browse files Browse the repository at this point in the history
IAM fix
  • Loading branch information
chughts committed Jun 13, 2018
2 parents 60ea8bc + 3793cac commit 5cf12f9
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 61 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ Node-RED Watson Nodes for IBM Cloud
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>

### New in version 0.7.0
- Assistant, Discovery, Natural Language Understanding, Personality Insights,
- In this release STT in Stream mode with IAM Keys does not work.
- Assistant, Discovery, Language Identify, Language Translator,
Natural Language Understanding, Personality Insights,
Speech to Text, Text to Speech, Tone Analyzer nodes updated
to allow for use of iam key for authentication.
to allow for use of IAM key for authentication.
- Migrated STT node off deprecated methods.
- Fix to Tone Analyzer Node to preserve credentials on config reopen.
- Fix to Tone Analyzer to allow json objects and arrays as payload.
- Fix to STT where auto-connect was not being preserved when reopening configuration.
- Bump to 2018-03-05 version date for Discovery service.
- Move to V3 of Language Translator
- Migrated Discovery Nodes off deprecated methods.
- Remove Deprecated Retrieve and Rank Nodes

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"temp": "^0.8.3",
"qs": "6.x",
"image-type": "^2.0.2",
"watson-developer-cloud": "^3.4.5",
"watson-developer-cloud": "^3.5.0",
"kuromoji": "^0.1.1",
"word-count": "^0.2.2",
"is-docx": "^0.0.3",
Expand Down Expand Up @@ -40,9 +40,9 @@
"watson-discovery-v1": "services/discovery/v1.js",
"watson-discovery-v1-document-loader": "services/discovery/v1-document-loader.js",
"watson-discovery-v1-query-builder": "services/discovery/v1-query-builder.js",
"watson-language-translator-v2": "services/language_translator/v2.js",
"watson-language-translator-identify-v2": "services/language_translator_identify/v2.js",
"watson-language-translator-util-v2": "services/language_translator_util/v2.js",
"watson-language-translator-v3": "services/language_translator/v3.js",
"watson-language-translator-identify-v3": "services/language_translator_identify/v3.js",
"watson-language-translator-util-v3": "services/language_translator_util/v3.js",
"watson-natural-language-classifier-v1": "services/natural_language_classifier/v1.js",
"watson-natural-language-understanding-v1": "services/natural_language_understanding/v1.js",
"watson-personality-insights-v3": "services/personality_insights/v3.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-input-password" placeholder="Password"/>
</div>
<div class="form-row credentials" style="display: none;">
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey" placeholder="API Key"></input>
</div>

<div class="form-row credentials">
<label>&nbsp;</label>
Expand Down Expand Up @@ -237,6 +241,7 @@
'ru': 'Russian',
'tr': 'Turkish',
'zh': 'Chinese',
'zh-TW' : 'Taiwanese',
'zht': 'Traditional Chinese'
};

Expand Down Expand Up @@ -307,8 +312,9 @@
tor.checkModels = function () {
var u = $('#node-input-username').val();
var p = $('#node-input-password').val();
var k = $('#node-input-apikey').val();
var e = $('#node-input-service-endpoint').val();
var creds = {un: u, pwd: p};
var creds = {un: u, pwd: p, key: k};

if ($('#node-input-neural').prop('checked')) {
creds.n = 'Y';
Expand All @@ -335,8 +341,9 @@
tor.getCredentials = function () {
var u = $('#node-input-username').val();
var p = $('#node-input-password').val();
var k = $('#node-input-apikey').val();

if (u && u.length && p) {
if ( (k && k.length) || (u && u.length && p) ) {
if (!tor.models) {
tor.checkModels();
if (tor.models) {$('#node-input-action').parent().show(); }
Expand Down Expand Up @@ -366,7 +373,10 @@
$('select#node-input-destlang').empty();

available_destlang.forEach(function (val) {
var lang = val.split('-')[1];
// can now have languages like zh-TW, so simple split on -
// no longer works.
// var lang = val.split('-')[1];
let lang = val.replace(tor.srclang_selected + '-', '');

var selectedText = '';
if (tor.destlang_selected === lang) {
Expand Down Expand Up @@ -559,6 +569,7 @@
var output_lang = tor.domain_type.map(function (a) {
return a.target;
});

input_lang_unique = input_lang.filter(tor.checkUnique);
output_lang_unique = output_lang.filter(tor.checkUnique);

Expand Down Expand Up @@ -599,6 +610,12 @@
tor.checkActionSelected();
}
});
$('#node-input-apikey').change(function(val){
tor.getCredentials();
if (tor.have_credentials) {
tor.checkActionSelected();
}
});

$('#node-input-action').change(function(val){
tor.action = $('#node-input-action').val();
Expand Down Expand Up @@ -727,6 +744,7 @@
srclang: {value: 'en'},
destlang: {value: 'fr'},
password: {value: ''},
apikey: {value: ''},
custom: {value: ''},
domainhidden: {value: ''},
srclanghidden: {value: ''},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function (RED) {
// the edited ones are not being taken.
const SERVICE_IDENTIFIER = 'language-translator';
var pkg = require('../../package.json'),
LanguageTranslatorV2 = require('watson-developer-cloud/language-translator/v2'),
LanguageTranslatorV3 = require('watson-developer-cloud/language-translator/v3'),
//cfenv = require('cfenv'),
payloadutils = require('../../utilities/payload-utils'),
serviceutils = require('../../utilities/service-utils'),
Expand All @@ -34,6 +34,8 @@ module.exports = function (RED) {
password = null,
sUsername = null,
sPassword = null,
apikey = null,
sApikey = null,
//service = cfenv.getAppEnv().getServiceCreds(/language translator/i),
service = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
endpoint = '',
Expand All @@ -43,8 +45,9 @@ module.exports = function (RED) {
temp.track();

if (service) {
sUsername = service.username;
sPassword = service.password;
sUsername = service.username ? service.username : '';
sPassword = service.password ? service.password : '';
sApikey = service.apikey ? service.apikey : '';
sEndpoint = service.url;
}

Expand All @@ -64,22 +67,27 @@ module.exports = function (RED) {
var lt = null,
neural = req.query.n ? true : false,
serviceSettings = {
username: sUsername ? sUsername : req.query.un,
password: sPassword ? sPassword : req.query.pwd,
version: 'v2',
version: '2018-05-01',
url: endpoint,
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
};

if (sApikey || req.query.key) {
serviceSettings.iam_apikey = sApikey ? sApikey : req.query.key;
} else {
serviceSettings.username = sUsername ? sUsername : req.query.un;
serviceSettings.password = sPassword ? sPassword : req.query.pwd;
}

if (neural) {
serviceSettings.headers['X-Watson-Technology-Preview'] = '2017-07-01';
}

lt = new LanguageTranslatorV2(serviceSettings);
lt = new LanguageTranslatorV3(serviceSettings);

lt.getModels({}, function (err, models) {
lt.listModels({}, function (err, models) {
if (err) {
res.json(err);
}
Expand All @@ -105,8 +113,8 @@ module.exports = function (RED) {
var node = this;


function initialCheck(u, p) {
if (!u || !p) {
function initialCheck(u, p, k) {
if (!k && (!u || !p)) {
return Promise.reject('Missing Watson Language Translator service credentials');
}
return Promise.resolve();
Expand Down Expand Up @@ -188,7 +196,7 @@ module.exports = function (RED) {
}

model_id = srclang + '-' + destlang;
if (domain !== 'news') {
if (domain !== 'news' && domain !== 'general') {
model_id += ('-' + domain);
}
return Promise.resolve(model_id);
Expand Down Expand Up @@ -374,14 +382,19 @@ module.exports = function (RED) {
var p = null,
language_translator = null,
serviceSettings = {
username: username,
password: password,
version: 'v2',
version: '2018-05-01',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
};

if (apikey) {
serviceSettings.iam_apikey = apikey;
} else {
serviceSettings.username = username;
serviceSettings.password = password;
}

if (endpoint) {
serviceSettings.url = endpoint;
}
Expand All @@ -390,7 +403,7 @@ module.exports = function (RED) {
serviceSettings.headers['X-Watson-Technology-Preview'] = '2017-07-01';
}

language_translator = new LanguageTranslatorV2(serviceSettings);
language_translator = new LanguageTranslatorV3(serviceSettings);

// We have credentials, and know the mode. Further required fields checks
// are specific to the requested action.
Expand Down Expand Up @@ -434,6 +447,7 @@ module.exports = function (RED) {
// Credentials are needed for each of the modes.
username = sUsername || this.credentials.username;
password = sPassword || this.credentials.password || config.password;
apikey = sApikey || this.credentials.apikey || config.apikey;

endpoint = sEndpoint;
if ((!config['default-endpoint']) && config['service-endpoint']) {
Expand All @@ -442,7 +456,7 @@ module.exports = function (RED) {

node.status({});

initialCheck(username, password)
initialCheck(username, password, apikey)
.then(function(){
return payloadCheck(msg);
})
Expand Down Expand Up @@ -472,7 +486,8 @@ module.exports = function (RED) {
RED.nodes.registerType('watson-translator', SMTNode, {
credentials: {
username: {type:'text'},
password: {type:'password'}
password: {type:'password'},
apikey: {type:'password'}
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<label for="node-input-password"><i class="fa fa-key"></i> Password</label>
<input type="password" id="node-input-password" placeholder="Password">
</div>
<div class="form-row credentials" style="display: none;">
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey" placeholder="API Key"></input>
</div>

<div class="form-row credentials">
<label>&nbsp;</label>
Expand All @@ -46,7 +50,7 @@
</script>

<script type="text/x-red" data-help-name="watson-language-translator-identify">
<p>Packaged in with release 0.5.14 of node-red-node-watson</p>
<p>Packaged in with release 0.7.0 of node-red-node-watson</p>
<p>The Watson Language Translator service can be used to identify languages used in a text input. <p>
<p>Node input : </p>
<ul>
Expand Down Expand Up @@ -97,7 +101,8 @@
},
credentials: {
username: {type:'text'},
password: {type:'password'}
password: {type:'password'},
apikey: {type: 'password'}
},
color: 'rgb(84,149,230)',
inputs: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,28 @@
module.exports = function (RED) {
const SERVICE_IDENTIFIER = 'language-translator';
var pkg = require('../../package.json'),
LanguageTranslatorV2 = require('watson-developer-cloud/language-translator/v2'),
LanguageTranslatorV3 = require('watson-developer-cloud/language-translator/v3'),
payloadutils = require('../../utilities/payload-utils'),
serviceutils = require('../../utilities/service-utils'),
service = serviceutils.getServiceCreds(SERVICE_IDENTIFIER),
username = null,
password = null,
sUsername = null,
sPassword = null,
apikey = null,
sApikey = null,
endpoint = '', sEndpoint = '';
//endpointUrl = 'https://gateway.watsonplatform.net/language-translator/api';

if (service) {
sUsername = service.username;
sPassword = service.password;
sUsername = service.username ? service.username : '';
sPassword = service.password ? service.password : '';
sApikey = service.apikey ? service.apikey : '';
sEndpoint = service.url;
}

function initialCheck(u, p) {
if (!u || !p) {
function initialCheck(u, p, k) {
if (!k && (!u || !p)) {
return Promise.reject('Missing Watson Language Translator service credentials');
}
return Promise.resolve();
Expand All @@ -52,19 +55,24 @@ module.exports = function (RED) {
var p = new Promise(function resolver(resolve, reject){
var language_translator = null,
serviceSettings = {
username: username,
password: password,
version: 'v2',
version: '2018-05-01',
headers: {
'User-Agent': pkg.name + '-' + pkg.version
}
};

if (apikey) {
serviceSettings.iam_apikey = apikey;
} else {
serviceSettings.username = username;
serviceSettings.password = password;
}

if (endpoint) {
serviceSettings.url = endpoint;
}

language_translator = new LanguageTranslatorV2(serviceSettings);
language_translator = new LanguageTranslatorV3(serviceSettings);

language_translator.identify({text: msg.payload}, function (err, response) {
if (err) {
Expand All @@ -91,14 +99,15 @@ module.exports = function (RED) {
this.on('input', function (msg) {
username = sUsername || this.credentials.username;
password = sPassword || this.credentials.password;
apikey = sApikey || this.credentials.apikey;

endpoint = sEndpoint;
if ((!config['default-endpoint']) && config['service-endpoint']) {
endpoint = config['service-endpoint'];
}

node.status({});
initialCheck(username, password)
initialCheck(username, password, apikey)
.then(function(){
return payloadCheck(msg);
})
Expand All @@ -118,7 +127,8 @@ module.exports = function (RED) {
RED.nodes.registerType('watson-language-translator-identify', Node, {
credentials: {
username: {type:'text'},
password: {type:'password'}
password: {type:'password'},
apikey: {type:'password'}
}
});
};
Loading

0 comments on commit 5cf12f9

Please sign in to comment.