Skip to content

Commit

Permalink
fix(tenant): Correctly configure storage for multi tenant agents
Browse files Browse the repository at this point in the history
Fixes #1353

Signed-off-by: martin auer <[email protected]>
  • Loading branch information
auer-martin committed Mar 2, 2023
1 parent 1bda3f0 commit 2c3df42
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 14 additions & 2 deletions packages/tenants/src/context/TenantSessionCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
InjectionSymbols,
Logger,
WalletApi,
WalletError,
} from '@aries-framework/core'
import { Mutex, withTimeout } from 'async-mutex'

Expand Down Expand Up @@ -180,7 +181,14 @@ export class TenantSessionCoordinator {

private async createAgentContext(tenantRecord: TenantRecord) {
const tenantDependencyManager = this.rootAgentContext.dependencyManager.createChild()
const tenantConfig = this.rootAgentContext.config.extend(tenantRecord.config)

const tenantConfig = this.rootAgentContext.config.extend({
...tenantRecord.config,
walletConfig: {
...this.rootAgentContext.config?.walletConfig,
...tenantRecord.config.walletConfig,
},
})

const agentContext = new AgentContext({
contextCorrelationId: tenantRecord.id,
Expand All @@ -194,7 +202,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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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, KeyDerivationMethod } from '@aries-framework/core'
import { Mutex, withTimeout } from 'async-mutex'

import { getAgentConfig, getAgentContext, mockFunction } from '../../../../core/tests/helpers'
Expand Down Expand Up @@ -91,19 +91,18 @@ 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)

expect(wallet.initialize).toHaveBeenCalledWith(tenantRecord.config.walletConfig)
const extendedWalletConfig = { ...tenantRecord.config.walletConfig, keyDerivationMethod: KeyDerivationMethod.Raw }
expect(wallet.initialize).toHaveBeenCalledWith(extendedWalletConfig)
expect(tenantSessionMutexMock.acquireSession).toHaveBeenCalledTimes(1)
expect(extendSpy).toHaveBeenCalledWith(tenantRecord.config)
expect(extendSpy).toHaveBeenCalledWith({ ...tenantRecord.config, walletConfig: extendedWalletConfig })
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,
Expand Down Expand Up @@ -138,7 +137,8 @@ describe('TenantSessionCoordinator', () => {

await expect(tenantSessionCoordinator.getContextForSession(tenantRecord)).rejects.toThrowError('Test error')

expect(wallet.initialize).toHaveBeenCalledWith(tenantRecord.config.walletConfig)
const extendedWalletConfig = { ...tenantRecord.config.walletConfig, keyDerivationMethod: KeyDerivationMethod.Raw }
expect(wallet.initialize).toHaveBeenCalledWith(extendedWalletConfig)
expect(tenantSessionMutexMock.acquireSession).toHaveBeenCalledTimes(1)
expect(tenantSessionMutexMock.releaseSession).toHaveBeenCalledTimes(1)
})
Expand Down Expand Up @@ -194,8 +194,9 @@ describe('TenantSessionCoordinator', () => {
})

// Initialize should only be called once
const extendedWalletConfig = { ...tenantRecord.config.walletConfig, keyDerivationMethod: KeyDerivationMethod.Raw }
expect(wallet.initialize).toHaveBeenCalledWith(extendedWalletConfig)
expect(wallet.initialize).toHaveBeenCalledTimes(1)
expect(wallet.initialize).toHaveBeenCalledWith(tenantRecord.config.walletConfig)

expect(tenantAgentContext1).toBe(tenantAgentContext2)
})
Expand Down

0 comments on commit 2c3df42

Please sign in to comment.