🔀 Merge branch 'export_plain' of https://github.com/AllanDaemon/n8n into AllanDaemon-export_plain

This commit is contained in:
Jan Oberhauser 2021-02-21 22:07:11 +01:00
commit 4b0776d85a
2 changed files with 39 additions and 1 deletions

View file

@ -3,6 +3,11 @@ import {
flags, flags,
} from '@oclif/command'; } from '@oclif/command';
import {
Credentials,
UserSettings,
} from 'n8n-core';
import { import {
IDataObject IDataObject
} from 'n8n-workflow'; } from 'n8n-workflow';
@ -10,6 +15,7 @@ import {
import { import {
Db, Db,
GenericHelpers, GenericHelpers,
ICredentialsDecryptedDb,
} from '../../src'; } from '../../src';
import * as fs from 'fs'; import * as fs from 'fs';
@ -21,8 +27,9 @@ export class ExportCredentialsCommand extends Command {
static examples = [ static examples = [
`$ n8n export:credentials --all`, `$ n8n export:credentials --all`,
`$ n8n export:credentials --id=5 --output=file.json`, `$ n8n export:credentials --id=5 --output=file.json`,
`$ n8n export:credentials --all --output=backups/latest/`, `$ n8n export:credentials --all --output=backups/latest.json`,
`$ n8n export:credentials --backup --output=backups/latest/`, `$ n8n export:credentials --backup --output=backups/latest/`,
`$ n8n export:credentials --all --decrypted --output=backups/decrypted.json`,
]; ];
static flags = { static flags = {
@ -46,6 +53,9 @@ export class ExportCredentialsCommand extends Command {
separate: flags.boolean({ separate: flags.boolean({
description: 'Exports one file per credential (useful for versioning). Must inform a directory via --output.', description: 'Exports one file per credential (useful for versioning). Must inform a directory via --output.',
}), }),
decrypted: flags.boolean({
description: 'Exports data decrypted / in plain text. ALL SENSITIVE INFORMATION WILL BE VISIBLE IN THE FILES. Use to migrate from a installation to another that have a different secret key (in the config file).',
}),
}; };
async run() { async run() {
@ -108,6 +118,21 @@ export class ExportCredentialsCommand extends Command {
const credentials = await Db.collections.Credentials!.find(findQuery); const credentials = await Db.collections.Credentials!.find(findQuery);
if (flags.decrypted) {
const encryptionKey = await UserSettings.getEncryptionKey();
if (encryptionKey === undefined) {
throw new Error('No encryption key got found to decrypt the credentials!');
}
for (let i = 0; i < credentials.length; i++) {
const c = credentials[i];
const {name, type, nodesAccess, data} = credentials[i];
const credential = new Credentials(name, type, nodesAccess, data);
const plainData = credential.getData(encryptionKey);
(credentials[i] as ICredentialsDecryptedDb).data = plainData;
}
}
if (credentials.length === 0) { if (credentials.length === 0) {
throw new Error('No credentials found with specified filters.'); throw new Error('No credentials found with specified filters.');
} }

View file

@ -3,6 +3,11 @@ import {
flags, flags,
} from '@oclif/command'; } from '@oclif/command';
import {
Credentials,
UserSettings,
} from 'n8n-core';
import { import {
Db, Db,
GenericHelpers, GenericHelpers,
@ -64,7 +69,15 @@ export class ImportCredentialsCommand extends Command {
throw new Error(`File does not seem to contain credentials.`); throw new Error(`File does not seem to contain credentials.`);
} }
const encryptionKey = await UserSettings.getEncryptionKey();
if (encryptionKey === undefined) {
throw new Error('No encryption key got found to encrypt the credentials!');
}
for (i = 0; i < fileContents.length; i++) { for (i = 0; i < fileContents.length; i++) {
if (typeof fileContents[i].data === 'object') {
// plain data / decrypted input. Should be encrypted first.
Credentials.prototype.setData.call(fileContents[i], fileContents[i].data, encryptionKey);
}
await Db.collections.Credentials!.save(fileContents[i]); await Db.collections.Credentials!.save(fileContents[i]);
} }
} }