-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1348 lines (1224 loc) · 51.9 KB
/
server.js
File metadata and controls
1348 lines (1224 loc) · 51.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import express from 'express';
import session from 'express-session';
import passport from 'passport';
import { Strategy as SamlStrategy } from '@node-saml/passport-saml';
import cors from 'cors';
import pg from 'pg';
import mysql from 'mysql2/promise';
import oracledb from 'oracledb';
import fs from 'fs';
import dotenv from 'dotenv';
import winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
dotenv.config({ path: '.env.server' });
const app = express();
const PORT = process.env.PORT || 3002;
// Create logs directory
if (!fs.existsSync('./logs')) fs.mkdirSync('./logs');
// Winston logger configuration
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { service: 'db-client' },
transports: [
// Activity logs (user actions)
new DailyRotateFile({
filename: 'logs/activity-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '90d',
level: 'info',
format: winston.format.combine(
winston.format((info) => info.type === 'ACTIVITY' || info.type === 'COMPLIANCE' ? info : false)(),
winston.format.json()
)
}),
// Error logs
new DailyRotateFile({
filename: 'logs/error-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '90d',
level: 'error'
}),
// Security audit logs
new DailyRotateFile({
filename: 'logs/security-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '365d',
level: 'warn',
format: winston.format.combine(
winston.format((info) => info.type === 'SECURITY' ? info : false)(),
winston.format.json()
)
}),
// Console output (simplified)
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.printf(({ level, message, timestamp, user, action }) => {
const userStr = user ? `[${user}]` : '';
const actionStr = action ? `[${action}]` : '';
return `${timestamp} ${level} ${userStr}${actionStr} ${message}`;
})
)
})
]
});
// Logging utility
const log = {
info: (msg, user = null, meta = {}) => logger.info(msg, { user, ...meta }),
warn: (msg, user = null, meta = {}) => logger.warn(msg, { user, ...meta }),
error: (msg, user = null, err = null, meta = {}) => logger.error(msg, { user, error: err?.message, stack: err?.stack, ...meta }),
activity: (action, user, details = '', meta = {}) => logger.info(`${action}: ${details}`, { user, action, details, type: 'ACTIVITY', ...meta }),
security: (event, user, details = '', meta = {}) => logger.warn(`SECURITY: ${event}`, { user, event, details, type: 'SECURITY', ...meta }),
compliance: (event, user, details = '', meta = {}) => logger.info(`COMPLIANCE: ${event}`, { user, event, details, type: 'COMPLIANCE', ...meta })
};
const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:5173';
const SESSION_SECRET = process.env.SESSION_SECRET || 'db-browser-secret';
const INACTIVITY_TIMEOUT = parseInt(process.env.INACTIVITY_TIMEOUT) || 5 * 60 * 1000; // 5 minutes
const MAX_SESSION_AGE = parseInt(process.env.MAX_SESSION_AGE) || 60 * 60 * 1000; // 60 minutes
// Track active sessions per user (single session enforcement)
const activeSessions = new Map(); // email -> sessionID
// Inactivity timeout tracking
const sessionTimeouts = new Map(); // sessionID -> timeout
const activeQueries = new Map(); // sessionID -> boolean (query running)
const resetInactivityTimer = (sessionID, email) => {
// Don't reset timer if query is actively running
if (activeQueries.get(sessionID)) {
return;
}
// Clear existing timeout
if (sessionTimeouts.has(sessionID)) {
clearTimeout(sessionTimeouts.get(sessionID));
}
// Set new timeout
const timeout = setTimeout(() => {
// Double check no query is running before expiring
if (activeQueries.get(sessionID)) {
resetInactivityTimer(sessionID, email); // Reschedule
return;
}
log.activity('SESSION_EXPIRED', email, 'Inactivity timeout', { sessionId: sessionID, timeout: INACTIVITY_TIMEOUT });
log.security('SESSION_TIMEOUT', email, `Inactivity: ${INACTIVITY_TIMEOUT}ms`, { sessionId: sessionID });
// Clean up session
const dbClient = dbClients.get(sessionID);
if (dbClient) {
const { type, client, conn } = dbClient;
if (type === 'postgres' && client) client.end().catch(() => {});
else if (type === 'mysql' && conn) conn.end().catch(() => {});
dbClients.delete(sessionID);
}
activeSessions.delete(email);
sessionTimeouts.delete(sessionID);
activeQueries.delete(sessionID);
}, INACTIVITY_TIMEOUT);
sessionTimeouts.set(sessionID, timeout);
};
app.use((req, res, next) => {
res.setHeader("Content-Security-Policy", "default-src 'self' https: data: 'unsafe-inline'");
next();
});
app.use(cors({ origin: FRONTEND_URL, credentials: true }));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false,
rolling: true,
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: MAX_SESSION_AGE,
sameSite: 'lax'
}
}));
app.use(passport.initialize());
app.use(passport.session());
// Log unauthorized access attempts
app.use((req, res, next) => {
if (req.path.startsWith('/api/db') && !req.isAuthenticated()) {
log.security('UNAUTHORIZED_ACCESS_ATTEMPT', 'anonymous', `${req.method} ${req.path}`, {
ip: req.ip,
userAgent: req.headers['user-agent'],
path: req.path,
method: req.method
});
}
next();
});
// Middleware to track activity and enforce single session
app.use((req, res, next) => {
if (req.isAuthenticated() && req.user) {
const email = req.user.email;
const currentSessionID = req.sessionID;
// Check if user has another active session
if (activeSessions.has(email)) {
const existingSessionID = activeSessions.get(email);
if (existingSessionID !== currentSessionID) {
// Terminate old session
const oldDbClient = dbClients.get(existingSessionID);
if (oldDbClient) {
const { type, client, conn } = oldDbClient;
if (type === 'postgres' && client) client.end().catch(() => {});
else if (type === 'mysql' && conn) conn.end().catch(() => {});
dbClients.delete(existingSessionID);
}
if (sessionTimeouts.has(existingSessionID)) {
clearTimeout(sessionTimeouts.get(existingSessionID));
sessionTimeouts.delete(existingSessionID);
}
log.warn(`Previous session terminated`, email);
log.security('SESSION_TERMINATED', email, 'Single session enforcement', { oldSessionId: existingSessionID, newSessionId: currentSessionID });
}
}
// Set current session as active
activeSessions.set(email, currentSessionID);
log.activity('LOGIN', email, `Session: ${currentSessionID}`, { sessionId: currentSessionID, ip: req.ip });
log.security('USER_LOGIN', email, `IP: ${req.ip}`, { sessionId: currentSessionID, ip: req.ip, userAgent: req.headers['user-agent'] });
// Reset inactivity timer
resetInactivityTimer(currentSessionID, email);
}
next();
});
let samlConfig = JSON.parse(fs.readFileSync('./saml-config.json', 'utf8'));
passport.use(new SamlStrategy({
entryPoint: samlConfig.entryPoint,
issuer: samlConfig.issuer,
identityProviderIssuer: samlConfig.identityProviderIssuer,
callbackUrl: samlConfig.callbackUrl,
cert: samlConfig.cert
}, (profile, done) => {
const user = {
email: profile.email || profile['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'] || profile.nameID,
name: profile.firstName || profile.displayName || profile.nameID || 'User'
};
done(null, user);
}));
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
app.get('/api/auth/saml', passport.authenticate('saml'));
app.post('/api/auth/saml/callback', passport.authenticate('saml', {
failureRedirect: '/',
failureFlash: true
}), (req, res) => {
log.activity('SAML_AUTH_SUCCESS', req.user.email, `IP: ${req.ip}`, { ip: req.ip });
log.security('SAML_AUTHENTICATION', req.user.email, 'Success', { ip: req.ip, userAgent: req.headers['user-agent'] });
res.redirect(FRONTEND_URL);
}, (err, req, res, next) => {
log.error('SAML authentication failed', null, err, { ip: req.ip });
log.security('SAML_AUTH_FAILED', 'unknown', err.message, { ip: req.ip });
res.status(500).send('Authentication failed: ' + err.message);
});
app.get('/api/auth/user', (req, res) => req.isAuthenticated() ? res.json(req.user) : res.status(401).json({ error: 'Not authenticated' }));
app.post('/api/auth/logout', (req, res) => {
const email = req.user?.email;
const sessionID = req.sessionID;
log.activity('LOGOUT', email, `Session: ${sessionID}`, { sessionId: sessionID, ip: req.ip });
log.security('USER_LOGOUT', email, `IP: ${req.ip}`, { sessionId: sessionID, ip: req.ip });
// Clean up database connection
const dbClient = dbClients.get(sessionID);
if (dbClient) {
const { type, client, conn } = dbClient;
if (type === 'postgres' && client) client.end().catch(() => {});
else if (type === 'mysql' && conn) conn.end().catch(() => {});
dbClients.delete(sessionID);
log.compliance('DB_DISCONNECT_ON_LOGOUT', email, type, { sessionId: sessionID });
}
// Clean up session tracking
if (email) {
activeSessions.delete(email);
}
if (sessionTimeouts.has(sessionID)) {
clearTimeout(sessionTimeouts.get(sessionID));
sessionTimeouts.delete(sessionID);
}
req.logout(() => {
req.session.destroy(() => {
res.clearCookie('connect.sid');
res.json({ success: true });
});
});
});
const dbClients = new Map(); // Store clients by session ID
app.post('/api/db/connect', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const { type, host, port, database, username, password, serviceName } = req.body;
const user = req.user.email;
try {
if (type === 'postgres') {
const client = new pg.Client({ host, port, database, user: username, password });
await client.connect();
dbClients.set(req.sessionID, { type, client });
req.session.dbConnected = true;
req.session.dbType = type;
log.activity('DB_CONNECT', user, `${type}://${host}:${port}/${database}`, { dbType: type, host, port, database, dbUser: username });
log.security('DATABASE_ACCESS', user, `Connected to ${type}`, { dbType: type, host, port, database, dbUser: username, ip: req.ip });
log.compliance('DATA_ACCESS_GRANTED', user, `${type}://${host}/${database}`, { dbType: type, host, database, dbUser: username });
res.json({ success: true });
} else if (type === 'mysql') {
const conn = await mysql.createConnection({ host, port, database, user: username, password });
dbClients.set(req.sessionID, { type, conn });
req.session.dbConnected = true;
req.session.dbType = type;
log.activity('DB_CONNECT', user, `${type}://${host}:${port}/${database}`, { dbType: type, host, port, database, dbUser: username });
log.security('DATABASE_ACCESS', user, `Connected to ${type}`, { dbType: type, host, port, database, dbUser: username, ip: req.ip });
log.compliance('DATA_ACCESS_GRANTED', user, `${type}://${host}/${database}`, { dbType: type, host, database, dbUser: username });
res.json({ success: true });
} else if (type === 'oracle') {
const connectString = `${host}:${port}/${serviceName || database}`;
const conn = await oracledb.getConnection({ user: username, password, connectString });
dbClients.set(req.sessionID, { type, conn });
req.session.dbConnected = true;
req.session.dbType = type;
log.activity('DB_CONNECT', user, `${type}://${connectString}`, { dbType: type, host, port, database, dbUser: username });
log.security('DATABASE_ACCESS', user, `Connected to ${type}`, { dbType: type, host, port, database, dbUser: username, ip: req.ip });
log.compliance('DATA_ACCESS_GRANTED', user, `${type}://${connectString}`, { dbType: type, host, database, dbUser: username });
res.json({ success: true });
} else {
res.status(400).json({ error: 'Invalid database type' });
}
} catch (err) {
log.error('Database connection failed', user, err, { dbType: type, host, port, database });
log.security('DB_CONNECTION_FAILED', user, err.message, { dbType: type, host, port, database, ip: req.ip });
res.status(500).json({ error: err.message });
}
});
const getDbClient = (sessionID) => {
return dbClients.get(sessionID);
};
const isMutatingQuery = (sql) => {
const normalized = sql.trim().toUpperCase();
return /^(INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|TRUNCATE|REPLACE)\s/.test(normalized);
};
app.post('/api/db/query', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const { query } = req.body;
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
const user = req.user.email;
if (!query || !query.trim()) return res.status(400).json({ error: 'Query cannot be empty' });
if (!type) return res.status(400).json({ error: 'Not connected to database' });
const isMutating = isMutatingQuery(query);
const MAX_ROWS = 10000; // Limit to prevent memory issues
const queryPreview = query.length > 100 ? query.substring(0, 100) + '...' : query;
// Mark query as active to prevent session timeout
activeQueries.set(req.sessionID, true);
log.activity('QUERY_EXECUTE', user, `${isMutating ? '[MUTATING] ' : ''}${queryPreview.replace(/\n/g, ' ')}`, {
dbType: type,
isMutating,
queryLength: query.length,
ip: req.ip
});
if (isMutating) {
log.security('MUTATING_QUERY', user, queryPreview.replace(/\n/g, ' '), { dbType: type, ip: req.ip });
log.compliance('DATA_MODIFICATION_ATTEMPT', user, 'Mutating query executed', { dbType: type, queryPreview });
}
try {
if (type === 'postgres') {
if (isMutating) await client.query('BEGIN');
const result = await client.query(query);
// Limit rows if too many
const rows = result.rows.length > MAX_ROWS ? result.rows.slice(0, MAX_ROWS) : result.rows;
const truncated = result.rows.length > MAX_ROWS;
log.info(`Query returned ${result.rowCount} rows${truncated ? ' (truncated)' : ''}`, user, {
rowCount: result.rowCount,
truncated,
dbType: type
});
if (truncated) {
log.compliance('RESULT_TRUNCATED', user, `${result.rows.length} rows truncated to ${MAX_ROWS}`, {
totalRows: result.rows.length,
returnedRows: MAX_ROWS
});
}
res.json({
rows,
fields: result.fields.map(f => f.name),
isMutating,
rowCount: result.rowCount,
truncated,
totalRows: result.rows.length
});
} else if (type === 'mysql') {
if (isMutating) await conn.query('START TRANSACTION');
// MySQL: Use query() for DDL and SELECT with LIMIT, execute() for others
const isDDL = /^\s*(CREATE|ALTER|DROP|TRUNCATE|RENAME|GRANT|REVOKE)\s/i.test(query);
const isSelectWithLimit = /^\s*SELECT.*LIMIT/is.test(query);
let rows, fields;
if (isDDL || isSelectWithLimit) {
[rows, fields] = await conn.query(query);
} else {
[rows, fields] = await conn.execute(query);
}
// Limit rows if too many
const limitedRows = Array.isArray(rows) && rows.length > MAX_ROWS ? rows.slice(0, MAX_ROWS) : rows;
const truncated = Array.isArray(rows) && rows.length > MAX_ROWS;
const rowCount = Array.isArray(rows) ? rows.length : rows.affectedRows;
log.info(`Query returned ${rowCount} rows${truncated ? ' (truncated)' : ''}`, user, {
rowCount,
truncated,
dbType: type
});
if (truncated) {
log.compliance('RESULT_TRUNCATED', user, `${rows.length} rows truncated to ${MAX_ROWS}`, {
totalRows: rows.length,
returnedRows: MAX_ROWS
});
}
res.json({
rows: limitedRows,
fields: fields.map(f => f.name),
isMutating,
rowCount,
truncated,
totalRows: Array.isArray(rows) ? rows.length : rows.affectedRows
});
} else if (type === 'oracle') {
if (isMutating) await conn.execute('BEGIN NULL; END;'); // Oracle transaction start
// Oracle: Remove trailing semicolons (not supported in execute())
const cleanQuery = query.trim().replace(/;+$/, '');
const result = await conn.execute(cleanQuery, [], {
outFormat: oracledb.OUT_FORMAT_OBJECT,
maxRows: MAX_ROWS + 1 // Fetch one extra to detect truncation
});
const rows = result.rows || [];
const truncated = rows.length > MAX_ROWS;
const limitedRows = truncated ? rows.slice(0, MAX_ROWS) : rows;
const rowCount = result.rowsAffected || rows.length;
log.info(`Query returned ${rowCount} rows${truncated ? ' (truncated)' : ''}`, user, {
rowCount,
truncated,
dbType: type
});
if (truncated) {
log.compliance('RESULT_TRUNCATED', user, `${rows.length} rows truncated to ${MAX_ROWS}`, {
totalRows: rows.length,
returnedRows: MAX_ROWS
});
}
// Extract field names from first row
const fields = limitedRows.length > 0 ? Object.keys(limitedRows[0]) : [];
res.json({
rows: limitedRows,
fields,
isMutating,
rowCount,
truncated,
totalRows: rows.length
});
}
} catch (err) {
log.error('Query execution failed', user, err, { dbType: type, queryPreview, isMutating });
log.security('QUERY_FAILED', user, err.message, { dbType: type, isMutating, ip: req.ip });
if (isMutating) {
try {
if (type === 'postgres') await client.query('ROLLBACK');
else if (type === 'mysql') await conn.query('ROLLBACK');
else if (type === 'oracle') await conn.execute('ROLLBACK');
log.warn('Auto-rollback on query error', user, { dbType: type });
log.compliance('AUTO_ROLLBACK', user, 'Transaction rolled back due to error', { dbType: type });
} catch (rollbackErr) {
log.error('Auto-rollback failed', user, rollbackErr, { dbType: type });
log.security('ROLLBACK_FAILED', user, rollbackErr.message, { dbType: type });
}
}
res.status(500).json({ error: err.message });
} finally {
// Mark query as complete, allow session timeout to resume
activeQueries.set(req.sessionID, false);
resetInactivityTimer(req.sessionID, user);
}
});
app.post('/api/db/commit', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
const user = req.user.email;
try {
if (type === 'postgres') await client.query('COMMIT');
else if (type === 'mysql') await conn.query('COMMIT');
else if (type === 'oracle') await conn.commit();
log.activity('TRANSACTION_COMMIT', user, type, { dbType: type, ip: req.ip });
log.compliance('DATA_MODIFICATION_COMMITTED', user, 'Transaction committed', { dbType: type });
res.json({ success: true });
} catch (err) {
log.error('Transaction commit failed', user, err, { dbType: type });
log.security('COMMIT_FAILED', user, err.message, { dbType: type, ip: req.ip });
res.status(500).json({ error: err.message });
}
});
app.post('/api/db/rollback', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
const user = req.user.email;
try {
if (type === 'postgres') await client.query('ROLLBACK');
else if (type === 'mysql') await conn.query('ROLLBACK');
else if (type === 'oracle') await conn.rollback();
log.activity('TRANSACTION_ROLLBACK', user, type, { dbType: type, ip: req.ip });
log.compliance('DATA_MODIFICATION_REVERTED', user, 'Transaction rolled back', { dbType: type });
res.json({ success: true });
} catch (err) {
log.error('Transaction rollback failed', user, err, { dbType: type });
log.security('ROLLBACK_FAILED', user, err.message, { dbType: type, ip: req.ip });
res.status(500).json({ error: err.message });
}
});
app.post('/api/db/schema', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const tables = await client.query(`
SELECT table_name, table_type, table_schema
FROM information_schema.tables
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name
`);
const schemas = await client.query(`
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema')
ORDER BY schema_name
`);
res.json({ tables: tables.rows, schemas: schemas.rows });
} else if (type === 'mysql') {
const [tables] = await conn.execute(`
SELECT table_name, table_type, table_schema
FROM information_schema.tables
WHERE table_schema = DATABASE()
ORDER BY table_name
`);
const [schemas] = await conn.execute(`
SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
ORDER BY schema_name
`);
res.json({ tables, schemas });
} else if (type === 'oracle') {
const tables = await conn.execute(`
SELECT table_name, 'TABLE' as table_type, owner as table_schema
FROM all_tables
WHERE owner NOT IN ('SYS', 'SYSTEM', 'OUTLN', 'DBSNMP')
ORDER BY owner, table_name
`);
const schemas = await conn.execute(`
SELECT DISTINCT owner as schema_name
FROM all_tables
WHERE owner NOT IN ('SYS', 'SYSTEM', 'OUTLN', 'DBSNMP')
ORDER BY owner
`);
res.json({
tables: tables.rows.map(r => ({ table_name: r[0], table_type: r[1], table_schema: r[2] })),
schemas: schemas.rows.map(r => ({ schema_name: r[0] }))
});
}
} catch (err) {
log.error('Schema load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
app.post('/api/db/table-info', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const { tableName, schema } = req.body;
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
if (!tableName) return res.status(400).json({ error: 'Table name required' });
try {
if (type === 'postgres') {
const columns = await client.query(`
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = $1 AND table_schema = $2
ORDER BY ordinal_position
`, [tableName, schema || 'public']);
res.json({ columns: columns.rows });
} else if (type === 'mysql') {
const [columns] = await conn.execute(`
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = ? AND table_schema = ?
ORDER BY ordinal_position
`, [tableName, schema || conn.config.database]);
res.json({ columns });
}
} catch (err) {
log.error('Table info load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
app.post('/api/db/table-data', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const { tableName, schema, limit = 100, offset = 0 } = req.body;
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
if (!tableName) return res.status(400).json({ error: 'Table name required' });
try {
if (type === 'postgres') {
const fullName = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
const result = await client.query(`SELECT * FROM ${fullName} LIMIT $1 OFFSET $2`, [limit, offset]);
res.json({ rows: result.rows, fields: result.fields.map(f => f.name) });
} else if (type === 'mysql') {
const fullName = schema ? `\`${schema}\`.\`${tableName}\`` : `\`${tableName}\``;
const [rows, fields] = await conn.execute(`SELECT * FROM ${fullName} LIMIT ? OFFSET ?`, [limit, offset]);
res.json({ rows, fields: fields.map(f => f.name) });
} else if (type === 'oracle') {
const fullName = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;
const result = await conn.execute(`SELECT * FROM ${fullName} OFFSET :offset ROWS FETCH NEXT :limit ROWS ONLY`, { offset, limit });
res.json({
rows: result.rows.map(r => {
const obj = {};
result.metaData.forEach((col, i) => obj[col.name] = r[i]);
return obj;
}),
fields: result.metaData.map(c => c.name)
});
}
} catch (err) {
log.error('Table data load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get all functions/procedures
app.post('/api/db/functions', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT routine_name, routine_schema, routine_type, data_type as return_type
FROM information_schema.routines
WHERE routine_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY routine_schema, routine_name
`);
res.json({ functions: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT routine_name, routine_schema, routine_type, data_type as return_type
FROM information_schema.routines
WHERE routine_schema = DATABASE()
ORDER BY routine_name
`);
res.json({ functions: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT object_name as routine_name, owner as routine_schema, object_type as routine_type
FROM all_objects
WHERE object_type IN ('FUNCTION', 'PROCEDURE', 'PACKAGE')
AND owner NOT IN ('SYS', 'SYSTEM')
ORDER BY owner, object_name
`);
res.json({
functions: result.rows.map(r => ({
routine_name: r[0],
routine_schema: r[1],
routine_type: r[2]
}))
});
}
} catch (err) {
log.error('Functions load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get all views
app.post('/api/db/views', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT table_name, table_schema, view_definition
FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name
`);
res.json({ views: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT table_name, table_schema, view_definition
FROM information_schema.views
WHERE table_schema = DATABASE()
ORDER BY table_name
`);
res.json({ views: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT view_name as table_name, owner as table_schema, text as view_definition
FROM all_views
WHERE owner NOT IN ('SYS', 'SYSTEM')
ORDER BY owner, view_name
`);
res.json({
views: result.rows.map(r => ({
table_name: r[0],
table_schema: r[1],
view_definition: r[2]
}))
});
}
} catch (err) {
log.error('Views load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get all indexes
app.post('/api/db/indexes', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT indexname, tablename, schemaname, indexdef
FROM pg_indexes
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY schemaname, tablename, indexname
`);
res.json({ indexes: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT index_name as indexname, table_name as tablename, table_schema as schemaname,
index_type, non_unique
FROM information_schema.statistics
WHERE table_schema = DATABASE()
GROUP BY index_name, table_name, table_schema, index_type, non_unique
ORDER BY table_name, index_name
`);
res.json({ indexes: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT index_name as indexname, table_name as tablename, owner as schemaname,
index_type, uniqueness
FROM all_indexes
WHERE owner NOT IN ('SYS', 'SYSTEM')
ORDER BY owner, table_name, index_name
`);
res.json({
indexes: result.rows.map(r => ({
indexname: r[0],
tablename: r[1],
schemaname: r[2],
index_type: r[3],
uniqueness: r[4]
}))
});
}
} catch (err) {
log.error('Indexes load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get all sequences
app.post('/api/db/sequences', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT sequence_name, sequence_schema, data_type, start_value, increment
FROM information_schema.sequences
WHERE sequence_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY sequence_schema, sequence_name
`);
res.json({ sequences: result.rows });
} else if (type === 'mysql') {
// MySQL doesn't have sequences in the same way
res.json({ sequences: [] });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT sequence_name, sequence_owner as sequence_schema, min_value, max_value, increment_by
FROM all_sequences
WHERE sequence_owner NOT IN ('SYS', 'SYSTEM')
ORDER BY sequence_owner, sequence_name
`);
res.json({
sequences: result.rows.map(r => ({
sequence_name: r[0],
sequence_schema: r[1],
min_value: r[2],
max_value: r[3],
increment_by: r[4]
}))
});
}
} catch (err) {
log.error('Sequences load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get all triggers
app.post('/api/db/triggers', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT trigger_name, event_object_table as table_name, trigger_schema,
event_manipulation, action_timing
FROM information_schema.triggers
WHERE trigger_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY trigger_schema, event_object_table, trigger_name
`);
res.json({ triggers: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT trigger_name, event_object_table as table_name, trigger_schema,
event_manipulation, action_timing
FROM information_schema.triggers
WHERE trigger_schema = DATABASE()
ORDER BY event_object_table, trigger_name
`);
res.json({ triggers: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT trigger_name, table_name, owner as trigger_schema,
triggering_event, trigger_type, status
FROM all_triggers
WHERE owner NOT IN ('SYS', 'SYSTEM')
ORDER BY owner, table_name, trigger_name
`);
res.json({
triggers: result.rows.map(r => ({
trigger_name: r[0],
table_name: r[1],
trigger_schema: r[2],
triggering_event: r[3],
trigger_type: r[4],
status: r[5]
}))
});
}
} catch (err) {
log.error('Triggers load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// EXPLAIN query
app.post('/api/db/explain', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const { query } = req.body;
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
if (!query || !query.trim()) return res.status(400).json({ error: 'Query cannot be empty' });
try {
if (type === 'postgres') {
const result = await client.query(`EXPLAIN (FORMAT JSON, ANALYZE false) ${query}`);
res.json({ plan: result.rows[0]['QUERY PLAN'] });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`EXPLAIN FORMAT=JSON ${query}`);
res.json({ plan: JSON.parse(rows[0]['EXPLAIN']) });
} else if (type === 'oracle') {
await conn.execute(`EXPLAIN PLAN FOR ${query}`);
const result = await conn.execute(`
SELECT plan_table_output
FROM TABLE(DBMS_XPLAN.DISPLAY())
`);
res.json({ plan: result.rows.map(r => r[0]).join('\n') });
}
} catch (err) {
log.error('EXPLAIN failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get active sessions
app.post('/api/db/sessions', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT pid, usename, application_name, client_addr, state,
query, query_start, state_change, wait_event_type, wait_event
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
ORDER BY query_start DESC
`);
res.json({ sessions: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT id, user, host, db, command, time, state, info as query
FROM information_schema.processlist
WHERE id <> CONNECTION_ID()
ORDER BY time DESC
`);
res.json({ sessions: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT s.sid, s.serial#, s.username, s.program, s.status,
s.sql_id, sq.sql_text as query
FROM v$session s
LEFT JOIN v$sql sq ON s.sql_id = sq.sql_id
WHERE s.type = 'USER' AND s.username IS NOT NULL
ORDER BY s.logon_time DESC
`);
res.json({
sessions: result.rows.map(r => ({
sid: r[0], serial: r[1], username: r[2], program: r[3],
status: r[4], sql_id: r[5], query: r[6]
}))
});
}
} catch (err) {
log.error('Sessions load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});
// Get locks
app.post('/api/db/locks', async (req, res) => {
if (!req.isAuthenticated()) return res.status(401).json({ error: 'Not authenticated' });
const dbClient = getDbClient(req.sessionID); if (!dbClient) return res.status(400).json({ error: "Not connected to database" }); const { type, client, conn } = dbClient;
try {
if (type === 'postgres') {
const result = await client.query(`
SELECT l.locktype, l.relation::regclass as relation, l.mode, l.granted,
a.pid, a.usename, a.query, a.query_start
FROM pg_locks l
JOIN pg_stat_activity a ON l.pid = a.pid
WHERE l.pid <> pg_backend_pid()
ORDER BY l.granted, a.query_start
`);
res.json({ locks: result.rows });
} else if (type === 'mysql') {
const [rows] = await conn.execute(`
SELECT r.trx_id, r.trx_state, r.trx_started, r.trx_wait_started,
r.trx_mysql_thread_id, l.lock_mode, l.lock_type, l.lock_table
FROM information_schema.innodb_trx r
LEFT JOIN information_schema.innodb_locks l ON r.trx_id = l.lock_trx_id
ORDER BY r.trx_started
`);
res.json({ locks: rows });
} else if (type === 'oracle') {
const result = await conn.execute(`
SELECT s.sid, s.serial#, s.username, l.type, l.lmode, l.request,
o.object_name, s.program
FROM v$lock l
JOIN v$session s ON l.sid = s.sid
LEFT JOIN dba_objects o ON l.id1 = o.object_id
WHERE s.username IS NOT NULL
ORDER BY s.username, o.object_name
`);
res.json({
locks: result.rows.map(r => ({
sid: r[0], serial: r[1], username: r[2], type: r[3],
lmode: r[4], request: r[5], object_name: r[6], program: r[7]
}))
});
}
} catch (err) {
log.error('Locks load failed', req.user.email, err);
res.status(500).json({ error: err.message });
}
});