2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { jsonParse } from 'n8n-workflow';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2021-07-14 10:18:46 -07:00
|
|
|
import { Eq } from './QueryFunctions';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function theHiveApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2023-02-27 19:39:43 -08:00
|
|
|
body: IDataObject = {},
|
2022-08-17 08:50:24 -07:00
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
2023-02-27 19:39:43 -08:00
|
|
|
) {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('theHiveApi');
|
2020-12-02 02:24:25 -08:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${credentials.url}/api${resource}`,
|
2020-12-02 02:24:25 -08:00
|
|
|
body,
|
2022-12-02 12:54:28 -08:00
|
|
|
rejectUnauthorized: !credentials.allowUnauthorizedCerts,
|
2020-12-02 02:24:25 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
2020-12-02 02:54:10 -08:00
|
|
|
options = Object.assign({}, options, option);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:54:10 -08:00
|
|
|
if (Object.keys(body).length === 0) {
|
2020-12-02 02:24:25 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:54:10 -08:00
|
|
|
if (Object.keys(query).length === 0) {
|
2020-12-02 02:24:25 -08:00
|
|
|
delete options.qs;
|
|
|
|
}
|
2022-11-22 08:57:17 -08:00
|
|
|
return this.helpers.requestWithAuthentication.call(this, 'theHiveApi', options);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers functions
|
|
|
|
export function mapResource(resource: string): string {
|
|
|
|
switch (resource) {
|
|
|
|
case 'alert':
|
|
|
|
return 'alert';
|
|
|
|
case 'case':
|
|
|
|
return 'case';
|
|
|
|
case 'observable':
|
|
|
|
return 'case_artifact';
|
|
|
|
case 'task':
|
|
|
|
return 'case_task';
|
|
|
|
case 'log':
|
|
|
|
return 'case_task_log';
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitTags(tags: string): string[] {
|
2022-08-17 08:50:24 -07:00
|
|
|
return tags.split(',').filter((tag) => tag !== ' ' && tag);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function prepareOptional(optionals: IDataObject): IDataObject {
|
|
|
|
const response: IDataObject = {};
|
|
|
|
for (const key in optionals) {
|
2020-12-02 02:54:10 -08:00
|
|
|
if (optionals[key] !== undefined && optionals[key] !== null && optionals[key] !== '') {
|
2021-07-14 10:18:46 -07:00
|
|
|
if (['customFieldsJson', 'customFieldsUi'].indexOf(key) > -1) {
|
|
|
|
continue; // Ignore customFields, they need special treatment
|
2022-08-17 08:50:24 -07:00
|
|
|
} else if (moment(optionals[key] as string, moment.ISO_8601).isValid()) {
|
2020-12-02 02:24:25 -08:00
|
|
|
response[key] = Date.parse(optionals[key] as string);
|
|
|
|
} else if (key === 'artifacts') {
|
2022-10-21 09:46:31 -07:00
|
|
|
try {
|
2022-10-21 11:52:43 -07:00
|
|
|
response[key] = jsonParse(optionals[key] as string);
|
2022-10-21 09:46:31 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Invalid JSON for artifacts');
|
|
|
|
}
|
2020-12-02 02:24:25 -08:00
|
|
|
} else if (key === 'tags') {
|
|
|
|
response[key] = splitTags(optionals[key] as string);
|
|
|
|
} else {
|
|
|
|
response[key] = optionals[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function prepareCustomFields(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
additionalFields: IDataObject,
|
|
|
|
jsonParameters = false,
|
|
|
|
): Promise<IDataObject | undefined> {
|
2021-07-14 10:18:46 -07:00
|
|
|
// Check if the additionalFields object contains customFields
|
2022-12-02 12:54:28 -08:00
|
|
|
if (jsonParameters) {
|
2022-10-21 09:46:31 -07:00
|
|
|
let customFieldsJson = additionalFields.customFieldsJson;
|
2021-07-14 10:18:46 -07:00
|
|
|
// Delete from additionalFields as some operations (e.g. alert:update) do not run prepareOptional
|
|
|
|
// which would remove the extra fields
|
|
|
|
delete additionalFields.customFieldsJson;
|
|
|
|
|
|
|
|
if (typeof customFieldsJson === 'string') {
|
2022-10-21 09:46:31 -07:00
|
|
|
try {
|
2022-10-21 11:52:43 -07:00
|
|
|
customFieldsJson = jsonParse(customFieldsJson);
|
2022-10-21 09:46:31 -07:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error('Invalid JSON for customFields');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof customFieldsJson === 'object') {
|
|
|
|
const customFields = Object.keys(customFieldsJson as IDataObject).reduce((acc, curr) => {
|
|
|
|
acc[`customFields.${curr}`] = (customFieldsJson as IDataObject)[curr];
|
|
|
|
return acc;
|
|
|
|
}, {} as IDataObject);
|
|
|
|
|
|
|
|
return customFields;
|
2021-07-14 10:18:46 -07:00
|
|
|
} else if (customFieldsJson) {
|
|
|
|
throw Error('customFieldsJson value is invalid');
|
|
|
|
}
|
|
|
|
} else if (additionalFields.customFieldsUi) {
|
|
|
|
// Get Custom Field Types from TheHive
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('theHiveApi');
|
|
|
|
const version = credentials.apiVersion;
|
2021-07-14 10:18:46 -07:00
|
|
|
const endpoint = version === 'v1' ? '/customField' : '/list/custom_fields';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const requestResult = await theHiveApiRequest.call(this, 'GET', endpoint as string);
|
2021-07-14 10:18:46 -07:00
|
|
|
|
|
|
|
// Convert TheHive3 response to the same format as TheHive 4
|
|
|
|
// [{name, reference, type}]
|
2022-08-17 08:50:24 -07:00
|
|
|
const hiveCustomFields =
|
|
|
|
version === 'v1'
|
|
|
|
? requestResult
|
2023-02-27 19:39:43 -08:00
|
|
|
: Object.keys(requestResult as IDataObject).map((key) => requestResult[key]);
|
2021-07-14 10:18:46 -07:00
|
|
|
// Build reference to type mapping object
|
2022-08-17 08:50:24 -07:00
|
|
|
const referenceTypeMapping = hiveCustomFields.reduce(
|
|
|
|
(acc: IDataObject, curr: IDataObject) => ((acc[curr.reference as string] = curr.type), acc),
|
|
|
|
{},
|
|
|
|
);
|
2021-07-14 10:18:46 -07:00
|
|
|
|
|
|
|
// Build "fieldName": {"type": "value"} objects
|
2022-08-17 08:50:24 -07:00
|
|
|
const customFieldsUi = additionalFields.customFieldsUi as IDataObject;
|
|
|
|
const customFields: IDataObject = (customFieldsUi?.customFields as IDataObject[]).reduce(
|
|
|
|
(acc: IDataObject, curr: IDataObject) => {
|
|
|
|
const fieldName = curr.field as string;
|
2021-07-14 10:18:46 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
// Might be able to do some type conversions here if needed, TODO
|
2021-07-14 10:18:46 -07:00
|
|
|
|
2022-10-21 09:46:31 -07:00
|
|
|
const updatedField = `customFields.${fieldName}.${[referenceTypeMapping[fieldName]]}`;
|
|
|
|
acc[updatedField] = curr.value;
|
2022-08-17 08:50:24 -07:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{} as IDataObject,
|
|
|
|
);
|
2021-07-14 10:18:46 -07:00
|
|
|
|
|
|
|
delete additionalFields.customFieldsUi;
|
|
|
|
return customFields;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildCustomFieldSearch(customFields: IDataObject): IDataObject[] {
|
|
|
|
const searchQueries: IDataObject[] = [];
|
|
|
|
|
2022-10-21 09:46:31 -07:00
|
|
|
Object.keys(customFields).forEach((customFieldName) => {
|
|
|
|
searchQueries.push(Eq(customFieldName, customFields[customFieldName]));
|
2021-07-14 10:18:46 -07:00
|
|
|
});
|
|
|
|
return searchQueries;
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:24:25 -08:00
|
|
|
export function prepareSortQuery(sort: string, body: { query: [IDataObject] }) {
|
|
|
|
if (sort) {
|
|
|
|
const field = sort.substring(1);
|
|
|
|
const value = sort.charAt(0) === '+' ? 'asc' : 'desc';
|
|
|
|
const sortOption: IDataObject = {};
|
|
|
|
sortOption[field] = value;
|
2022-08-17 08:50:24 -07:00
|
|
|
body.query.push({
|
|
|
|
_name: 'sort',
|
|
|
|
_fields: [sortOption],
|
|
|
|
});
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
export function prepareRangeQuery(range: string, body: { query: IDataObject[] }) {
|
2020-12-02 02:24:25 -08:00
|
|
|
if (range && range !== 'all') {
|
2022-12-02 12:54:28 -08:00
|
|
|
body.query.push({
|
2022-08-17 08:50:24 -07:00
|
|
|
_name: 'page',
|
|
|
|
from: parseInt(range.split('-')[0], 10),
|
|
|
|
to: parseInt(range.split('-')[1], 10),
|
|
|
|
});
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
}
|