-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (230 loc) · 8.89 KB
/
Copy pathindex.js
File metadata and controls
260 lines (230 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// VeriNode Backend entrypoint.
//
// Bootstraps the centralized configuration system first, then initializes
// the OpenTelemetry tracer (see ./src/diagnostics/tracer.ts) so every
// downstream module that imports @opentelemetry/api inherits the configured
// global TracerProvider. The tracer module is written in TypeScript; we try
// the compiled CJS output first and fall back to a ts-node runtime compile
// when dist/ is not present (typical of dev).
const express = require('express');
// ---- Module loader for TypeScript sources ----
function loadTsModule(modulePath) {
const tryPaths = [
() => require('./dist/' + modulePath),
() => {
require('ts-node').register({ transpileOnly: true, project: './tsconfig.json' });
return require('./src/' + modulePath);
},
];
for (const load of tryPaths) {
try {
return load();
} catch (err) {
// try next path
}
}
return null;
}
// ---- Config ----
const configModule = loadTsModule('config/index');
const { initConfig, getConfig, getConfigValue, onConfigChange, reloadConfig } = configModule || {};
// ---- Express app ----
const app = express();
async function bootstrap() {
// 1. Initialize centralized configuration
if (initConfig) {
await initConfig({
configFile: process.env.CONFIG_FILE || './config.json',
watchFiles: [process.env.CONFIG_FILE || './config.json'],
});
console.log('[index] Config initialized');
// Handle SIGHUP for hot reload
process.on('SIGHUP', async () => {
if (reloadConfig) {
console.log('[index] Reloading configuration...');
await reloadConfig();
console.log('[index] Configuration reloaded');
}
});
} else {
console.warn('[index] Config module not loaded; running with env defaults');
}
// 1b. Start config drift auditor + expose debug endpoints
const driftModule = loadTsModule('config-drift/auditor');
const driftRoutesModule = loadTsModule('config-drift/routes');
if (driftModule && driftRoutesModule) {
try {
const { createConfigDriftAuditorFromEnv } = driftModule;
const { registerConfigDriftRoutes } = driftRoutesModule;
const auditor = createConfigDriftAuditorFromEnv({});
auditor.init().then(() => {
auditor.start();
registerConfigDriftRoutes(app, auditor);
console.log('[config-drift] Auditor started');
});
// best-effort shutdown hook
const shutdownHandler = () => {
try {
auditor.stop();
} catch {
// noop
}
};
process.once('SIGINT', shutdownHandler);
process.once('SIGTERM', shutdownHandler);
} catch (err) {
console.warn('[config-drift] Failed to start auditor:', (err && err.message) ? err.message : String(err));
}
} else {
console.warn('[config-drift] Drift modules not loaded');
}
// 2. Initialize tracing
const tracing = loadTsModule('diagnostics/tracer');
if (tracing && typeof tracing.initTracingFromConfig === 'function') {
const otelCfg = getConfigValue ? getConfigValue('telemetry.otel') : null;
if (otelCfg && otelCfg.enabled !== false) {
tracing.initTracingFromConfig(otelCfg);
} else {
tracing.initTracing();
}
} else if (tracing && typeof tracing.initTracing === 'function') {
tracing.initTracing();
} else {
indexLog.warn('OpenTelemetry tracer not loaded; running without tracing');
}
global.__verinode_tracing = tracing;
// 3. Set up Express middleware
app.use(express.json());
const rateLimiterModule = loadTsModule('security/rate_limiter');
if (rateLimiterModule && typeof rateLimiterModule.createRateLimitingMiddleware === 'function') {
const rateLimitingMiddleware = rateLimiterModule.createRateLimitingMiddleware({
redisUrl: process.env.RATE_LIMIT_REDIS_URL,
endpointTiers: {
'/': 'free',
'/debug/traces/config': 'pro',
'/health/pools': 'enterprise',
'/metrics': 'free',
'/debug/config-drift': 'pro',
'/debug/config-drift/history': 'pro',
'/debug/config-drift/ui': 'pro',
'/internal/archival/renew/:contractId': 'enterprise',
},
defaultTier: 'free',
});
app.use(rateLimitingMiddleware);
}
// 4. mTLS middleware
const mtlsModule = loadTsModule('security/mtls');
const mtlsManager = mtlsModule && typeof mtlsModule.createMtlsManager === 'function'
? mtlsModule.createMtlsManager()
: (mtlsModule && typeof mtlsModule.createMtlsManagerFromEnv === 'function'
? mtlsModule.createMtlsManagerFromEnv()
: null);
global.__verinode_mtls = mtlsManager;
if (mtlsManager && mtlsManager.current) {
console.log('[index] mTLS enabled');
}
app.use((req, res, next) => {
if (!mtlsManager || !mtlsManager.current) return next();
if (!req.client.authorized) {
mtlsManager.recordHandshakeFailure();
return res.status(401).json({ error: 'mTLS client certificate required' });
}
const peerCert = req.socket.getPeerCertificate();
const mtlsConfig = mtlsManager.config || {};
if (!mtlsModule.validatePeerCertificate(peerCert, {
trustDomain: mtlsConfig.trustDomain || 'cluster.local',
allowedSpiffeIds: mtlsConfig.allowedSpiffeIds || [],
})) {
mtlsManager.recordInvalidPeerIdentity();
return res.status(403).json({ error: 'mTLS peer SPIFFE identity is not allowed' });
}
return next();
});
// 5. Routes
app.get('/', (req, res) => res.send('VeriNode API is running'));
// /debug/traces/config — required by issue #15
app.get('/debug/traces/config', (req, res) => {
const t = global.__verinode_tracing;
if (!t || typeof t.getTraceConfig !== 'function') {
return res.status(503).json({ error: 'tracing not initialised' });
}
res.json(t.getTraceConfig());
});
// /health/pools — dual-pool connection stats
app.get('/health/pools', (req, res) => {
const pools = global.__verinode_pools;
if (!pools || typeof pools.getPoolHealth !== 'function') {
return res.status(503).json({ error: 'pool router not initialised' });
}
res.json(pools.getPoolHealth());
});
// /metrics — Prometheus text-format scrape endpoint
app.get('/metrics', async (req, res) => {
const chunks = [];
const pools = global.__verinode_pools;
if (pools && typeof pools.prometheusMetrics === 'function') {
chunks.push(pools.prometheusMetrics());
}
const dlq = getDeadLetterQueue();
if (dlq && typeof dlq.prometheusMetrics === 'function') {
chunks.push(await dlq.prometheusMetrics());
}
if (mtlsManager && typeof mtlsManager.prometheusMetrics === 'function') {
chunks.push(mtlsManager.prometheusMetrics());
}
if (chunks.length === 0) {
return res.status(503).type('text/plain').send('# metrics sources not initialised\n');
}
res.type('text/plain; version=0.0.4; charset=utf-8').send(chunks.join('\n'));
});
// POST /internal/archival/renew/:contractId — required by issue #20
app.post('/internal/archival/renew/:contractId', express.json(), async (req, res) => {
const listener = app.locals.archivalListener;
if (!listener) {
return res.status(503).json({ error: 'archival listener not initialised' });
}
try {
const result = await listener.renewNow(req.params.contractId);
res.json(result);
} catch (err) {
res.status(500).json({ error: err instanceof Error ? err.message : 'renewal failed' });
}
});
// 6. Start server
const port = getConfigValue ? (getConfigValue('app.port') || 3000) : (process.env.PORT || 3000);
if (mtlsManager && mtlsManager.config && mtlsManager.config.enabled) {
mtlsManager.startRotationWatch();
const https = require('https');
const server = https.createServer(mtlsManager.serverOptions(), app);
server.on('tlsClientError', () => mtlsManager.recordHandshakeFailure());
server.listen(port, () => console.log(`mTLS server running on port ${port}`));
} else {
const httpServer = app.listen(port, () => console.log(`Server running on port ${port}`));
await bootstrapTls(httpServer, port);
}
}
function getDeadLetterQueue() {
return app.locals.deadLetterQueue || global.__verinode_dlq || null;
}
async function bootstrapTls(httpServer, httpPort) {
try {
const tlsBootstrap = loadTsModule('tls/acme_rotation');
if (tlsBootstrap && typeof tlsBootstrap.bootstrapTlsFromConfig === 'function') {
await tlsBootstrap.bootstrapTlsFromConfig(app, { httpPort });
} else if (tlsBootstrap && typeof tlsBootstrap.bootstrapTlsFromEnv === 'function') {
await tlsBootstrap.bootstrapTlsFromEnv(app, { httpPort });
}
} catch (err) {
httpServer.close();
indexLog.error('TLS ACME bootstrap failed', { 'error.message': err instanceof Error ? err.message : String(err) });
process.exitCode = 1;
}
}
if (require.main === module) {
bootstrap().catch((err) => {
console.error('[index] Bootstrap failed', err);
process.exit(1);
});
}
module.exports = app;