n8n-nodes-tenable-community
Version:
n8n node for the Tenable One platform
72 lines (71 loc) • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TenableASMClient = void 0;
/**
* @file Client for interacting with the Tenable Attack Surface Management (ASM) API.
* Handles authentication and provides methods for all ASM-related operations.
*/
const n8n_workflow_1 = require("n8n-workflow");
/**
* @class TenableASMClient
* @description Implements the client for the Tenable Attack Surface Management (ASM) API.
*/
class TenableASMClient {
/**
* Creates an instance of the TenableASMClient.
* @param {ITriggerFunctions | IExecuteFunctions} context The n8n context object.
*/
constructor(context) {
this.context = context;
}
/**
* Sends a request to the Tenable ASM API.
* @private
* @param {IHttpRequestOptions} options The request options.
* @returns {Promise<unknown>} A promise that resolves to the API response.
* @throws {NodeApiError} If the API call fails.
*/
async sendRequest(options) {
var _a, _b, _c;
const credentials = await this.context.getCredentials('tenableOneApi');
const requestOptions = Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { 'X-ApiKeys': `accessKey=${credentials.accessKey};secretKey=${credentials.secretKey}` }), json: true, url: `https://asm.tenable.com/v1/${options.url}`, timeout: 60000 });
try {
return await this.context.helpers.httpRequest(requestOptions);
}
catch (error) {
if (error instanceof n8n_workflow_1.NodeApiError) {
const errorDetails = error.cause ? (_a = error.cause.response) === null || _a === void 0 ? void 0 : _a.body : { message: error.message };
const errorMessage = ((_c = (_b = errorDetails === null || errorDetails === void 0 ? void 0 : errorDetails.errors) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.message) || (errorDetails === null || errorDetails === void 0 ? void 0 : errorDetails.message) || 'No additional error details provided.';
throw new n8n_workflow_1.NodeApiError(this.context.getNode(), { message: `Tenable API Error: ${errorMessage}` });
}
throw new n8n_workflow_1.NodeApiError(this.context.getNode(), { message: error.message });
}
}
/**
* Retrieves a list of assets from Tenable.asm, handling pagination automatically.
* @param {object} options The options for the request.
* @param {object} options.body The request body for filtering and pagination.
* @returns {Promise<ITenableAsset[]>} A promise that resolves to the list of all assets.
*/
async listAssets(options) {
const allAssets = [];
let hasMore = true;
let page = 1;
const limit = 200;
while (hasMore) {
const response = await this.sendRequest({ method: 'POST', url: 'assets', body: Object.assign(Object.assign({}, options.body), { page, limit }) });
if (response.assets && Array.isArray(response.assets)) {
allAssets.push(...response.assets);
if (response.assets.length < limit) {
hasMore = false;
}
}
else {
hasMore = false;
}
page++;
}
return allAssets;
}
}
exports.TenableASMClient = TenableASMClient;