From 609197dce7b4b8b06195717120250c7b27889e63 Mon Sep 17 00:00:00 2001 From: martin auer Date: Thu, 2 Mar 2023 13:20:12 +0100 Subject: [PATCH] fix(tenant): Correctly configure storage for multi tenant agents Fixes hyperledger/aries-framework-javascript#1353 Signed-off-by: martin auer --- .../src/context/TenantSessionCoordinator.ts | 18 ++++++++++++++++-- .../__tests__/TenantSessionCoordinator.test.ts | 8 +++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/tenants/src/context/TenantSessionCoordinator.ts b/packages/tenants/src/context/TenantSessionCoordinator.ts index cdc7428a86..c085675d5a 100644 --- a/packages/tenants/src/context/TenantSessionCoordinator.ts +++ b/packages/tenants/src/context/TenantSessionCoordinator.ts @@ -10,6 +10,7 @@ import { InjectionSymbols, Logger, WalletApi, + WalletError, } from '@aries-framework/core' import { Mutex, withTimeout } from 'async-mutex' @@ -180,7 +181,16 @@ export class TenantSessionCoordinator { private async createAgentContext(tenantRecord: TenantRecord) { const tenantDependencyManager = this.rootAgentContext.dependencyManager.createChild() - const tenantConfig = this.rootAgentContext.config.extend(tenantRecord.config) + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { id, key, keyDerivationMethod, ...strippedWalletConfig } = this.rootAgentContext.config?.walletConfig ?? {} + const tenantConfig = this.rootAgentContext.config.extend({ + ...tenantRecord.config, + walletConfig: { + ...strippedWalletConfig, + ...tenantRecord.config.walletConfig, + }, + }) const agentContext = new AgentContext({ contextCorrelationId: tenantRecord.id, @@ -194,7 +204,11 @@ export class TenantSessionCoordinator { // and will also write the storage version to the storage, which is needed by the update assistant. We either // need to move this out of the module, or just keep using the module here. const walletApi = agentContext.dependencyManager.resolve(WalletApi) - await walletApi.initialize(tenantRecord.config.walletConfig) + + if (!tenantConfig.walletConfig) { + throw new WalletError('Cannot initialize tenant without Wallet config.') + } + await walletApi.initialize(tenantConfig.walletConfig) return agentContext } diff --git a/packages/tenants/src/context/__tests__/TenantSessionCoordinator.test.ts b/packages/tenants/src/context/__tests__/TenantSessionCoordinator.test.ts index dd659db44c..6744ef0359 100644 --- a/packages/tenants/src/context/__tests__/TenantSessionCoordinator.test.ts +++ b/packages/tenants/src/context/__tests__/TenantSessionCoordinator.test.ts @@ -1,7 +1,7 @@ import type { TenantAgentContextMapping } from '../TenantSessionCoordinator' import type { DependencyManager } from '@aries-framework/core' -import { AgentContext, AgentConfig, WalletApi } from '@aries-framework/core' +import { AgentConfig, AgentContext, WalletApi } from '@aries-framework/core' import { Mutex, withTimeout } from 'async-mutex' import { getAgentConfig, getAgentContext, mockFunction } from '../../../../core/tests/helpers' @@ -91,10 +91,8 @@ describe('TenantSessionCoordinator', () => { registerInstance: jest.fn(), resolve: jest.fn(() => wallet), } as unknown as DependencyManager - const mockConfig = jest.fn() as unknown as AgentConfig createChildSpy.mockReturnValue(tenantDependencyManager) - extendSpy.mockReturnValue(mockConfig) const tenantAgentContext = await tenantSessionCoordinator.getContextForSession(tenantRecord) @@ -103,7 +101,7 @@ describe('TenantSessionCoordinator', () => { expect(extendSpy).toHaveBeenCalledWith(tenantRecord.config) expect(createChildSpy).toHaveBeenCalledWith() expect(tenantDependencyManager.registerInstance).toHaveBeenCalledWith(AgentContext, expect.any(AgentContext)) - expect(tenantDependencyManager.registerInstance).toHaveBeenCalledWith(AgentConfig, mockConfig) + expect(tenantDependencyManager.registerInstance).toHaveBeenCalledWith(AgentConfig, expect.any(AgentConfig)) expect(tenantSessionCoordinator.tenantAgentContextMapping.tenant1).toEqual({ agentContext: tenantAgentContext, @@ -194,8 +192,8 @@ describe('TenantSessionCoordinator', () => { }) // Initialize should only be called once - expect(wallet.initialize).toHaveBeenCalledTimes(1) expect(wallet.initialize).toHaveBeenCalledWith(tenantRecord.config.walletConfig) + expect(wallet.initialize).toHaveBeenCalledTimes(1) expect(tenantAgentContext1).toBe(tenantAgentContext2) })