credentialVaultLocalClient.test.ts 7.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // @vitest-environment node
  2. import { afterEach, expect, it } from 'vitest'
  3. import { mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs'
  4. import { join } from 'node:path'
  5. import { tmpdir } from 'node:os'
  6. import { randomUUID } from 'node:crypto'
  7. import { createCredentialVaultClient, LocalCredentialVaultClient } from '../../src/server/credentialVault/localClient'
  8. import { sshFixture } from '../fixtures/credentialProtocols'
  9. const homes: string[] = [], clients: LocalCredentialVaultClient[] = []
  10. const session = 'a'.repeat(64), password = 'DUMMY-local-master-only', secret = 'DUMMY-local-website-secret'
  11. function client(home = mkdtempSync(join(tmpdir(), 'yaoyao-local-vault-'))) {
  12. if (!homes.includes(home)) homes.push(home)
  13. const value = new LocalCredentialVaultClient(home); clients.push(value)
  14. return { home, value, call: (command: string, body?: unknown, owner = 'dummy-owner', binding = session) => value.call(owner, binding, command, body) }
  15. }
  16. afterEach(async () => { clients.splice(0).forEach(c => c.close()); await Promise.resolve(); homes.splice(0).forEach(h => rmSync(h, { recursive: true, force: true })) })
  17. it('provides encrypted local CRUD and bounded task execution by default without an external service', async () => {
  18. const f = client()
  19. expect(await f.call('status')).toMatchObject({ online: true, initialized: false, unlocked: false, execution: 'local-adapters', reason: 'local_controlled', storageMode: 'local' })
  20. await f.call('initialize', { password })
  21. await expect(f.call('add', {})).rejects.toMatchObject({ code: 'vault_locked' })
  22. await f.call('unlock', { password })
  23. const entry = await f.call('add', { name: 'Dummy website', username: 'dummy', target: { kind: 'website', origin: 'https://example.test' }, secret })
  24. expect(entry).not.toHaveProperty('secret')
  25. const stored = readdirSync(join(f.home, 'credential-vault')).filter(name => name.endsWith('.vault')).map(name => readFileSync(join(f.home, 'credential-vault', name), 'utf8')).join('')
  26. expect(stored).not.toContain(secret); expect(stored).not.toContain(password); expect(stored).not.toContain('Dummy website')
  27. expect((await f.call('status')).entries).toHaveLength(1)
  28. await expect(f.call('read-secret', {id:entry.id})).rejects.toMatchObject({ code: 'vault_command_unknown' })
  29. await f.call('update', { id: entry.id, entry: { name: 'Updated', username: 'dummy', target: entry.target, revision: entry.revision } })
  30. expect((await f.call('status')).entries[0].name).toBe('Updated')
  31. await f.call('remove', { id: entry.id }); expect((await f.call('status')).entries).toEqual([])
  32. })
  33. it('binds one-use local execution to the owner and session and requires a configured usage policy',async()=>{
  34. const f=client();await f.call('initialize',{password});await f.call('unlock',{password})
  35. const entry=await f.call('add',{name:'Dummy',username:'dummy',target:{kind:'website',origin:'https://example.test'},secret})
  36. const input={credentialRef:entry.id,agentId:randomUUID(),workId:randomUUID(),runnerId:randomUUID(),runnerInstance:randomUUID(),runnerEpoch:randomUUID(),operation:'website.login',target:entry.target,seconds:60}
  37. const lease=await f.call('grant',input)
  38. await expect(f.call('execute',{id:lease.id,input},'another-owner')).rejects.toMatchObject({code:'vault_lease_revoked'})
  39. await expect(f.call('execute',{id:lease.id,input},'dummy-owner','b'.repeat(64))).rejects.toMatchObject({code:'vault_scope_forbidden'})
  40. expect(await f.call('execute',{id:lease.id,input})).toEqual({status:'manual_takeover_required',reason:'usage_policy_required',submitted:false})
  41. await expect(f.call('execute',{id:lease.id,input})).rejects.toMatchObject({code:'vault_lease_revoked'})
  42. })
  43. it('saves SSH offline, remembers the first host without authenticating, persists its pin and rejects a changed host before sending a password',async()=>{
  44. const f=client();await f.call('initialize',{password});await f.call('unlock',{password})
  45. let ssh=await sshFixture('password')
  46. try{
  47. const {hostKey:_initialKey,...target}=ssh.target
  48. await ssh.close() // The server is actually offline while the password is saved.
  49. const entry=await f.call('add',{name:'Dummy SSH',username:ssh.username,target,secret:ssh.secret,usage:{kind:'ssh.exec',command:'/usr/bin/uptime'}})
  50. ssh=await sshFixture('password',target.port);const hostKey=ssh.target.hostKey
  51. const input={credentialRef:entry.id,agentId:randomUUID(),workId:randomUUID(),runnerId:randomUUID(),runnerInstance:randomUUID(),runnerEpoch:randomUUID(),operation:'ssh.exec',target,seconds:60}
  52. expect(ssh.state.authMethods).toEqual([])
  53. await expect(f.call('grant',input)).rejects.toMatchObject({code:'vault_host_key_required'})
  54. await expect(f.call('prepare-host',{id:entry.id,revision:entry.revision},'dummy-owner','b'.repeat(64))).rejects.toMatchObject({code:'vault_locked'})
  55. const prepared=await f.call('prepare-host',{id:entry.id,revision:entry.revision})
  56. expect(prepared.target.hostKey).toBe(hostKey);expect(ssh.state.authMethods).toEqual([])
  57. await f.call('lock');await f.call('unlock',{password});expect((await f.call('status')).entries[0].target.hostKey).toBe(hostKey)
  58. const port=target.port;await ssh.close();ssh=await sshFixture('password',port)
  59. expect(ssh.target.hostKey).not.toBe(hostKey)
  60. expect((await f.call('prepare-host',{id:entry.id,revision:prepared.revision})).target.hostKey).toBe(hostKey)
  61. const bound={...input,target:prepared.target},lease=await f.call('grant',bound)
  62. await expect(f.call('execute',{id:lease.id,input:bound})).rejects.toMatchObject({code:'vault_host_key_rejected'})
  63. expect(ssh.state.authMethods).toEqual([]);expect(ssh.state.commands).toEqual([])
  64. expect((await f.call('status')).entries[0].target.hostKey).toBe(hostKey)
  65. }finally{await ssh.close()}
  66. },15000)
  67. it('isolates accounts and login sessions and starts locked after a server restart', async () => {
  68. const f = client(); await f.call('initialize', { password }); await f.call('unlock', { password })
  69. await f.call('add', { name: 'Dummy', username: 'dummy', target: { kind: 'website', origin: 'https://example.test' }, secret })
  70. expect(await f.call('status', undefined, 'another-owner')).toMatchObject({ initialized: false, entries: [] })
  71. expect(await f.call('status', undefined, 'dummy-owner', 'b'.repeat(64))).toMatchObject({ unlocked: false, entries: [] })
  72. await expect(f.call('add', {}, 'dummy-owner', 'b'.repeat(64))).rejects.toMatchObject({ code: 'vault_locked' })
  73. f.value.close(); await Promise.resolve()
  74. const restarted = client(f.home)
  75. expect(await restarted.call('status')).toMatchObject({ initialized: true, unlocked: false, entries: [] })
  76. await restarted.call('unlock', { password }); expect((await restarted.call('status')).entries).toHaveLength(1)
  77. })
  78. it('keeps an explicitly configured but unavailable private broker offline', async () => {
  79. const c = createCredentialVaultClient({ socket: '/missing/socket', tokenFile: '/missing/token', hermesUid: process.getuid!() + 1 }, '/unused')
  80. expect(c).not.toBeInstanceOf(LocalCredentialVaultClient)
  81. await expect(c.call('owner', session, 'status')).rejects.toMatchObject({ code: 'vault_offline' })
  82. c.close()
  83. })
  84. it('prevents concurrent brokers from writing the same local vault', async () => {
  85. const first = client(); await first.call('initialize', { password })
  86. const second = client(first.home)
  87. await expect(second.call('status')).rejects.toMatchObject({ code: 'vault_offline' })
  88. first.value.close(); await Promise.resolve()
  89. expect(await second.call('status')).toMatchObject({ initialized: true, unlocked: false })
  90. })