From c1c2b9890ad7beeec98805f9db896f79589b3fcf Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Mon, 28 Oct 2024 12:31:36 +0000 Subject: [PATCH 1/6] Added Zabbix node credentials --- .../credentials/ZabbixApi.credentials.ts | 78 +++++++++++++++++++ .../nodes-base/credentials/icons/Zabbix.svg | 1 + packages/nodes-base/package.json | 1 + 3 files changed, 80 insertions(+) create mode 100644 packages/nodes-base/credentials/ZabbixApi.credentials.ts create mode 100644 packages/nodes-base/credentials/icons/Zabbix.svg diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts new file mode 100644 index 0000000000000..e11ec89c8e70f --- /dev/null +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -0,0 +1,78 @@ +import type { + Icon, + ICredentialDataDecryptedObject, + ICredentialTestRequest, + ICredentialType, + IHttpRequestOptions, + INodeProperties, +} from 'n8n-workflow'; + +export class ZabbixApi implements ICredentialType { + name = 'zabbixApi'; + + displayName = 'Zabbix API'; + + documentationUrl = 'zabbix'; + + icon: Icon = 'file:icons/Zabbix.svg'; + + httpRequestNode = { + name: 'Zabbix', + docsUrl: 'zabbix', + apiBaseUrlPlaceholder: 'https://zabbix.digital-boss.dev/', + }; + + properties: INodeProperties[] = [ + { + displayName: 'URL', + name: 'url', + type: 'string', + default: '', + description: 'The base url', + }, + { + displayName: 'Username', + name: 'username', + type: 'string', + default: '', + }, + { + displayName: 'Password', + name: 'password', + type: 'string', + typeOptions: { + password: true, + }, + default: '', + }, + { + displayName: 'Testing Mode', + name: 'testingMode', + type: 'boolean', + default: false, + }, + ]; + + async authenticate( + credentials: ICredentialDataDecryptedObject, + requestOptions: IHttpRequestOptions, + ): Promise { + requestOptions.headers = { + 'Api-Username': credentials.username, + 'Api-Password': credentials.password, + }; + if (requestOptions.method === 'GET') { + delete requestOptions.body; + } + + return requestOptions; + } + + test: ICredentialTestRequest = { + request: { + baseURL: '={{$credentials.url}}', + url: '/', + method: 'GET', + }, + }; +} diff --git a/packages/nodes-base/credentials/icons/Zabbix.svg b/packages/nodes-base/credentials/icons/Zabbix.svg new file mode 100644 index 0000000000000..960a3a334122e --- /dev/null +++ b/packages/nodes-base/credentials/icons/Zabbix.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index f81ce3b24828b..d4f83de64ecac 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -379,6 +379,7 @@ "dist/credentials/XeroOAuth2Api.credentials.js", "dist/credentials/YourlsApi.credentials.js", "dist/credentials/YouTubeOAuth2Api.credentials.js", + "dist/credentials/ZabbixApi.credentials.js", "dist/credentials/ZammadBasicAuthApi.credentials.js", "dist/credentials/ZammadTokenAuthApi.credentials.js", "dist/credentials/ZendeskApi.credentials.js", From b6d32f53f9f26dde676f3e47382ba168e128cb5b Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Tue, 29 Oct 2024 11:18:44 +0000 Subject: [PATCH 2/6] Added both authentication methods --- .../credentials/ZabbixApi.credentials.ts | 71 ++++------ .../credentials/ZabbixAuthApi.credentials.ts | 130 ++++++++++++++++++ packages/nodes-base/package.json | 1 + 3 files changed, 161 insertions(+), 41 deletions(-) create mode 100644 packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts index e11ec89c8e70f..c3a773ffb3703 100644 --- a/packages/nodes-base/credentials/ZabbixApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -1,9 +1,8 @@ import type { + IAuthenticateGeneric, Icon, - ICredentialDataDecryptedObject, ICredentialTestRequest, ICredentialType, - IHttpRequestOptions, INodeProperties, } from 'n8n-workflow'; @@ -18,61 +17,51 @@ export class ZabbixApi implements ICredentialType { httpRequestNode = { name: 'Zabbix', - docsUrl: 'zabbix', - apiBaseUrlPlaceholder: 'https://zabbix.digital-boss.dev/', + docsUrl: 'https://www.zabbix.com/documentation/current/en/manual/api', + apiBaseUrl: '', }; properties: INodeProperties[] = [ { - displayName: 'URL', - name: 'url', + displayName: 'API Token', + name: 'apiToken', type: 'string', + typeOptions: { password: true }, default: '', - description: 'The base url', }, { - displayName: 'Username', - name: 'username', - type: 'string', - default: '', - }, - { - displayName: 'Password', - name: 'password', + displayName: 'URL', + name: 'url', type: 'string', - typeOptions: { - password: true, - }, + required: true, default: '', }, - { - displayName: 'Testing Mode', - name: 'testingMode', - type: 'boolean', - default: false, - }, ]; - async authenticate( - credentials: ICredentialDataDecryptedObject, - requestOptions: IHttpRequestOptions, - ): Promise { - requestOptions.headers = { - 'Api-Username': credentials.username, - 'Api-Password': credentials.password, - }; - if (requestOptions.method === 'GET') { - delete requestOptions.body; - } - - return requestOptions; - } + authenticate: IAuthenticateGeneric = { + type: 'generic', + properties: { + headers: { + Authorization: '=Bearer {{$credentials.apiToken}}', + }, + }, + }; test: ICredentialTestRequest = { request: { - baseURL: '={{$credentials.url}}', - url: '/', - method: 'GET', + url: 'https://zabbix.digital-boss.dev/zabbix/api_jsonrpc.php', + method: 'POST', + body: { + jsonrpc: '2.0', + method: 'apiinfo.version', + params: {}, + id: 1, + }, + headers: { + Authorization: 'Bearer {{$credentials.apiToken}}', + 'Content-Type': 'application/json-rpc', + }, + json: true, }, }; } diff --git a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts new file mode 100644 index 0000000000000..72e5ad7ec8816 --- /dev/null +++ b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts @@ -0,0 +1,130 @@ +import type { + IAuthenticateGeneric, + Icon, + ICredentialDataDecryptedObject, + ICredentialTestRequest, + ICredentialType, + IHttpRequestHelper, + INodeProperties, +} from 'n8n-workflow'; + +export class ZabbixAuthApi implements ICredentialType { + name = 'zabbixAuthApi'; + + displayName = 'Zabbix Auth API'; + + documentationUrl = 'zabbix'; + + icon: Icon = 'file:icons/Zabbix.svg'; + + httpRequestNode = { + name: 'Zabbix', + docsUrl: 'https://www.zabbix.com/documentation/current/en/manual/api', + apiBaseUrl: '', + }; + + properties: INodeProperties[] = [ + { + displayName: 'Session Token', + name: 'sessionToken', + type: 'hidden', + + typeOptions: { + expirable: true, + }, + default: '', + }, + { + displayName: 'URL', + name: 'url', + type: 'string', + required: true, + default: '', + }, + { + displayName: 'Username', + name: 'username', + type: 'string', + default: '', + }, + { + displayName: 'Password', + name: 'password', + type: 'string', + typeOptions: { + password: true, + }, + default: '', + }, + ]; + + authenticate: IAuthenticateGeneric = { + type: 'generic', + properties: { + headers: { + Authorization: '=Bearer {{$credentials.sessionToken}}', + }, + }, + }; + + async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) { + const url = credentials.url as string; + + const { token } = (await this.helpers.httpRequest({ + method: 'POST', + url, + body: { + jsonrpc: '2.0', + method: 'user.login', + params: { + username: credentials.username, + password: credentials.password, + }, + id: 1, + }, + headers: { + 'Content-Type': 'application/json-rpc', + }, + json: true, + })) as { token: string }; + return { sessionToken: token }; + } + + async postAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) { + const url = credentials.url as string; + const token = credentials.sessionToken as string; + + await this.helpers.httpRequest({ + method: 'POST', + url, + body: { + jsonrpc: '2.0', + method: 'user.logout', + params: [], + id: 1, + }, + headers: { + 'Content-Type': 'application/json-rpc', + Authorization: `Bearer ${token}`, + }, + json: true, + }); + } + + test: ICredentialTestRequest = { + request: { + method: 'POST', + url: 'https://zabbix.digital-boss.dev/zabbix/api_jsonrpc.php', + body: { + jsonrpc: '2.0', + method: 'apiinfo.version', + params: {}, + id: 1, + }, + headers: { + 'Content-Type': 'application/json-rpc', + }, + json: true, + }, + }; +} diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index d4f83de64ecac..a7524278234f3 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -380,6 +380,7 @@ "dist/credentials/YourlsApi.credentials.js", "dist/credentials/YouTubeOAuth2Api.credentials.js", "dist/credentials/ZabbixApi.credentials.js", + "dist/credentials/ZabbixAuthApi.credentials.js", "dist/credentials/ZammadBasicAuthApi.credentials.js", "dist/credentials/ZammadTokenAuthApi.credentials.js", "dist/credentials/ZendeskApi.credentials.js", From 510ea8d94f9e7085eda1213a19099dc7f8ad2925 Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Wed, 30 Oct 2024 10:37:02 +0000 Subject: [PATCH 3/6] Added Zabbix credentials test --- .../nodes-base/credentials/ZabbixApi.credentials.ts | 9 ++++++--- .../credentials/ZabbixAuthApi.credentials.ts | 13 ++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts index c3a773ffb3703..2af1f751014e3 100644 --- a/packages/nodes-base/credentials/ZabbixApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -53,9 +53,12 @@ export class ZabbixApi implements ICredentialType { method: 'POST', body: { jsonrpc: '2.0', - method: 'apiinfo.version', - params: {}, - id: 1, + method: 'host.get', + params: { + output: ['hostid', 'host'], + selectInterfaces: ['interfaceid', 'ip'], + }, + id: 2, }, headers: { Authorization: 'Bearer {{$credentials.apiToken}}', diff --git a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts index 72e5ad7ec8816..acf3cc731c25f 100644 --- a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts @@ -60,11 +60,7 @@ export class ZabbixAuthApi implements ICredentialType { authenticate: IAuthenticateGeneric = { type: 'generic', - properties: { - headers: { - Authorization: '=Bearer {{$credentials.sessionToken}}', - }, - }, + properties: {}, }; async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) { @@ -117,8 +113,11 @@ export class ZabbixAuthApi implements ICredentialType { url: 'https://zabbix.digital-boss.dev/zabbix/api_jsonrpc.php', body: { jsonrpc: '2.0', - method: 'apiinfo.version', - params: {}, + method: 'host.get', + params: { + output: ['hostid'], + }, + auth: '={{$credentials.sessionToken}}', id: 1, }, headers: { From 80a977b87917b2ebedf454aaf906319c3fdadeb0 Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Wed, 30 Oct 2024 11:06:14 +0000 Subject: [PATCH 4/6] Fixed url for test --- packages/nodes-base/credentials/ZabbixApi.credentials.ts | 3 ++- packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts index 2af1f751014e3..47bf6e29fc03b 100644 --- a/packages/nodes-base/credentials/ZabbixApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -49,7 +49,8 @@ export class ZabbixApi implements ICredentialType { test: ICredentialTestRequest = { request: { - url: 'https://zabbix.digital-boss.dev/zabbix/api_jsonrpc.php', + baseURL: '={{$credentials.url}}'.replace(/\/$/, ''), + url: '/zabbix/api_jsonrpc.php', method: 'POST', body: { jsonrpc: '2.0', diff --git a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts index acf3cc731c25f..cd93b05d009c5 100644 --- a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts @@ -100,7 +100,7 @@ export class ZabbixAuthApi implements ICredentialType { id: 1, }, headers: { - 'Content-Type': 'application/json-rpc', + Accept: 'application/json-rpc', Authorization: `Bearer ${token}`, }, json: true, @@ -110,7 +110,8 @@ export class ZabbixAuthApi implements ICredentialType { test: ICredentialTestRequest = { request: { method: 'POST', - url: 'https://zabbix.digital-boss.dev/zabbix/api_jsonrpc.php', + baseURL: '={{$credentials.url}}'.replace(/\/$/, ''), + url: '/zabbix/api_jsonrpc.php', body: { jsonrpc: '2.0', method: 'host.get', From ad79b73af064ac3922870dc2ddf50ed1f185df8c Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Tue, 19 Nov 2024 13:16:47 +0200 Subject: [PATCH 5/6] Resolved comments from PR --- .../credentials/ZabbixApi.credentials.ts | 18 ++- .../credentials/ZabbixAuthApi.credentials.ts | 130 ------------------ packages/nodes-base/package.json | 1 - 3 files changed, 12 insertions(+), 137 deletions(-) delete mode 100644 packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts index 47bf6e29fc03b..5070b844aa9c4 100644 --- a/packages/nodes-base/credentials/ZabbixApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -43,6 +43,7 @@ export class ZabbixApi implements ICredentialType { properties: { headers: { Authorization: '=Bearer {{$credentials.apiToken}}', + 'Content-Type': 'application/json-rpc', }, }, }; @@ -50,7 +51,7 @@ export class ZabbixApi implements ICredentialType { test: ICredentialTestRequest = { request: { baseURL: '={{$credentials.url}}'.replace(/\/$/, ''), - url: '/zabbix/api_jsonrpc.php', + url: '/api_jsonrpc.php', method: 'POST', body: { jsonrpc: '2.0', @@ -61,11 +62,16 @@ export class ZabbixApi implements ICredentialType { }, id: 2, }, - headers: { - Authorization: 'Bearer {{$credentials.apiToken}}', - 'Content-Type': 'application/json-rpc', - }, - json: true, }, + rules: [ + { + type: 'responseSuccessBody', + properties: { + key: 'error', + value: 'invalid_auth', + message: 'Invalid access token', + }, + }, + ], }; } diff --git a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts b/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts deleted file mode 100644 index cd93b05d009c5..0000000000000 --- a/packages/nodes-base/credentials/ZabbixAuthApi.credentials.ts +++ /dev/null @@ -1,130 +0,0 @@ -import type { - IAuthenticateGeneric, - Icon, - ICredentialDataDecryptedObject, - ICredentialTestRequest, - ICredentialType, - IHttpRequestHelper, - INodeProperties, -} from 'n8n-workflow'; - -export class ZabbixAuthApi implements ICredentialType { - name = 'zabbixAuthApi'; - - displayName = 'Zabbix Auth API'; - - documentationUrl = 'zabbix'; - - icon: Icon = 'file:icons/Zabbix.svg'; - - httpRequestNode = { - name: 'Zabbix', - docsUrl: 'https://www.zabbix.com/documentation/current/en/manual/api', - apiBaseUrl: '', - }; - - properties: INodeProperties[] = [ - { - displayName: 'Session Token', - name: 'sessionToken', - type: 'hidden', - - typeOptions: { - expirable: true, - }, - default: '', - }, - { - displayName: 'URL', - name: 'url', - type: 'string', - required: true, - default: '', - }, - { - displayName: 'Username', - name: 'username', - type: 'string', - default: '', - }, - { - displayName: 'Password', - name: 'password', - type: 'string', - typeOptions: { - password: true, - }, - default: '', - }, - ]; - - authenticate: IAuthenticateGeneric = { - type: 'generic', - properties: {}, - }; - - async preAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) { - const url = credentials.url as string; - - const { token } = (await this.helpers.httpRequest({ - method: 'POST', - url, - body: { - jsonrpc: '2.0', - method: 'user.login', - params: { - username: credentials.username, - password: credentials.password, - }, - id: 1, - }, - headers: { - 'Content-Type': 'application/json-rpc', - }, - json: true, - })) as { token: string }; - return { sessionToken: token }; - } - - async postAuthentication(this: IHttpRequestHelper, credentials: ICredentialDataDecryptedObject) { - const url = credentials.url as string; - const token = credentials.sessionToken as string; - - await this.helpers.httpRequest({ - method: 'POST', - url, - body: { - jsonrpc: '2.0', - method: 'user.logout', - params: [], - id: 1, - }, - headers: { - Accept: 'application/json-rpc', - Authorization: `Bearer ${token}`, - }, - json: true, - }); - } - - test: ICredentialTestRequest = { - request: { - method: 'POST', - baseURL: '={{$credentials.url}}'.replace(/\/$/, ''), - url: '/zabbix/api_jsonrpc.php', - body: { - jsonrpc: '2.0', - method: 'host.get', - params: { - output: ['hostid'], - }, - auth: '={{$credentials.sessionToken}}', - id: 1, - }, - headers: { - 'Content-Type': 'application/json-rpc', - }, - json: true, - }, - }; -} diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 3439f0b164e45..46c15a8fa31c5 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -381,7 +381,6 @@ "dist/credentials/YourlsApi.credentials.js", "dist/credentials/YouTubeOAuth2Api.credentials.js", "dist/credentials/ZabbixApi.credentials.js", - "dist/credentials/ZabbixAuthApi.credentials.js", "dist/credentials/ZammadBasicAuthApi.credentials.js", "dist/credentials/ZammadTokenAuthApi.credentials.js", "dist/credentials/ZendeskApi.credentials.js", From 58eb10d2c549dc353c647e8bf5944f003fcf1e88 Mon Sep 17 00:00:00 2001 From: Adina Totorean Date: Thu, 21 Nov 2024 14:18:05 +0200 Subject: [PATCH 6/6] Updated credential test --- .../credentials/ZabbixApi.credentials.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/nodes-base/credentials/ZabbixApi.credentials.ts b/packages/nodes-base/credentials/ZabbixApi.credentials.ts index 5070b844aa9c4..1c081b30da0ad 100644 --- a/packages/nodes-base/credentials/ZabbixApi.credentials.ts +++ b/packages/nodes-base/credentials/ZabbixApi.credentials.ts @@ -23,17 +23,18 @@ export class ZabbixApi implements ICredentialType { properties: INodeProperties[] = [ { - displayName: 'API Token', - name: 'apiToken', + displayName: 'URL', + name: 'url', + required: true, type: 'string', - typeOptions: { password: true }, default: '', }, { - displayName: 'URL', - name: 'url', - type: 'string', + displayName: 'API Token', + name: 'apiToken', required: true, + type: 'string', + typeOptions: { password: true }, default: '', }, ]; @@ -67,8 +68,8 @@ export class ZabbixApi implements ICredentialType { { type: 'responseSuccessBody', properties: { - key: 'error', - value: 'invalid_auth', + key: 'result', + value: undefined, message: 'Invalid access token', }, },