Skip to content

Commit

Permalink
fix(tenant): Correctly configure storage for multi tenant agents (ope…
Browse files Browse the repository at this point in the history
…nwallet-foundation#1359)

Fixes hyperledger#1353

Signed-off-by: martin auer <martin.auer97@gmail.com>
  • Loading branch information
auer-martin committed Mar 3, 2023
1 parent cb4e469 commit 7795975
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 16 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,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,
Expand All @@ -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
}
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 } from '@aries-framework/core'
import { Mutex, withTimeout } from 'async-mutex'

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

Expand All @@ -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,
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 7795975

Please sign in to comment.