@open-mercato/starter 0.10.1-develop.8115.1.f93e1af159 → 0.10.1-develop.8123.1.acf6ca6db8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/__tests__/database-write-guard.test.mjs +1 -0
- package/src/__tests__/encryption-preflight.pg.test.mjs +168 -0
- package/src/__tests__/encryption-preflight.test.mjs +299 -0
- package/src/__tests__/setup-streaming.test.mjs +3 -0
- package/src/__tests__/starter.test.mjs +10 -1
- package/src/encryption-preflight.mjs +261 -0
- package/src/env-setup.mjs +32 -4
- package/src/service-modes.mjs +1 -1
- package/src/steps.mjs +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/starter",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8123.1.acf6ca6db8",
|
|
4
4
|
"description": "One-command, cross-platform bootstrap for Open Mercato dev environments: prerequisite doctor, corporate TLS trust, idempotent setup steps, and a supervised hybrid or fully containerized stack.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -96,6 +96,7 @@ test('the starter passes the authorized immutable connection snapshot to migrati
|
|
|
96
96
|
const context = {
|
|
97
97
|
repoRoot: dir, env: {}, flags: { noInfra: true, remoteDatabaseArgs: ['--allow-remote-database=staging.example:6543/app'] }, log() {},
|
|
98
98
|
dbMarker: 'init-fixture', dbMigrationsMarker: 'migrations-fixture', migrationsFingerprint: 'fixture',
|
|
99
|
+
assertEncryptionCompatibleImpl: async ({ env }) => ({ status: 'verified', settings: env }),
|
|
99
100
|
assertDatabaseWriteAllowedImpl: options => assertDatabaseWriteAllowed({ ...options, loadModule: () => pg, input: {}, output: {} }),
|
|
100
101
|
runStreamingImpl(_command, args, options) {
|
|
101
102
|
commands.push({ args, databaseUrl: options.env.DATABASE_URL })
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { fileURLToPath } from 'node:url'
|
|
6
|
+
import { test } from 'node:test'
|
|
7
|
+
import { createServer } from 'node:http'
|
|
8
|
+
import { spawnSync } from 'node:child_process'
|
|
9
|
+
import { GenericContainer, Wait } from 'testcontainers'
|
|
10
|
+
import pg from 'pg'
|
|
11
|
+
import { assertEncryptionCompatible } from '../encryption-preflight.mjs'
|
|
12
|
+
import { createKmsService } from '@open-mercato/shared/lib/encryption/kms'
|
|
13
|
+
import { encryptWithAesGcm } from '@open-mercato/shared/lib/encryption/aes'
|
|
14
|
+
import { EnvMasterKeyKmsAdapter, FormsEncryptionService } from '@open-mercato/core/modules/forms/services/encryption-service'
|
|
15
|
+
|
|
16
|
+
const repository = fileURLToPath(new URL('../../../..', import.meta.url))
|
|
17
|
+
const tenant = '00000000-0000-4000-8000-000000000001'
|
|
18
|
+
const organization = '00000000-0000-4000-8000-000000000002'
|
|
19
|
+
|
|
20
|
+
test('disposable PostgreSQL: actual isolated helpers prove keys and rejected reuse never changes stored bytes', { skip: process.env.OM_PG_INTEGRATION !== '1', timeout: 180_000 }, async () => {
|
|
21
|
+
const container = await new GenericContainer('postgres:16-alpine')
|
|
22
|
+
.withEnvironment({ POSTGRES_PASSWORD: 'disposable-fixture', POSTGRES_DB: 'fixture' })
|
|
23
|
+
.withExposedPorts({ container: 5432, host: 0 })
|
|
24
|
+
.withWaitStrategy(Wait.forLogMessage('database system is ready to accept connections', 2))
|
|
25
|
+
.withStartupTimeout(60_000).start()
|
|
26
|
+
const projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-preflight-standalone-'))
|
|
27
|
+
const originalEnv = { ...process.env }
|
|
28
|
+
let client
|
|
29
|
+
let vault
|
|
30
|
+
try {
|
|
31
|
+
fs.writeFileSync(path.join(projectDir, 'package.json'), '{"private":true,"type":"module"}')
|
|
32
|
+
fs.symlinkSync(path.join(repository, 'node_modules'), path.join(projectDir, 'node_modules'), 'dir')
|
|
33
|
+
const url = `postgres://postgres:disposable-fixture@${container.getHost()}:${container.getMappedPort(5432)}/fixture`
|
|
34
|
+
const env = { ...process.env, DATABASE_URL: url, TENANT_DATA_ENCRYPTION: 'yes', TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'original-fixture-key', TENANT_DATA_ENCRYPTION_KEY: '', ALLOW_DERIVED_KMS_FALLBACK: 'false', VAULT_ADDR: '', VAULT_TOKEN: '', NODE_ENV: 'production' }
|
|
35
|
+
const run = (changes = {}, project = projectDir) => assertEncryptionCompatible({ projectDir: project, env: { ...env, ...changes } })
|
|
36
|
+
client = new pg.Client({ connectionString: url }); await client.connect()
|
|
37
|
+
assert.equal((await run()).status, 'empty')
|
|
38
|
+
await assert.rejects(run({ DATABASE_URL: '' }), /writes are blocked/)
|
|
39
|
+
await client.query('CREATE TABLE mikro_orm_migrations_fixture (name text)')
|
|
40
|
+
await client.query("INSERT INTO mikro_orm_migrations_fixture VALUES ('new-migration')")
|
|
41
|
+
assert.equal((await run()).status, 'empty')
|
|
42
|
+
await client.query('CREATE TABLE protected_fixture (tenant_id uuid, value text, doc jsonb)')
|
|
43
|
+
assert.equal((await run()).status, 'empty')
|
|
44
|
+
await client.query('INSERT INTO protected_fixture VALUES ($1, $2, $3)', [tenant, 'plaintext', '{}'])
|
|
45
|
+
assert.equal((await run()).status, 'plaintext-only')
|
|
46
|
+
await assert.rejects(run({ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '' }), /writes are blocked/)
|
|
47
|
+
await assert.rejects(run({ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '', VAULT_ADDR: 'http://127.0.0.1:1', VAULT_TOKEN: 'unavailable-fixture-token' }), /writes are blocked/)
|
|
48
|
+
assert.equal((await run({ TENANT_DATA_ENCRYPTION: 'off', TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '' })).status, 'plaintext-only')
|
|
49
|
+
process.env = { ...env }
|
|
50
|
+
const kms = createKmsService()
|
|
51
|
+
const dek = await kms.getTenantDek(tenant)
|
|
52
|
+
const sealed = encryptWithAesGcm('private-existing-data', dek.key).raw
|
|
53
|
+
const nested = encryptWithAesGcm(JSON.stringify({ inner: sealed }), dek.key).raw
|
|
54
|
+
await client.query('UPDATE protected_fixture SET value = $1, doc = $2', [sealed, JSON.stringify({ nested: [nested] })])
|
|
55
|
+
const before = (await client.query('SELECT row_to_json(record) AS record FROM protected_fixture record')).rows
|
|
56
|
+
assert.equal((await run()).status, 'verified')
|
|
57
|
+
assert.equal((await run({}, path.join(repository, 'apps', 'mercato'))).status, 'verified')
|
|
58
|
+
await client.query('CREATE TABLE document_contents (tenant_id uuid, yjs_state bytea, content_text text)')
|
|
59
|
+
await client.query('CREATE TABLE document_versions (tenant_id uuid, yjs_snapshot bytea)')
|
|
60
|
+
await client.query('INSERT INTO document_contents VALUES ($1, $2, $3)', [tenant, Buffer.from([1, 2, 3]), 'urn:om:sku:v2'])
|
|
61
|
+
await client.query('INSERT INTO document_versions VALUES ($1, $2)', [tenant, Buffer.from([4, 5, 6])])
|
|
62
|
+
assert.equal((await run()).status, 'verified')
|
|
63
|
+
await client.query('UPDATE document_contents SET content_text = $1', [sealed])
|
|
64
|
+
assert.equal((await run()).status, 'verified')
|
|
65
|
+
await client.query('CREATE TABLE audit_fixture (tenant_id uuid, message text, protected_value text)')
|
|
66
|
+
await client.query("INSERT INTO audit_fixture SELECT $1::uuid, repeat('audit', 150), NULL::text FROM generate_series(1, 150000)", [tenant])
|
|
67
|
+
assert.equal((await run()).status, 'verified')
|
|
68
|
+
await client.query('UPDATE audit_fixture SET protected_value = $1', [sealed])
|
|
69
|
+
assert.equal((await run()).status, 'verified')
|
|
70
|
+
await client.query('DROP TABLE audit_fixture')
|
|
71
|
+
|
|
72
|
+
for (const changes of [
|
|
73
|
+
{ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'wrong-fixture-key' },
|
|
74
|
+
{ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '' },
|
|
75
|
+
{ TENANT_DATA_ENCRYPTION: 'off' },
|
|
76
|
+
{ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '', VAULT_ADDR: 'http://127.0.0.1:1', VAULT_TOKEN: 'unavailable-fixture-token' },
|
|
77
|
+
]) await assert.rejects(run(changes), /writes are blocked/)
|
|
78
|
+
const requests = []
|
|
79
|
+
let vaultStatus = 200
|
|
80
|
+
vault = createServer((request, response) => {
|
|
81
|
+
requests.push(request.method)
|
|
82
|
+
response.writeHead(vaultStatus, { 'content-type': 'application/json' })
|
|
83
|
+
response.end(JSON.stringify({ data: { data: vaultStatus === 200 ? { key: dek.key } : {} } }))
|
|
84
|
+
})
|
|
85
|
+
await new Promise(resolve => vault.listen(0, '127.0.0.1', resolve))
|
|
86
|
+
const vaultEnv = { TENANT_DATA_ENCRYPTION_FALLBACK_KEY: '', VAULT_ADDR: `http://127.0.0.1:${vault.address().port}`, VAULT_TOKEN: 'read-only-fixture-token' }
|
|
87
|
+
assert.equal((await run(vaultEnv)).status, 'verified')
|
|
88
|
+
for (const status of [403, 404, 503]) {
|
|
89
|
+
vaultStatus = status
|
|
90
|
+
await assert.rejects(run(vaultEnv), /writes are blocked/)
|
|
91
|
+
}
|
|
92
|
+
assert.ok(requests.length >= 4)
|
|
93
|
+
assert.ok(requests.every(method => method === 'GET'))
|
|
94
|
+
assert.deepEqual((await client.query('SELECT row_to_json(record) AS record FROM protected_fixture record')).rows, before)
|
|
95
|
+
await client.query('CREATE TABLE onboarding_requests (tenant_id uuid, email text)')
|
|
96
|
+
const systemDek = await kms.getTenantDek('system:onboarding:onboarding_request')
|
|
97
|
+
await client.query('INSERT INTO onboarding_requests VALUES ($1, $2)', [tenant, encryptWithAesGcm('system-existing-data', systemDek.key).raw])
|
|
98
|
+
assert.equal((await run()).status, 'verified')
|
|
99
|
+
await client.query('CREATE TABLE forms_encryption_key (organization_id uuid, key_version int, wrapped_dek bytea)')
|
|
100
|
+
await client.query('CREATE TABLE forms_form_submission_revision (organization_id uuid, data bytea)')
|
|
101
|
+
const masterKey = Buffer.alloc(32, 8)
|
|
102
|
+
const formsDek = Buffer.alloc(32, 9)
|
|
103
|
+
const adapter = new EnvMasterKeyKmsAdapter(masterKey)
|
|
104
|
+
const wrappedDek = await adapter.wrap(formsDek)
|
|
105
|
+
await client.query('INSERT INTO forms_encryption_key VALUES ($1, 1, $2)', [organization, wrappedDek])
|
|
106
|
+
const forms = new FormsEncryptionService({ kmsAdapter: adapter, emFactory: () => ({ findOne: async () => ({ keyVersion: 1, wrappedDek }) }) })
|
|
107
|
+
const encrypted = await forms.encrypt(organization, Buffer.from('existing-revision'))
|
|
108
|
+
await client.query('INSERT INTO forms_form_submission_revision VALUES ($1, $2)', [organization, encrypted])
|
|
109
|
+
const formsEnv = { FORMS_ENCRYPTION_MASTER_KEY: masterKey.toString('base64') }
|
|
110
|
+
assert.equal((await run(formsEnv)).status, 'verified')
|
|
111
|
+
await assert.rejects(run({ FORMS_ENCRYPTION_MASTER_KEY: Buffer.alloc(32, 7).toString('base64') }), /writes are blocked/)
|
|
112
|
+
await assert.rejects(run({ FORMS_ENCRYPTION_MASTER_KEY: '', FORMS_ENCRYPTION_KMS_KEY_ID: '' }), /writes are blocked/)
|
|
113
|
+
const stored = (await client.query('SELECT data FROM forms_form_submission_revision')).rows[0].data
|
|
114
|
+
assert.deepEqual(stored, encrypted)
|
|
115
|
+
const envBefore = 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=wrong-fixture-key\n'
|
|
116
|
+
fs.writeFileSync(path.join(projectDir, '.env'), envBefore)
|
|
117
|
+
await assert.rejects(run({ TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'wrong-fixture-key' }), /writes are blocked/)
|
|
118
|
+
assert.equal(fs.readFileSync(path.join(projectDir, '.env'), 'utf8'), envBefore)
|
|
119
|
+
const { databaseStep } = await import('../steps.mjs')
|
|
120
|
+
const appDir = path.join(projectDir, 'apps', 'mercato')
|
|
121
|
+
fs.mkdirSync(appDir, { recursive: true })
|
|
122
|
+
const appEnv = `DATABASE_URL=${url}\nTENANT_DATA_ENCRYPTION_FALLBACK_KEY=wrong-fixture-key\n`
|
|
123
|
+
fs.writeFileSync(path.join(appDir, '.env'), appEnv)
|
|
124
|
+
const mutations = []
|
|
125
|
+
await assert.rejects(databaseStep.apply({
|
|
126
|
+
repoRoot: projectDir, env: { ...env, TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'wrong-fixture-key' }, flags: {}, log() {},
|
|
127
|
+
runStreamingImpl(...args) { mutations.push(args); throw new Error('mutation must not launch') },
|
|
128
|
+
}), /writes are blocked/)
|
|
129
|
+
assert.deepEqual(mutations, [])
|
|
130
|
+
const guardMarker = path.join(projectDir, 'guarded-mutation')
|
|
131
|
+
const command = `node -e 'require("node:fs").writeFileSync(process.env.TEST_GUARD_MARKER,"ran")'`
|
|
132
|
+
const containerRun = spawnSync('sh', [path.join(repository, 'docker', 'scripts', 'init-or-migrate.sh')], {
|
|
133
|
+
cwd: appDir,
|
|
134
|
+
env: { ...env, TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'wrong-fixture-key', INIT_MARKER_FILE: path.join(projectDir, 'seed-marker'), INIT_COMMAND: command, MIGRATE_COMMAND: command, SYNC_ROLE_ACLS_COMMAND: command, TEST_GUARD_MARKER: guardMarker },
|
|
135
|
+
encoding: 'utf8', timeout: 30_000,
|
|
136
|
+
})
|
|
137
|
+
assert.notEqual(containerRun.status, 0)
|
|
138
|
+
assert.match(containerRun.stdout, /writes are blocked/)
|
|
139
|
+
assert.equal(fs.existsSync(guardMarker), false)
|
|
140
|
+
assert.equal(fs.existsSync(path.join(projectDir, 'seed-marker')), false)
|
|
141
|
+
const imageScripts = path.join(projectDir, 'docker', 'scripts')
|
|
142
|
+
fs.mkdirSync(imageScripts, { recursive: true })
|
|
143
|
+
fs.copyFileSync(path.join(repository, 'docker', 'scripts', 'init-or-migrate.sh'), path.join(imageScripts, 'init-or-migrate.sh'))
|
|
144
|
+
fs.copyFileSync(path.join(repository, 'packages', 'starter', 'src', 'encryption-preflight.mjs'), path.join(imageScripts, 'encryption-preflight.mjs'))
|
|
145
|
+
const linkedScripts = path.join(projectDir, 'linked-scripts')
|
|
146
|
+
fs.symlinkSync(imageScripts, linkedScripts, 'junction')
|
|
147
|
+
const acceptedContainer = spawnSync('sh', [path.join(linkedScripts, 'init-or-migrate.sh')], {
|
|
148
|
+
cwd: appDir,
|
|
149
|
+
env: { ...env, ...formsEnv, INIT_MARKER_FILE: path.join(projectDir, 'seed-marker'), INIT_COMMAND: command, MIGRATE_COMMAND: command, SYNC_ROLE_ACLS_COMMAND: command, TEST_GUARD_MARKER: guardMarker },
|
|
150
|
+
encoding: 'utf8', timeout: 30_000,
|
|
151
|
+
})
|
|
152
|
+
assert.equal(acceptedContainer.status, 0, acceptedContainer.stdout + acceptedContainer.stderr)
|
|
153
|
+
assert.equal(fs.readFileSync(guardMarker, 'utf8'), 'ran')
|
|
154
|
+
assert.equal(fs.existsSync(path.join(projectDir, 'seed-marker')), true)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
assert.equal(fs.readFileSync(path.join(appDir, '.env'), 'utf8'), appEnv)
|
|
158
|
+
assert.deepEqual((await client.query('SELECT data FROM forms_form_submission_revision')).rows[0].data, encrypted)
|
|
159
|
+
|
|
160
|
+
assert.equal((await run({ DATABASE_URL: url.replace(/fixture$/, 'missing_fixture') })).status, 'empty')
|
|
161
|
+
} finally {
|
|
162
|
+
process.env = originalEnv
|
|
163
|
+
if (vault) await new Promise(resolve => vault.close(resolve))
|
|
164
|
+
if (client) await client.end()
|
|
165
|
+
fs.rmSync(projectDir, { recursive: true, force: true })
|
|
166
|
+
await container.stop()
|
|
167
|
+
}
|
|
168
|
+
})
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { test } from 'node:test'
|
|
6
|
+
import { EventEmitter } from 'node:events'
|
|
7
|
+
import { spawnSync } from 'node:child_process'
|
|
8
|
+
import { fileURLToPath } from 'node:url'
|
|
9
|
+
import { assertEncryptionCompatible, inspectEncryptionCompatibility, runEncryptionGuardedSetupCommand } from '../encryption-preflight.mjs'
|
|
10
|
+
import { encryptWithAesGcm, decryptWithAesGcm } from '@open-mercato/shared/lib/encryption/aes'
|
|
11
|
+
|
|
12
|
+
const correct = Buffer.alloc(32, 1).toString('base64')
|
|
13
|
+
const wrong = Buffer.alloc(32, 2).toString('base64')
|
|
14
|
+
const tenant = 'scope-fixture'
|
|
15
|
+
const sealed = encryptWithAesGcm('private-fixture', correct).raw
|
|
16
|
+
|
|
17
|
+
test('symlinked helper entrypoints reject incompatible configuration instead of silently succeeding', () => {
|
|
18
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'om-preflight-symlink-'))
|
|
19
|
+
try {
|
|
20
|
+
const scripts = [
|
|
21
|
+
new URL('../encryption-preflight.mjs', import.meta.url),
|
|
22
|
+
new URL('../../../../scripts/dev-encryption-preflight.mjs', import.meta.url),
|
|
23
|
+
new URL('../../../create-app/template/scripts/dev-encryption-preflight.mjs', import.meta.url),
|
|
24
|
+
]
|
|
25
|
+
for (const [index, script] of scripts.entries()) {
|
|
26
|
+
const scriptPath = fileURLToPath(script)
|
|
27
|
+
const linkedDirectory = path.join(directory, `helper-${index}`)
|
|
28
|
+
fs.symlinkSync(path.dirname(scriptPath), linkedDirectory, 'junction')
|
|
29
|
+
const link = path.join(linkedDirectory, path.basename(scriptPath))
|
|
30
|
+
for (const [mode, expectedStatus] of [['--encryption-probe', 1], ['--run-setup-command', 78]]) {
|
|
31
|
+
const result = spawnSync(process.execPath, [link, mode, 'exit 91'], {
|
|
32
|
+
cwd: directory,
|
|
33
|
+
env: { ...process.env, DATABASE_URL: '' },
|
|
34
|
+
encoding: 'utf8',
|
|
35
|
+
timeout: 10_000,
|
|
36
|
+
})
|
|
37
|
+
assert.equal(result.status, expectedStatus, result.stderr)
|
|
38
|
+
if (mode === '--run-setup-command') assert.match(result.stderr, /database writes are blocked/)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} finally { fs.rmSync(directory, { recursive: true, force: true }) }
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
function fixture(records, options = {}) {
|
|
45
|
+
const queries = []
|
|
46
|
+
let fetched = false
|
|
47
|
+
const client = { async query(sql) {
|
|
48
|
+
queries.push(sql)
|
|
49
|
+
if (sql.includes('FROM pg_class')) {
|
|
50
|
+
const base = { schema: 'public', name: options.table ?? 'fixture', kind: 'r', rls: false }
|
|
51
|
+
return { rows: [{ ...base, column: 'value', type: 'text', ...options.metadata }, ...(records.some(record => record.tenant_id) ? [{ ...base, column: 'tenant_id', type: 'uuid' }] : [])] }
|
|
52
|
+
}
|
|
53
|
+
if (sql.startsWith('SELECT EXISTS')) return { rows: [{ populated: records.length > 0 }] }
|
|
54
|
+
if (sql.startsWith('SELECT DISTINCT')) return { rows: [...new Set(records.map(record => record.tenant_id).filter(Boolean))].map(scope => ({ scope })) }
|
|
55
|
+
if (sql.startsWith('FETCH')) { const rows = fetched ? [] : records.map(record => ({ record: structuredClone(record) })); fetched = true; return { rows } }
|
|
56
|
+
return { rows: [] }
|
|
57
|
+
} }
|
|
58
|
+
const keyReads = []
|
|
59
|
+
const kms = {
|
|
60
|
+
isHealthy: () => true,
|
|
61
|
+
async getTenantDek(keyId) { keyReads.push(keyId); return { key: correct } },
|
|
62
|
+
async createTenantDek() { assert.fail('preflight must never create a key') },
|
|
63
|
+
}
|
|
64
|
+
return { client, kms, decrypt: decryptWithAesGcm, resolveKeyId: entityId => `system:${entityId}`, queries, keyReads }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
test('authenticates actual AES envelopes and nested JSON without any SQL or KMS mutation', async () => {
|
|
68
|
+
const nested = encryptWithAesGcm(JSON.stringify({ inner: sealed }), correct).raw
|
|
69
|
+
const input = fixture([{ tenant_id: tenant, doc: [{ secret: nested }] }])
|
|
70
|
+
assert.equal(await inspectEncryptionCompatibility(input), 'verified')
|
|
71
|
+
assert.deepEqual(input.keyReads, [tenant])
|
|
72
|
+
assert.equal(input.queries.at(-1), 'ROLLBACK')
|
|
73
|
+
assert.ok(input.queries.includes('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY'))
|
|
74
|
+
assert.ok(input.queries.every(sql => !/\b(INSERT|UPDATE|DELETE|CREATE|ALTER|DROP)\b/.test(sql)))
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('wrong keys, missing keys and unavailable KMS all reject with transaction rollback', async () => {
|
|
78
|
+
for (const getTenantDek of [async () => ({ key: wrong }), async () => null, async () => { throw new Error('secret-should-not-escape') }]) {
|
|
79
|
+
const input = fixture([{ tenant_id: tenant, value: sealed }])
|
|
80
|
+
input.kms.getTenantDek = getTenantDek
|
|
81
|
+
await assert.rejects(inspectEncryptionCompatibility(input))
|
|
82
|
+
assert.equal(input.queries.at(-1), 'ROLLBACK')
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('system-scoped onboarding keeps its system key after tenant assignment', async () => {
|
|
87
|
+
const input = fixture([{ tenant_id: tenant, value: sealed }], { table: 'onboarding_requests' })
|
|
88
|
+
assert.equal(await inspectEncryptionCompatibility(input), 'verified')
|
|
89
|
+
assert.deepEqual(input.keyReads, ['system:onboarding:onboarding_request'])
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('empty, plaintext-only and unavailable KMS have distinct safe outcomes', async () => {
|
|
93
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([])), 'empty')
|
|
94
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([{ tenant_id: tenant, value: 'plain' } ])), 'plaintext-only')
|
|
95
|
+
const unavailable = fixture([{ tenant_id: tenant, value: 'plain' }])
|
|
96
|
+
unavailable.kms.getTenantDek = async () => null
|
|
97
|
+
await assert.rejects(inspectEncryptionCompatibility(unavailable))
|
|
98
|
+
assert.equal(await inspectEncryptionCompatibility({ ...unavailable, ...fixture([{ value: 'plain' }]), kms: unavailable.kms, encryptionEnabled: false }), 'plaintext-only')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('unknown versions, malformed ciphertext, unknown scope, incomplete scans and RLS fail closed', async () => {
|
|
102
|
+
for (const input of [
|
|
103
|
+
fixture([{ tenant_id: tenant, value: sealed.replace(/v1$/, 'v2') }]),
|
|
104
|
+
fixture([{ tenant_id: tenant, value: 'AAAAAAAAAAAAAAAA:badpayload:AAAAAAAAAAAAAAAAAAAAAA==:v1' }]),
|
|
105
|
+
fixture([{ value: sealed }]),
|
|
106
|
+
{ ...fixture([{ tenant_id: tenant, value: sealed }]), maxRows: 0 },
|
|
107
|
+
{ ...fixture([{ tenant_id: tenant, value: sealed }]), maxBytes: 0 },
|
|
108
|
+
fixture([], { metadata: { rls: true } }),
|
|
109
|
+
fixture([], { metadata: { kind: 'f' } }),
|
|
110
|
+
fixture([{ value: '\\x0102' }], { metadata: { type: 'bytea' } }),
|
|
111
|
+
]) await assert.rejects(inspectEncryptionCompatibility(input))
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('wrapper redacts all provider errors and preserves environment bytes on rejection', async () => {
|
|
115
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-encryption-redaction-'))
|
|
116
|
+
try {
|
|
117
|
+
const file = path.join(dir, '.env')
|
|
118
|
+
const before = 'TENANT_DATA_ENCRYPTION_KEY=fixture-secret\n'
|
|
119
|
+
fs.writeFileSync(file, before)
|
|
120
|
+
await assert.rejects(assertEncryptionCompatible({ projectDir: dir, env: {}, runProbe: async () => { throw new Error('fixture-secret https://token@vault.example') } }), error => {
|
|
121
|
+
assert.ok(!error.message.includes('fixture-secret'))
|
|
122
|
+
assert.ok(!error.message.includes('vault.example'))
|
|
123
|
+
return /writes are blocked/.test(error.message)
|
|
124
|
+
})
|
|
125
|
+
assert.equal(fs.readFileSync(file, 'utf8'), before)
|
|
126
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('starter rejects incompatible reuse before migration, initialize, marker or env writes', async () => {
|
|
130
|
+
const { databaseStep } = await import('../steps.mjs')
|
|
131
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-encryption-step-'))
|
|
132
|
+
try {
|
|
133
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
134
|
+
const file = path.join(app, '.env'); const before = 'DATABASE_URL=postgres://fixture@localhost/app\n'
|
|
135
|
+
fs.writeFileSync(file, before)
|
|
136
|
+
const commands = []
|
|
137
|
+
await assert.rejects(databaseStep.apply({
|
|
138
|
+
repoRoot: dir, env: {}, flags: {}, log() {},
|
|
139
|
+
assertDatabaseWriteAllowedImpl: async () => ({ connectionEnv: {} }),
|
|
140
|
+
assertEncryptionCompatibleImpl: () => assertEncryptionCompatible({ projectDir: app, env: {}, runProbe: async () => 'rejected' }),
|
|
141
|
+
runStreamingImpl(...args) { commands.push(args) },
|
|
142
|
+
}), /writes are blocked/)
|
|
143
|
+
assert.deepEqual(commands, [])
|
|
144
|
+
assert.equal(fs.readFileSync(file, 'utf8'), before)
|
|
145
|
+
assert.deepEqual(fs.readdirSync(dir), ['apps'])
|
|
146
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('all consumers ship the same preflight implementation and invoke it at the existing write boundary', () => {
|
|
150
|
+
const source = fs.readFileSync(new URL('../encryption-preflight.mjs', import.meta.url))
|
|
151
|
+
for (const file of ['../../../../scripts/dev-encryption-preflight.mjs', '../../../create-app/template/scripts/dev-encryption-preflight.mjs']) {
|
|
152
|
+
assert.deepEqual(source, fs.readFileSync(new URL(file, import.meta.url)))
|
|
153
|
+
}
|
|
154
|
+
for (const file of ['../../../../scripts/dev.mjs', '../../../create-app/template/scripts/dev.mjs']) {
|
|
155
|
+
const script = fs.readFileSync(new URL(file, import.meta.url), 'utf8')
|
|
156
|
+
const boundary = script.slice(script.indexOf('async function guardDatabaseMutation'), script.indexOf('async function applyDatabaseNameOverrideIfRequested'))
|
|
157
|
+
assert.ok(boundary.indexOf('assertEncryptionCompatible') > boundary.indexOf('assertDatabaseWriteAllowed'))
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
test('environment convergence never generates an encryption fallback for reused Vault or alternate-key configuration', async () => {
|
|
163
|
+
const { envFilesStep } = await import('../steps.mjs')
|
|
164
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-encryption-env-'))
|
|
165
|
+
try {
|
|
166
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
167
|
+
const before = 'VAULT_ADDR=http://vault.fixture\nVAULT_TOKEN=fixture-token\nTENANT_DATA_ENCRYPTION_KEY=existing-fixture-key\n'
|
|
168
|
+
fs.writeFileSync(path.join(dir, '.env'), before)
|
|
169
|
+
fs.writeFileSync(path.join(app, '.env'), before)
|
|
170
|
+
await envFilesStep.apply({ repoRoot: dir, company: { env: {} }, log() {} })
|
|
171
|
+
for (const file of [path.join(dir, '.env'), path.join(app, '.env')]) {
|
|
172
|
+
const after = fs.readFileSync(file, 'utf8')
|
|
173
|
+
assert.ok(after.startsWith(before))
|
|
174
|
+
assert.ok(!after.includes('TENANT_DATA_ENCRYPTION_FALLBACK_KEY'))
|
|
175
|
+
}
|
|
176
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
test('ordinary version-suffixed plaintext remains compatible before and after authenticated decryption', async () => {
|
|
181
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([{ tenant_id: tenant, value: 'customeraccount1:entity:key:v1' }])), 'plaintext-only')
|
|
182
|
+
const plaintext = 'urn:om:sku:v2'
|
|
183
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([{ value: plaintext }])), 'plaintext-only')
|
|
184
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([{ tenant_id: tenant, value: plaintext }])), 'plaintext-only')
|
|
185
|
+
const encrypted = encryptWithAesGcm(plaintext, correct).raw
|
|
186
|
+
assert.equal(await inspectEncryptionCompatibility(fixture([{ tenant_id: tenant, value: encrypted }])), 'verified')
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
test('container command wrapper freezes verified settings and never spawns on rejection', async () => {
|
|
191
|
+
let spawned = false
|
|
192
|
+
await assert.rejects(runEncryptionGuardedSetupCommand('fixture-command', {
|
|
193
|
+
verify: async () => { throw new Error('rejected') },
|
|
194
|
+
spawnCommand: () => { spawned = true; throw new Error('must not launch') },
|
|
195
|
+
}), /rejected/)
|
|
196
|
+
assert.equal(spawned, false)
|
|
197
|
+
let received
|
|
198
|
+
const result = await runEncryptionGuardedSetupCommand('fixture-command', {
|
|
199
|
+
verify: async () => ({ settings: { DATABASE_URL: 'verified-fixture', TENANT_DATA_ENCRYPTION_KEY: 'verified-fixture-key' } }),
|
|
200
|
+
spawnCommand: (command, args, options) => {
|
|
201
|
+
received = { command, args, env: options.env }
|
|
202
|
+
const child = new EventEmitter()
|
|
203
|
+
child.kill = () => {}
|
|
204
|
+
setImmediate(() => child.emit('exit', 7))
|
|
205
|
+
return child
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
assert.equal(result, 7)
|
|
209
|
+
assert.equal(received.command, 'sh')
|
|
210
|
+
assert.equal(received.env.DATABASE_URL, 'verified-fixture')
|
|
211
|
+
assert.equal(received.env.TENANT_DATA_ENCRYPTION_KEY, 'verified-fixture-key')
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
test('brand-new environments receive unique encryption configuration shared by root and app', async () => {
|
|
216
|
+
const { ensureEnvFiles } = await import('../env-setup.mjs')
|
|
217
|
+
const directories = []
|
|
218
|
+
const keys = []
|
|
219
|
+
try {
|
|
220
|
+
for (const index of [0, 1]) {
|
|
221
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), `om-fresh-encryption-${index}-`)); directories.push(dir)
|
|
222
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
223
|
+
fs.writeFileSync(path.join(app, '.env.example'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=public-fixture-placeholder\nJWT_SECRET=placeholder\n')
|
|
224
|
+
ensureEnvFiles(dir, { log() {}, warn() {}, env: {} })
|
|
225
|
+
const rootKey = fs.readFileSync(path.join(dir, '.env'), 'utf8').match(/^TENANT_DATA_ENCRYPTION_FALLBACK_KEY=(.+)$/m)?.[1]
|
|
226
|
+
assert.match(rootKey, /^[a-f0-9]{64}$/)
|
|
227
|
+
assert.ok(fs.readFileSync(path.join(app, '.env'), 'utf8').includes(`TENANT_DATA_ENCRYPTION_FALLBACK_KEY=${rootKey}`))
|
|
228
|
+
keys.push(rootKey)
|
|
229
|
+
}
|
|
230
|
+
assert.notEqual(keys[0], keys[1])
|
|
231
|
+
} finally { for (const dir of directories) fs.rmSync(dir, { recursive: true, force: true }) }
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
test('supplied root, ambient and company key/KMS settings survive missing app environment creation without introducing a fallback', async () => {
|
|
236
|
+
const { ensureEnvFiles } = await import('../env-setup.mjs')
|
|
237
|
+
const { parseEnv } = await import('node:util')
|
|
238
|
+
const cases = [
|
|
239
|
+
{ root: 'TENANT_DATA_ENCRYPTION_KEY=root-fixture-key\n', env: {}, extraDefaults: {}, expected: ['TENANT_DATA_ENCRYPTION_KEY', 'root-fixture-key'] },
|
|
240
|
+
{ root: 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=user-original\n', env: {}, extraDefaults: { TENANT_DATA_ENCRYPTION_FALLBACK_KEY: 'company-default' }, expected: ['TENANT_DATA_ENCRYPTION_FALLBACK_KEY', 'user-original'] },
|
|
241
|
+
{ env: { TENANT_DATA_ENCRYPTION_KEY: 'ambient-fixture-key' }, extraDefaults: {}, expected: ['TENANT_DATA_ENCRYPTION_KEY', 'ambient-fixture-key'] },
|
|
242
|
+
{ env: { VAULT_ADDR: '' }, extraDefaults: { VAULT_ADDR: 'https://vault.fixture', VAULT_TOKEN: 'fixture-token', POSTGRES_PASSWORD: 'private-fixture-password' }, expected: ['VAULT_ADDR', 'https://vault.fixture'] },
|
|
243
|
+
]
|
|
244
|
+
for (const configuration of cases) {
|
|
245
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-supplied-encryption-'))
|
|
246
|
+
try {
|
|
247
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
248
|
+
fs.writeFileSync(path.join(app, '.env.example'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=public-fixture-key\n# TENANT_DATA_ENCRYPTION_KEY=commented-example\nTENANT_DATA_ENCRYPTION_KEY=\n# VAULT_ADDR=http://localhost:8200\n# VAULT_TOKEN=default-fixture\n')
|
|
249
|
+
if (configuration.root) fs.writeFileSync(path.join(dir, '.env'), configuration.root)
|
|
250
|
+
const logs = []
|
|
251
|
+
ensureEnvFiles(dir, { ...configuration, log: value => logs.push(value), warn() {} })
|
|
252
|
+
assert.ok(!logs.join(' ').includes('fixture-token'))
|
|
253
|
+
assert.ok(!logs.join(' ').includes('private-fixture-password'))
|
|
254
|
+
assert.ok(!logs.join(' ').includes(configuration.expected[1]))
|
|
255
|
+
const appSettings = parseEnv(fs.readFileSync(path.join(app, '.env'), 'utf8'))
|
|
256
|
+
assert.equal(appSettings[configuration.expected[0]], configuration.expected[1])
|
|
257
|
+
const expectedFallback = configuration.expected[0] === 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY' ? configuration.expected[1] : ''
|
|
258
|
+
assert.equal(appSettings.TENANT_DATA_ENCRYPTION_FALLBACK_KEY, expectedFallback)
|
|
259
|
+
if (!expectedFallback) assert.ok(!fs.readFileSync(path.join(dir, '.env'), 'utf8').includes('TENANT_DATA_ENCRYPTION_FALLBACK_KEY='))
|
|
260
|
+
const result = await assertEncryptionCompatible({ projectDir: app, env: {}, runProbe: async (_project, settings) => {
|
|
261
|
+
assert.equal(settings[configuration.expected[0]], configuration.expected[1])
|
|
262
|
+
assert.equal(settings.TENANT_DATA_ENCRYPTION_FALLBACK_KEY, expectedFallback)
|
|
263
|
+
return 'plaintext-only'
|
|
264
|
+
} })
|
|
265
|
+
assert.equal(result.status, 'plaintext-only')
|
|
266
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
267
|
+
}
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
test('supplied encryption strings round-trip through root/app dotenv without changing quotes, slashes or hash characters', async () => {
|
|
272
|
+
const { ensureEnvFiles } = await import('../env-setup.mjs')
|
|
273
|
+
const { parseEnv } = await import('node:util')
|
|
274
|
+
for (const secret of ['fixture"key', String.raw`fixture\key`, 'fixture#key', 'fixture$key']) {
|
|
275
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-key-roundtrip-'))
|
|
276
|
+
try {
|
|
277
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
278
|
+
fs.writeFileSync(path.join(app, '.env.example'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=public-fixture-key\nTENANT_DATA_ENCRYPTION_KEY=\n')
|
|
279
|
+
ensureEnvFiles(dir, { env: {}, extraDefaults: { TENANT_DATA_ENCRYPTION_KEY: secret }, log() {}, warn() {} })
|
|
280
|
+
assert.equal(parseEnv(fs.readFileSync(path.join(dir, '.env'), 'utf8')).TENANT_DATA_ENCRYPTION_KEY, secret)
|
|
281
|
+
assert.equal(parseEnv(fs.readFileSync(path.join(app, '.env'), 'utf8')).TENANT_DATA_ENCRYPTION_KEY, secret)
|
|
282
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
test('unrepresentable supplied encryption configuration fails before environment creation without echoing key material', async () => {
|
|
287
|
+
const { ensureEnvFiles } = await import('../env-setup.mjs')
|
|
288
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-key-render-rejection-'))
|
|
289
|
+
try {
|
|
290
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
291
|
+
fs.writeFileSync(path.join(app, '.env.example'), 'TENANT_DATA_ENCRYPTION_KEY=\n')
|
|
292
|
+
assert.throws(() => ensureEnvFiles(dir, { env: {}, extraDefaults: { TENANT_DATA_ENCRYPTION_KEY: "fixture'key" }, log() {}, warn() {} }), error => {
|
|
293
|
+
assert.ok(!error.message.includes("fixture'key"))
|
|
294
|
+
return /do not edit or regenerate encryption keys/.test(error.message)
|
|
295
|
+
})
|
|
296
|
+
assert.equal(fs.existsSync(path.join(dir, '.env')), false)
|
|
297
|
+
assert.equal(fs.existsSync(path.join(app, '.env')), false)
|
|
298
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
299
|
+
})
|
|
@@ -192,6 +192,7 @@ test('database setup does not mark pending or failed migrations as complete', as
|
|
|
192
192
|
const ctx = {
|
|
193
193
|
repoRoot, env: {}, flags: { noInfra: true }, log() {},
|
|
194
194
|
assertDatabaseWriteAllowedImpl: async () => ({ remote: false }),
|
|
195
|
+
assertEncryptionCompatibleImpl: async ({ env }) => ({ status: 'verified', settings: env }),
|
|
195
196
|
dbMigrationsMarker: 'db-migrations-test', dbMarker: 'db-initialized-test', migrationsFingerprint: 'fingerprint',
|
|
196
197
|
runStreamingImpl: async (_command, args) => { invocations.push(args[0]); return migration.promise },
|
|
197
198
|
}
|
|
@@ -212,6 +213,7 @@ test('database setup records successful migration but never a failed initializat
|
|
|
212
213
|
const ctx = {
|
|
213
214
|
repoRoot, env: {}, flags: { noInfra: true }, log() {},
|
|
214
215
|
assertDatabaseWriteAllowedImpl: async () => ({ remote: false }),
|
|
216
|
+
assertEncryptionCompatibleImpl: async ({ env }) => ({ status: 'verified', settings: env }),
|
|
215
217
|
dbMigrationsMarker: 'db-migrations-test', dbMarker: 'db-initialized-test', migrationsFingerprint: 'fingerprint',
|
|
216
218
|
runStreamingImpl: async (_command, args) => args[0] === 'db:migrate' ? 0 : 3,
|
|
217
219
|
}
|
|
@@ -238,6 +240,7 @@ test('database setup keeps the initialization credentials visible when output is
|
|
|
238
240
|
repoRoot, env: {}, flags: { noInfra: true }, log() {}, logs,
|
|
239
241
|
promptLog: (line) => visible.push(line),
|
|
240
242
|
assertDatabaseWriteAllowedImpl: async () => ({ remote: false }),
|
|
243
|
+
assertEncryptionCompatibleImpl: async ({ env }) => ({ status: 'verified', settings: env }),
|
|
241
244
|
dbMigrationsMarker: 'db-migrations-test', dbMarker: 'db-initialized-test', migrationsFingerprint: 'fingerprint',
|
|
242
245
|
runStreamingImpl: async (_command, args, options) => {
|
|
243
246
|
if (args[0] === 'initialize') {
|
|
@@ -205,7 +205,7 @@ test('resolveSpawnCommand rejects cmd metacharacters instead of degrading into i
|
|
|
205
205
|
})
|
|
206
206
|
|
|
207
207
|
function quietCtx(overrides = {}) {
|
|
208
|
-
return { assertDatabaseWriteAllowedImpl: async () => ({ remote: false }), log: () => {}, flags: {}, company: { steps: { disable: [], extra: [] } }, ...overrides }
|
|
208
|
+
return { assertEncryptionCompatibleImpl: async ({ env }) => ({ status: 'verified', settings: env }), assertDatabaseWriteAllowedImpl: async () => ({ remote: false }), log: () => {}, flags: {}, company: { steps: { disable: [], extra: [] } }, ...overrides }
|
|
209
209
|
}
|
|
210
210
|
|
|
211
211
|
test('runSteps skips satisfied steps, applies unsatisfied ones, and stops on StepBlocked with guidance', async () => {
|
|
@@ -492,6 +492,15 @@ test('ensureEnvFiles writes the documented dev credentials and threads them into
|
|
|
492
492
|
)
|
|
493
493
|
})
|
|
494
494
|
|
|
495
|
+
test('ensureEnvFiles gives the app .env the same generated tenant encryption key as the compose .env', () => {
|
|
496
|
+
const repo = makeFakeAppRepo()
|
|
497
|
+
fs.appendFileSync(path.join(repo, 'apps', 'mercato', '.env.example'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY=\n')
|
|
498
|
+
ensureEnvFiles(repo, { log: () => {}, warn: () => {} })
|
|
499
|
+
const rootKey = readEnvValue(path.join(repo, '.env'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY')
|
|
500
|
+
assert.ok(rootKey && Buffer.byteLength(rootKey) >= 32)
|
|
501
|
+
assert.equal(readEnvValue(path.join(repo, 'apps', 'mercato', '.env'), 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY'), rootKey)
|
|
502
|
+
})
|
|
503
|
+
|
|
495
504
|
test('ensureEnvFiles never overwrites credentials already present in .env', () => {
|
|
496
505
|
const repo = makeFakeAppRepo()
|
|
497
506
|
fs.writeFileSync(path.join(repo, '.env'), 'POSTGRES_PASSWORD=custom-pass\nOM_INIT_SUPERADMIN_PASSWORD=custom-admin\n')
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { createRequire } from 'node:module'
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
5
|
+
import { spawn } from 'node:child_process'
|
|
6
|
+
import { parseEnv } from 'node:util'
|
|
7
|
+
|
|
8
|
+
const FAILURE = '[setup] Encryption compatibility could not be verified; database writes are blocked. Restore the original TENANT_DATA_ENCRYPTION settings and encryption keys, and check Vault/KMS access (including historical tenant keys and FORMS_ENCRYPTION_MASTER_KEY). Do not regenerate keys or reinstall this database. Confirm DATABASE_URL targets the intended database and matching application packages are installed and built. Inspect permissions and unsupported encrypted stores with your administrator, or select a separate empty database. No preflight database or KMS writes were performed.'
|
|
9
|
+
const MAX_ROWS = 100_000
|
|
10
|
+
const SAMPLE_ROWS_PER_SCOPE = 5
|
|
11
|
+
const ENVELOPE_PATTERN = '[A-Za-z0-9+/]{16}:[A-Za-z0-9+/]*={0,2}:[A-Za-z0-9+/]{22}==:v[0-9]+'
|
|
12
|
+
const ENVELOPE_CANDIDATE = new RegExp(`^${ENVELOPE_PATTERN}$`)
|
|
13
|
+
const OPAQUE_BINARY_COLUMNS = new Set(['document_contents.yjs_state', 'document_versions.yjs_snapshot'])
|
|
14
|
+
const MAX_BYTES = 64 * 1024 * 1024
|
|
15
|
+
const TIMEOUT_MS = 60_000
|
|
16
|
+
|
|
17
|
+
function requireFrom(projectDir) {
|
|
18
|
+
return createRequire(path.join(projectDir, 'package.json'))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function importFrom(projectDir, name) {
|
|
22
|
+
return import(pathToFileURL(requireFrom(projectDir).resolve(name)).href)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function settingsFor(projectDir, envFiles, env) {
|
|
26
|
+
const settings = {}
|
|
27
|
+
for (const file of [...envFiles, path.join(projectDir, '.env')]) {
|
|
28
|
+
try { Object.assign(settings, parseEnv(fs.readFileSync(file, 'utf8'))) }
|
|
29
|
+
catch (error) { if (error.code !== 'ENOENT') throw new Error(FAILURE) }
|
|
30
|
+
}
|
|
31
|
+
return { ...settings, ...env }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class EncryptionCompatibilityError extends Error {
|
|
35
|
+
constructor() { super(FAILURE); this.name = 'EncryptionCompatibilityError' }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function assertEncryptionCompatible({ projectDir = process.cwd(), envFiles = [], env = process.env, runProbe = runIsolatedProbe } = {}) {
|
|
39
|
+
try {
|
|
40
|
+
const settings = settingsFor(projectDir, envFiles, env)
|
|
41
|
+
const status = await runProbe(projectDir, settings)
|
|
42
|
+
if (!['empty', 'plaintext-only', 'verified'].includes(status)) throw new Error(FAILURE)
|
|
43
|
+
return { status, settings }
|
|
44
|
+
} catch { throw new EncryptionCompatibilityError() }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function runIsolatedProbe(projectDir, settings) {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), '--encryption-probe', projectDir], {
|
|
50
|
+
cwd: projectDir,
|
|
51
|
+
env: { ...settings, TENANT_DATA_ENCRYPTION_DEBUG: 'off', OM_LOG_DESTINATION: 'stderr', OM_LOG_LEVEL: 'error' },
|
|
52
|
+
stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
|
|
53
|
+
})
|
|
54
|
+
let status
|
|
55
|
+
const timer = setTimeout(() => { child.kill('SIGKILL') }, TIMEOUT_MS)
|
|
56
|
+
child.on('message', message => { if (typeof message === 'string') status = message })
|
|
57
|
+
child.once('error', () => { clearTimeout(timer); reject(new Error(FAILURE)) })
|
|
58
|
+
child.once('exit', code => {
|
|
59
|
+
clearTimeout(timer)
|
|
60
|
+
if (code === 0 && status) resolve(status)
|
|
61
|
+
else reject(new Error(FAILURE))
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function quote(identifier) {
|
|
67
|
+
return `"${identifier.replaceAll('"', '""')}"`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function byteBuffer(value) {
|
|
71
|
+
if (typeof value !== 'string' || !/^\\x(?:[0-9a-f]{2})+$/i.test(value)) throw new Error(FAILURE)
|
|
72
|
+
return Buffer.from(value.slice(2), 'hex')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function inspectEncryptionCompatibility({ client, kms, decrypt, resolveKeyId, loadForms, encryptionEnabled = true, maxRows = MAX_ROWS, maxBytes = MAX_BYTES }) {
|
|
76
|
+
let rowsRead = 0
|
|
77
|
+
let bytesRead = 0
|
|
78
|
+
let ciphertexts = 0
|
|
79
|
+
let applicationRows = 0
|
|
80
|
+
const scopes = new Set()
|
|
81
|
+
const keys = new Map()
|
|
82
|
+
let forms
|
|
83
|
+
const getForms = async () => forms ??= await loadForms()
|
|
84
|
+
const inspectValue = async (value, keyId, depth = 0) => {
|
|
85
|
+
if (depth > 32) throw new Error(FAILURE)
|
|
86
|
+
if (typeof value === 'string') {
|
|
87
|
+
if (ENVELOPE_CANDIDATE.test(value)) {
|
|
88
|
+
if (!encryptionEnabled || !keyId) throw new Error(FAILURE)
|
|
89
|
+
let dek = keys.get(keyId)
|
|
90
|
+
if (!dek) {
|
|
91
|
+
dek = await kms.getTenantDek(keyId)
|
|
92
|
+
if (!dek) throw new Error(FAILURE)
|
|
93
|
+
keys.set(keyId, dek)
|
|
94
|
+
}
|
|
95
|
+
const plaintext = decrypt(value, dek.key)
|
|
96
|
+
if (plaintext === null) throw new Error(FAILURE)
|
|
97
|
+
ciphertexts += 1
|
|
98
|
+
await inspectValue(plaintext, keyId, depth + 1)
|
|
99
|
+
} else if (/^\s*[\[{]/.test(value)) {
|
|
100
|
+
let parsed
|
|
101
|
+
try { parsed = JSON.parse(value) } catch { return }
|
|
102
|
+
await inspectValue(parsed, keyId, depth + 1)
|
|
103
|
+
}
|
|
104
|
+
} else if (Array.isArray(value)) {
|
|
105
|
+
for (const item of value) await inspectValue(item, keyId, depth + 1)
|
|
106
|
+
} else if (value && typeof value === 'object') {
|
|
107
|
+
for (const item of Object.values(value)) await inspectValue(item, keyId, depth + 1)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
await client.query('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY')
|
|
111
|
+
try {
|
|
112
|
+
await client.query("SET LOCAL statement_timeout = '10s'")
|
|
113
|
+
await client.query('SET LOCAL row_security = off')
|
|
114
|
+
const { rows: tables } = await client.query(`SELECT n.nspname AS schema, c.relname AS name, c.relkind AS kind,
|
|
115
|
+
c.relrowsecurity AS rls, a.attname AS column, COALESCE(base.typname, t.typname) AS type
|
|
116
|
+
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
117
|
+
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped
|
|
118
|
+
JOIN pg_type t ON t.oid = a.atttypid
|
|
119
|
+
LEFT JOIN pg_type base ON base.oid = NULLIF(t.typbasetype, 0)
|
|
120
|
+
WHERE c.relkind IN ('r', 'p', 'm', 'f') AND n.nspname NOT IN ('pg_catalog', 'information_schema')
|
|
121
|
+
AND n.nspname NOT LIKE 'pg_toast%' AND n.nspname NOT LIKE 'pg_temp%'
|
|
122
|
+
ORDER BY n.nspname, c.relname, a.attnum`)
|
|
123
|
+
const inventory = new Map()
|
|
124
|
+
for (const column of tables) {
|
|
125
|
+
if (column.rls || column.kind === 'f') throw new Error(FAILURE)
|
|
126
|
+
const id = `${quote(column.schema)}.${quote(column.name)}`
|
|
127
|
+
if (!inventory.has(id)) inventory.set(id, { ...column, binary: [], columns: [] })
|
|
128
|
+
inventory.get(id).columns.push(column.column)
|
|
129
|
+
if (['bytea', '_bytea'].includes(column.type)) inventory.get(id).binary.push(column.column)
|
|
130
|
+
}
|
|
131
|
+
for (const [table, metadata] of inventory) {
|
|
132
|
+
const { rows: presence } = await client.query(`SELECT EXISTS(SELECT 1 FROM ${table}) AS populated`)
|
|
133
|
+
if (!presence[0]?.populated) continue
|
|
134
|
+
if (!metadata.name.startsWith('mikro_orm_migrations')) applicationRows += 1
|
|
135
|
+
if (metadata.columns.includes('tenant_id') && metadata.name !== 'onboarding_requests') {
|
|
136
|
+
const { rows: tenantScopes } = await client.query(`SELECT DISTINCT tenant_id::text AS scope FROM ${table} WHERE tenant_id IS NOT NULL`)
|
|
137
|
+
for (const { scope } of tenantScopes) scopes.add(scope)
|
|
138
|
+
}
|
|
139
|
+
if (metadata.name === 'onboarding_requests') scopes.add(resolveKeyId('onboarding:onboarding_request', 'system', null))
|
|
140
|
+
const selectedColumns = metadata.columns.filter(column => !OPAQUE_BINARY_COLUMNS.has(`${metadata.name}.${column}`))
|
|
141
|
+
const binaryColumns = metadata.binary.filter(column => selectedColumns.includes(column))
|
|
142
|
+
const knownForms = metadata.name === 'forms_encryption_key' || metadata.name === 'forms_form_submission_revision' || metadata.name === 'forms_form_attachment'
|
|
143
|
+
if (binaryColumns.length && !knownForms) {
|
|
144
|
+
const { rows: binaryPresence } = await client.query(`SELECT EXISTS(SELECT 1 FROM ${table} WHERE ${binaryColumns.map(column => `${quote(column)} IS NOT NULL`).join(' OR ')}) AS populated`)
|
|
145
|
+
if (binaryPresence[0]?.populated) throw new Error(FAILURE)
|
|
146
|
+
}
|
|
147
|
+
const projection = selectedColumns.length ? `SELECT ${selectedColumns.map(quote).join(', ')} FROM ${table}` : `SELECT NULL AS empty FROM ${table}`
|
|
148
|
+
const binaryPredicate = binaryColumns.map(column => `record->>'${column.replaceAll("'", "''")}' IS NOT NULL`).join(' OR ')
|
|
149
|
+
const partition = metadata.name.startsWith('forms_') ? "record->>'organization_id', record->>'key_version', record->>'encryption_key_version'" : "record->>'tenant_id'"
|
|
150
|
+
await client.query(`DECLARE encryption_preflight NO SCROLL CURSOR FOR
|
|
151
|
+
WITH projected AS (SELECT to_jsonb(source) AS record FROM (${projection}) source),
|
|
152
|
+
candidates AS (SELECT record, row_number() OVER (PARTITION BY ${partition} ORDER BY record->>'id') AS sample
|
|
153
|
+
FROM projected WHERE record::text ~ '${ENVELOPE_PATTERN}' ${binaryPredicate ? `OR ${binaryPredicate}` : ''})
|
|
154
|
+
SELECT record FROM candidates WHERE sample <= ${SAMPLE_ROWS_PER_SCOPE}`)
|
|
155
|
+
try {
|
|
156
|
+
while (true) {
|
|
157
|
+
const { rows } = await client.query('FETCH FORWARD 100 FROM encryption_preflight')
|
|
158
|
+
if (rows.length === 0) break
|
|
159
|
+
for (const { record } of rows) {
|
|
160
|
+
rowsRead += 1
|
|
161
|
+
bytesRead += Buffer.byteLength(JSON.stringify(record))
|
|
162
|
+
if (rowsRead > maxRows || bytesRead > maxBytes) throw new Error(FAILURE)
|
|
163
|
+
const keyId = metadata.name === 'onboarding_requests'
|
|
164
|
+
? resolveKeyId('onboarding:onboarding_request', 'system', record.tenant_id)
|
|
165
|
+
: record.tenant_id
|
|
166
|
+
if (keyId) scopes.add(keyId)
|
|
167
|
+
for (const column of binaryColumns) {
|
|
168
|
+
const value = record[column]
|
|
169
|
+
if (value === null) continue
|
|
170
|
+
const adapter = await getForms()
|
|
171
|
+
if (metadata.name === 'forms_encryption_key' && column === 'wrapped_dek') {
|
|
172
|
+
await adapter.unwrap(byteBuffer(value))
|
|
173
|
+
} else if ((metadata.name === 'forms_form_submission_revision' && column === 'data')
|
|
174
|
+
|| (metadata.name === 'forms_form_attachment' && column === 'payload_inline')) {
|
|
175
|
+
await adapter.decrypt(record.organization_id, byteBuffer(value))
|
|
176
|
+
} else throw new Error(FAILURE)
|
|
177
|
+
ciphertexts += 1
|
|
178
|
+
delete record[column]
|
|
179
|
+
}
|
|
180
|
+
await inspectValue(record, keyId)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
} finally { await client.query('CLOSE encryption_preflight') }
|
|
184
|
+
}
|
|
185
|
+
if (applicationRows && encryptionEnabled) {
|
|
186
|
+
for (const scope of scopes) {
|
|
187
|
+
if (!keys.has(scope) && !await kms.getTenantDek(scope)) throw new Error(FAILURE)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return ciphertexts ? 'verified' : applicationRows ? 'plaintext-only' : 'empty'
|
|
191
|
+
} finally { await client.query('ROLLBACK') }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function probe(projectDir) {
|
|
195
|
+
if (!process.env.DATABASE_URL?.trim()) throw new Error(FAILURE)
|
|
196
|
+
const { Client } = createRequire(requireFrom(projectDir).resolve('@open-mercato/cli'))('pg')
|
|
197
|
+
const client = new Client({ connectionString: process.env.DATABASE_URL, connectionTimeoutMillis: 5000, query_timeout: 15_000 })
|
|
198
|
+
client.on('error', () => {})
|
|
199
|
+
try {
|
|
200
|
+
try { await client.connect() }
|
|
201
|
+
catch (error) { if (error.code === '3D000') return 'empty'; throw error }
|
|
202
|
+
const { createKmsService } = await importFrom(projectDir, '@open-mercato/shared/lib/encryption/kms')
|
|
203
|
+
const { decryptWithAesGcm } = await importFrom(projectDir, '@open-mercato/shared/lib/encryption/aes')
|
|
204
|
+
const { resolveEncryptionKeyId } = await importFrom(projectDir, '@open-mercato/shared/lib/encryption/tenantDataEncryptionService')
|
|
205
|
+
const { isTenantDataEncryptionEnabled } = await importFrom(projectDir, '@open-mercato/shared/lib/encryption/toggles')
|
|
206
|
+
return await inspectEncryptionCompatibility({
|
|
207
|
+
encryptionEnabled: isTenantDataEncryptionEnabled(),
|
|
208
|
+
client, kms: createKmsService(), decrypt: decryptWithAesGcm, resolveKeyId: resolveEncryptionKeyId,
|
|
209
|
+
loadForms: async () => {
|
|
210
|
+
const { resolveKmsAdapter, FormsEncryptionService } = await importFrom(projectDir, '@open-mercato/core/modules/forms/services/encryption-service')
|
|
211
|
+
const adapter = resolveKmsAdapter(process.env)
|
|
212
|
+
const service = new FormsEncryptionService({ kmsAdapter: adapter, emFactory: () => ({
|
|
213
|
+
findOne: async (_entity, { organizationId, keyVersion }) => {
|
|
214
|
+
const { rows } = await client.query('SELECT wrapped_dek FROM forms_encryption_key WHERE organization_id = $1 AND key_version = $2', [organizationId, keyVersion])
|
|
215
|
+
return rows[0] ? { wrappedDek: rows[0].wrapped_dek } : null
|
|
216
|
+
},
|
|
217
|
+
}) })
|
|
218
|
+
return { unwrap: value => adapter.unwrap(value), decrypt: (organizationId, value) => service.decrypt(organizationId, value) }
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
} finally { await client.end() }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function isEntryPoint() {
|
|
225
|
+
try {
|
|
226
|
+
return Boolean(process.argv[1]) && fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url))
|
|
227
|
+
} catch { return false }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (isEntryPoint() && process.argv[2] === '--encryption-probe') {
|
|
231
|
+
try {
|
|
232
|
+
const status = await probe(process.argv[3])
|
|
233
|
+
await new Promise((resolve, reject) => process.send(status, error => error ? reject(error) : resolve()))
|
|
234
|
+
process.exit(0)
|
|
235
|
+
}
|
|
236
|
+
catch { process.exit(1) }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
export async function runEncryptionGuardedSetupCommand(command, { projectDir = process.cwd(), verify = assertEncryptionCompatible, spawnCommand = spawn } = {}) {
|
|
241
|
+
const compatibility = await verify({ projectDir })
|
|
242
|
+
const child = spawnCommand('sh', ['-lc', command], { cwd: projectDir, env: compatibility.settings, stdio: 'inherit' })
|
|
243
|
+
const interrupt = () => child.kill('SIGINT')
|
|
244
|
+
const terminate = () => child.kill('SIGTERM')
|
|
245
|
+
process.once('SIGINT', interrupt)
|
|
246
|
+
process.once('SIGTERM', terminate)
|
|
247
|
+
try {
|
|
248
|
+
return await new Promise((resolve, reject) => {
|
|
249
|
+
child.once('error', () => reject(new Error(FAILURE)))
|
|
250
|
+
child.once('exit', code => resolve(code ?? 1))
|
|
251
|
+
})
|
|
252
|
+
} finally {
|
|
253
|
+
process.removeListener('SIGINT', interrupt)
|
|
254
|
+
process.removeListener('SIGTERM', terminate)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (isEntryPoint() && process.argv[2] === '--run-setup-command') {
|
|
259
|
+
try { process.exit(await runEncryptionGuardedSetupCommand(process.argv[3])) }
|
|
260
|
+
catch { console.error(FAILURE); process.exit(78) }
|
|
261
|
+
}
|
package/src/env-setup.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
+
import { parseEnv } from 'node:util'
|
|
3
4
|
|
|
4
5
|
import { addEnvValue, readEnvValue, replaceEnvLine, uncommentEnvLine, writeEnvFileText } from './env-file.mjs'
|
|
5
6
|
import { secretFingerprint, secretHex } from './secrets.mjs'
|
|
7
|
+
import { replaceValues } from './service-modes.mjs'
|
|
6
8
|
|
|
7
9
|
// Node port of the Windows launcher's Initialize-EnvFiles: fill-missing-only,
|
|
8
10
|
// machine-local secrets are generated once and never overwritten on re-runs.
|
|
@@ -11,10 +13,22 @@ import { secretFingerprint, secretHex } from './secrets.mjs'
|
|
|
11
13
|
// outlives .env, so generated credentials drift from the volume on every
|
|
12
14
|
// re-clone or env regeneration, and the documented login stops working.
|
|
13
15
|
|
|
14
|
-
export function ensureEnvFiles(repoRoot, { log = console.log, warn = console.warn, extraDefaults = {} } = {}) {
|
|
16
|
+
export function ensureEnvFiles(repoRoot, { log = console.log, warn = console.warn, extraDefaults = {}, env = process.env } = {}) {
|
|
15
17
|
const rootEnv = path.join(repoRoot, '.env')
|
|
16
18
|
const appEnvExample = path.join(repoRoot, 'apps', 'mercato', '.env.example')
|
|
17
19
|
const appEnv = path.join(repoRoot, 'apps', 'mercato', '.env')
|
|
20
|
+
const encryptionSettings = ['TENANT_DATA_ENCRYPTION', 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY', 'TENANT_DATA_ENCRYPTION_KEY', 'VAULT_ADDR', 'VAULT_TOKEN', 'VAULT_KV_PATH', 'VAULT_REQUEST_TIMEOUT_MS', 'VAULT_RECOVERY_COOLDOWN_MS', 'FORMS_ENCRYPTION_MASTER_KEY', 'FORMS_ENCRYPTION_KMS_KEY_ID']
|
|
21
|
+
const rootSettings = () => fs.existsSync(rootEnv) ? parseEnv(fs.readFileSync(rootEnv, 'utf8')) : {}
|
|
22
|
+
const configuredValue = (key) => [env[key], rootSettings()[key], extraDefaults[key]].find(value => String(value ?? '').trim())
|
|
23
|
+
const encryptionConfigured = ['TENANT_DATA_ENCRYPTION_FALLBACK_KEY', 'TENANT_DATA_ENCRYPTION_KEY', 'VAULT_ADDR', 'VAULT_TOKEN'].some(key => configuredValue(key) !== undefined)
|
|
24
|
+
const freshEnvironment = !fs.existsSync(rootEnv) && !fs.existsSync(appEnv) && !encryptionConfigured
|
|
25
|
+
const renderEncryptionValues = (text, values) => {
|
|
26
|
+
try { return replaceValues(text, values) }
|
|
27
|
+
catch { throw new Error('Cannot safely preserve supplied encryption configuration in .env. Restore the original environment file or retain the existing external configuration; do not edit or regenerate encryption keys.') }
|
|
28
|
+
}
|
|
29
|
+
const pendingDefaults = Object.fromEntries(Object.entries(extraDefaults).filter(([key]) => encryptionSettings.includes(key) && readEnvValue(rootEnv, key) === null).map(([key, value]) => [key, String(value)]))
|
|
30
|
+
const pendingApp = fs.existsSync(appEnv) ? {} : Object.fromEntries(encryptionSettings.map(key => [key, configuredValue(key)]).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)]))
|
|
31
|
+
renderEncryptionValues('', { ...pendingDefaults, ...pendingApp })
|
|
18
32
|
|
|
19
33
|
if (!fs.existsSync(rootEnv)) {
|
|
20
34
|
writeEnvFileText(rootEnv, '# Open Mercato compose environment (generated by the hybrid starter)\n')
|
|
@@ -25,20 +39,22 @@ export function ensureEnvFiles(repoRoot, { log = console.log, warn = console.war
|
|
|
25
39
|
if (!jwtSecret || !jwtSecret.trim()) jwtSecret = secretHex(32)
|
|
26
40
|
|
|
27
41
|
const setSecret = (key, value) => {
|
|
28
|
-
if (
|
|
42
|
+
if (readEnvValue(rootEnv, key) !== null) return
|
|
43
|
+
const serialized = encryptionSettings.includes(key) ? renderEncryptionValues('', { [key]: String(value) }).trimEnd().slice(key.length + 1) : value
|
|
44
|
+
if (addEnvValue(rootEnv, key, serialized)) log(` set ${key} (${secretFingerprint(value)})`)
|
|
29
45
|
}
|
|
30
46
|
const setValue = (key, value) => {
|
|
31
47
|
if (addEnvValue(rootEnv, key, value)) log(` set ${key}=${value}`)
|
|
32
48
|
}
|
|
33
49
|
|
|
34
50
|
setSecret('JWT_SECRET', jwtSecret)
|
|
35
|
-
setSecret('TENANT_DATA_ENCRYPTION_FALLBACK_KEY', secretHex(
|
|
51
|
+
if (freshEnvironment) setSecret('TENANT_DATA_ENCRYPTION_FALLBACK_KEY', secretHex(32))
|
|
36
52
|
const meiliKey = readEnvValue(rootEnv, 'MEILISEARCH_MASTER_KEY')?.trim() || secretHex(16)
|
|
37
53
|
setSecret('MEILISEARCH_MASTER_KEY', meiliKey)
|
|
38
54
|
setValue('APP_URL', `http://localhost:${readEnvValue(rootEnv, 'APP_PORT')?.trim() || '3000'}`)
|
|
39
55
|
setValue('OM_INIT_SUPERADMIN_EMAIL', 'superadmin@acme.com')
|
|
40
56
|
for (const [key, value] of Object.entries(extraDefaults)) {
|
|
41
|
-
|
|
57
|
+
setSecret(key, String(value))
|
|
42
58
|
}
|
|
43
59
|
// Dev-default credentials, not secrets (see header). Set after extraDefaults
|
|
44
60
|
// so a company preset — like anything already in .env — wins.
|
|
@@ -53,8 +69,20 @@ export function ensureEnvFiles(repoRoot, { log = console.log, warn = console.war
|
|
|
53
69
|
if (fs.existsSync(appEnvExample)) {
|
|
54
70
|
let content = fs.readFileSync(appEnvExample, 'utf8')
|
|
55
71
|
content = replaceEnvLine(content, 'JWT_SECRET', jwtSecret)
|
|
72
|
+
if (encryptionConfigured && configuredValue('TENANT_DATA_ENCRYPTION_FALLBACK_KEY') === undefined) {
|
|
73
|
+
content = replaceEnvLine(content, 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY', '')
|
|
74
|
+
}
|
|
75
|
+
const suppliedSettings = Object.fromEntries(encryptionSettings.map(key => [key, configuredValue(key)]).filter(([, value]) => value !== undefined).map(([key, value]) => [key, String(value)]))
|
|
76
|
+
content = renderEncryptionValues(content, suppliedSettings)
|
|
56
77
|
content = replaceEnvLine(content, 'AUTH_SECRET', secretHex(32))
|
|
57
78
|
content = replaceEnvLine(content, 'LOOKUP_HASH_PEPPER', secretHex(16))
|
|
79
|
+
// .env.example ships no tenant encryption key (#7231); reuse the compose
|
|
80
|
+
// key so the host app and containers encrypt with the same root secret.
|
|
81
|
+
content = replaceEnvLine(
|
|
82
|
+
content,
|
|
83
|
+
'TENANT_DATA_ENCRYPTION_FALLBACK_KEY',
|
|
84
|
+
readEnvValue(rootEnv, 'TENANT_DATA_ENCRYPTION_FALLBACK_KEY')?.trim() || secretHex(32),
|
|
85
|
+
)
|
|
58
86
|
// 127.0.0.1, never `localhost`: on modern Windows localhost resolves
|
|
59
87
|
// to ::1 first while the published container ports listen on IPv4.
|
|
60
88
|
content = replaceEnvLine(
|
package/src/service-modes.mjs
CHANGED
|
@@ -77,7 +77,7 @@ function assignmentPattern(key) {
|
|
|
77
77
|
return new RegExp(String.raw`^(?:export[ \t]+)?${key}[ \t]*=[ \t]*(?:"[^"]*"|'[^']*'|[^\r\n]*)(?:[^\r\n]*)(?:\r?\n|$)`, 'gm')
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
function replaceValues(text, values) {
|
|
80
|
+
export function replaceValues(text, values) {
|
|
81
81
|
let result = text
|
|
82
82
|
for (const [key, value] of Object.entries(values)) {
|
|
83
83
|
const pattern = assignmentPattern(key)
|
package/src/steps.mjs
CHANGED
|
@@ -14,6 +14,7 @@ import { addEnvValue, readEnvValue } from './env-file.mjs'
|
|
|
14
14
|
import { describeInfraProblem, startInfra } from './infra.mjs'
|
|
15
15
|
import { configureServiceMode } from './service-modes.mjs'
|
|
16
16
|
import { assertDatabaseWriteAllowed } from './database-write-guard.mjs'
|
|
17
|
+
import { assertEncryptionCompatible } from './encryption-preflight.mjs'
|
|
17
18
|
import { WITHOUT_DOCKER_GUIDE_URL, findUnhealthyServices, requestExistingServicesConsent } from './infra-preflight.mjs'
|
|
18
19
|
import { ensureLlmProvider } from './providers.mjs'
|
|
19
20
|
import { verifyServiceConnections } from './service-connections.mjs'
|
|
@@ -710,6 +711,13 @@ export const databaseStep = {
|
|
|
710
711
|
})
|
|
711
712
|
if (result?.connectionEnv) ctx.env = { ...ctx.env, ...result.connectionEnv }
|
|
712
713
|
if (result?.argument) ctx.remoteDatabaseArgument = result.argument
|
|
714
|
+
const compatibility = await (ctx.assertEncryptionCompatibleImpl ?? assertEncryptionCompatible)({
|
|
715
|
+
projectDir: path.join(ctx.repoRoot, 'apps', 'mercato'),
|
|
716
|
+
envFiles: [path.join(ctx.repoRoot, '.env')],
|
|
717
|
+
env: ctx.env,
|
|
718
|
+
})
|
|
719
|
+
ctx.env = { ...ctx.env, ...compatibility.settings }
|
|
720
|
+
ctx.log(` encryption preflight: ${compatibility.status}`)
|
|
713
721
|
} finally {
|
|
714
722
|
ctx.logs?.resume()
|
|
715
723
|
ctx.logs?.resumeActivity?.()
|