-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
390 lines (338 loc) · 14.3 KB
/
index.js
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
const P = require('bluebird');
const _ = require('lodash');
const express = require('express');
const path = require('path');
const fs = require('fs');
const cors = require('cors');
const bodyParser = require('body-parser');
const jwt = require('express-jwt');
const requireAll = require('require-all');
const auth = require('./services/auth');
const ResourcesRoutes = require('./routes/resources');
const ActionsRoutes = require('./routes/actions');
const AssociationsRoutes = require('./routes/associations');
const StatRoutes = require('./routes/stats');
const SessionRoute = require('./routes/sessions');
const ForestRoutes = require('./routes/forest');
const HealthCheckRoute = require('./routes/healthcheck');
const Schemas = require('./generators/schemas');
const SchemaSerializer = require('./serializers/schema');
const logger = require('./services/logger');
const pathService = require('./services/path');
const Integrator = require('./integrations');
const errorHandler = require('./services/error-handler');
const ApimapSender = require('./services/apimap-sender');
const ipWhitelist = require('./services/ip-whitelist');
const SchemaFileUpdater = require('./services/schema-file-updater');
const ApimapFieldsFormater = require('./services/apimap-fields-formater');
const ConfigStore = require('./services/config-store');
const ProjectDirectoryUtils = require('./utils/project-directory');
const pathProjectAbsolute = new ProjectDirectoryUtils().getAbsolutePath();
const ENVIRONMENT_DEVELOPMENT = !process.env.NODE_ENV
|| ['dev', 'development'].includes(process.env.NODE_ENV);
const SCHEMA_FILENAME = `${pathProjectAbsolute}/.forestadmin-schema.json`;
const DISABLE_AUTO_SCHEMA_APPLY = process.env.FOREST_DISABLE_AUTO_SCHEMA_APPLY
&& JSON.parse(process.env.FOREST_DISABLE_AUTO_SCHEMA_APPLY);
const REGEX_COOKIE_SESSION_TOKEN = /forest_session_token=([^;]*)/;
const configStore = ConfigStore.getInstance();
let jwtAuthenticator;
function getModels() {
const models = configStore.Implementation.getModels();
_.each(models, (model, modelName) => {
model.modelName = modelName;
});
return _.values(models);
}
function requireAllModels(modelsDir) {
if (modelsDir) {
try {
const isJavascriptOrTypescriptFileName = (fileName) =>
fileName.endsWith('.js') || (fileName.endsWith('.ts') && !fileName.endsWith('.d.ts'));
// NOTICE: Ends with `.spec.js`, `.spec.ts`, `.test.js` or `.test.ts`.
const isTestFileName = (fileName) => fileName.match(/(?:\.test|\.spec)\.(?:js||ts)$/g);
requireAll({
dirname: modelsDir,
excludeDirs: /^__tests__$/,
filter: (fileName) =>
isJavascriptOrTypescriptFileName(fileName) && !isTestFileName(fileName),
recursive: true,
});
} catch (error) {
logger.error('Cannot read a file for the following reason: ', error);
}
}
// NOTICE: User didn't provide a modelsDir but may already have required them manually so they
// might be available.
return P.resolve(getModels())
.catch((error) => {
logger.error('Cannot read a file for the following reason: ', error);
return P.resolve([]);
});
}
exports.Schemas = Schemas;
exports.logger = logger;
exports.ResourcesRoute = {};
exports.ensureAuthenticated = (request, response, next) => {
auth.authenticate(request, response, next, jwtAuthenticator);
};
let app = null;
function buildSchema() {
const { lianaOptions, Implementation } = configStore;
const absModelDirs = configStore.modelsDir ? path.resolve('.', configStore.modelsDir) : undefined;
return requireAllModels(absModelDirs)
.then(async (models) => {
configStore.integrator = new Integrator(lianaOptions, Implementation);
await Schemas.perform(
Implementation,
configStore.integrator,
models,
lianaOptions,
);
return models;
});
}
exports.init = (Implementation) => {
const { opts } = Implementation;
configStore.Implementation = Implementation;
configStore.lianaOptions = opts;
if (opts.onlyCrudModule === true) {
return buildSchema();
}
if (app) {
logger.warn('Forest init function called more than once. Only the first call has been processed.');
return app;
}
app = express();
const pathMounted = pathService.generate('*', opts);
auth.initAuth(opts);
if (opts.secretKey) {
logger.warn('DEPRECATION WARNING: The use of secretKey and authKey options is deprecated. Please use envSecret and authSecret instead.');
opts.envSecret = opts.secretKey;
opts.authSecret = opts.authKey;
}
// CORS
let allowedOrigins = ['localhost:4200', /\.forestadmin\.com$/];
if (process.env.CORS_ORIGINS) {
allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(','));
}
app.use(pathMounted, cors({
origin: allowedOrigins,
maxAge: 86400, // NOTICE: 1 day
credentials: true,
}));
// Mime type
app.use(pathMounted, bodyParser.json());
// Authentication
if (opts.authSecret) {
jwtAuthenticator = jwt({
secret: opts.authSecret,
credentialsRequired: false,
getToken: (request) => {
if (request.headers) {
if (request.headers.authorization
&& request.headers.authorization.split(' ')[0] === 'Bearer') {
return request.headers.authorization.split(' ')[1];
}
// NOTICE: Necessary for downloads authentication.
if (request.headers.cookie) {
const match = request.headers.cookie.match(REGEX_COOKIE_SESSION_TOKEN);
if (match && match[1]) {
return match[1];
}
}
}
return null;
},
});
} else {
logger.error('Your Forest authSecret seems to be missing. Can you check that you properly set a Forest authSecret in the Forest initializer?');
}
if (!opts.envSecret) {
logger.error('Your Forest envSecret seems to be missing. Can you check that you properly set a Forest envSecret in the Forest initializer?');
}
if (jwtAuthenticator) {
const pathsPublic = [/^\/forest\/sessions.*$/];
app.use(pathMounted, jwtAuthenticator.unless({ path: pathsPublic }));
}
new HealthCheckRoute(app, opts).perform();
new SessionRoute(app, opts).perform();
// Init
return buildSchema()
.then((models) => {
let directorySmartImplementation;
if (opts.configDir) {
directorySmartImplementation = path.resolve('.', opts.configDir);
} else {
directorySmartImplementation = `${path.resolve('.')}/forest`;
}
if (fs.existsSync(directorySmartImplementation)) {
return requireAllModels(directorySmartImplementation);
}
if (opts.configDir) {
logger.error('The Forest configDir option you configured does not seem to be an existing directory.');
}
return models;
})
.each((model) => {
const modelName = configStore.Implementation.getModelName(model);
configStore.integrator.defineRoutes(app, model, configStore.Implementation);
const resourcesRoute = new ResourcesRoutes(app, model);
resourcesRoute.perform();
exports.ResourcesRoute[modelName] = resourcesRoute;
new AssociationsRoutes(
app,
model,
configStore.Implementation,
configStore.integrator,
configStore.lianaOptions,
).perform();
new ActionsRoutes(
app,
model,
configStore.Implementation,
configStore.integrator,
configStore.lianaOptions,
).perform();
new StatRoutes(
app,
model,
configStore.Implementation,
configStore.lianaOptions,
).perform();
})
.then(() => new ForestRoutes(app, configStore.lianaOptions).perform())
.then(() => app.use(pathMounted, errorHandler.catchIfAny))
.then(() => {
if (!opts.envSecret) { return; }
if (opts.envSecret.length !== 64) {
logger.error('Your envSecret does not seem to be correct. Can you check on Forest that you copied it properly in the Forest initialization?');
return;
}
const collections = _.values(Schemas.schemas);
configStore.integrator.defineCollections(collections);
collections
.filter((collection) => collection.actions && collection.actions.length)
// NOTICE: Check each Smart Action declaration to detect configuration errors.
.forEach((collection) => {
const isFieldsInvalid = (action) => action.fields && !Array.isArray(action.fields);
collection.actions.forEach((action) => {
if (!action.name) {
logger.warn(`An unnamed Smart Action of collection "${collection.name}" has been ignored.`);
} else if (isFieldsInvalid(action)) {
logger.error(`Cannot find the fields you defined for the Smart action "${action.name}" of your "${collection.name}" collection. The fields option must be an array.`);
}
});
// NOTICE: Ignore actions without a name.
collection.actions = collection.actions.filter((action) => action.name);
});
const schemaSerializer = new SchemaSerializer();
const { options: serializerOptions } = schemaSerializer;
let collectionsSent;
let metaSent;
if (ENVIRONMENT_DEVELOPMENT) {
const expressVersion = process.env.npm_package_dependencies_express;
const meta = {
database_type: configStore.Implementation.getDatabaseType(),
liana: configStore.Implementation.getLianaName(),
liana_version: configStore.Implementation.getLianaVersion(),
engine: 'nodejs',
engine_version: process.versions && process.versions.node,
framework: expressVersion ? 'express' : 'other',
framework_version: expressVersion,
orm_version: configStore.Implementation.getOrmVersion(),
};
const content = new SchemaFileUpdater(SCHEMA_FILENAME, collections, meta, serializerOptions)
.perform();
collectionsSent = content.collections;
metaSent = content.meta;
} else {
try {
const content = fs.readFileSync(SCHEMA_FILENAME);
if (!content) {
logger.error('The .forestadmin-schema.json file is empty.');
logger.error('The schema cannot be synchronized with Forest Admin servers.');
return;
}
const contentParsed = JSON.parse(content.toString());
collectionsSent = contentParsed.collections;
metaSent = contentParsed.meta;
} catch (error) {
if (error.code === 'ENOENT') {
logger.error('The .forestadmin-schema.json file does not exist.');
} else {
logger.error('The content of .forestadmin-schema.json file is not a correct JSON.');
}
logger.error('The schema cannot be synchronized with Forest Admin servers.');
return;
}
}
if (DISABLE_AUTO_SCHEMA_APPLY) { return; }
const schemaSent = schemaSerializer.perform(collectionsSent, metaSent);
new ApimapSender(opts.envSecret, schemaSent).perform();
})
.then(() => ipWhitelist
.retrieve(opts.envSecret)
// NOTICE: An error log (done by the service) is enough in case of retrieval error.
.catch(() => {}))
.then(() => {
if (opts.expressParentApp) {
opts.expressParentApp.use('/forest', app);
}
return app;
})
.catch((error) => {
logger.error('An error occured while computing the Forest schema. Your application schema cannot be synchronized with Forest. Your admin panel might not reflect your application models definition. ', error);
throw error;
});
};
exports.collection = (name, opts) => {
if (_.isEmpty(Schemas.schemas) && opts.modelsDir) {
logger.error(`Cannot customize your collection named "${name}" properly. Did you call the "collection" method in the /forest directory?`);
return;
}
let collection = _.find(Schemas.schemas, { name });
if (!collection) {
collection = _.find(Schemas.schemas, { nameOld: name });
if (collection) {
name = collection.name;
logger.warn(`DEPRECATION WARNING: Collection names are now based on the models names. Please rename the collection "${collection.nameOld}" of your Forest customisation in "${collection.name}".`);
}
}
if (collection) {
if (!Schemas.schemas[name].actions) { Schemas.schemas[name].actions = []; }
if (!Schemas.schemas[name].segments) { Schemas.schemas[name].segments = []; }
Schemas.schemas[name].actions = _.union(opts.actions, Schemas.schemas[name].actions);
Schemas.schemas[name].segments = _.union(opts.segments, Schemas.schemas[name].segments);
// NOTICE: Smart Field definition case
opts.fields = new ApimapFieldsFormater(opts.fields, name).perform();
Schemas.schemas[name].fields = _.concat(opts.fields, Schemas.schemas[name].fields);
if (opts.searchFields) {
Schemas.schemas[name].searchFields = opts.searchFields;
}
} else if (opts.fields && opts.fields.length) {
// NOTICE: Smart Collection definition case
opts.name = name;
opts.idField = 'id';
opts.isVirtual = true;
opts.isSearchable = !!opts.isSearchable;
opts.fields = new ApimapFieldsFormater(opts.fields, name).perform();
Schemas.schemas[name] = opts;
}
};
exports.logger = require('./services/logger');
exports.StatSerializer = require('./serializers/stat');
exports.ResourceSerializer = require('./serializers/resource');
exports.ResourceDeserializer = require('./deserializers/resource');
exports.BaseFiltersParser = require('./services/base-filters-parser');
exports.BaseOperatorDateParser = require('./services/base-operator-date-parser');
exports.RecordsGetter = require('./services/exposed/records-getter');
exports.RecordsCounter = require('./services/exposed/records-counter');
exports.RecordsExporter = require('./services/exposed/records-exporter');
exports.RecordGetter = require('./services/exposed/record-getter');
exports.RecordUpdater = require('./services/exposed/record-updater');
exports.RecordCreator = require('./services/exposed/record-creator');
exports.RecordRemover = require('./services/exposed/record-remover');
exports.RecordsRemover = require('./services/exposed/records-remover');
exports.RecordSerializer = require('./services/exposed/record-serializer');
exports.PermissionMiddlewareCreator = require('./middlewares/permissions');
exports.PUBLIC_ROUTES = ['/', '/healthcheck', '/sessions', '/sessions-google'];