feat(identity): prepare guarded local admin seed credentials
This commit is contained in:
parent
c7e2160b10
commit
d4aa7d5c79
|
|
@ -40,6 +40,7 @@ Phase 2E adds [supplier payment records and reversals](docs/supplier-payments-ap
|
||||||
Phase 2F adds [private versioned supplier documents and review](docs/supplier-documents-api.md).
|
Phase 2F adds [private versioned supplier documents and review](docs/supplier-documents-api.md).
|
||||||
Phase 2G adds [versioned product bills of materials](docs/bom-api.md).
|
Phase 2G adds [versioned product bills of materials](docs/bom-api.md).
|
||||||
Phase 2H adds [production batches](docs/production-batches-api.md).
|
Phase 2H adds [production batches](docs/production-batches-api.md).
|
||||||
|
For a development owner, see [local admin setup](docs/local-admin.md).
|
||||||
|
|
||||||
### Swagger UI
|
### Swagger UI
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Local admin account
|
||||||
|
|
||||||
|
The development-only seed uses the existing one-time owner bootstrap. It creates no committed default password. A local PostgreSQL server must be available using the `DATABASE_URL` in `.env`, and `NODE_ENV` must be `development`.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pnpm db:generate
|
||||||
|
pnpm db:deploy
|
||||||
|
pnpm build
|
||||||
|
pnpm admin:prepare-local
|
||||||
|
pnpm admin:seed-local
|
||||||
|
```
|
||||||
|
|
||||||
|
`admin:prepare-local` creates an ignored `.env.local-admin` file with `OWNER_EMAIL=admin@manicandles.local`, organization/name settings and a random password. The command refuses to overwrite it. Read the password from that local file; do not commit or share it. `admin:seed-local` passes those values to the existing bootstrap command and prints only the resulting organization and user IDs. The seed accepts only a development environment with a loopback PostgreSQL URL. A second seed fails if an owner already exists; it does not reset the existing password. Use the normal identity workflow to change credentials. Create production administrators separately using fresh credentials and a controlled deployment procedure.
|
||||||
|
|
@ -23,6 +23,8 @@
|
||||||
"start:watch": "node --watch dist/main.js",
|
"start:watch": "node --watch dist/main.js",
|
||||||
"db:status": "prisma migrate status",
|
"db:status": "prisma migrate status",
|
||||||
"bootstrap:owner": "node dist/cli/bootstrap-owner.js",
|
"bootstrap:owner": "node dist/cli/bootstrap-owner.js",
|
||||||
|
"admin:prepare-local": "node dist/cli/local-admin.js prepare",
|
||||||
|
"admin:seed-local": "node dist/cli/local-admin.js seed",
|
||||||
"db:test": "node scripts/test-migrations.mjs",
|
"db:test": "node scripts/test-migrations.mjs",
|
||||||
"migrations:check": "node scripts/check-migrations.mjs",
|
"migrations:check": "node scripts/check-migrations.mjs",
|
||||||
"security:audit": "pnpm audit --prod --audit-level=high"
|
"security:audit": "pnpm audit --prod --audit-level=high"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
import 'dotenv/config';
|
||||||
|
import { spawnSync } from 'node:child_process';
|
||||||
|
import { readFile, writeFile } from 'node:fs/promises';
|
||||||
|
import { resolve } from 'node:path';
|
||||||
|
import { parse } from 'dotenv';
|
||||||
|
import {
|
||||||
|
assertLocalAdminTarget,
|
||||||
|
localAdminSettings,
|
||||||
|
} from '../identity/local-admin-seed';
|
||||||
|
|
||||||
|
const credentialFile = resolve('.env.local-admin');
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
assertLocalAdminTarget(process.env.NODE_ENV, process.env.DATABASE_URL);
|
||||||
|
const mode = process.argv[2];
|
||||||
|
if (mode === 'prepare') {
|
||||||
|
const settings = localAdminSettings();
|
||||||
|
await writeFile(
|
||||||
|
credentialFile,
|
||||||
|
Object.entries(settings)
|
||||||
|
.map(([key, value]) => `${key}=${value}`)
|
||||||
|
.join('\n') + '\n',
|
||||||
|
{ flag: 'wx', mode: 0o600 },
|
||||||
|
);
|
||||||
|
console.log('Local admin credentials prepared in .env.local-admin');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mode === 'seed') {
|
||||||
|
const settings = parse(await readFile(credentialFile));
|
||||||
|
const result = spawnSync(
|
||||||
|
process.execPath,
|
||||||
|
[resolve('dist/cli/bootstrap-owner.js')],
|
||||||
|
{
|
||||||
|
env: { ...process.env, ...settings },
|
||||||
|
stdio: 'inherit',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (result.error) throw result.error;
|
||||||
|
if (result.status !== 0) process.exitCode = result.status ?? 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new Error('Use prepare or seed');
|
||||||
|
}
|
||||||
|
|
||||||
|
void main().catch((error: unknown) => {
|
||||||
|
console.error(
|
||||||
|
error instanceof Error ? error.message : 'Local admin setup failed',
|
||||||
|
);
|
||||||
|
process.exitCode = 1;
|
||||||
|
});
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
import { randomBytes } from 'node:crypto';
|
||||||
|
|
||||||
|
export function assertLocalAdminTarget(nodeEnv?: string, databaseUrl?: string) {
|
||||||
|
if (nodeEnv !== 'development')
|
||||||
|
throw new Error('Local admin seed requires NODE_ENV=development');
|
||||||
|
let url: URL;
|
||||||
|
try {
|
||||||
|
url = new URL(databaseUrl ?? '');
|
||||||
|
} catch {
|
||||||
|
throw new Error('Local admin seed requires a valid PostgreSQL URL');
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
!['postgresql:', 'postgres:'].includes(url.protocol) ||
|
||||||
|
!['localhost', '127.0.0.1', '[::1]'].includes(url.hostname)
|
||||||
|
)
|
||||||
|
throw new Error('Local admin seed requires a loopback PostgreSQL host');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function localAdminSettings() {
|
||||||
|
return {
|
||||||
|
ORGANIZATION_NAME: 'Mani Candles Local',
|
||||||
|
OWNER_NAME: 'Local Admin',
|
||||||
|
OWNER_EMAIL: 'admin@manicandles.local',
|
||||||
|
OWNER_PASSWORD: randomBytes(32).toString('base64url'),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
import {
|
||||||
|
assertLocalAdminTarget,
|
||||||
|
localAdminSettings,
|
||||||
|
} from '../src/identity/local-admin-seed';
|
||||||
|
|
||||||
|
describe('local admin seed guard', () => {
|
||||||
|
it('accepts only development against loopback PostgreSQL', () => {
|
||||||
|
expect(() =>
|
||||||
|
assertLocalAdminTarget(
|
||||||
|
'development',
|
||||||
|
'postgresql://u:p@127.0.0.1:5432/local',
|
||||||
|
),
|
||||||
|
).not.toThrow();
|
||||||
|
expect(() =>
|
||||||
|
assertLocalAdminTarget('production', 'postgresql://u:p@127.0.0.1/local'),
|
||||||
|
).toThrow();
|
||||||
|
expect(() =>
|
||||||
|
assertLocalAdminTarget(
|
||||||
|
'development',
|
||||||
|
'postgresql://u:p@db.example.com/local',
|
||||||
|
),
|
||||||
|
).toThrow();
|
||||||
|
expect(() =>
|
||||||
|
assertLocalAdminTarget('development', 'https://localhost/local'),
|
||||||
|
).toThrow();
|
||||||
|
});
|
||||||
|
it('generates a unique strong password without a committed default', () => {
|
||||||
|
const first = localAdminSettings();
|
||||||
|
const second = localAdminSettings();
|
||||||
|
expect(first.OWNER_EMAIL).toBe('admin@manicandles.local');
|
||||||
|
expect(first.OWNER_PASSWORD.length).toBeGreaterThanOrEqual(40);
|
||||||
|
expect(first.OWNER_PASSWORD).not.toBe(second.OWNER_PASSWORD);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in New Issue