n8n-nodes-tenable-community
Version:
n8n node for the Tenable One platform
81 lines (80 loc) • 3.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.vulnerabiltyManagementApiRequest = vulnerabiltyManagementApiRequest;
/**
* A generic API request function for the Tenable Vulnerability Management API.
* This function builds the request details based on the provided resource and
* operation, and handles authentication. It will throw an error on API failure.
*
* @param {IExecuteFunctions | ITriggerFunctions} this The n8n function context.
* @param {string} resource The API resource to target (e.g., 'assets').
* @param {string} operation The operation to perform (e.g., 'listAssets').
* @param {IDataObject} [body] The body of the request.
* @param {IDataObject} [qs] The query string for the request.
* @returns {Promise<any>} The JSON response from the API.
*/
async function vulnerabiltyManagementApiRequest(resource, operation, body = {}, qs = {}) {
const credentials = await this.getCredentials('tenableApi');
const baseUrl = 'https://cloud.tenable.com';
let method = 'GET';
let endpoint = '';
// Logic to determine endpoint and method based on resource and operation
if (resource === 'assets') {
if (operation === 'listAssets') {
endpoint = '/assets';
}
else if (operation === 'getAssetDetails') {
endpoint = `/assets/${qs.assetUuid}`;
delete qs.assetUuid; // Clean up qs
}
else if (operation === 'importAssets') {
method = 'POST';
endpoint = '/import/assets';
}
else if (operation === 'listAssetImportJobs') {
endpoint = '/import/asset-jobs';
}
else if (operation === 'getImportJobStatus') {
endpoint = `/import/asset-jobs/${qs.assetImportJobUuid}`;
delete qs.assetImportJobUuid; // Clean up qs
}
else if (operation === 'bulkUpdateAcr') {
method = 'POST';
endpoint = '/api/v2/assets/bulk-jobs/acr';
}
else if (operation === 'moveAssets') {
method = 'POST';
endpoint = '/api/v2/assets/bulk-jobs/move-to-network';
}
else if (operation === 'bulkDeleteAssets') {
method = 'POST';
endpoint = '/api/v2/assets/bulk-jobs/delete';
}
}
else if (resource === 'vulnerability') {
if (operation === 'export') {
method = 'POST';
endpoint = '/vulns/export';
}
}
const options = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-ApiKeys': `accessKey=${credentials.accessKey};secretKey=${credentials.secretKey}`,
},
method,
qs,
body,
url: `${baseUrl}${endpoint}`,
json: true,
};
if (Object.keys(body).length === 0) {
delete options.body;
}
if (Object.keys(qs).length === 0) {
delete options.qs;
}
// Let the caller handle the try/catch block to allow for custom error handling
return this.helpers.httpRequest(options);
}