@itentialopensource/adapter-utils 5.1.1 → 5.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/lib/authenticationHandler.js +263 -0
- package/lib/brokerHandler.js +42 -17
- package/lib/genericHandler.js +87 -136
- package/lib/requestHandler.js +195 -8
- package/package.json +3 -1
- package/refs?service=git-upload-pack +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
1
|
|
|
2
|
+
## 5.1.3 [08-28-2023]
|
|
3
|
+
|
|
4
|
+
* aws auth changes - for all aws auth
|
|
5
|
+
|
|
6
|
+
See merge request itentialopensource/adapter-utils!270
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 5.1.2 [08-28-2023]
|
|
11
|
+
|
|
12
|
+
* Resolve ADAPT-2852 - Broker authentication
|
|
13
|
+
|
|
14
|
+
Closes ADAPT-2852
|
|
15
|
+
|
|
16
|
+
See merge request itentialopensource/adapter-utils!269
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
2
20
|
## 5.1.1 [08-22-2023]
|
|
3
21
|
|
|
4
22
|
* fix scrubing arrays - was not making copy
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/* @copyright Itential, LLC 2023 */
|
|
2
|
+
|
|
3
|
+
// Set globals
|
|
4
|
+
/* global log */
|
|
5
|
+
/* eslint global-require:warn */
|
|
6
|
+
/* eslint import/no-dynamic-require:warn */
|
|
7
|
+
/* eslint-disable consistent-return */
|
|
8
|
+
|
|
9
|
+
const aws4 = require('aws4');
|
|
10
|
+
const AWS = require('aws-sdk');
|
|
11
|
+
const http = require('http');
|
|
12
|
+
const querystring = require('querystring');
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* INTERNAL FUNCTION: update device broker properties from service instance config and adapter sample properties
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
class AuthenticationHandler {
|
|
19
|
+
/**
|
|
20
|
+
* Adapter Authentication Handler
|
|
21
|
+
* @constructor
|
|
22
|
+
*/
|
|
23
|
+
constructor(prongId, properties, reqH) {
|
|
24
|
+
this.myid = prongId;
|
|
25
|
+
this.allProps = properties;
|
|
26
|
+
this.requestHandlerInst = reqH;
|
|
27
|
+
|
|
28
|
+
// set up the properties I care about
|
|
29
|
+
this.refreshProperties(properties);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* refreshProperties is used to set up all of the properties for the broker handler.
|
|
34
|
+
* It allows properties to be changed later by simply calling refreshProperties rather
|
|
35
|
+
* than having to restart the broker handler.
|
|
36
|
+
*
|
|
37
|
+
* @function refreshProperties
|
|
38
|
+
* @param {Object} properties - an object containing all of the properties
|
|
39
|
+
*/
|
|
40
|
+
refreshProperties(properties) {
|
|
41
|
+
const origin = `${this.myid}-authenticationHandler-refreshProperties`;
|
|
42
|
+
log.trace(origin);
|
|
43
|
+
|
|
44
|
+
if (!properties) {
|
|
45
|
+
log.error(`${origin}: Authentication Handler received no properties!`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @summary Gets the hcma authorization for the call
|
|
51
|
+
*
|
|
52
|
+
* @function getAWSAuthorization
|
|
53
|
+
* @param {string} method - the method for the action we are setting authorization for
|
|
54
|
+
* @param {object} requestObj - an object that contains all of the possible parts of the request (payload, uriPathVars,
|
|
55
|
+
* uriQuery, uriOptions and addlHeaders (optional). Can be a stringified Object.
|
|
56
|
+
* @param {string} uriPath - the path for the call. (required)
|
|
57
|
+
* @param {string} service - the AWS service we are talking to
|
|
58
|
+
* @param {object} [stsParams] - STS Parameters to use for authentication.
|
|
59
|
+
* @param {string} [roleName] - RoleName to authenticate against
|
|
60
|
+
*
|
|
61
|
+
* @return {Object} the headers to add to the request
|
|
62
|
+
*/
|
|
63
|
+
getAWSAuthorization(method, requestObj, uriPath, service, STSParams, roleName, callback) {
|
|
64
|
+
const origin = `${this.id}-adapter-getAuthorization`;
|
|
65
|
+
log.trace(origin);
|
|
66
|
+
|
|
67
|
+
// Form path
|
|
68
|
+
let uriPathTranslated = '';
|
|
69
|
+
if (requestObj.uriQuery && uriPath.indexOf('?') === -1) {
|
|
70
|
+
const query = requestObj.uriQuery;
|
|
71
|
+
uriPathTranslated = `${uriPath}?${querystring.stringify(query)}`;
|
|
72
|
+
} else if (requestObj.uriQuery && uriPath.endsWith('?')) {
|
|
73
|
+
const query = requestObj.uriQuery;
|
|
74
|
+
uriPathTranslated = `${uriPath}${querystring.stringify(query)}`;
|
|
75
|
+
} else {
|
|
76
|
+
uriPathTranslated = uriPath;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// set up the options for the AWS signature
|
|
80
|
+
const options = {
|
|
81
|
+
host: this.allProps.host,
|
|
82
|
+
method,
|
|
83
|
+
path: uriPathTranslated.replace(/\/\/+/g, '/'),
|
|
84
|
+
service,
|
|
85
|
+
region: this.allProps.region
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// add any provided headers for the call
|
|
89
|
+
if (requestObj.addlHeaders) {
|
|
90
|
+
options.headers = requestObj.addlHeaders;
|
|
91
|
+
}
|
|
92
|
+
// remove ? if there is no query and the last character is ?
|
|
93
|
+
if (options.path.endsWith('?')) {
|
|
94
|
+
options.path = options.path.substring(0, options.path.length - 1);
|
|
95
|
+
}
|
|
96
|
+
// If there is a body (POST, PATCH, PUT) add the body to the call
|
|
97
|
+
if (method !== 'GET' && method !== 'DELETE') {
|
|
98
|
+
if (typeof requestObj.payload === 'string') {
|
|
99
|
+
options.body = requestObj.payload;
|
|
100
|
+
} else {
|
|
101
|
+
options.body = JSON.stringify(requestObj.payload);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// override anything set in callProperties
|
|
105
|
+
if (requestObj.callProperties) {
|
|
106
|
+
if (requestObj.callProperties.host) {
|
|
107
|
+
options.host = requestObj.callProperties.host;
|
|
108
|
+
}
|
|
109
|
+
if (requestObj.callProperties.region) {
|
|
110
|
+
options.region = requestObj.callProperties.region;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
log.debug(`SIG OPTIONS: ${JSON.stringify(options)}`);
|
|
114
|
+
|
|
115
|
+
/* STS AUTHENTICATION */
|
|
116
|
+
if (STSParams) {
|
|
117
|
+
log.info('Using STS for AWS Authentication');
|
|
118
|
+
|
|
119
|
+
// set the original AWS access information (from properties)
|
|
120
|
+
AWS.config.update({
|
|
121
|
+
sessionToken: this.allProps.authentication.aws_session_token,
|
|
122
|
+
accessKeyId: this.allProps.authentication.aws_access_key,
|
|
123
|
+
secretAccessKey: this.allProps.authentication.aws_secret_key,
|
|
124
|
+
region: this.allProps.region
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// use STS to get the AWS access information for the user defined in STWS Params
|
|
128
|
+
const sts = new AWS.STS();
|
|
129
|
+
const stsData = {
|
|
130
|
+
RoleArn: STSParams.RoleArn,
|
|
131
|
+
RoleSessionName: STSParams.RoleSessionName,
|
|
132
|
+
DurationSeconds: 3600
|
|
133
|
+
};
|
|
134
|
+
return sts.assumeRole(stsData, (err, data) => {
|
|
135
|
+
if (err) {
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
// extract the user specific info from the response
|
|
139
|
+
const accessKeyId = data.Credentials.AccessKeyId;
|
|
140
|
+
const secretAccessKey = data.Credentials.SecretAccessKey;
|
|
141
|
+
const sessionToken = data.Credentials.SessionToken;
|
|
142
|
+
|
|
143
|
+
// call the signature with the user specific information
|
|
144
|
+
const authOpts = aws4.sign(options, { accessKeyId, secretAccessKey, sessionToken });
|
|
145
|
+
if (sessionToken) {
|
|
146
|
+
authOpts.headers['X-Amz-Security-Token'] = sessionToken;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// return the headers
|
|
150
|
+
return callback(authOpts.headers);
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/* ADAPTER PROPERTIES AUTHENTICATION */
|
|
155
|
+
if (!roleName && !this.allProps.authentication.aws_iam_role) {
|
|
156
|
+
log.info('Using Adapter PROPERTIES for AWS Authentication');
|
|
157
|
+
|
|
158
|
+
// call the signature with the property information
|
|
159
|
+
const authOpts = aws4.sign(options, { accessKeyId: this.allProps.authentication.aws_access_key, secretAccessKey: this.allProps.authentication.aws_secret_key, sessionToken: this.allProps.authentication.aws_session_token });
|
|
160
|
+
if (this.allProps.authentication.aws_session_token) {
|
|
161
|
+
authOpts.headers['X-Amz-Security-Token'] = this.allProps.authentication.aws_session_token;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// return the headers
|
|
165
|
+
return callback(authOpts.headers);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* ROLE NAME AUTHENTICATION */
|
|
169
|
+
log.info('Using roleName for AWS Authentication');
|
|
170
|
+
|
|
171
|
+
// set up information for token - this is always ec2!
|
|
172
|
+
const getTokenOptions = {
|
|
173
|
+
method: 'PUT',
|
|
174
|
+
url: 'http://169.254.169.254/latest/api/token',
|
|
175
|
+
headers: {
|
|
176
|
+
'X-aws-ec2-metadata-token-ttl-seconds': 21600
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// get token from AWS internal server
|
|
181
|
+
const req1 = http.request(getTokenOptions, (res1) => {
|
|
182
|
+
let response = '';
|
|
183
|
+
|
|
184
|
+
// HERE??? - can we check status here or does this need to be in end???
|
|
185
|
+
if (res1.statusCode < 200 || res1.statusCode >= 300) {
|
|
186
|
+
return callback(null, new Error(`Invalid status code recieved: ${res1.statusCode}`));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// add data to the response
|
|
190
|
+
res1.on('data', (resp) => {
|
|
191
|
+
response += resp;
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// process everything when the call finishes
|
|
195
|
+
res1.on('end', () => {
|
|
196
|
+
if (!response) {
|
|
197
|
+
return callback(null, 'No authentication token');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// get token and set up to make IAM role call - this is always ec2!
|
|
201
|
+
const tokenKey = response;
|
|
202
|
+
let myRole = roleName;
|
|
203
|
+
if (!roleName) {
|
|
204
|
+
myRole = this.allProps.authentication.aws_iam_role;
|
|
205
|
+
}
|
|
206
|
+
const toptions = {
|
|
207
|
+
method: 'GET',
|
|
208
|
+
hostname: '169.254.169.254',
|
|
209
|
+
path: `/latest/meta-data/iam/security-credentials/${myRole}`,
|
|
210
|
+
headers: {
|
|
211
|
+
'X-aws-ec2-metadata-token': tokenKey
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// IAM Role call
|
|
216
|
+
const req2 = http.request(toptions, (res2) => {
|
|
217
|
+
let response2 = '';
|
|
218
|
+
|
|
219
|
+
// HERE??? - can we check status here or does this need to be in end???
|
|
220
|
+
if (res2.statusCode < 200 || res2.statusCode >= 300) {
|
|
221
|
+
return callback(null, new Error(`Invalid status code recieved: ${res2.statusCode}`));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// add data to the response
|
|
225
|
+
res2.on('data', (chunk) => {
|
|
226
|
+
response2 += chunk;
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// process everything when the call finishes
|
|
230
|
+
res2.on('end', () => {
|
|
231
|
+
if (!response2) {
|
|
232
|
+
return callback(null, 'No authentication token');
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
// get role keys from response so we can sign the request
|
|
236
|
+
const awsResponse = JSON.parse(response2);
|
|
237
|
+
const accessKeyId = awsResponse.AccessKeyId;
|
|
238
|
+
const secretAccessKey = awsResponse.SecretAccessKey;
|
|
239
|
+
const sessionToken = awsResponse.Token;
|
|
240
|
+
|
|
241
|
+
// sign the request
|
|
242
|
+
const authOpts = aws4.sign(options, { accessKeyId, secretAccessKey, sessionToken });
|
|
243
|
+
if (sessionToken) {
|
|
244
|
+
authOpts.headers['X-Amz-Security-Token'] = sessionToken;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// return the headers
|
|
248
|
+
return callback(authOpts.headers);
|
|
249
|
+
} catch (e) {
|
|
250
|
+
return callback(null, e);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
req2.on('error', (err2) => callback(null, err2));
|
|
255
|
+
req2.end();
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
req1.on('error', (err1) => callback(null, err1));
|
|
259
|
+
req1.end();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
module.exports = AuthenticationHandler;
|
package/lib/brokerHandler.js
CHANGED
|
@@ -102,13 +102,13 @@ class BrokerHandler {
|
|
|
102
102
|
* @param {Callback} callback - A map where the entity is the key and the
|
|
103
103
|
* value is true or false
|
|
104
104
|
*/
|
|
105
|
-
hasEntities(entityType, entityList, callback) {
|
|
105
|
+
hasEntities(entityType, entityList, callOptions, callback) {
|
|
106
106
|
const origin = `${this.myid}-brokerHandler-hasEntities`;
|
|
107
107
|
log.trace(origin);
|
|
108
108
|
|
|
109
109
|
switch (entityType) {
|
|
110
110
|
case 'Device':
|
|
111
|
-
return this.hasDevices(entityList, callback);
|
|
111
|
+
return this.hasDevices(entityList, callOptions, callback);
|
|
112
112
|
default:
|
|
113
113
|
return callback(null, `${this.myid} does not support entity ${entityType}`);
|
|
114
114
|
}
|
|
@@ -121,7 +121,7 @@ class BrokerHandler {
|
|
|
121
121
|
* @param {Callback} callback - A map where the device is the key and the
|
|
122
122
|
* value is true or false
|
|
123
123
|
*/
|
|
124
|
-
hasDevices(deviceList, callback) {
|
|
124
|
+
hasDevices(deviceList, callOptions, callback) {
|
|
125
125
|
const origin = `${this.myid}-brokerHandler-hasDevices`;
|
|
126
126
|
log.trace(origin);
|
|
127
127
|
|
|
@@ -170,7 +170,7 @@ class BrokerHandler {
|
|
|
170
170
|
return map;
|
|
171
171
|
}, {});
|
|
172
172
|
const apiCalls = deviceList.map((device) => new Promise((resolve) => {
|
|
173
|
-
this.getDevice(device, (result, error) => {
|
|
173
|
+
this.getDevice(device, callOptions, (result, error) => {
|
|
174
174
|
if (error) {
|
|
175
175
|
log.debug(`In map error: ${JSON.stringify(device)}`);
|
|
176
176
|
return resolve({ name: device, found: false });
|
|
@@ -201,7 +201,7 @@ class BrokerHandler {
|
|
|
201
201
|
* @param {getCallback} callback - a callback function to return the result
|
|
202
202
|
* (appliance) or the error
|
|
203
203
|
*/
|
|
204
|
-
getDevice(deviceName, callback) {
|
|
204
|
+
getDevice(deviceName, callOptions, callback) {
|
|
205
205
|
const meth = 'brokerHandler-getDevice';
|
|
206
206
|
const origin = `${this.myid}-${meth}`;
|
|
207
207
|
log.trace(origin);
|
|
@@ -229,7 +229,7 @@ class BrokerHandler {
|
|
|
229
229
|
}
|
|
230
230
|
};
|
|
231
231
|
|
|
232
|
-
return this.getDevicesFiltered(opts, (devs, ferr) => {
|
|
232
|
+
return this.getDevicesFiltered(opts, callOptions, (devs, ferr) => {
|
|
233
233
|
// if we received an error or their is no response on the results return an error
|
|
234
234
|
if (ferr) {
|
|
235
235
|
return callback(null, ferr);
|
|
@@ -243,9 +243,14 @@ class BrokerHandler {
|
|
|
243
243
|
const callPromises = [];
|
|
244
244
|
for (let i = 0; i < this.allProps.devicebroker.getDevice.length; i += 1) {
|
|
245
245
|
// Perform component calls here.
|
|
246
|
+
const callProps = this.allProps.devicebroker.getDevice[i];
|
|
247
|
+
callProps.headers = {
|
|
248
|
+
...(callProps.headers || {}),
|
|
249
|
+
...(callOptions.headers || {})
|
|
250
|
+
};
|
|
246
251
|
callPromises.push(
|
|
247
252
|
new Promise((resolve, reject) => {
|
|
248
|
-
this.requestHandlerInst.iapMakeGenericCall('getDevice',
|
|
253
|
+
this.requestHandlerInst.iapMakeGenericCall(callOptions, 'getDevice', callProps, [devs.list[0]], [deviceName], (callRet, callErr) => {
|
|
249
254
|
// return an error
|
|
250
255
|
if (callErr) {
|
|
251
256
|
reject(callErr);
|
|
@@ -288,7 +293,7 @@ class BrokerHandler {
|
|
|
288
293
|
* @param {getCallback} callback - a callback function to return the result
|
|
289
294
|
* (appliances) or the error
|
|
290
295
|
*/
|
|
291
|
-
getDevicesFiltered(options, callback) {
|
|
296
|
+
getDevicesFiltered(options, callOptions, callback) {
|
|
292
297
|
const meth = 'brokerHandler-getDevicesFiltered';
|
|
293
298
|
const origin = `${this.myid}-${meth}`;
|
|
294
299
|
log.trace(origin);
|
|
@@ -382,9 +387,14 @@ class BrokerHandler {
|
|
|
382
387
|
const callPromises = [];
|
|
383
388
|
for (let i = 0; i < this.allProps.devicebroker.getDevicesFiltered.length; i += 1) {
|
|
384
389
|
// Perform component calls here.
|
|
390
|
+
const callProps = this.allProps.devicebroker.getDevicesFiltered[i];
|
|
391
|
+
callProps.headers = {
|
|
392
|
+
...(callProps.headers || {}),
|
|
393
|
+
...(callOptions.headers || {})
|
|
394
|
+
};
|
|
385
395
|
callPromises.push(
|
|
386
396
|
new Promise((resolve, reject) => {
|
|
387
|
-
this.requestHandlerInst.iapMakeGenericCall('getDevicesFiltered',
|
|
397
|
+
this.requestHandlerInst.iapMakeGenericCall(callOptions, 'getDevicesFiltered', callProps, [{ fake: 'fakedata' }], filterName, (callRet, callErr) => {
|
|
388
398
|
// return an error
|
|
389
399
|
if (callErr) {
|
|
390
400
|
console.debug(`in cache iap call failed with error ${callErr}`);
|
|
@@ -456,7 +466,7 @@ class BrokerHandler {
|
|
|
456
466
|
* @param {configCallback} callback - callback function to return the result
|
|
457
467
|
* (appliance isAlive) or the error
|
|
458
468
|
*/
|
|
459
|
-
isAlive(deviceName, callback) {
|
|
469
|
+
isAlive(deviceName, callOptions, callback) {
|
|
460
470
|
const meth = 'brokerHandler-isAlive';
|
|
461
471
|
const origin = `${this.myid}-${meth}`;
|
|
462
472
|
log.trace(origin);
|
|
@@ -483,7 +493,7 @@ class BrokerHandler {
|
|
|
483
493
|
name: deviceName
|
|
484
494
|
}
|
|
485
495
|
};
|
|
486
|
-
return this.getDevicesFiltered(opts, (devs, ferr) => {
|
|
496
|
+
return this.getDevicesFiltered(opts, callOptions, (devs, ferr) => {
|
|
487
497
|
// if we received an error or their is no response on the results return an error
|
|
488
498
|
if (ferr) {
|
|
489
499
|
return callback(null, ferr);
|
|
@@ -497,9 +507,14 @@ class BrokerHandler {
|
|
|
497
507
|
const callPromises = [];
|
|
498
508
|
for (let i = 0; i < this.allProps.devicebroker.isAlive.length; i += 1) {
|
|
499
509
|
// Perform component calls here.
|
|
510
|
+
const callProps = this.allProps.devicebroker.isAlive[i];
|
|
511
|
+
callProps.headers = {
|
|
512
|
+
...(callProps.headers || {}),
|
|
513
|
+
...(callOptions.headers || {})
|
|
514
|
+
};
|
|
500
515
|
callPromises.push(
|
|
501
516
|
new Promise((resolve, reject) => {
|
|
502
|
-
this.requestHandlerInst.iapMakeGenericCall('isAlive',
|
|
517
|
+
this.requestHandlerInst.iapMakeGenericCall(callOptions, 'isAlive', callProps, [devs.list[0]], null, (callRet, callErr) => {
|
|
503
518
|
// return an error
|
|
504
519
|
if (callErr) {
|
|
505
520
|
reject(callErr);
|
|
@@ -547,7 +562,7 @@ class BrokerHandler {
|
|
|
547
562
|
* @param {configCallback} callback - callback function to return the result
|
|
548
563
|
* (appliance config) or the error
|
|
549
564
|
*/
|
|
550
|
-
getConfig(deviceName, format, callback) {
|
|
565
|
+
getConfig(deviceName, format, callOptions, callback) {
|
|
551
566
|
const meth = 'brokerHandler-getConfig';
|
|
552
567
|
const origin = `${this.myid}-${meth}`;
|
|
553
568
|
log.trace(origin);
|
|
@@ -574,7 +589,7 @@ class BrokerHandler {
|
|
|
574
589
|
name: deviceName
|
|
575
590
|
}
|
|
576
591
|
};
|
|
577
|
-
return this.getDevicesFiltered(opts, (devs, ferr) => {
|
|
592
|
+
return this.getDevicesFiltered(opts, callOptions, (devs, ferr) => {
|
|
578
593
|
// if we received an error or their is no response on the results return an error
|
|
579
594
|
if (ferr) {
|
|
580
595
|
return callback(null, ferr);
|
|
@@ -588,9 +603,14 @@ class BrokerHandler {
|
|
|
588
603
|
const callPromises = [];
|
|
589
604
|
for (let i = 0; i < this.allProps.devicebroker.getConfig.length; i += 1) {
|
|
590
605
|
// Perform component calls here.
|
|
606
|
+
const callProps = this.allProps.devicebroker.getConfig[i];
|
|
607
|
+
callProps.headers = {
|
|
608
|
+
...(callProps.headers || {}),
|
|
609
|
+
...(callOptions.headers || {})
|
|
610
|
+
};
|
|
591
611
|
callPromises.push(
|
|
592
612
|
new Promise((resolve, reject) => {
|
|
593
|
-
this.requestHandlerInst.iapMakeGenericCall('getConfig',
|
|
613
|
+
this.requestHandlerInst.iapMakeGenericCall(callOptions, 'getConfig', callProps, [devs.list[0]], null, (callRet, callErr) => {
|
|
594
614
|
// return an error
|
|
595
615
|
if (callErr) {
|
|
596
616
|
reject(callErr);
|
|
@@ -642,7 +662,7 @@ class BrokerHandler {
|
|
|
642
662
|
* @param {getCallback} callback - callback function to return the result
|
|
643
663
|
* (count) or the error
|
|
644
664
|
*/
|
|
645
|
-
iapGetDeviceCount(callback) {
|
|
665
|
+
iapGetDeviceCount(callOptions, callback) {
|
|
646
666
|
const meth = 'brokerHandler-iapGetDeviceCount';
|
|
647
667
|
const origin = `${this.myid}-${meth}`;
|
|
648
668
|
log.trace(origin);
|
|
@@ -690,9 +710,14 @@ class BrokerHandler {
|
|
|
690
710
|
const callPromises = [];
|
|
691
711
|
for (let i = 0; i < this.allProps.devicebroker.getCount.length; i += 1) {
|
|
692
712
|
// Perform component calls here.
|
|
713
|
+
const callProps = this.allProps.devicebroker.getCount[i];
|
|
714
|
+
callProps.headers = {
|
|
715
|
+
...(callProps.headers || {}),
|
|
716
|
+
...(callOptions.headers || {})
|
|
717
|
+
};
|
|
693
718
|
callPromises.push(
|
|
694
719
|
new Promise((resolve, reject) => {
|
|
695
|
-
this.requestHandlerInst.iapMakeGenericCall('getCount',
|
|
720
|
+
this.requestHandlerInst.iapMakeGenericCall(callOptions, 'getCount', callProps, [{ fake: 'fakedata' }], null, (callRet, callErr) => {
|
|
696
721
|
// return an error
|
|
697
722
|
if (callErr) {
|
|
698
723
|
reject(callErr);
|
package/lib/genericHandler.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
// Set globals
|
|
4
4
|
/* global log */
|
|
5
|
+
/* eslint consistent-return: warn */
|
|
5
6
|
|
|
6
7
|
/* NodeJS internal utilities */
|
|
7
8
|
|
|
@@ -40,18 +41,13 @@ class GenericHandler {
|
|
|
40
41
|
* Makes the requested generic call
|
|
41
42
|
*
|
|
42
43
|
* @function expandedGenericAdapterRequest
|
|
43
|
-
* @param {
|
|
44
|
-
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
47
|
-
* @param {
|
|
48
|
-
*
|
|
49
|
-
* @param {
|
|
50
|
-
* Can be a stringified Object.
|
|
51
|
-
* @param {Object} requestBody - the body to add to the request (optional).
|
|
52
|
-
* Can be a stringified Object.
|
|
53
|
-
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
54
|
-
* Can be a stringified Object.
|
|
44
|
+
* @param {object} metadata - metadata for the call (optional).
|
|
45
|
+
* @param {string} uriPath - the path of the api call - do not include the host, port, base path or version (optional)
|
|
46
|
+
* @param {string} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (optional)
|
|
47
|
+
* @param {object} pathVars - the parameters to be put within the url path (optional).
|
|
48
|
+
* @param {object} queryData - the parameters to be put on the url (optional).
|
|
49
|
+
* @param {object} requestBody - the body to add to the request (optional).
|
|
50
|
+
* @param {object} addlHeaders - additional headers to be put on the call (optional).
|
|
55
51
|
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
56
52
|
* or the error
|
|
57
53
|
*/
|
|
@@ -60,34 +56,6 @@ class GenericHandler {
|
|
|
60
56
|
const origin = `${this.myid}-${meth}`;
|
|
61
57
|
log.trace(origin);
|
|
62
58
|
|
|
63
|
-
// if metadata says not to use BasePath
|
|
64
|
-
if (metadata && metadata.basepath && metadata.basepath.toUpperCase() === 'NOBASE') {
|
|
65
|
-
return this.genericAdapterRequestNoBasePath(uriPath, restMethod, queryData, requestBody, addlHeaders, callback);
|
|
66
|
-
}
|
|
67
|
-
// use BasePath
|
|
68
|
-
return this.genericAdapterRequest(uriPath, restMethod, queryData, requestBody, addlHeaders, callback);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Makes the requested generic call
|
|
73
|
-
*
|
|
74
|
-
* @function genericAdapterRequest
|
|
75
|
-
* @param {String} uriPath - the path of the api call - do not include the host, port, base path or version (required)
|
|
76
|
-
* @param {String} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
|
|
77
|
-
* @param {Object} queryData - the parameters to be put on the url (optional).
|
|
78
|
-
* Can be a stringified Object.
|
|
79
|
-
* @param {Object} requestBody - the body to add to the request (optional).
|
|
80
|
-
* Can be a stringified Object.
|
|
81
|
-
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
82
|
-
* Can be a stringified Object.
|
|
83
|
-
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
84
|
-
* or the error
|
|
85
|
-
*/
|
|
86
|
-
genericAdapterRequest(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
|
|
87
|
-
const meth = 'genericHandler-genericAdapterRequest';
|
|
88
|
-
const origin = `${this.myid}-${meth}`;
|
|
89
|
-
log.trace(origin);
|
|
90
|
-
|
|
91
59
|
/* HERE IS WHERE YOU VALIDATE DATA */
|
|
92
60
|
if (uriPath === undefined || uriPath === null || uriPath === '') {
|
|
93
61
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Data', ['uriPath'], null, null, null);
|
|
@@ -100,13 +68,23 @@ class GenericHandler {
|
|
|
100
68
|
return callback(null, errorObj);
|
|
101
69
|
}
|
|
102
70
|
|
|
71
|
+
// Need to add dynamic/runtime path values here
|
|
72
|
+
if (pathVars) {
|
|
73
|
+
const pathKeys = Object.keys(pathVars);
|
|
74
|
+
|
|
75
|
+
// replace each key found in the path with its value
|
|
76
|
+
for (let i = 0; i < pathKeys.length; i += 1) {
|
|
77
|
+
uriPath.replace(`{${pathKeys[i]}}`, pathVars[pathKeys[i]]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
103
81
|
/* HERE IS WHERE YOU SET THE DATA TO PASS INTO REQUEST */
|
|
104
82
|
// remove any leading / and split the uripath into path variables
|
|
105
83
|
let myPath = uriPath;
|
|
106
84
|
while (myPath.indexOf('/') === 0) {
|
|
107
85
|
myPath = myPath.substring(1);
|
|
108
86
|
}
|
|
109
|
-
const
|
|
87
|
+
const pathComp = myPath.split('/');
|
|
110
88
|
const queryParamsAvailable = queryData;
|
|
111
89
|
const queryParams = {};
|
|
112
90
|
const bodyVars = requestBody;
|
|
@@ -122,7 +100,7 @@ class GenericHandler {
|
|
|
122
100
|
// set up the request object - payload, uriPathVars, uriQuery, uriOptions, addlHeaders
|
|
123
101
|
const reqObj = {
|
|
124
102
|
payload: bodyVars,
|
|
125
|
-
uriPathVars:
|
|
103
|
+
uriPathVars: pathComp,
|
|
126
104
|
uriQuery: queryParams,
|
|
127
105
|
uriOptions: {}
|
|
128
106
|
};
|
|
@@ -130,6 +108,9 @@ class GenericHandler {
|
|
|
130
108
|
if (addlHeaders) {
|
|
131
109
|
reqObj.addlHeaders = addlHeaders;
|
|
132
110
|
}
|
|
111
|
+
if (metadata && metadata.datatype) {
|
|
112
|
+
reqObj.datatype = metadata.datatype;
|
|
113
|
+
}
|
|
133
114
|
|
|
134
115
|
// determine the call and return flag
|
|
135
116
|
let action = 'getGenerics';
|
|
@@ -145,9 +126,51 @@ class GenericHandler {
|
|
|
145
126
|
returnF = false;
|
|
146
127
|
}
|
|
147
128
|
|
|
129
|
+
// Change the call and return flag if no base path
|
|
130
|
+
if (metadata && metadata.basepath && metadata.basepath.toUpperCase() === 'NOBASE') {
|
|
131
|
+
action = 'getGenericsNoBase';
|
|
132
|
+
returnF = true;
|
|
133
|
+
if (restMethod.toUpperCase() === 'POST') {
|
|
134
|
+
action = 'createGenericNoBase';
|
|
135
|
+
} else if (restMethod.toUpperCase() === 'PUT') {
|
|
136
|
+
action = 'updateGenericNoBase';
|
|
137
|
+
} else if (restMethod.toUpperCase() === 'PATCH') {
|
|
138
|
+
action = 'patchGenericNoBase';
|
|
139
|
+
} else if (restMethod.toUpperCase() === 'DELETE') {
|
|
140
|
+
action = 'deleteGenericNoBase';
|
|
141
|
+
returnF = false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
148
145
|
try {
|
|
149
146
|
// Make the call -
|
|
150
147
|
// identifyRequest(entity, action, requestObj, returnDataFlag, callback)
|
|
148
|
+
// See if AWS call requiring authentication
|
|
149
|
+
if (metadata && metadata.service) {
|
|
150
|
+
return this.requestHandlerInst.getAWSAuthorization(restMethod, reqObj, uriPath, metadata.service, null, null, (signature, authError) => {
|
|
151
|
+
if (authError) {
|
|
152
|
+
return callback(null, authError);
|
|
153
|
+
}
|
|
154
|
+
reqObj.addlHeaders = { ...reqObj.addlHeaders, ...signature };
|
|
155
|
+
this.requestHandlerInst.identifyRequest('.generic', action, reqObj, returnF, (irReturnData, irReturnError) => {
|
|
156
|
+
// if we received an error or their is no response on the results
|
|
157
|
+
// return an error
|
|
158
|
+
if (irReturnError) {
|
|
159
|
+
/* HERE IS WHERE YOU CAN ALTER THE ERROR MESSAGE */
|
|
160
|
+
return callback(null, irReturnError);
|
|
161
|
+
}
|
|
162
|
+
if (!Object.hasOwnProperty.call(irReturnData, 'response')) {
|
|
163
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Invalid Response', ['genericAdapterRequest'], null, null, null);
|
|
164
|
+
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
165
|
+
return callback(null, errorObj);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/* HERE IS WHERE YOU CAN ALTER THE RETURN DATA */
|
|
169
|
+
// return the response
|
|
170
|
+
return callback(irReturnData, null);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
}
|
|
151
174
|
return this.requestHandlerInst.identifyRequest('.generic', action, reqObj, returnF, (irReturnData, irReturnError) => {
|
|
152
175
|
// if we received an error or their is no response on the results
|
|
153
176
|
// return an error
|
|
@@ -172,108 +195,36 @@ class GenericHandler {
|
|
|
172
195
|
}
|
|
173
196
|
}
|
|
174
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Makes the requested generic call
|
|
200
|
+
*
|
|
201
|
+
* @function genericAdapterRequest
|
|
202
|
+
* @param {string} uriPath - the path of the api call - do not include the host, port, base path or version (required)
|
|
203
|
+
* @param {string} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
|
|
204
|
+
* @param {object} queryData - the parameters to be put on the url (optional).
|
|
205
|
+
* @param {object} requestBody - the body to add to the request (optional).
|
|
206
|
+
* @param {object} addlHeaders - additional headers to be put on the call (optional).
|
|
207
|
+
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
208
|
+
* or the error
|
|
209
|
+
*/
|
|
210
|
+
genericAdapterRequest(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
|
|
211
|
+
return this.expandedGenericAdapterRequest(null, uriPath, restMethod, null, queryData, requestBody, addlHeaders, callback);
|
|
212
|
+
}
|
|
213
|
+
|
|
175
214
|
/**
|
|
176
215
|
* Makes the requested generic call with no base path or version
|
|
177
216
|
*
|
|
178
217
|
* @function genericAdapterRequestNoBasePath
|
|
179
|
-
* @param {
|
|
180
|
-
* @param {
|
|
181
|
-
* @param {
|
|
182
|
-
*
|
|
183
|
-
* @param {
|
|
184
|
-
* Can be a stringified Object.
|
|
185
|
-
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
186
|
-
* Can be a stringified Object.
|
|
218
|
+
* @param {string} uriPath - the path of the api call - do not include the host, port, base path or version (required)
|
|
219
|
+
* @param {string} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
|
|
220
|
+
* @param {object} queryData - the parameters to be put on the url (optional).
|
|
221
|
+
* @param {object} requestBody - the body to add to the request (optional).
|
|
222
|
+
* @param {object} addlHeaders - additional headers to be put on the call (optional).
|
|
187
223
|
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
188
224
|
* or the error
|
|
189
225
|
*/
|
|
190
226
|
genericAdapterRequestNoBasePath(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
|
|
191
|
-
|
|
192
|
-
const origin = `${this.myid}-${meth}`;
|
|
193
|
-
log.trace(origin);
|
|
194
|
-
|
|
195
|
-
/* HERE IS WHERE YOU VALIDATE DATA */
|
|
196
|
-
if (uriPath === undefined || uriPath === null || uriPath === '') {
|
|
197
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Data', ['uriPath'], null, null, null);
|
|
198
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
199
|
-
return callback(null, errorObj);
|
|
200
|
-
}
|
|
201
|
-
if (restMethod === undefined || restMethod === null || restMethod === '') {
|
|
202
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Missing Data', ['restMethod'], null, null, null);
|
|
203
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
204
|
-
return callback(null, errorObj);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/* HERE IS WHERE YOU SET THE DATA TO PASS INTO REQUEST */
|
|
208
|
-
// remove any leading / and split the uripath into path variables
|
|
209
|
-
let myPath = uriPath;
|
|
210
|
-
while (myPath.indexOf('/') === 0) {
|
|
211
|
-
myPath = myPath.substring(1);
|
|
212
|
-
}
|
|
213
|
-
const pathVars = myPath.split('/');
|
|
214
|
-
const queryParamsAvailable = queryData;
|
|
215
|
-
const queryParams = {};
|
|
216
|
-
const bodyVars = requestBody;
|
|
217
|
-
|
|
218
|
-
// loop in template. long callback arg name to avoid identifier conflicts
|
|
219
|
-
Object.keys(queryParamsAvailable).forEach((thisKeyInQueryParamsAvailable) => {
|
|
220
|
-
if (queryParamsAvailable[thisKeyInQueryParamsAvailable] !== undefined && queryParamsAvailable[thisKeyInQueryParamsAvailable] !== null
|
|
221
|
-
&& queryParamsAvailable[thisKeyInQueryParamsAvailable] !== '') {
|
|
222
|
-
queryParams[thisKeyInQueryParamsAvailable] = queryParamsAvailable[thisKeyInQueryParamsAvailable];
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
// set up the request object - payload, uriPathVars, uriQuery, uriOptions, addlHeaders
|
|
227
|
-
const reqObj = {
|
|
228
|
-
payload: bodyVars,
|
|
229
|
-
uriPathVars: pathVars,
|
|
230
|
-
uriQuery: queryParams,
|
|
231
|
-
uriOptions: {}
|
|
232
|
-
};
|
|
233
|
-
// add headers if provided
|
|
234
|
-
if (addlHeaders) {
|
|
235
|
-
reqObj.addlHeaders = addlHeaders;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// determine the call and return flag
|
|
239
|
-
let action = 'getGenericsNoBase';
|
|
240
|
-
let returnF = true;
|
|
241
|
-
if (restMethod.toUpperCase() === 'POST') {
|
|
242
|
-
action = 'createGenericNoBase';
|
|
243
|
-
} else if (restMethod.toUpperCase() === 'PUT') {
|
|
244
|
-
action = 'updateGenericNoBase';
|
|
245
|
-
} else if (restMethod.toUpperCase() === 'PATCH') {
|
|
246
|
-
action = 'patchGenericNoBase';
|
|
247
|
-
} else if (restMethod.toUpperCase() === 'DELETE') {
|
|
248
|
-
action = 'deleteGenericNoBase';
|
|
249
|
-
returnF = false;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
try {
|
|
253
|
-
// Make the call -
|
|
254
|
-
// identifyRequest(entity, action, requestObj, returnDataFlag, callback)
|
|
255
|
-
return this.requestHandlerInst.identifyRequest('.generic', action, reqObj, returnF, (irReturnData, irReturnError) => {
|
|
256
|
-
// if we received an error or their is no response on the results
|
|
257
|
-
// return an error
|
|
258
|
-
if (irReturnError) {
|
|
259
|
-
/* HERE IS WHERE YOU CAN ALTER THE ERROR MESSAGE */
|
|
260
|
-
return callback(null, irReturnError);
|
|
261
|
-
}
|
|
262
|
-
if (!Object.hasOwnProperty.call(irReturnData, 'response')) {
|
|
263
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Invalid Response', ['genericAdapterRequestNoBasePath'], null, null, null);
|
|
264
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
265
|
-
return callback(null, errorObj);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/* HERE IS WHERE YOU CAN ALTER THE RETURN DATA */
|
|
269
|
-
// return the response
|
|
270
|
-
return callback(irReturnData, null);
|
|
271
|
-
});
|
|
272
|
-
} catch (ex) {
|
|
273
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.myid, meth, 'Caught Exception', null, null, null, ex);
|
|
274
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
275
|
-
return callback(null, errorObj);
|
|
276
|
-
}
|
|
227
|
+
return this.expandedGenericAdapterRequest({ basepath: 'NOBASE' }, uriPath, restMethod, null, queryData, requestBody, addlHeaders, callback);
|
|
277
228
|
}
|
|
278
229
|
}
|
|
279
230
|
|
package/lib/requestHandler.js
CHANGED
|
@@ -25,6 +25,7 @@ const ConnectorCl = require(path.join(__dirname, '/connectorRest.js'));
|
|
|
25
25
|
const PropUtilCl = require(path.join(__dirname, '/propertyUtil.js'));
|
|
26
26
|
const TransUtilCl = require(path.join(__dirname, '/translatorUtil.js'));
|
|
27
27
|
const DBUtilCl = require(path.join(__dirname, '/dbUtil.js'));
|
|
28
|
+
const AuthHandlerCl = require(path.join(__dirname, '/authenticationHandler.js'));
|
|
28
29
|
|
|
29
30
|
let id = null;
|
|
30
31
|
const allowFailover = 'AD.300';
|
|
@@ -388,6 +389,7 @@ class RequestHandler {
|
|
|
388
389
|
this.brokerHandler = new BrokerHandlerCl(this.myid, this.props, this.directory, this);
|
|
389
390
|
this.genericHandler = new GenericHandlerCl(this.myid, this.props, this);
|
|
390
391
|
this.cacheHandler = new CacheHandlerCl(this.myid, this.props, this.directory, this);
|
|
392
|
+
this.authHandler = new AuthHandlerCl(this.myid, this.props, this);
|
|
391
393
|
} catch (e) {
|
|
392
394
|
// handle any exception
|
|
393
395
|
const origin = `${this.myid}-requestHandler-constructor`;
|
|
@@ -529,6 +531,7 @@ class RequestHandler {
|
|
|
529
531
|
* @summary Make the provided call(s) - could be one of many
|
|
530
532
|
*
|
|
531
533
|
* @function iapMakeGenericCall
|
|
534
|
+
* @param {object} metadata - metadata for the call (optional).
|
|
532
535
|
* @param {string} callName - the name of the call (required)
|
|
533
536
|
* @param {object} callProps - the proeprties for the broker call (required)
|
|
534
537
|
* @param {object} devResp - the device details to extract needed inputs (required)
|
|
@@ -536,7 +539,7 @@ class RequestHandler {
|
|
|
536
539
|
*
|
|
537
540
|
* @param {getCallback} callback - a callback function to return the result of the call
|
|
538
541
|
*/
|
|
539
|
-
iapMakeGenericCall(callName, callProps, devResp, filterName, callback) {
|
|
542
|
+
iapMakeGenericCall(metadata, callName, callProps, devResp, filterName, callback) {
|
|
540
543
|
const meth = 'requestHandler-iapMakeGenericCall';
|
|
541
544
|
const origin = `${this.myid}-${meth}`;
|
|
542
545
|
log.trace(`${origin}: ${callName}`);
|
|
@@ -651,7 +654,7 @@ class RequestHandler {
|
|
|
651
654
|
|
|
652
655
|
// !! using Generic makes it easier on the Adapter Builder (just need to change the path)
|
|
653
656
|
// !! you can also replace with a specific call if that is easier
|
|
654
|
-
return this.
|
|
657
|
+
return this.expandedGenericAdapterRequest(metadata, uriPath, uriMethod, null, callQuery, callBody, callHeaders, (result, error) => {
|
|
655
658
|
// if we received an error or their is no response on the results return an error
|
|
656
659
|
if (error) {
|
|
657
660
|
if (handleFail === 'fail') {
|
|
@@ -848,6 +851,23 @@ class RequestHandler {
|
|
|
848
851
|
|
|
849
852
|
// Determine the Protocol so the appropriate handler can be called
|
|
850
853
|
if (entitySchema.protocol.toUpperCase() === 'REST') {
|
|
854
|
+
// override the data types on the request
|
|
855
|
+
if (requestObj && requestObj.datatype) {
|
|
856
|
+
// must have a legitimate request data type
|
|
857
|
+
if (requestObj.datatype.request && (requestObj.datatype.request.toUpperCase() === 'JSON'
|
|
858
|
+
|| requestObj.datatype.request.toUpperCase() === 'XML' || requestObj.datatype.request.toUpperCase() === 'URLENCODE'
|
|
859
|
+
|| requestObj.datatype.request.toUpperCase() === 'URLQUERY' || requestObj.datatype.request.toUpperCase() === 'FORM'
|
|
860
|
+
|| requestObj.datatype.request.toUpperCase() === 'JSON2XML' || requestObj.datatype.request.toUpperCase() === 'PLAIN')) {
|
|
861
|
+
entitySchema.requestDatatype = requestObj.datatype.request;
|
|
862
|
+
}
|
|
863
|
+
// must have a legitimate response data type
|
|
864
|
+
if (requestObj.datatype.response && (requestObj.datatype.response.toUpperCase() === 'JSON'
|
|
865
|
+
|| requestObj.datatype.response.toUpperCase() === 'XML' || requestObj.datatype.response.toUpperCase() === 'URLENCODE'
|
|
866
|
+
|| requestObj.datatype.response.toUpperCase() === 'XML2JSON' || requestObj.datatype.response.toUpperCase() === 'PLAIN')) {
|
|
867
|
+
entitySchema.responseDatatype = requestObj.datatype.response;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
851
871
|
return this.restHandler.genericRestRequest(entity, action, entitySchema, requestObj, translate, (result, error) => {
|
|
852
872
|
const overallDiff = process.hrtime(overallTime);
|
|
853
873
|
const overallEnd = `${Math.round(((overallDiff[0] * NS_PER_SEC) + overallDiff[1]) / 1000000)}ms`;
|
|
@@ -1537,7 +1557,30 @@ class RequestHandler {
|
|
|
1537
1557
|
log.trace(origin);
|
|
1538
1558
|
|
|
1539
1559
|
try {
|
|
1540
|
-
return this.brokerHandler.hasEntities(entityType, entityList, callback);
|
|
1560
|
+
return this.brokerHandler.hasEntities(entityType, entityList, {}, callback);
|
|
1561
|
+
} catch (e) {
|
|
1562
|
+
// handle any exception
|
|
1563
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker hasEntities Failed');
|
|
1564
|
+
return callback(null, errorObj);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
/**
|
|
1569
|
+
* @summary Determines if this adapter supports any in a list of entities with custom auth
|
|
1570
|
+
*
|
|
1571
|
+
* @function hasEntitiesAuth
|
|
1572
|
+
* @param {String} entityType - the entity type to check for
|
|
1573
|
+
* @param {Array} entityList - the list of entities we are looking for
|
|
1574
|
+
*
|
|
1575
|
+
* @param {Callback} callback - A map where the entity is the key and the
|
|
1576
|
+
* value is true or false
|
|
1577
|
+
*/
|
|
1578
|
+
hasEntitiesAuth(entityType, entityList, callOptions, callback) {
|
|
1579
|
+
const origin = `${this.myid}-requestHandler-hasEntitiesAuth`;
|
|
1580
|
+
log.trace(origin);
|
|
1581
|
+
|
|
1582
|
+
try {
|
|
1583
|
+
return this.brokerHandler.hasEntities(entityType, entityList, callOptions, callback);
|
|
1541
1584
|
} catch (e) {
|
|
1542
1585
|
// handle any exception
|
|
1543
1586
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker hasEntities Failed');
|
|
@@ -1559,7 +1602,29 @@ class RequestHandler {
|
|
|
1559
1602
|
log.trace(origin);
|
|
1560
1603
|
|
|
1561
1604
|
try {
|
|
1562
|
-
return this.brokerHandler.getDevice(deviceName, callback);
|
|
1605
|
+
return this.brokerHandler.getDevice(deviceName, {}, callback);
|
|
1606
|
+
} catch (e) {
|
|
1607
|
+
// handle any exception
|
|
1608
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getDevice Failed');
|
|
1609
|
+
return callback(null, errorObj);
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/**
|
|
1614
|
+
* @summary Get Appliance that match the deviceName with custom auth
|
|
1615
|
+
*
|
|
1616
|
+
* @function getDeviceAuth
|
|
1617
|
+
* @param {String} deviceName - the deviceName to find (required)
|
|
1618
|
+
*
|
|
1619
|
+
* @param {getCallback} callback - a callback function to return the result
|
|
1620
|
+
* (appliance) or the error
|
|
1621
|
+
*/
|
|
1622
|
+
getDeviceAuth(deviceName, callOptions, callback) {
|
|
1623
|
+
const origin = `${this.myid}-requestHandler-getDeviceAuth`;
|
|
1624
|
+
log.trace(origin);
|
|
1625
|
+
|
|
1626
|
+
try {
|
|
1627
|
+
return this.brokerHandler.getDevice(deviceName, callOptions, callback);
|
|
1563
1628
|
} catch (e) {
|
|
1564
1629
|
// handle any exception
|
|
1565
1630
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getDevice Failed');
|
|
@@ -1579,8 +1644,31 @@ class RequestHandler {
|
|
|
1579
1644
|
getDevicesFiltered(options, callback) {
|
|
1580
1645
|
const origin = `${this.myid}-requestHandler-getDevicesFiltered`;
|
|
1581
1646
|
log.trace(origin);
|
|
1647
|
+
|
|
1648
|
+
try {
|
|
1649
|
+
return this.brokerHandler.getDevicesFiltered(options, {}, callback);
|
|
1650
|
+
} catch (e) {
|
|
1651
|
+
// handle any exception
|
|
1652
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getDevicesFiltered Failed');
|
|
1653
|
+
return callback(null, errorObj);
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
/**
|
|
1658
|
+
* @summary Get Appliances that match the filter with custom auth
|
|
1659
|
+
*
|
|
1660
|
+
* @function getDevicesFilteredAuth
|
|
1661
|
+
* @param {Object} options - the data to use to filter the appliances (optional)
|
|
1662
|
+
*
|
|
1663
|
+
* @param {getCallback} callback - a callback function to return the result
|
|
1664
|
+
* (appliances) or the error
|
|
1665
|
+
*/
|
|
1666
|
+
getDevicesFilteredAuth(options, callOptions, callback) {
|
|
1667
|
+
const origin = `${this.myid}-requestHandler-getDevicesFiltered`;
|
|
1668
|
+
log.trace(origin);
|
|
1669
|
+
|
|
1582
1670
|
try {
|
|
1583
|
-
return this.brokerHandler.getDevicesFiltered(options, callback);
|
|
1671
|
+
return this.brokerHandler.getDevicesFiltered(options, callOptions, callback);
|
|
1584
1672
|
} catch (e) {
|
|
1585
1673
|
// handle any exception
|
|
1586
1674
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getDevicesFiltered Failed');
|
|
@@ -1602,7 +1690,29 @@ class RequestHandler {
|
|
|
1602
1690
|
log.trace(origin);
|
|
1603
1691
|
|
|
1604
1692
|
try {
|
|
1605
|
-
return this.brokerHandler.isAlive(deviceName, callback);
|
|
1693
|
+
return this.brokerHandler.isAlive(deviceName, {}, callback);
|
|
1694
|
+
} catch (e) {
|
|
1695
|
+
// handle any exception
|
|
1696
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker isAlive Failed');
|
|
1697
|
+
return callback(null, errorObj);
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
* @summary Gets the status for the provided appliance with custom auth
|
|
1703
|
+
*
|
|
1704
|
+
* @function isAliveAuth
|
|
1705
|
+
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1706
|
+
*
|
|
1707
|
+
* @param {configCallback} callback - callback function to return the result
|
|
1708
|
+
* (appliance isAlive) or the error
|
|
1709
|
+
*/
|
|
1710
|
+
isAliveAuth(deviceName, callOptions, callback) {
|
|
1711
|
+
const origin = `${this.myid}-requestHandler-isAliveAuth`;
|
|
1712
|
+
log.trace(origin);
|
|
1713
|
+
|
|
1714
|
+
try {
|
|
1715
|
+
return this.brokerHandler.isAlive(deviceName, callOptions, callback);
|
|
1606
1716
|
} catch (e) {
|
|
1607
1717
|
// handle any exception
|
|
1608
1718
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker isAlive Failed');
|
|
@@ -1625,7 +1735,30 @@ class RequestHandler {
|
|
|
1625
1735
|
log.trace(origin);
|
|
1626
1736
|
|
|
1627
1737
|
try {
|
|
1628
|
-
return this.brokerHandler.getConfig(deviceName, format, callback);
|
|
1738
|
+
return this.brokerHandler.getConfig(deviceName, format, {}, callback);
|
|
1739
|
+
} catch (e) {
|
|
1740
|
+
// handle any exception
|
|
1741
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getConfig Failed');
|
|
1742
|
+
return callback(null, errorObj);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
/**
|
|
1747
|
+
* @summary Gets a config for the provided Appliance with custom auth
|
|
1748
|
+
*
|
|
1749
|
+
* @function getConfigAuth
|
|
1750
|
+
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1751
|
+
* @param {String} format - the desired format of the config. (optional)
|
|
1752
|
+
*
|
|
1753
|
+
* @param {configCallback} callback - callback function to return the result
|
|
1754
|
+
* (appliance config) or the error
|
|
1755
|
+
*/
|
|
1756
|
+
getConfigAuth(deviceName, format, callOptions, callback) {
|
|
1757
|
+
const origin = `${this.myid}-requestHandler-getConfigAuth`;
|
|
1758
|
+
log.trace(origin);
|
|
1759
|
+
|
|
1760
|
+
try {
|
|
1761
|
+
return this.brokerHandler.getConfig(deviceName, format, callOptions, callback);
|
|
1629
1762
|
} catch (e) {
|
|
1630
1763
|
// handle any exception
|
|
1631
1764
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker getConfig Failed');
|
|
@@ -1646,7 +1779,28 @@ class RequestHandler {
|
|
|
1646
1779
|
log.trace(origin);
|
|
1647
1780
|
|
|
1648
1781
|
try {
|
|
1649
|
-
return this.brokerHandler.iapGetDeviceCount(callback);
|
|
1782
|
+
return this.brokerHandler.iapGetDeviceCount({}, callback);
|
|
1783
|
+
} catch (e) {
|
|
1784
|
+
// handle any exception
|
|
1785
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker iapGetDeviceCount Failed');
|
|
1786
|
+
return callback(null, errorObj);
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
/**
|
|
1791
|
+
* @summary Gets the device count from the system with custom auth
|
|
1792
|
+
*
|
|
1793
|
+
* @function iapGetDeviceCount
|
|
1794
|
+
*
|
|
1795
|
+
* @param {getCallback} callback - callback function to return the result
|
|
1796
|
+
* (count) or the error
|
|
1797
|
+
*/
|
|
1798
|
+
iapGetDeviceCountAuth(callOptions, callback) {
|
|
1799
|
+
const origin = `${this.myid}-requestHandler-iapGetDeviceCountAuth`;
|
|
1800
|
+
log.trace(origin);
|
|
1801
|
+
|
|
1802
|
+
try {
|
|
1803
|
+
return this.brokerHandler.iapGetDeviceCount(callOptions, callback);
|
|
1650
1804
|
} catch (e) {
|
|
1651
1805
|
// handle any exception
|
|
1652
1806
|
const errorObj = this.transUtil.checkAndReturn(e, origin, 'Broker iapGetDeviceCount Failed');
|
|
@@ -1746,6 +1900,39 @@ class RequestHandler {
|
|
|
1746
1900
|
return callback(null, errorObj);
|
|
1747
1901
|
}
|
|
1748
1902
|
}
|
|
1903
|
+
|
|
1904
|
+
/* ********************************************** */
|
|
1905
|
+
/* */
|
|
1906
|
+
/* EXPOSES AUTH HANDLER */
|
|
1907
|
+
/* */
|
|
1908
|
+
/* ********************************************** */
|
|
1909
|
+
|
|
1910
|
+
/**
|
|
1911
|
+
* @summary Gets the hcma authorization for the call
|
|
1912
|
+
*
|
|
1913
|
+
* @function getAWSAuthorization
|
|
1914
|
+
* @param {string} method - the method for the action we are setting authorization for
|
|
1915
|
+
* @param {object} requestObj - an object that contains all of the possible parts of the request (payload, uriPathVars,
|
|
1916
|
+
* uriQuery, uriOptions and addlHeaders (optional). Can be a stringified Object.
|
|
1917
|
+
* @param {string} uriPath - the path for the call. (required)
|
|
1918
|
+
* @param {string} service - the AWS service we are talking to
|
|
1919
|
+
* @param {object} [stsParams] - STS Parameters to use for authentication.
|
|
1920
|
+
* @param {string} [roleName] - RoleName to authenticate against
|
|
1921
|
+
*
|
|
1922
|
+
* @return {Object} the headers to add to the request
|
|
1923
|
+
*/
|
|
1924
|
+
getAWSAuthorization(method, requestObj, uriPath, service, STSParams, roleName, callback) {
|
|
1925
|
+
const origin = `${this.myid}-requestHandler-getAWSAuthentication`;
|
|
1926
|
+
log.trace(origin);
|
|
1927
|
+
|
|
1928
|
+
try {
|
|
1929
|
+
return this.authHandler.getAWSAuthorization(method, requestObj, uriPath, null, null, service, callback);
|
|
1930
|
+
} catch (e) {
|
|
1931
|
+
// handle any exception
|
|
1932
|
+
const errorObj = this.transUtil.checkAndReturn(e, origin, 'AWS Authentication Request Failed');
|
|
1933
|
+
return callback(null, errorObj);
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1749
1936
|
}
|
|
1750
1937
|
|
|
1751
1938
|
module.exports = RequestHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itentialopensource/adapter-utils",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.3",
|
|
4
4
|
"description": "Itential Adapter Utility Libraries",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"postinstall": "node utils/setup.js",
|
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"ajv": "^8.12.0",
|
|
29
29
|
"async-lock": "^1.4.0",
|
|
30
|
+
"aws-sdk": "^2.1363.0",
|
|
31
|
+
"aws4": "^1.9.1",
|
|
30
32
|
"cookie": "^0.5.0",
|
|
31
33
|
"crypto-js": "^4.1.1",
|
|
32
34
|
"ejs": "^3.1.9",
|
|
Binary file
|