Load credentials from the database (#1741)

* Changes to types so that credentials can be always loaded from DB

This first commit changes all return types from the execute functions
and calls to get credentials to be async so we can use await.

This is a first step as previously credentials were loaded in memory and
always available. We will now be loading them from the DB which requires
turning the whole call chain async.

* Fix updated files

* Removed unnecessary credential loading to improve performance

* Fix typo

*  Fix issue

* Updated new nodes to load credentials async

*  Remove not needed comment

Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Omar Ajoue
2021-08-20 18:57:30 +02:00
committed by GitHub
parent 178235e148
commit 7ce7285f7a
242 changed files with 450 additions and 481 deletions

View File

@@ -182,7 +182,11 @@ export class TheHive implements INodeType {
return returnData;
},
async loadCustomFields(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const version = this.getCredentials('theHiveApi')?.apiVersion;
const credentials = await this.getCredentials('theHiveApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'Credentials could not be obtained');
}
const version = credentials.apiVersion;
const endpoint = version === 'v1' ? '/customField' : '/list/custom_fields';
const requestResult = await theHiveApiRequest.call(
@@ -208,7 +212,7 @@ export class TheHive implements INodeType {
},
async loadObservableOptions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
// if v1 is not used we remove 'count' option
const version = this.getCredentials('theHiveApi')?.apiVersion;
const version = (await this.getCredentials('theHiveApi'))?.apiVersion;
const options = [
...(version === 'v1') ? [{ name: 'Count', value: 'count', description: 'Count observables' }] : [],
@@ -223,7 +227,7 @@ export class TheHive implements INodeType {
return options;
},
async loadObservableTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const version = this.getCredentials('theHiveApi')?.apiVersion;
const version = (await this.getCredentials('theHiveApi'))?.apiVersion;
const endpoint = version === 'v1' ? '/observable/type?range=all' : '/list/list_artifactDataType';
const dataTypes = await theHiveApiRequest.call(
@@ -267,7 +271,11 @@ export class TheHive implements INodeType {
return returnData;
},
async loadTaskOptions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const version = this.getCredentials('theHiveApi')?.apiVersion;
const credentials = await this.getCredentials('theHiveApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'Credentials could not be obtained');
}
const version = credentials.apiVersion;
const options = [
...(version === 'v1') ? [{ name: 'Count', value: 'count', description: 'Count tasks' }] : [],
{ name: 'Create', value: 'create', description: 'Create a task' },
@@ -280,7 +288,11 @@ export class TheHive implements INodeType {
return options;
},
async loadAlertOptions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const version = this.getCredentials('theHiveApi')?.apiVersion;
const credentials = await this.getCredentials('theHiveApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'Credentials could not be obtained');
}
const version = credentials.apiVersion;
const options = [
...(version === 'v1') ? [{ name: 'Count', value: 'count', description: 'Count alerts' }] : [],
{ name: 'Create', value: 'create', description: 'Create alert' },
@@ -296,7 +308,11 @@ export class TheHive implements INodeType {
return options;
},
async loadCaseOptions(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const version = this.getCredentials('theHiveApi')?.apiVersion;
const credentials = await this.getCredentials('theHiveApi');
if (credentials === undefined) {
throw new NodeOperationError(this.getNode(), 'Credentials could not be obtained');
}
const version = credentials.apiVersion;
const options = [
...(version === 'v1') ? [{ name: 'Count', value: 'count', description: 'Count a case' }] : [],
{ name: 'Create', value: 'create', description: 'Create a case' },
@@ -528,9 +544,8 @@ export class TheHive implements INodeType {
{},
);
}
if (operation === 'getAll') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -961,7 +976,7 @@ export class TheHive implements INodeType {
if (operation === 'get') {
const observableId = this.getNodeParameter('id', i) as string;
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const version = credentials.apiVersion;
@@ -1006,7 +1021,7 @@ export class TheHive implements INodeType {
}
if (operation === 'getAll') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -1079,7 +1094,7 @@ export class TheHive implements INodeType {
}
if (operation === 'search') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -1343,7 +1358,7 @@ export class TheHive implements INodeType {
if (operation === 'get') {
const caseId = this.getNodeParameter('id', i) as string;
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const version = credentials.apiVersion;
@@ -1388,7 +1403,7 @@ export class TheHive implements INodeType {
}
if (operation === 'getAll') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -1632,7 +1647,7 @@ export class TheHive implements INodeType {
if (operation === 'get') {
const taskId = this.getNodeParameter('id', i) as string;
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const version = credentials.apiVersion;
@@ -1676,7 +1691,7 @@ export class TheHive implements INodeType {
if (operation === 'getAll') {
// get all require a case id (it retursn all tasks for a specific case)
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -1751,7 +1766,7 @@ export class TheHive implements INodeType {
}
if (operation === 'search') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
@@ -1974,7 +1989,7 @@ export class TheHive implements INodeType {
if (operation === 'get') {
const logId = this.getNodeParameter('id', i) as string;
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const version = credentials.apiVersion;
@@ -2018,7 +2033,7 @@ export class TheHive implements INodeType {
}
if (operation === 'getAll') {
const credentials = this.getCredentials('theHiveApi') as IDataObject;
const credentials = await this.getCredentials('theHiveApi') as IDataObject;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;