2023-10-23 04:39:35 -07:00
|
|
|
import { Container } from 'typedi';
|
|
|
|
import { Cipher } from 'n8n-core';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { SettingsRepository } from '@db/repositories/settings.repository';
|
2023-08-25 01:33:46 -07:00
|
|
|
import type { ExternalSecretsSettings } from '@/Interfaces';
|
|
|
|
import { License } from '@/License';
|
|
|
|
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
|
|
|
|
import { ExternalSecretsProviders } from '@/ExternalSecrets/ExternalSecretsProviders.ee';
|
2023-10-23 04:39:35 -07:00
|
|
|
import { InternalHooks } from '@/InternalHooks';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { mockInstance } from '../../shared/mocking';
|
2023-08-25 01:33:46 -07:00
|
|
|
import {
|
|
|
|
DummyProvider,
|
|
|
|
ErrorProvider,
|
|
|
|
FailedProvider,
|
|
|
|
MockProviders,
|
|
|
|
} from '../../shared/ExternalSecrets/utils';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-08-25 01:33:46 -07:00
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
describe('External Secrets Manager', () => {
|
|
|
|
const connectedDate = '2023-08-01T12:32:29.000Z';
|
|
|
|
let settings: string | null = null;
|
2023-08-25 01:33:46 -07:00
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
const mockProvidersInstance = new MockProviders();
|
|
|
|
const license = mockInstance(License);
|
|
|
|
const settingsRepo = mockInstance(SettingsRepository);
|
|
|
|
mockInstance(InternalHooks);
|
|
|
|
const cipher = Container.get(Cipher);
|
2023-08-25 01:33:46 -07:00
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
let providersMock: ExternalSecretsProviders;
|
|
|
|
let manager: ExternalSecretsManager;
|
|
|
|
|
|
|
|
const createMockSettings = (settings: ExternalSecretsSettings): string => {
|
|
|
|
return cipher.encrypt(settings);
|
|
|
|
};
|
|
|
|
|
|
|
|
const decryptSettings = (settings: string) => {
|
|
|
|
return JSON.parse(cipher.decrypt(settings));
|
|
|
|
};
|
2023-08-25 01:33:46 -07:00
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
providersMock = mockInstance(ExternalSecretsProviders, mockProvidersInstance);
|
2023-10-23 04:39:35 -07:00
|
|
|
settings = createMockSettings({
|
|
|
|
dummy: { connected: true, connectedAt: new Date(connectedDate), settings: {} },
|
2023-08-25 01:33:46 -07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockProvidersInstance.setProviders({
|
|
|
|
dummy: DummyProvider,
|
|
|
|
});
|
2023-10-23 04:39:35 -07:00
|
|
|
license.isExternalSecretsEnabled.mockReturnValue(true);
|
|
|
|
settingsRepo.getEncryptedSecretsProviderSettings.mockResolvedValue(settings);
|
2023-10-25 07:35:22 -07:00
|
|
|
manager = new ExternalSecretsManager(mock(), settingsRepo, license, providersMock, cipher);
|
2023-08-25 01:33:46 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
manager?.shutdown();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should get secret', async () => {
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
expect(manager.getSecret('dummy', 'test1')).toBe('value1');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not throw errors during init', async () => {
|
|
|
|
mockProvidersInstance.setProviders({
|
|
|
|
dummy: ErrorProvider,
|
|
|
|
});
|
|
|
|
expect(async () => manager!.init()).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not throw errors during shutdown', async () => {
|
|
|
|
mockProvidersInstance.setProviders({
|
|
|
|
dummy: ErrorProvider,
|
|
|
|
});
|
|
|
|
|
|
|
|
await manager.init();
|
|
|
|
expect(() => manager!.shutdown()).not.toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should save provider settings', async () => {
|
|
|
|
const settingsSpy = jest.spyOn(settingsRepo, 'saveEncryptedSecretsProviderSettings');
|
|
|
|
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
await manager.setProviderSettings('dummy', {
|
|
|
|
test: 'value',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(decryptSettings(settingsSpy.mock.calls[0][0])).toEqual({
|
|
|
|
dummy: {
|
|
|
|
connected: true,
|
|
|
|
connectedAt: connectedDate,
|
|
|
|
settings: {
|
|
|
|
test: 'value',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should call provider update functions on a timer', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(0);
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not call provider update functions if the not licensed', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2023-10-23 04:39:35 -07:00
|
|
|
license.isExternalSecretsEnabled.mockReturnValue(false);
|
2023-08-25 01:33:46 -07:00
|
|
|
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(0);
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not call provider update functions if the provider has an error', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
mockProvidersInstance.setProviders({
|
|
|
|
dummy: FailedProvider,
|
|
|
|
});
|
|
|
|
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
const updateSpy = jest.spyOn(manager.getProvider('dummy')!, 'update');
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(0);
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
expect(updateSpy).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should reinitialize a provider when save provider settings', async () => {
|
|
|
|
await manager.init();
|
|
|
|
|
|
|
|
const dummyInitSpy = jest.spyOn(DummyProvider.prototype, 'init');
|
|
|
|
|
|
|
|
await manager.setProviderSettings('dummy', {
|
|
|
|
test: 'value',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(dummyInitSpy).toBeCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|