-
Notifications
You must be signed in to change notification settings - Fork 86
fix(security): harden agent provisioning execution #1708
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
Open
sign-mark
wants to merge
5
commits into
credebl:main
Choose a base branch
from
sign-mark:agent/harden-agent-provisioning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+318
−68
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b3137c1
fix(security): harden agent provisioning execution
sign-mark e7ca844
fix(security): sanitize agent provisioning failures
sign-mark 06018ed
fix(security): preserve provisioning compatibility
sign-mark 1def944
fix(security): validate agent endpoint documents
sign-mark dd465c3
fix(security): address SonarQube issues in agent provisioning
ajile-in File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
apps/agent-provisioning/src/agent-provisioning.service.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| const mockExecFile = jest.fn(); | ||
| const mockReadFile = jest.fn(); | ||
| const mockAccess = jest.fn(); | ||
|
|
||
| jest.mock('child_process', () => ({ execFile: mockExecFile })); | ||
| jest.mock('util', () => ({ promisify: jest.fn(() => mockExecFile) })); | ||
| jest.mock('fs', () => ({ promises: { access: mockAccess, readFile: mockReadFile } })); | ||
|
|
||
| import { AgentType } from '@credebl/enum/enum'; | ||
| import { AgentProvisioningService } from './agent-provisioning.service'; | ||
|
|
||
| const payload = { | ||
| orgId: 'org-123', | ||
| externalIp: '127.0.0.1', | ||
| walletName: 'wallet', | ||
| walletPassword: 'wallet-secret', | ||
| seed: 'seed', | ||
| webhookEndpoint: 'https://example.test/webhook', | ||
| walletStorageHost: 'postgres', | ||
| walletStoragePort: '5432', | ||
| walletStorageUser: 'user', | ||
| walletStoragePassword: 'storage-secret', | ||
| internalIp: '127.0.0.1', | ||
| containerName: 'issuer-agent', | ||
| agentType: AgentType.AFJ, | ||
| orgName: 'Organization', | ||
| indyLedger: '[]', | ||
| protocol: 'http', | ||
| credoImage: 'credo:latest', | ||
| tenant: false, | ||
| inboundEndpoint: '127.0.0.1' | ||
| }; | ||
|
|
||
| describe('AgentProvisioningService', () => { | ||
| const logger = { log: jest.fn(), error: jest.fn() }; | ||
| const service = new AgentProvisioningService(logger as never); | ||
| const savedEnv = { ...process.env }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| process.env = { | ||
| ...savedEnv, | ||
| AFJ_AGENT_SPIN_UP: '/apps/agent-provisioning/AFJ/scripts/start_agent.sh', | ||
| AFJ_AGENT_ENDPOINT_PATH: '/apps/agent-provisioning/AFJ/endpoints/', | ||
| SCHEMA_FILE_SERVER_URL: 'https://schema.example', | ||
| AGENT_API_KEY: 'agent-key', | ||
| AWS_ACCOUNT_ID: 'account', | ||
| S3_BUCKET_ARN: 'bucket', | ||
| CLUSTER_NAME: 'cluster', | ||
| TASKDEFINITION_FAMILY: 'family', | ||
| ADMIN_TG_ARN: 'admin-tg', | ||
| INBOUND_TG_ARN: 'inbound-tg', | ||
| FILESYSTEMID: 'filesystem', | ||
| ECS_SUBNET_ID: 'subnet', | ||
| ECS_SECURITY_GROUP_ID: 'security-group' | ||
| }; | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env = savedEnv; | ||
| }); | ||
|
|
||
| it('executes the provisioning script without a shell and returns the generated endpoint', async () => { | ||
| mockExecFile.mockResolvedValue({ stdout: '', stderr: '' }); | ||
| mockReadFile.mockResolvedValue('{"CONTROLLER_ENDPOINT":"https://agent.example"}'); | ||
|
|
||
| await expect(service.walletProvision(payload)).resolves.toEqual({ agentEndPoint: 'https://agent.example' }); | ||
| expect(mockExecFile).toHaveBeenCalledWith( | ||
| expect.stringContaining('/apps/agent-provisioning/AFJ/scripts/start_agent.sh'), | ||
| expect.arrayContaining([payload.orgId, payload.containerName, payload.walletPassword]), | ||
| expect.objectContaining({ timeout: 300000 }) | ||
| ); | ||
| }); | ||
|
|
||
| it('normalizes organization-derived container names before executing the script and reading its endpoint', async () => { | ||
| mockExecFile.mockResolvedValue({ stdout: '', stderr: '' }); | ||
| mockReadFile.mockResolvedValue('{"CONTROLLER_ENDPOINT":"https://agent.example"}'); | ||
|
|
||
| await expect(service.walletProvision({ ...payload, containerName: 'Crédit Agricole, Inc.' })).resolves.toEqual({ | ||
| agentEndPoint: 'https://agent.example' | ||
| }); | ||
| expect(mockExecFile).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.arrayContaining(['Credit_Agricole_Inc']), | ||
| expect.any(Object) | ||
| ); | ||
| expect(mockReadFile).toHaveBeenCalledWith(expect.stringContaining('org-123_Credit_Agricole_Inc.json'), 'utf8'); | ||
| }); | ||
|
|
||
| it('rejects non-string identifiers before executing a script', async () => { | ||
| await expect(service.walletProvision({ ...payload, orgId: 123 as unknown as string })).rejects.toThrow( | ||
| 'orgId contains unsafe characters' | ||
| ); | ||
| expect(mockExecFile).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('uses a configured provisioning timeout', async () => { | ||
| process.env.AFJ_AGENT_PROVISION_TIMEOUT_MS = '600000'; | ||
| mockExecFile.mockResolvedValue({ stdout: '', stderr: '' }); | ||
| mockReadFile.mockResolvedValue('{"CONTROLLER_ENDPOINT":"https://agent.example"}'); | ||
|
|
||
| await service.walletProvision(payload); | ||
| expect(mockExecFile).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.any(Array), | ||
| expect.objectContaining({ timeout: 600000 }) | ||
| ); | ||
| }); | ||
|
|
||
| it.each([{}, 1, [], '', ' '])('rejects invalid CONTROLLER_ENDPOINT values', async (endpoint) => { | ||
| mockExecFile.mockResolvedValue({ stdout: '', stderr: '' }); | ||
| mockReadFile.mockResolvedValue(JSON.stringify({ CONTROLLER_ENDPOINT: endpoint })); | ||
|
|
||
| await expect(service.walletProvision(payload)).rejects.toThrow('Missing CONTROLLER_ENDPOINT'); | ||
| }); | ||
|
|
||
| it('propagates a provisioning script failure instead of attempting to read an endpoint file', async () => { | ||
| const failure = Object.assign(new Error('script failed'), { | ||
| code: 17, | ||
| stdout: 'stdout-secret', | ||
| stderr: 'stderr-secret' | ||
| }); | ||
| mockExecFile.mockRejectedValue(failure); | ||
|
|
||
| await expect(service.walletProvision(payload)).rejects.toThrow('Agent provisioning script failed'); | ||
| expect(mockReadFile).not.toHaveBeenCalled(); | ||
| expect(logger.error).toHaveBeenCalled(); | ||
| expect(JSON.stringify(logger.error.mock.calls)).not.toContain('stdout-secret'); | ||
| expect(JSON.stringify(logger.error.mock.calls)).not.toContain('stderr-secret'); | ||
| expect(JSON.stringify(logger.error.mock.calls)).toContain('exit code 17'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.