2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IRequestOptions,
|
|
|
|
IHttpRequestMethods,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2022-08-01 13:47:55 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { Connector, ElasticSecurityApiCredentials } from './types';
|
2021-09-22 08:48:50 -07:00
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
export function tolerateTrailingSlash(baseUrl: string) {
|
|
|
|
return baseUrl.endsWith('/') ? baseUrl.substr(0, baseUrl.length - 1) : baseUrl;
|
|
|
|
}
|
|
|
|
|
2021-09-22 08:48:50 -07:00
|
|
|
export async function elasticSecurityApiRequest(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-09-22 08:48:50 -07:00
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2024-08-23 10:19:05 -07:00
|
|
|
const { baseUrl: rawBaseUrl } = (await this.getCredentials(
|
|
|
|
'elasticSecurityApi',
|
|
|
|
)) as ElasticSecurityApiCredentials;
|
2021-09-22 08:48:50 -07:00
|
|
|
|
|
|
|
const baseUrl = tolerateTrailingSlash(rawBaseUrl);
|
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-09-22 08:48:50 -07:00
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `${baseUrl}/api${endpoint}`,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-08-23 10:19:05 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'elasticSecurityApi', options);
|
2021-09-22 08:48:50 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error?.error?.error === 'Not Acceptable' && error?.error?.message) {
|
|
|
|
error.error.error = `${error.error.error}: ${error.error.message}`;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-09-22 08:48:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function elasticSecurityApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-09-22 08:48:50 -07:00
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-11-08 06:28:21 -08:00
|
|
|
let _page = 1;
|
2021-09-22 08:48:50 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
2022-12-02 06:25:21 -08:00
|
|
|
let responseData: any;
|
2021-09-22 08:48:50 -07:00
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as 'case' | 'caseComment';
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await elasticSecurityApiRequest.call(this, method, endpoint, body, qs);
|
2022-11-08 06:28:21 -08:00
|
|
|
_page++;
|
2021-09-22 08:48:50 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const items = resource === 'case' ? responseData.cases : responseData;
|
2021-09-22 08:48:50 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push(...(items as IDataObject[]));
|
2021-09-22 08:48:50 -07:00
|
|
|
} while (returnData.length < responseData.total);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handleListing(
|
|
|
|
this: IExecuteFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2021-09-22 08:48:50 -07:00
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
2021-09-22 08:48:50 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await elasticSecurityApiRequestAllItems.call(this, method, endpoint, body, qs);
|
2021-09-22 08:48:50 -07:00
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const responseData = await elasticSecurityApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
method,
|
|
|
|
endpoint,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', 0);
|
2021-09-22 08:48:50 -07:00
|
|
|
|
|
|
|
return responseData.slice(0, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a connector name and type from a connector ID.
|
|
|
|
*
|
|
|
|
* https://www.elastic.co/guide/en/kibana/master/get-connector-api.html
|
|
|
|
*/
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getConnector(this: IExecuteFunctions, connectorId: string) {
|
2021-09-22 08:48:50 -07:00
|
|
|
const endpoint = `/actions/connector/${connectorId}`;
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
connector_type_id: type,
|
2022-08-01 13:47:55 -07:00
|
|
|
} = (await elasticSecurityApiRequest.call(this, 'GET', endpoint)) as Connector;
|
2021-09-22 08:48:50 -07:00
|
|
|
|
|
|
|
return { id, name, type };
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export function throwOnEmptyUpdate(this: IExecuteFunctions, resource: string) {
|
2021-09-22 08:48:50 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Please enter at least one field to update for the ${resource}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function getVersion(this: IExecuteFunctions, endpoint: string) {
|
|
|
|
const { version } = (await elasticSecurityApiRequest.call(this, 'GET', endpoint)) as {
|
2021-09-22 08:48:50 -07:00
|
|
|
version?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!version) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'Cannot retrieve version for resource');
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|