From a009c658341315250639bf820a966ee84111004a Mon Sep 17 00:00:00 2001 From: Michael Kret Date: Fri, 6 May 2022 12:15:09 +0300 Subject: [PATCH 1/3] :zap: added function to credentials helper --- packages/cli/src/CredentialsHelper.ts | 10 +++++++++ packages/cli/src/api/credentials.api.ts | 21 ++++--------------- .../cli/test/integration/shared/testDb.ts | 7 ++----- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/packages/cli/src/CredentialsHelper.ts b/packages/cli/src/CredentialsHelper.ts index 7707cd1a39228..169af8d870291 100644 --- a/packages/cli/src/CredentialsHelper.ts +++ b/packages/cli/src/CredentialsHelper.ts @@ -51,6 +51,8 @@ import { } from '.'; // eslint-disable-next-line import/no-cycle import { User } from './databases/entities/User'; +// eslint-disable-next-line import/no-cycle +import { CredentialsEntity } from './databases/entities/CredentialsEntity'; const mockNodeTypes: INodeTypes = { nodeTypes: {} as INodeTypeData, @@ -764,3 +766,11 @@ export async function getCredentialWithoutUser( const credential = await Db.collections.Credentials.findOne(credentialId); return credential; } + +export function createCoreCredential(credential: CredentialsEntity, encrypt = false): Credentials { + const { id, name, type, nodesAccess, data } = credential; + if (encrypt) { + return new Credentials({ id: null, name }, type, nodesAccess); + } + return new Credentials({ id: id.toString(), name }, type, nodesAccess, data); +} diff --git a/packages/cli/src/api/credentials.api.ts b/packages/cli/src/api/credentials.api.ts index 6894523609413..6601b82df3c7b 100644 --- a/packages/cli/src/api/credentials.api.ts +++ b/packages/cli/src/api/credentials.api.ts @@ -23,6 +23,7 @@ import { RESPONSE_ERROR_MESSAGES } from '../constants'; import { CredentialsEntity } from '../databases/entities/CredentialsEntity'; import { SharedCredentials } from '../databases/entities/SharedCredentials'; import { validateEntity } from '../GenericHelpers'; +import { createCoreCredential } from '../CredentialsHelper'; import type { CredentialRequest } from '../requests'; import * as config from '../../config'; import { externalHooks } from '../Server'; @@ -165,11 +166,7 @@ credentialsController.post( } // Encrypt the data - const coreCredential = new Credentials( - { id: null, name: newCredential.name }, - newCredential.type, - newCredential.nodesAccess, - ); + const coreCredential = createCoreCredential(newCredential, true); // @ts-ignore coreCredential.setData(newCredential.data, encryptionKey); @@ -301,12 +298,7 @@ credentialsController.patch( ); } - const coreCredential = new Credentials( - { id: credential.id.toString(), name: credential.name }, - credential.type, - credential.nodesAccess, - credential.data, - ); + const coreCredential = createCoreCredential(credential); const decryptedData = coreCredential.getData(encryptionKey); @@ -410,12 +402,7 @@ credentialsController.get( ); } - const coreCredential = new Credentials( - { id: credential.id.toString(), name: credential.name }, - credential.type, - credential.nodesAccess, - credential.data, - ); + const coreCredential = createCoreCredential(credential); return { id: id.toString(), diff --git a/packages/cli/test/integration/shared/testDb.ts b/packages/cli/test/integration/shared/testDb.ts index 55ad557ecae7c..bdde5c920a9d5 100644 --- a/packages/cli/test/integration/shared/testDb.ts +++ b/packages/cli/test/integration/shared/testDb.ts @@ -16,6 +16,7 @@ import { mysqlMigrations } from '../../../src/databases/mysqldb/migrations'; import { postgresMigrations } from '../../../src/databases/postgresdb/migrations'; import { sqliteMigrations } from '../../../src/databases/sqlite/migrations'; import { categorize, getPostgresSchemaSection } from './utils'; +import { createCoreCredential } from '../../../src/CredentialsHelper'; import type { Role } from '../../../src/databases/entities/Role'; import type { User } from '../../../src/databases/entities/User'; @@ -420,11 +421,7 @@ export const getMySqlOptions = ({ name }: { name: string }): ConnectionOptions = async function encryptCredentialData(credential: CredentialsEntity) { const encryptionKey = await UserSettings.getEncryptionKey(); - const coreCredential = new Credentials( - { id: null, name: credential.name }, - credential.type, - credential.nodesAccess, - ); + const coreCredential = createCoreCredential(credential, true); // @ts-ignore coreCredential.setData(credential.data, encryptionKey); From 51a14cf3610776d82bb298c62d1adb7196a41cac Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Thu, 2 Jun 2022 19:21:45 +0200 Subject: [PATCH 2/3] Refactor function name --- packages/cli/src/CredentialsHelper.ts | 2 +- packages/cli/src/api/credentials.api.ts | 8 ++++---- packages/cli/test/integration/shared/testDb.ts | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/CredentialsHelper.ts b/packages/cli/src/CredentialsHelper.ts index 169af8d870291..7968a20a13d61 100644 --- a/packages/cli/src/CredentialsHelper.ts +++ b/packages/cli/src/CredentialsHelper.ts @@ -767,7 +767,7 @@ export async function getCredentialWithoutUser( return credential; } -export function createCoreCredential(credential: CredentialsEntity, encrypt = false): Credentials { +export function createCredentiasFromCredentialsEntity(credential: CredentialsEntity, encrypt = false): Credentials { const { id, name, type, nodesAccess, data } = credential; if (encrypt) { return new Credentials({ id: null, name }, type, nodesAccess); diff --git a/packages/cli/src/api/credentials.api.ts b/packages/cli/src/api/credentials.api.ts index 6601b82df3c7b..67d235957c1f5 100644 --- a/packages/cli/src/api/credentials.api.ts +++ b/packages/cli/src/api/credentials.api.ts @@ -23,7 +23,7 @@ import { RESPONSE_ERROR_MESSAGES } from '../constants'; import { CredentialsEntity } from '../databases/entities/CredentialsEntity'; import { SharedCredentials } from '../databases/entities/SharedCredentials'; import { validateEntity } from '../GenericHelpers'; -import { createCoreCredential } from '../CredentialsHelper'; +import { createCredentiasFromCredentialsEntity } from '../CredentialsHelper'; import type { CredentialRequest } from '../requests'; import * as config from '../../config'; import { externalHooks } from '../Server'; @@ -166,7 +166,7 @@ credentialsController.post( } // Encrypt the data - const coreCredential = createCoreCredential(newCredential, true); + const coreCredential = createCredentiasFromCredentialsEntity(newCredential, true); // @ts-ignore coreCredential.setData(newCredential.data, encryptionKey); @@ -298,7 +298,7 @@ credentialsController.patch( ); } - const coreCredential = createCoreCredential(credential); + const coreCredential = createCredentiasFromCredentialsEntity(credential); const decryptedData = coreCredential.getData(encryptionKey); @@ -402,7 +402,7 @@ credentialsController.get( ); } - const coreCredential = createCoreCredential(credential); + const coreCredential = createCredentiasFromCredentialsEntity(credential); return { id: id.toString(), diff --git a/packages/cli/test/integration/shared/testDb.ts b/packages/cli/test/integration/shared/testDb.ts index bdde5c920a9d5..c433029213ffc 100644 --- a/packages/cli/test/integration/shared/testDb.ts +++ b/packages/cli/test/integration/shared/testDb.ts @@ -16,7 +16,7 @@ import { mysqlMigrations } from '../../../src/databases/mysqldb/migrations'; import { postgresMigrations } from '../../../src/databases/postgresdb/migrations'; import { sqliteMigrations } from '../../../src/databases/sqlite/migrations'; import { categorize, getPostgresSchemaSection } from './utils'; -import { createCoreCredential } from '../../../src/CredentialsHelper'; +import { createCredentiasFromCredentialsEntity } from '../../../src/CredentialsHelper'; import type { Role } from '../../../src/databases/entities/Role'; import type { User } from '../../../src/databases/entities/User'; @@ -421,7 +421,7 @@ export const getMySqlOptions = ({ name }: { name: string }): ConnectionOptions = async function encryptCredentialData(credential: CredentialsEntity) { const encryptionKey = await UserSettings.getEncryptionKey(); - const coreCredential = createCoreCredential(credential, true); + const coreCredential = createCredentiasFromCredentialsEntity(credential, true); // @ts-ignore coreCredential.setData(credential.data, encryptionKey); From bbc0e19faba3bd81c76db0638e1ca9ff0c0a09ba Mon Sep 17 00:00:00 2001 From: Omar Ajoue Date: Thu, 2 Jun 2022 19:49:38 +0200 Subject: [PATCH 3/3] Fix lint issues --- packages/cli/src/CredentialsHelper.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/CredentialsHelper.ts b/packages/cli/src/CredentialsHelper.ts index 7968a20a13d61..0837afedb674f 100644 --- a/packages/cli/src/CredentialsHelper.ts +++ b/packages/cli/src/CredentialsHelper.ts @@ -767,7 +767,10 @@ export async function getCredentialWithoutUser( return credential; } -export function createCredentiasFromCredentialsEntity(credential: CredentialsEntity, encrypt = false): Credentials { +export function createCredentiasFromCredentialsEntity( + credential: CredentialsEntity, + encrypt = false, +): Credentials { const { id, name, type, nodesAccess, data } = credential; if (encrypt) { return new Credentials({ id: null, name }, type, nodesAccess);