Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tenant): Correctly configure storage for multi tenant agents #1359

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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