mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Deduplicate encryption logic (#3434)
* ⚡ added function to credentials helper
* Refactor function name
* Fix lint issues
Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
parent
bdb84130d6
commit
4b8472b7bb
|
@ -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,
|
||||
|
@ -768,3 +770,14 @@ export async function getCredentialWithoutUser(
|
|||
const credential = await Db.collections.Credentials.findOne(credentialId);
|
||||
return credential;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
return new Credentials({ id: id.toString(), name }, type, nodesAccess, data);
|
||||
}
|
||||
|
|
|
@ -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 { createCredentiasFromCredentialsEntity } 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 = createCredentiasFromCredentialsEntity(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 = createCredentiasFromCredentialsEntity(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 = createCredentiasFromCredentialsEntity(credential);
|
||||
|
||||
return {
|
||||
id: id.toString(),
|
||||
|
|
|
@ -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 { createCredentiasFromCredentialsEntity } 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 = createCredentiasFromCredentialsEntity(credential, true);
|
||||
|
||||
// @ts-ignore
|
||||
coreCredential.setData(credential.data, encryptionKey);
|
||||
|
|
Loading…
Reference in a new issue