credentialVaultLocalClient.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 { createCredentialVaultClient, LocalCredentialVaultClient } from '../../src/server/credentialVault/localClient'
  7. const homes: string[] = [], clients: LocalCredentialVaultClient[] = []
  8. const session = 'a'.repeat(64), password = 'DUMMY-local-master-only', secret = 'DUMMY-local-website-secret'
  9. function client(home = mkdtempSync(join(tmpdir(), 'yaoyao-local-vault-'))) {
  10. if (!homes.includes(home)) homes.push(home)
  11. const value = new LocalCredentialVaultClient(home); clients.push(value)
  12. return { home, value, call: (command: string, body?: unknown, owner = 'dummy-owner', binding = session) => value.call(owner, binding, command, body) }
  13. }
  14. afterEach(async () => { clients.splice(0).forEach(c => c.close()); await Promise.resolve(); homes.splice(0).forEach(h => rmSync(h, { recursive: true, force: true })) })
  15. it('provides encrypted local CRUD by default without an external service or Bot executor', async () => {
  16. const f = client()
  17. expect(await f.call('status')).toMatchObject({ online: true, initialized: false, unlocked: false, execution: 'disabled', storageMode: 'local' })
  18. await f.call('initialize', { password })
  19. await expect(f.call('add', {})).rejects.toMatchObject({ code: 'vault_locked' })
  20. await f.call('unlock', { password })
  21. const entry = await f.call('add', { name: 'Dummy website', username: 'dummy', target: { kind: 'website', origin: 'https://example.test' }, secret })
  22. expect(entry).not.toHaveProperty('secret')
  23. const stored = readdirSync(join(f.home, 'credential-vault')).filter(name => name.endsWith('.vault')).map(name => readFileSync(join(f.home, 'credential-vault', name), 'utf8')).join('')
  24. expect(stored).not.toContain(secret); expect(stored).not.toContain(password); expect(stored).not.toContain('Dummy website')
  25. expect((await f.call('status')).entries).toHaveLength(1)
  26. await expect(f.call('grant', {})).rejects.toMatchObject({ code: 'vault_executor_not_enabled' })
  27. await expect(f.call('execute', {})).rejects.toMatchObject({ code: 'vault_executor_not_enabled' })
  28. await f.call('update', { id: entry.id, entry: { name: 'Updated', username: 'dummy', target: entry.target, revision: entry.revision } })
  29. expect((await f.call('status')).entries[0].name).toBe('Updated')
  30. await f.call('remove', { id: entry.id }); expect((await f.call('status')).entries).toEqual([])
  31. })
  32. it('isolates accounts and login sessions and starts locked after a server restart', async () => {
  33. const f = client(); await f.call('initialize', { password }); await f.call('unlock', { password })
  34. await f.call('add', { name: 'Dummy', username: 'dummy', target: { kind: 'website', origin: 'https://example.test' }, secret })
  35. expect(await f.call('status', undefined, 'another-owner')).toMatchObject({ initialized: false, entries: [] })
  36. expect(await f.call('status', undefined, 'dummy-owner', 'b'.repeat(64))).toMatchObject({ unlocked: false, entries: [] })
  37. await expect(f.call('add', {}, 'dummy-owner', 'b'.repeat(64))).rejects.toMatchObject({ code: 'vault_locked' })
  38. f.value.close(); await Promise.resolve()
  39. const restarted = client(f.home)
  40. expect(await restarted.call('status')).toMatchObject({ initialized: true, unlocked: false, entries: [] })
  41. await restarted.call('unlock', { password }); expect((await restarted.call('status')).entries).toHaveLength(1)
  42. })
  43. it('keeps an explicitly configured but unavailable private broker offline', async () => {
  44. const c = createCredentialVaultClient({ socket: '/missing/socket', tokenFile: '/missing/token', hermesUid: process.getuid!() + 1 }, '/unused')
  45. expect(c).not.toBeInstanceOf(LocalCredentialVaultClient)
  46. await expect(c.call('owner', session, 'status')).rejects.toMatchObject({ code: 'vault_offline' })
  47. c.close()
  48. })
  49. it('prevents concurrent brokers from writing the same local vault', async () => {
  50. const first = client(); await first.call('initialize', { password })
  51. const second = client(first.home)
  52. await expect(second.call('status')).rejects.toMatchObject({ code: 'vault_offline' })
  53. first.value.close(); await Promise.resolve()
  54. expect(await second.call('status')).toMatchObject({ initialized: true, unlocked: false })
  55. })