|
| 1 | +/** |
| 2 | + * Issue #3367: Server can enter unrecoverable auth state where all tokens are invalid. |
| 3 | + * |
| 4 | + * After state directory churn (delete + ag init --force), the server should |
| 5 | + * automatically recover by re-reading keys.json from disk on the next |
| 6 | + * validation failure. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 10 | +import { mkdir, writeFile, rm } from 'node:fs/promises'; |
| 11 | +import { statSync } from 'node:fs'; |
| 12 | +import { join } from 'node:path'; |
| 13 | +import { AuthManager } from '../services/auth/AuthManager.js'; |
| 14 | + |
| 15 | +describe('Issue #3367: Auth state recovery after state directory loss', () => { |
| 16 | + let tmpDir: string; |
| 17 | + let keysFile: string; |
| 18 | + let auth: AuthManager; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + tmpDir = join('/tmp', `test-3367-${Date.now()}-${Math.random().toString(36).slice(2)}`); |
| 22 | + await mkdir(tmpDir, { recursive: true }); |
| 23 | + keysFile = join(tmpDir, 'keys.json'); |
| 24 | + auth = new AuthManager(keysFile, 'master-secret'); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await rm(tmpDir, { recursive: true, force: true }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('reload() picks up new keys after state dir wipe and recreate', async () => { |
| 32 | + // Step 1: Create initial key |
| 33 | + const { key: oldKey } = await auth.createKey('old-key', 100, undefined, 'admin'); |
| 34 | + expect(auth.validate(oldKey).valid).toBe(true); |
| 35 | + |
| 36 | + // Step 2: Delete keys.json (simulate state dir wipe) |
| 37 | + await rm(keysFile); |
| 38 | + |
| 39 | + // Step 3: Old key should still work (in-memory) |
| 40 | + expect(auth.validate(oldKey).valid).toBe(true); |
| 41 | + |
| 42 | + // Step 4: Write new keys.json (simulate ag init --force creating new token) |
| 43 | + const newAuth = new AuthManager(keysFile, 'master-secret'); |
| 44 | + const { key: newKey } = await newAuth.createKey('new-key', 100, undefined, 'admin'); |
| 45 | + await newAuth.save(); |
| 46 | + |
| 47 | + // Step 5: Old key still works (in-memory), new key doesn't |
| 48 | + expect(auth.validate(oldKey).valid).toBe(true); |
| 49 | + expect(auth.validate(newKey).valid).toBe(false); |
| 50 | + |
| 51 | + // Step 6: Reload should pick up new keys |
| 52 | + const reloaded = await auth.reload(); |
| 53 | + expect(reloaded).toBe(true); |
| 54 | + |
| 55 | + // Step 7: New key should now work, old key should not |
| 56 | + expect(auth.validate(newKey).valid).toBe(true); |
| 57 | + expect(auth.validate(oldKey).valid).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('reload() does nothing if keys.json has not changed', async () => { |
| 61 | + await auth.createKey('test-key', 100, undefined, 'admin'); |
| 62 | + await auth.load(); // ensure mtime is tracked |
| 63 | + const reloaded = await auth.reload(); |
| 64 | + expect(reloaded).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('reload() handles missing keys.json gracefully', async () => { |
| 68 | + const { key: existingKey } = await auth.createKey('graceful-key', 100, undefined, 'admin'); |
| 69 | + expect(auth.validate(existingKey).valid).toBe(true); |
| 70 | + await rm(keysFile); |
| 71 | + const reloaded = await auth.reload(); |
| 72 | + expect(reloaded).toBe(false); |
| 73 | + expect(auth.validate(existingKey).valid).toBe(true); |
| 74 | + expect(auth.isHealthy()).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + it('reload() is safe to call concurrently', async () => { |
| 78 | + await auth.createKey('test-key', 100, undefined, 'admin'); |
| 79 | + const results = await Promise.all([auth.reload(), auth.reload(), auth.reload()]); |
| 80 | + expect(results.every(r => typeof r === 'boolean')).toBe(true); |
| 81 | + }); |
| 82 | + |
| 83 | + it('isHealthy() returns false when keys.json missing but keys exist in memory', async () => { |
| 84 | + await auth.createKey('test-key', 100, undefined, 'admin'); |
| 85 | + expect(auth.isHealthy()).toBe(true); |
| 86 | + await rm(keysFile); |
| 87 | + expect(auth.isHealthy()).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + it('isHealthy() returns true when keys.json exists', async () => { |
| 91 | + await auth.createKey('test-key', 100, undefined, 'admin'); |
| 92 | + expect(auth.isHealthy()).toBe(true); |
| 93 | + }); |
| 94 | + |
| 95 | + it('isHealthy() returns true when no keys and no file (fresh start)', () => { |
| 96 | + expect(auth.isHealthy()).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it('save() updates mtime so subsequent reload() is a no-op', async () => { |
| 100 | + await auth.createKey('key1', 100, undefined, 'admin'); |
| 101 | + const mtimeAfterSave = statSync(keysFile).mtimeMs; |
| 102 | + const reloaded = await auth.reload(); |
| 103 | + expect(reloaded).toBe(false); |
| 104 | + }); |
| 105 | +}); |
0 commit comments