* Endpoint to preset credentials

*  Endpoint to preset credentials

*  Improvements

* 🐛 Small fix

* 🐛 Small fix
This commit is contained in:
Ricardo Espinoza 2020-07-07 13:03:53 -04:00 committed by GitHub
parent b1ee1efcb1
commit 8aff042e04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 10 deletions

View file

@ -128,15 +128,23 @@ const config = convict({
credentials: { credentials: {
overwrite: { overwrite: {
// Allows to set default values for credentials which data: {
// get automatically prefilled and the user does not get // Allows to set default values for credentials which
// displayed and can not change. // get automatically prefilled and the user does not get
// Format: { CREDENTIAL_NAME: { PARAMTER: VALUE }} // displayed and can not change.
doc: 'Overwrites for credentials', // Format: { CREDENTIAL_NAME: { PARAMTER: VALUE }}
format: '*', doc: 'Overwrites for credentials',
default: '{}', format: '*',
env: 'CREDENTIALS_OVERWRITE' default: '{}',
} env: 'CREDENTIALS_OVERWRITE_DATA'
},
endpoint: {
doc: 'Fetch credentials from API',
format: String,
default: '',
env: 'CREDENTIALS_OVERWRITE_ENDPOINT',
},
},
}, },
executions: { executions: {

View file

@ -20,7 +20,7 @@ class CredentialsOverwritesClass {
return; return;
} }
const data = await GenericHelpers.getConfigValue('credentials.overwrite') as string; const data = await GenericHelpers.getConfigValue('credentials.overwrite.data') as string;
try { try {
this.overwriteData = JSON.parse(data); this.overwriteData = JSON.parse(data);
@ -30,6 +30,7 @@ class CredentialsOverwritesClass {
} }
applyOverwrite(type: string, data: ICredentialDataDecryptedObject) { applyOverwrite(type: string, data: ICredentialDataDecryptedObject) {
const overwrites = this.get(type); const overwrites = this.get(type);
if (overwrites === undefined) { if (overwrites === undefined) {

View file

@ -58,6 +58,9 @@ import {
WorkflowExecuteAdditionalData, WorkflowExecuteAdditionalData,
WorkflowRunner, WorkflowRunner,
GenericHelpers, GenericHelpers,
CredentialsOverwrites,
ICredentialsOverwrite,
LoadNodesAndCredentials,
} from './'; } from './';
import { import {
@ -105,6 +108,7 @@ class App {
testWebhooks: TestWebhooks.TestWebhooks; testWebhooks: TestWebhooks.TestWebhooks;
endpointWebhook: string; endpointWebhook: string;
endpointWebhookTest: string; endpointWebhookTest: string;
endpointPresetCredentials: string;
externalHooks: IExternalHooksClass; externalHooks: IExternalHooksClass;
saveDataErrorExecution: string; saveDataErrorExecution: string;
saveDataSuccessExecution: string; saveDataSuccessExecution: string;
@ -119,6 +123,8 @@ class App {
sslKey: string; sslKey: string;
sslCert: string; sslCert: string;
presetCredentialsLoaded: boolean;
constructor() { constructor() {
this.app = express(); this.app = express();
@ -141,6 +147,9 @@ class App {
this.sslCert = config.get('ssl_cert'); this.sslCert = config.get('ssl_cert');
this.externalHooks = ExternalHooks(); this.externalHooks = ExternalHooks();
this.presetCredentialsLoaded = false;
this.endpointPresetCredentials = config.get('credentials.overwrite.endpoint') as string;
} }
@ -1650,6 +1659,40 @@ class App {
}); });
if (this.endpointPresetCredentials !== '') {
// POST endpoint to set preset credentials
this.app.post(`/${this.endpointPresetCredentials}`, async (req: express.Request, res: express.Response) => {
if (this.presetCredentialsLoaded === false) {
const body = req.body as ICredentialsOverwrite;
if (req.headers['content-type'] !== 'application/json') {
ResponseHelper.sendErrorResponse(res, new Error('Body must be a valid JSON, make sure the content-type is application/json'));
return;
}
const loadNodesAndCredentials = LoadNodesAndCredentials();
const credentialsOverwrites = CredentialsOverwrites();
await credentialsOverwrites.init(body);
const credentialTypes = CredentialTypes();
await credentialTypes.init(loadNodesAndCredentials.credentialTypes);
this.presetCredentialsLoaded = true;
ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200);
} else {
ResponseHelper.sendErrorResponse(res, new Error('Preset credentials can be set once'));
}
});
}
// Serve the website // Serve the website
const startTime = (new Date()).toUTCString(); const startTime = (new Date()).toUTCString();
const editorUiPath = require.resolve('n8n-editor-ui'); const editorUiPath = require.resolve('n8n-editor-ui');