|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
14 | | -import { existsSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
| 14 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
15 | 15 | import { join } from 'node:path'; |
16 | 16 | import { tmpdir } from 'node:os'; |
17 | 17 | import { ConnectionCredentialStore } from './connection-credential-store.js'; |
@@ -398,3 +398,143 @@ describe('[#31554] a single node keeps the binding credential in its file', () = |
398 | 398 | expect(existsSync(credPath)).toBe(true); |
399 | 399 | }); |
400 | 400 | }); |
| 401 | + |
| 402 | +/** |
| 403 | + * A single node's credential file follows `OS_HOME`, the home the rest of the |
| 404 | + * instance's state follows (the default database, the uploads root, the |
| 405 | + * install ledger). Under the working directory it sat in the container's own |
| 406 | + * layer, so recreating the container — not restarting it — lost the binding |
| 407 | + * and an online-licensed node fell back to Community until someone bound it |
| 408 | + * again. With `OS_HOME` on the data volume the file now outlives the container. |
| 409 | + */ |
| 410 | +describe('a single node keeps the binding credential under OS_HOME', () => { |
| 411 | + let home: string; |
| 412 | + let project: string; |
| 413 | + let legacyPath: string; |
| 414 | + let homePath: string; |
| 415 | + beforeEach(() => { |
| 416 | + home = join(dir, 'home'); |
| 417 | + project = join(dir, 'project'); |
| 418 | + mkdirSync(project, { recursive: true }); |
| 419 | + legacyPath = join(project, '.objectstack', 'cloud-connection.json'); |
| 420 | + homePath = join(home, 'cloud-connection.json'); |
| 421 | + vi.spyOn(process, 'cwd').mockReturnValue(project); |
| 422 | + vi.stubEnv('OS_HOME', home); |
| 423 | + vi.stubEnv('OS_INTERNAL_FRESH_HOME', undefined); |
| 424 | + }); |
| 425 | + afterEach(() => { vi.unstubAllEnvs(); vi.restoreAllMocks(); }); |
| 426 | + |
| 427 | + /** A plugin on the default credential path, booted the way the kernel boots it. */ |
| 428 | + async function boot(config: Record<string, unknown> = {}) { |
| 429 | + const rawApp = makeRawApp(); |
| 430 | + const registered = new Map<string, unknown>(); |
| 431 | + const { ctx, fire } = makeCtx(rawApp, { auth: sessionAuth('admin'), manifest: { register: vi.fn() } }); |
| 432 | + (ctx as any).registerService = (name: string, svc: unknown) => registered.set(name, svc); |
| 433 | + const plugin = new CloudConnectionPlugin({ singleEnvironment: true, environmentId: 'env_1', controlPlaneUrl: 'http://cloud.test', ...config }); |
| 434 | + await plugin.init(ctx as any); |
| 435 | + await plugin.start(ctx as any); |
| 436 | + await fire(); |
| 437 | + const reader = registered.get(CLOUD_BINDING_CREDENTIAL_SERVICE) as CloudBindingCredentialReader; |
| 438 | + return { rawApp, info: ctx.logger.info, error: ctx.logger.error, reader }; |
| 439 | + } |
| 440 | + |
| 441 | + async function bind(rawApp: ReturnType<typeof makeRawApp>) { |
| 442 | + vi.stubGlobal('fetch', vi.fn(async (url: any) => { |
| 443 | + const u = String(url); |
| 444 | + if (u.includes('/auth/device/token')) return new Response(JSON.stringify({ access_token: 'op-token' }), { status: 200 }); |
| 445 | + if (u.includes('/cloud-connection/bind')) { |
| 446 | + return new Response(JSON.stringify({ success: true, data: { bound: true, runtime_id: 'rt-1', runtime_token: 'oscc_minted', connection: { organization_id: 'org_x' } } }), { status: 200 }); |
| 447 | + } |
| 448 | + throw new Error(`unexpected fetch ${u}`); |
| 449 | + })); |
| 450 | + const res = await rawApp.routes.get('POST /api/v1/cloud-connection/bind/poll')!(makeC('http://localhost:3000/x', { device_code: 'dc' })); |
| 451 | + expect(res.payload.success).toBe(true); |
| 452 | + } |
| 453 | + |
| 454 | + function writeLegacy(token: string) { |
| 455 | + new ConnectionCredentialStore(legacyPath).write({ runtimeToken: token, runtimeId: 'rt-old', environmentId: 'env_1' }); |
| 456 | + return readFileSync(legacyPath, 'utf8'); |
| 457 | + } |
| 458 | + |
| 459 | + function movedLines(info: ReturnType<typeof vi.fn>) { |
| 460 | + return info.mock.calls.map(([m]) => String(m)).filter((m) => m.includes(legacyPath)); |
| 461 | + } |
| 462 | + |
| 463 | + it('resolves the default store under OS_HOME, and a bind writes the credential there, 0600', async () => { |
| 464 | + expect(new ConnectionCredentialStore().path).toBe(homePath); |
| 465 | + const { rawApp, reader } = await boot(); |
| 466 | + await bind(rawApp); |
| 467 | + expect(new ConnectionCredentialStore(homePath).read()?.runtimeToken).toBe('oscc_minted'); |
| 468 | + expect(statSync(homePath).mode & 0o777).toBe(0o600); |
| 469 | + expect(existsSync(legacyPath)).toBe(false); |
| 470 | + expect(reader.read()?.runtimeToken).toBe('oscc_minted'); |
| 471 | + }); |
| 472 | + |
| 473 | + it('moves the working-directory file an earlier release wrote, once: 0600, one log line, nothing left behind', async () => { |
| 474 | + const bytes = writeLegacy('oscc_legacy'); |
| 475 | + const first = await boot(); |
| 476 | + expect(readFileSync(homePath, 'utf8')).toBe(bytes); |
| 477 | + expect(statSync(homePath).mode & 0o777).toBe(0o600); |
| 478 | + expect(existsSync(legacyPath)).toBe(false); |
| 479 | + expect(first.reader.read()?.runtimeToken).toBe('oscc_legacy'); |
| 480 | + const lines = movedLines(first.info); |
| 481 | + expect(lines).toHaveLength(1); |
| 482 | + expect(lines[0]).toContain(homePath); |
| 483 | + expect(lines[0]).not.toContain('oscc_legacy'); |
| 484 | + expect(first.error).not.toHaveBeenCalled(); |
| 485 | + |
| 486 | + // The next boot has nothing to move and says nothing about it. |
| 487 | + const second = await boot(); |
| 488 | + expect(movedLines(second.info)).toHaveLength(0); |
| 489 | + expect(readFileSync(homePath, 'utf8')).toBe(bytes); |
| 490 | + expect(second.reader.read()?.runtimeToken).toBe('oscc_legacy'); |
| 491 | + }); |
| 492 | + |
| 493 | + it('keeps the OS_HOME file when both exist, and leaves the working-directory file untouched', async () => { |
| 494 | + const legacyBytes = writeLegacy('oscc_legacy'); |
| 495 | + new ConnectionCredentialStore(homePath).write({ runtimeToken: 'oscc_home', runtimeId: 'rt-home' }); |
| 496 | + const homeBytes = readFileSync(homePath, 'utf8'); |
| 497 | + const { info, reader } = await boot(); |
| 498 | + expect(reader.read()?.runtimeToken).toBe('oscc_home'); |
| 499 | + expect(readFileSync(homePath, 'utf8')).toBe(homeBytes); |
| 500 | + expect(readFileSync(legacyPath, 'utf8')).toBe(legacyBytes); |
| 501 | + expect(statSync(legacyPath).mode & 0o777).toBe(0o600); |
| 502 | + expect(movedLines(info)).toHaveLength(0); |
| 503 | + }); |
| 504 | + |
| 505 | + it('control: with OS_HOME unset the file stays where it always was, and nothing moves', async () => { |
| 506 | + vi.stubEnv('OS_HOME', undefined); |
| 507 | + expect(new ConnectionCredentialStore().path).toBe(legacyPath); |
| 508 | + const bytes = writeLegacy('oscc_legacy'); |
| 509 | + const { info, reader } = await boot(); |
| 510 | + expect(readFileSync(legacyPath, 'utf8')).toBe(bytes); |
| 511 | + expect(existsSync(homePath)).toBe(false); |
| 512 | + expect(reader.read()?.runtimeToken).toBe('oscc_legacy'); |
| 513 | + expect(movedLines(info)).toHaveLength(0); |
| 514 | + }); |
| 515 | + |
| 516 | + it('`os dev --fresh`: the credential stays inside the ephemeral home, and the project file is neither moved nor read', async () => { |
| 517 | + vi.stubEnv('OS_INTERNAL_FRESH_HOME', home); |
| 518 | + const bytes = writeLegacy('oscc_legacy'); |
| 519 | + expect(new ConnectionCredentialStore().path).toBe(homePath); |
| 520 | + const { rawApp, info, reader } = await boot(); |
| 521 | + expect(reader.read()).toBeNull(); |
| 522 | + expect(existsSync(homePath)).toBe(false); |
| 523 | + expect(movedLines(info)).toHaveLength(0); |
| 524 | + await bind(rawApp); |
| 525 | + expect(new ConnectionCredentialStore(homePath).read()?.runtimeToken).toBe('oscc_minted'); |
| 526 | + expect(readFileSync(legacyPath, 'utf8')).toBe(bytes); |
| 527 | + }); |
| 528 | + |
| 529 | + it('an explicit `credentialPath` keeps its precedence over OS_HOME, and moves nothing into it', async () => { |
| 530 | + const explicit = join(dir, 'srv', 'cloud-connection.json'); |
| 531 | + const bytes = writeLegacy('oscc_legacy'); |
| 532 | + const { rawApp, info } = await boot({ credentialPath: explicit }); |
| 533 | + expect(existsSync(explicit)).toBe(false); |
| 534 | + expect(movedLines(info)).toHaveLength(0); |
| 535 | + await bind(rawApp); |
| 536 | + expect(new ConnectionCredentialStore(explicit).read()?.runtimeToken).toBe('oscc_minted'); |
| 537 | + expect(existsSync(homePath)).toBe(false); |
| 538 | + expect(readFileSync(legacyPath, 'utf8')).toBe(bytes); |
| 539 | + }); |
| 540 | +}); |
0 commit comments