2020-01-25 23:48:38 -08:00
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ICredentialsOverwrite,
|
|
|
|
GenericHelpers,
|
|
|
|
} from './';
|
|
|
|
|
|
|
|
|
|
|
|
class CredentialsOverwritesClass {
|
|
|
|
|
|
|
|
private overwriteData: ICredentialsOverwrite = {};
|
|
|
|
|
2020-05-14 05:27:19 -07:00
|
|
|
async init(overwriteData?: ICredentialsOverwrite) {
|
|
|
|
if (overwriteData !== undefined) {
|
|
|
|
// If data is already given it can directly be set instead of
|
|
|
|
// loaded from environment
|
|
|
|
this.overwriteData = overwriteData;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-25 23:48:38 -08:00
|
|
|
const data = await GenericHelpers.getConfigValue('credentials.overwrite') as string;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.overwriteData = JSON.parse(data);
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`The credentials-overwrite is not valid JSON.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyOverwrite(type: string, data: ICredentialDataDecryptedObject) {
|
|
|
|
const overwrites = this.get(type);
|
|
|
|
|
|
|
|
if (overwrites === undefined) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData = JSON.parse(JSON.stringify(data));
|
|
|
|
Object.assign(returnData, overwrites);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(type: string): ICredentialDataDecryptedObject | undefined {
|
|
|
|
return this.overwriteData[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
getAll(): ICredentialsOverwrite {
|
|
|
|
return this.overwriteData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let credentialsOverwritesInstance: CredentialsOverwritesClass | undefined;
|
|
|
|
|
|
|
|
export function CredentialsOverwrites(): CredentialsOverwritesClass {
|
|
|
|
if (credentialsOverwritesInstance === undefined) {
|
|
|
|
credentialsOverwritesInstance = new CredentialsOverwritesClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
return credentialsOverwritesInstance;
|
|
|
|
}
|