-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoperationsServer.ts
More file actions
365 lines (324 loc) · 12.6 KB
/
Copy pathoperationsServer.ts
File metadata and controls
365 lines (324 loc) · 12.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
// @ts-nocheck
import cluster from 'cluster';
import zlib from 'node:zlib';
import * as env from '../utility/environment/environmentManager.ts';
env.initSync();
import * as terms from '../utility/hdbTerms.ts';
import harperLogger from '../utility/logging/harper_logger.ts';
import fastify, { FastifyInstance, FastifyReply, FastifyRequest, FastifyServerOptions } from 'fastify';
import fastifyCors, { type FastifyCorsOptions } from '@fastify/cors';
import fastifyCompress from '@fastify/compress';
import fastifyStatic from '@fastify/static';
import requestTimePlugin from './serverHelpers/requestTimePlugin.js';
import guidePath from 'path';
import { PACKAGE_ROOT } from '../utility/packageUtils.js';
import * as globalSchema from '../utility/globalSchema.ts';
import * as commonUtils from '../utility/common_utils.ts';
import * as userSchema from '../security/user.ts';
import { server as serverRegistration, type ServerOptions } from '../server/Server.ts';
import {
authHandler,
authAndEnsureUserOnRequest,
handlePostRequest,
serverErrorHandler,
reqBodyValidationHandler,
} from './serverHelpers/serverHandlers.js';
import { registerBunFastifyInstance } from './http.ts';
import { registerContentHandlers } from './serverHelpers/contentTypes.ts';
import type { OperationFunctionName } from './serverHelpers/serverUtilities.ts';
type ParsedSqlObject = any;
import { generateJsonApi } from '../resources/openApi.ts';
import { Resources } from '../resources/Resources.ts';
import { ServerError } from '../utility/errors/hdbError.ts';
import { sendItcEvent } from './threads/itc.js';
import { onMessageByType } from './threads/manageThreads.js';
const DEFAULT_HEADERS_TIMEOUT = 60000;
const REQ_MAX_BODY_SIZE = env.get(terms.CONFIG_PARAMS.OPERATIONSAPI_NETWORK_MAXREQUESTBODYSIZE) ?? 1024 * 1024 * 1024; //this defaults to 1GB in bytes
const TRUE_COMPARE_VAL = 'TRUE';
const { CONFIG_PARAMS } = terms;
let server;
export { operationsServer as hdbServer };
export { operationsServer as startOnMainThread };
/**
* Builds a Harper server.
*/
async function operationsServer(options: ServerOptions & { resources?: Resources }) {
try {
harperLogger.debug('In Fastify server' + process.cwd());
harperLogger.debug(`Running with NODE_ENV set as: ${process.env.NODE_ENV}`);
harperLogger.debug(`Harper server process ${process.pid} starting up.`);
global.clustering_on = false;
global.isMaster = cluster.isMaster;
await setUp();
// if we have a secure port, need to use the secure HTTP server for fastify (it can be used for HTTP as well)
const isHttps = options.securePort > 0;
//generate a Fastify server instance
server = buildServer(isHttps, options.resources);
//make sure the process waits for the server to be fully instantiated before moving forward
await server.ready();
if (!options) options = {};
options.usageType = 'operations-api';
// fastify can't clean up properly
try {
// now that server is fully loaded/ready, start listening on port provided in config settings or just use
// zero to wait for sockets from the main thread
serverRegistration.http(server.server, options);
// On Bun, register the Fastify instance so requests can be delegated via inject()
if (typeof globalThis.Bun !== 'undefined') {
const port = options.port || options.securePort || env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_PORT);
if (port) registerBunFastifyInstance(port, server);
}
if (!server.server.closeIdleConnections) {
// before Node v18, closeIdleConnections is not available, and we have to setup a listener for fastify
// to handle closing by setting up the dynamic port
await server.listen({ port: 0, host: '::' });
}
} catch (err) {
server.close();
harperLogger.error(err);
harperLogger.error(`Error configuring operations server`);
throw err;
}
} catch (err) {
console.error(`Failed to build server on ${process.pid}`, err);
harperLogger.fatal(err);
process.exit(1);
}
}
/**
* Makes sure global values are set and that clustering connections are set/ready before server starts.
*/
async function setUp() {
harperLogger.trace('Configuring Harper process.');
globalSchema.setSchemaDataToGlobal();
return userSchema.setUsersWithRolesCache();
}
export interface ImpersonatePayload {
username?: string;
role?: {
permission: Partial<userSchema.UserRoleNamedPermissions & userSchema.UserRoleDatabasePermissions>;
};
role_name?: string;
}
interface BaseOperationRequestBody {
operation: OperationFunctionName;
bypassAuth: boolean;
hdb_user?: userSchema.User;
hdbAuthHeader?: unknown;
bypass_auth?: boolean;
impersonate?: ImpersonatePayload;
password?: string;
payload?: string;
sql?: string;
parsedSqlObject?: ParsedSqlObject;
[key: string]: unknown;
}
type SearchOperation = BaseOperationRequestBody;
interface SearchOperationRequestBody {
search_operation: SearchOperation;
}
export type OperationRequestBody = BaseOperationRequestBody & Partial<SearchOperationRequestBody>;
export interface OperationRequest {
body: OperationRequestBody;
}
export interface OperationResult {
message?: any;
}
/**
* This method configures and returns a Fastify server - for either HTTP or HTTPS - based on the provided config settings
*/
function buildServer(isHttps: boolean, resources: Resources): FastifyInstance {
harperLogger.debug(`Harper process starting to build ${isHttps ? 'HTTPS' : 'HTTP'} server.`);
const serverOpts = getServerOptions(isHttps);
const app = fastify(serverOpts);
// Fastify does not set this property in the initial app construction
app.server.headersTimeout = getHeaderTimeoutConfig();
// Set a top-level error handler for the server - all errors caught/thrown within the API will bubble up to this
// handler so that they can be handled in a coordinated way
app.setErrorHandler(serverErrorHandler);
const corsOptions = getCORSOpts();
if (corsOptions) {
app.register(fastifyCors, corsOptions);
}
app.register(function (instance, options, done) {
instance.setNotFoundHandler(function (request, reply) {
if (reply.sent || reply.raw.headersSent || reply.raw.writableEnded) return;
app.server.emit('unhandled', request.raw, reply.raw);
});
done();
});
app.register(requestTimePlugin);
// This handles all get requests for the studio
app.register(fastifyCompress, {
brotliOptions: {
params: {
[zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, // useful for APIs that primarily return text
[zlib.constants.BROTLI_PARAM_QUALITY]: 2, // default is 4, max is 11, min is 0
},
},
});
registerContentHandlers(app);
// Add a simple health check
app.get('/health', () => 'Harper is running.');
// Add a top-level GET handler for browsers.
app.register(fastifyStatic, { root: guidePath.join(PACKAGE_ROOT, 'studio/web') });
const studioOn = env.get(terms.HDB_SETTINGS_NAMES.LOCAL_STUDIO_ON);
if (!commonUtils.isEmpty(studioOn) && studioOn.toString().toLowerCase() === 'true') {
app.get('/', (req, res) => res.sendFile('index.html'));
} else {
app.get('/', (req, res) => res.sendFile('running.html'));
}
// Describe the APIs.
app.get('/api/openapi/rest', { preValidation: [authAndEnsureUserOnRequest] }, restOpenAPIHandler(resources));
// Add the top-level POST handler.
app.post<{ Body: OperationRequestBody }, { isOperation?: boolean }>(
'/',
{
preValidation: [reqBodyValidationHandler, authHandler],
config: { isOperation: true },
},
handler
);
harperLogger.debug(`Harper process starting up ${isHttps ? 'HTTPS' : 'HTTP'} server listener.`);
return app;
}
let nextOpenApiRequestId = 1;
let openApiResponseListenerAttached = false;
const pendingOpenApiRequests = new Map<number, (openapi: unknown) => void>();
function attachOpenApiResponseListener() {
if (openApiResponseListenerAttached) return;
onMessageByType(terms.ITC_EVENT_TYPES.RESOURCE_OPENAPI_RESPONSE, ({ message }: any) => {
const resolve = pendingOpenApiRequests.get(message.requestId);
if (resolve) {
pendingOpenApiRequests.delete(message.requestId);
resolve(message.openapi);
}
});
openApiResponseListenerAttached = true;
}
function queryWorkerForOpenApi(serverHttpURL: string): Promise<unknown> {
attachOpenApiResponseListener();
const requestId = nextOpenApiRequestId++;
return new Promise<unknown>((resolve, reject) => {
const timeoutHandle = setTimeout(() => {
pendingOpenApiRequests.delete(requestId);
reject(new ServerError('Timeout fetching OpenAPI spec from worker thread', 503));
}, 5000);
pendingOpenApiRequests.set(requestId, (openapi) => {
clearTimeout(timeoutHandle);
resolve(openapi);
});
sendItcEvent({
type: terms.ITC_EVENT_TYPES.RESOURCE_OPENAPI_REQUEST,
message: { requestId, serverHttpURL },
}).catch((err: unknown) => {
clearTimeout(timeoutHandle);
pendingOpenApiRequests.delete(requestId);
reject(err);
});
});
}
function restOpenAPIHandler(resources: Resources) {
const httpPort = env.get(terms.CONFIG_PARAMS.HTTP_PORT);
const httpSecurePort = env.get(terms.CONFIG_PARAMS.HTTP_SECUREPORT);
return async (req: FastifyRequest & { hdb_user?: { role?: { permission?: { super_user: boolean } } } }) => {
if (req.hdb_user?.role?.permission?.super_user) {
const serverHttpURL = calculateRestHttpURL(httpPort, httpSecurePort, req);
if (resources.size > 0) {
return generateJsonApi(resources, serverHttpURL);
}
return queryWorkerForOpenApi(serverHttpURL);
} else {
harperLogger.warn(
`{"ip":"${req.socket.remoteAddress}", "error":"attempt to access /api/openapi/rest without being super_user"`
);
return new ServerError(`Forbidden`, 403);
}
};
}
export function calculateRestHttpURL(
httpPort: string | undefined,
httpSecurePort: string | undefined,
req: { hostname: string; protocol: string }
): string {
const httpURL = new URL(`${req.protocol}://${req.hostname}`);
// note that the request is from fastify, which doesn't seem to have a correct hostname property (includes port), so the URL needs to be used for hostname
if (httpURL.hostname.toLowerCase() === 'localhost' || httpURL.hostname.match(/^[\d.:]+$/)) {
// Only use ports when running against localhost, or an ip address.
if (httpSecurePort) {
httpURL.port = httpSecurePort;
httpURL.protocol = 'https:';
} else if (httpPort) {
httpURL.port = httpPort;
httpURL.protocol = 'http:';
}
} else {
// Otherwise, assume that port forwarding is happening, and possibly SSL termination.
httpURL.port = '443';
httpURL.protocol = 'https:';
}
return httpURL.toString();
}
function handler(req: FastifyRequest<{ Body?: OperationRequestBody }>, reply: FastifyReply) {
// if the operation is a restart, we have to tell the client not to use keep alive on this connection
// anymore; it needs to be closed because this thread is going to be terminated
if (req.body?.operation?.startsWith('restart')) {
reply.header('Connection', 'close');
}
//if no error is thrown below, the response 'data' returned from the handler will be returned with 200/OK code
return handlePostRequest(req, reply);
}
interface HttpServerOptions extends FastifyServerOptions {
https?: boolean;
http2?: boolean;
}
/**
* Builds server options object to pass to Fastify when using server factory.
*/
function getServerOptions(isHttps: boolean): HttpServerOptions {
const server_timeout = env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_TIMEOUT);
const keep_alive_timeout = env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_KEEPALIVETIMEOUT);
return {
bodyLimit: REQ_MAX_BODY_SIZE,
connectionTimeout: server_timeout,
keepAliveTimeout: keep_alive_timeout,
forceCloseConnections: true,
return503OnClosing: false,
http2: env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_HTTP2),
https: isHttps /* && {
allowHTTP1: true,
},*/,
};
}
/**
* Builds CORS options object to pass to cors plugin when/if it needs to be registered with Fastify
*/
function getCORSOpts(): FastifyCorsOptions {
const propsCors = env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_CORS);
const propsCorsAccesslist = env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_CORSACCESSLIST);
let corsOptions: FastifyCorsOptions;
if (propsCors && (propsCors === true || propsCors.toUpperCase() === TRUE_COMPARE_VAL)) {
corsOptions = {
origin: true,
allowedHeaders: ['Content-Type', 'Authorization', 'Accept'],
credentials: false,
};
if (
propsCorsAccesslist &&
propsCorsAccesslist.length > 0 &&
propsCorsAccesslist[0] !== null &&
propsCorsAccesslist[0] !== '*'
) {
corsOptions.origin = (origin, callback) => {
return callback(null, propsCorsAccesslist.indexOf(origin) !== -1);
};
}
}
return corsOptions;
}
/**
* Returns header timeout value from config file or, if not entered, the default value
*/
function getHeaderTimeoutConfig(): number {
return env.get(CONFIG_PARAMS.OPERATIONSAPI_NETWORK_HEADERSTIMEOUT) ?? DEFAULT_HEADERS_TIMEOUT;
}