Hub v1: install API, licences, health dashboard, alerts

- Protocol 1 (docs/hub-protocol.md): public key, register, signed heartbeats
- Ed25519 licence tokens (30-day validity, status, expiry, grace, limits)
- Dashboard: installs, install detail, customers and licences, alerts,
  settings and audit log; two-factor login required
- Alert checks every 5 minutes, emailed once; heartbeat roll-up after 30 days
- Unit tests for encryption, tokens, signatures, TOTP and alert rules

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
This commit is contained in:
2026-10-26 12:12:51 +02:00
co-authored by Claude Opus 5.5
commit e72330893a
56 changed files with 16952 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
/**
* Create a Hub login (or reset its password and two-factor).
*
* npm run create-admin -- --email you@example.com --name "Your Name"
*
* Asks for the password on the terminal. Two-factor is set up at the first login.
*/
require('dotenv').config();
const readline = require('readline');
const bcrypt = require('bcryptjs');
const prisma = require('../src/config/db');
function arg(name) {
const i = process.argv.indexOf(`--${name}`);
return i > -1 ? process.argv[i + 1] : null;
}
function ask(question) {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, answer => { rl.close(); resolve(answer); }));
}
(async () => {
const email = String(arg('email') || '').trim().toLowerCase();
const name = arg('name') || email;
if (!email) throw new Error('Usage: npm run create-admin -- --email you@example.com --name "Your Name"');
const password = process.env.HUB_ADMIN_PASSWORD || await ask('Password (12+ characters): ');
if (String(password).length < 12) throw new Error('Use at least 12 characters.');
const hash = await bcrypt.hash(password, 12);
const admin = await prisma.adminUser.upsert({
where: { email },
update: { password: hash, name, totpEnabled: false, totpSecret: null, tokenVersion: { increment: 1 } },
create: { email, name, password: hash },
});
console.log(`Saved ${admin.email}. Log in to the Hub to set up two-factor.`);
process.exit(0);
})().catch(e => { console.error(e.message); process.exit(1); });
+30
View File
@@ -0,0 +1,30 @@
/**
* Create the Hub's licence signing key pair (Ed25519).
*
* npm run generate-keys → keys/hub-signing-key.pem (refuses to overwrite)
* npm run generate-keys -- --force → replace it (every install then needs the new public key!)
*
* The private key stays on the Hub server only — back it up somewhere safe. The public key it
* prints goes into Hope Events (backend/src/hub/publicKey.js) so installs can check licences.
*/
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const dir = path.join(__dirname, '..', 'keys');
const file = path.join(dir, 'hub-signing-key.pem');
if (fs.existsSync(file) && !process.argv.includes('--force')) {
console.error(`${file} already exists. Use --force to replace it (installs would need the new public key).`);
process.exit(1);
}
const { privateKey, publicKey } = crypto.generateKeyPairSync('ed25519');
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(file, privateKey.export({ type: 'pkcs8', format: 'pem' }), { mode: 0o600 });
console.log(`Private key written to ${file} — keep it secret and back it up.`);
console.log('Set HUB_SIGNING_KEY_FILE to that path in .env.\n');
console.log('Public key (for Hope Events, backend/src/hub/publicKey.js):\n');
console.log(publicKey.export({ type: 'spki', format: 'pem' }));