-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathnode-server.ts
More file actions
614 lines (547 loc) · 16.6 KB
/
Copy pathnode-server.ts
File metadata and controls
614 lines (547 loc) · 16.6 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
import type { IncomingHttpHeaders } from "http";
import { fromNodeProviderChain } from "@aws-sdk/credential-providers";
import aws4 from "aws4";
import bodyParser from "body-parser";
import compression from "compression";
import cors from "cors";
import express, { type NextFunction, type Response } from "express";
import fs from "fs";
import https from "https";
import fetch, { type RequestInit } from "node-fetch";
import path from "path";
import { pipeline } from "stream";
import { BooleanStringSchema, env } from "./env.js";
import { errorHandlingMiddleware, handleError } from "./error-handler.js";
import { logger as proxyLogger, requestLoggingMiddleware } from "./logging.js";
import { clientRoot, proxyServerRoot } from "./paths.js";
const app = express();
const DEFAULT_SERVICE_TYPE = "neptune-db";
interface DbQueryIncomingHttpHeaders extends IncomingHttpHeaders {
queryid?: string;
"graph-db-connection-url"?: string;
"aws-neptune-region"?: string;
"service-type"?: string;
"db-query-logging-enabled"?: string;
"sparql-endpoint-path"?: string;
}
interface LoggerIncomingHttpHeaders extends IncomingHttpHeaders {
level?: string;
message?: string;
}
app.use(requestLoggingMiddleware());
// Function to get IAM headers for AWS4 signing process.
async function getIAMHeaders(options: string | aws4.Request) {
const credentialProvider = fromNodeProviderChain();
const creds = await credentialProvider();
if (creds === undefined) {
throw new Error(
"IAM is enabled but credentials cannot be found on the credential provider chain.",
);
}
const headers = aws4.sign(options, {
accessKeyId: creds.accessKeyId,
secretAccessKey: creds.secretAccessKey,
...(creds.sessionToken && { sessionToken: creds.sessionToken }),
});
return headers;
}
// Function to retry fetch requests with exponential backoff.
const retryFetch = async (
url: URL,
options: any,
isIamEnabled: boolean,
region: string | undefined,
serviceType: string,
retryDelay = 10000,
refetchMaxRetries = 1,
) => {
for (let i = 0; i < refetchMaxRetries; i++) {
if (isIamEnabled) {
const data = await getIAMHeaders({
host: url.hostname,
port: url.port,
path: url.pathname + url.search,
service: serviceType,
region,
method: options.method,
body: options.body ?? undefined,
headers: options.headers,
});
options = {
host: url.hostname,
port: url.port,
path: url.pathname + url.search,
service: serviceType,
region,
method: options.method,
body: options.body ?? undefined,
headers: data.headers,
};
}
options = {
host: url.hostname,
port: url.port,
path: url.pathname + url.search,
service: serviceType,
method: options.method,
body: options.body ?? undefined,
headers: options.headers,
compress: false, // prevent automatic decompression
};
try {
const res = await fetch(url.href, options);
if (!res.ok) {
proxyLogger.error("!!Request failure!!");
return res;
} else {
return res;
}
} catch (err) {
if (refetchMaxRetries === 1) {
// Don't log about retries if retrying is not used
throw err;
} else if (i === refetchMaxRetries - 1) {
proxyLogger.error(err, "!!Proxy Retry Fetch Reached Maximum Tries!!");
throw err;
} else {
proxyLogger.debug("Proxy Retry Fetch Count::: " + i);
await new Promise(resolve => setTimeout(resolve, retryDelay));
}
}
}
// Should never reach this code
throw new Error("retryFetch failed to complete retry logic");
};
// Function to fetch data from the given URL and send it as a response.
async function fetchData(
res: Response,
next: NextFunction,
url: string,
options: RequestInit,
isIamEnabled: boolean,
region: string | undefined,
serviceType: string,
) {
try {
const response = await retryFetch(
new URL(url),
options,
isIamEnabled,
region,
serviceType,
);
// Set the headers from the fetch response to the client response
res.status(response.status);
for (const [key, value] of response.headers.entries()) {
res.setHeader(key, value);
}
// Pipe the raw fetch response body directly to the client response
if (response.body) {
pipeline(response.body, res, err => {
if (err) {
// Log the error as a warning, but otherwise ignore it
proxyLogger.warn("Pipeline error %o", err);
}
});
} else {
res.end();
}
} catch (error) {
next(error);
}
}
const defaultConnectionFolderPath = env.CONFIGURATION_FOLDER_PATH
? env.CONFIGURATION_FOLDER_PATH
: clientRoot;
app.use(compression()); // Use compression middleware
app.use(cors());
app.use(bodyParser.json({ limit: "50mb" }));
app.use(bodyParser.urlencoded({ extended: true, limit: "50mb" }));
app.use(
"/defaultConnection",
express.static(
path.join(defaultConnectionFolderPath, "defaultConnection.json"),
),
);
// Host the Graph Explorer UI static files
const staticFilesVirtualPath = "/explorer";
const staticFilesPath = path.join(clientRoot, "dist");
proxyLogger.info("Hosting client side static files from: %s", staticFilesPath);
proxyLogger.info(
"Hosting client side static files at: %s",
staticFilesVirtualPath ?? "/",
);
if (staticFilesVirtualPath) {
app.use(staticFilesVirtualPath, express.static(staticFilesPath));
} else {
app.use(express.static(staticFilesPath));
}
// POST endpoint for SPARQL queries.
app.post("/sparql", async (req, res, next) => {
// Gather info from the headers
const headers = req.headers as DbQueryIncomingHttpHeaders;
const queryId = headers["queryid"];
const graphDbConnectionUrl = headers["graph-db-connection-url"];
const sparqlEndpointPath = headers["sparql-endpoint-path"] || "/sparql";
const shouldLogDbQuery = BooleanStringSchema.default(false).parse(
headers["db-query-logging-enabled"],
);
const isIamEnabled = !!headers["aws-neptune-region"];
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
/// Function to cancel long running queries if the client disappears before completion
async function cancelQuery() {
if (!queryId) {
return;
}
proxyLogger.debug(`Cancelling request ${queryId}...`);
try {
await retryFetch(
new URL(`${graphDbConnectionUrl}${sparqlEndpointPath}/status`),
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: `cancelQuery&queryId=${encodeURIComponent(queryId)}&silent=true`,
},
isIamEnabled,
region,
serviceType,
);
} catch (err) {
// Not really an error
proxyLogger.warn(err, "Failed to cancel the query");
}
}
// Watch for a cancelled or aborted connection
req.on("close", async () => {
if (req.complete) {
return;
}
await cancelQuery();
});
res.on("close", async () => {
if (res.writableFinished) {
return;
}
await cancelQuery();
});
// Validate the input before making any external calls.
const queryString = req.body.query;
if (!queryString) {
res.status(400).send({ error: "[Proxy]SPARQL: Query not provided" });
return;
}
if (shouldLogDbQuery) {
proxyLogger.debug("[SPARQL] Received database query:\n%s", queryString);
}
const rawUrl = `${graphDbConnectionUrl}${sparqlEndpointPath}`;
let body = `query=${encodeURIComponent(queryString)}`;
if (queryId) {
body += `&queryId=${encodeURIComponent(queryId)}`;
}
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/sparql-results+json",
},
body,
};
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
// POST endpoint for Gremlin queries.
app.post("/gremlin", async (req, res, next) => {
// Gather info from the headers
const headers = req.headers as DbQueryIncomingHttpHeaders;
const queryId = headers["queryid"];
const graphDbConnectionUrl = headers["graph-db-connection-url"];
const shouldLogDbQuery = BooleanStringSchema.default(false).parse(
headers["db-query-logging-enabled"],
);
const isIamEnabled = !!headers["aws-neptune-region"];
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
// Validate the input before making any external calls.
const queryString = req.body.query;
if (!queryString) {
res.status(400).send({ error: "[Proxy] Gremlin: query not provided" });
return;
}
if (shouldLogDbQuery) {
proxyLogger.debug("[Gremlin] Received database query:\n%s", queryString);
}
/// Function to cancel long running queries if the client disappears before completion
async function cancelQuery() {
if (!queryId) {
return;
}
proxyLogger.debug(`Cancelling request ${queryId}...`);
try {
await retryFetch(
new URL(
`${graphDbConnectionUrl}/gremlin/status?cancelQuery&queryId=${encodeURIComponent(queryId)}`,
),
{ method: "GET" },
isIamEnabled,
region,
serviceType,
);
} catch (err) {
// Not really an error
proxyLogger.warn(err, "Failed to cancel the query");
}
}
// Watch for a cancelled or aborted connection
req.on("close", async () => {
if (req.complete) {
return;
}
await cancelQuery();
});
res.on("close", async () => {
if (res.writableFinished) {
return;
}
await cancelQuery();
});
const body = { gremlin: queryString, queryId };
const rawUrl = `${graphDbConnectionUrl}/gremlin`;
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.gremlin-v3.0+json",
},
body: JSON.stringify(body),
};
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
// POST endpoint for openCypher queries.
app.post("/openCypher", async (req, res, next) => {
const headers = req.headers as DbQueryIncomingHttpHeaders;
const shouldLogDbQuery = BooleanStringSchema.default(false).parse(
headers["db-query-logging-enabled"],
);
const queryString = req.body.query;
// Validate the input before making any external calls.
if (!queryString) {
res.status(400).send({ error: "[Proxy]OpenCypher: query not provided" });
return;
}
if (shouldLogDbQuery) {
proxyLogger.debug("[openCypher] Received database query:\n%s", queryString);
}
const rawUrl = `${headers["graph-db-connection-url"]}/openCypher`;
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: `query=${encodeURIComponent(queryString)}`,
};
const isIamEnabled = !!headers["aws-neptune-region"];
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
// GET endpoint to retrieve PropertyGraph statistics summary for Neptune Analytics.
app.get("/summary", async (req, res, next) => {
const headers = req.headers as DbQueryIncomingHttpHeaders;
const isIamEnabled = !!headers["aws-neptune-region"];
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
const rawUrl = `${headers["graph-db-connection-url"]}/summary?mode=detailed`;
const requestOptions = {
method: "GET",
};
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
// GET endpoint to retrieve PropertyGraph statistics summary for Neptune DB.
app.get("/pg/statistics/summary", async (req, res, next) => {
const headers = req.headers as DbQueryIncomingHttpHeaders;
const isIamEnabled = !!headers["aws-neptune-region"];
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
const rawUrl = `${headers["graph-db-connection-url"]}/pg/statistics/summary?mode=detailed`;
const requestOptions = {
method: "GET",
};
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
// GET endpoint to retrieve RDF statistics summary.
app.get("/rdf/statistics/summary", async (req, res, next) => {
const headers = req.headers as DbQueryIncomingHttpHeaders;
const isIamEnabled = !!headers["aws-neptune-region"];
const serviceType = isIamEnabled
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";
const rawUrl = `${headers["graph-db-connection-url"]}/rdf/statistics/summary?mode=detailed`;
const requestOptions = {
method: "GET",
};
const region = isIamEnabled ? headers["aws-neptune-region"] : "";
await fetchData(
res,
next,
rawUrl,
requestOptions,
isIamEnabled,
region,
serviceType,
);
});
app.get("/status", (_req, res) => {
res.send("OK");
});
app.post("/logger", (req, res, next) => {
const headers = req.headers as LoggerIncomingHttpHeaders;
let message;
let level;
try {
if (headers["level"] === undefined) {
throw new Error("No log level passed.");
} else {
level = headers["level"];
}
if (headers["message"] === undefined) {
throw new Error("No log message passed.");
} else {
message = JSON.parse(headers["message"]).replaceAll("\\", "");
}
if (level.toLowerCase() === "error") {
proxyLogger.error(message);
} else if (level.toLowerCase() === "warn") {
proxyLogger.warn(message);
} else if (level.toLowerCase() === "info") {
proxyLogger.info(message);
} else if (level.toLowerCase() === "debug") {
proxyLogger.debug(message);
} else if (level.toLowerCase() === "trace") {
proxyLogger.trace(message);
} else {
throw new Error("Tried to log to an unknown level.");
}
res.send("Log received.");
} catch (error) {
next(error);
}
});
// Error handler middleware to log errors and send appropriate response.
app.use(errorHandlingMiddleware());
app.use((_req, res) => {
res.status(404).send("The requested resource was not available");
});
// Relative paths to certificate files
const certificateKeyFilePath = path.join(
proxyServerRoot,
"cert-info/server.key",
);
const certificateFilePath = path.join(proxyServerRoot, "cert-info/server.crt");
// Get the port numbers to listen on
const host = env.HOST;
const httpPort = env.PROXY_SERVER_HTTP_PORT;
const httpsPort = env.PROXY_SERVER_HTTPS_PORT;
const useHttps =
env.PROXY_SERVER_HTTPS_CONNECTION &&
fs.existsSync(certificateKeyFilePath) &&
fs.existsSync(certificateFilePath);
// Log the server locations based on the configuration.
function logServerLocations() {
const scheme = useHttps ? "https" : "http";
let port = "";
// Only show the port if it is not one of the defaults
if (useHttps && httpsPort !== 443) {
port = `:${httpsPort}`;
} else if (!useHttps && httpPort !== 80) {
port = `:${httpPort}`;
}
const baseUrl = `${scheme}://${host}${port}`;
proxyLogger.info(`Proxy server located at ${baseUrl}`);
proxyLogger.info(
`Graph Explorer UI located at: ${baseUrl}${staticFilesVirtualPath ?? ""}`,
);
}
// Start the server on port 80 or 443 (if HTTPS is enabled)
function startServer() {
if (useHttps) {
const options = {
key: fs.readFileSync(certificateKeyFilePath),
cert: fs.readFileSync(certificateFilePath),
};
return https.createServer(options, app).listen(httpsPort, () => {
logServerLocations();
});
} else {
return app.listen(httpPort, () => {
logServerLocations();
});
}
}
const server = startServer();
process.on("uncaughtException", (error: Error) => {
handleError(error);
});
process.on("unhandledRejection", reason => {
handleError(reason);
});
// Watch for shutdown event and close gracefully.
process.on("SIGTERM", () => {
proxyLogger.info("SIGTERM signal received: closing HTTP server");
server.close(() => {
proxyLogger.info("HTTP server closed");
});
});