@codefresh-io/service-base 8.0.0 → 8.0.1-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +4 -3
- package/infra/express.js +28 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const express = require('express');
|
|
|
13
13
|
const monitor = require('@codefresh-io/cf-monitor');
|
|
14
14
|
const logging = require('./infra/logging');
|
|
15
15
|
const { openapi } = require('@codefresh-io/cf-openapi');
|
|
16
|
+
const expressInfra = require('./infra/express');
|
|
16
17
|
|
|
17
18
|
const OPTIONAL_COMPONENTS = {
|
|
18
19
|
mongo: { name: 'mongoClient' },
|
|
@@ -26,15 +27,15 @@ const exportedComponents = {
|
|
|
26
27
|
initService: service.init.bind(service),
|
|
27
28
|
stopService: service.stop.bind(service),
|
|
28
29
|
validation: require('./infra/validation'),
|
|
29
|
-
makeEndpoint:
|
|
30
|
+
makeEndpoint: expressInfra.makeEndpoint.bind(expressInfra),
|
|
30
31
|
getAuthenticatedEntity,
|
|
31
32
|
setAuthenticatedEntity,
|
|
32
33
|
request,
|
|
33
34
|
authEntity,
|
|
34
35
|
Promise,
|
|
35
36
|
express,
|
|
36
|
-
expressApp:
|
|
37
|
-
getLogger: logging.getLogger,
|
|
37
|
+
expressApp: expressInfra.expressApp,
|
|
38
|
+
getLogger: logging.getLogger.bind(logging),
|
|
38
39
|
config,
|
|
39
40
|
monitor,
|
|
40
41
|
};
|
package/infra/express.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
const { httpLoggerMiddlewareFactory } = require('@codefresh-io/cf-telemetry/logs/express');
|
|
3
|
-
const { Logger } = require('@codefresh-io/cf-telemetry/logs');
|
|
3
|
+
const { Logger, runInContext } = require('@codefresh-io/cf-telemetry/logs');
|
|
4
4
|
const express = require('express');
|
|
5
5
|
const qs = require('qs');
|
|
6
6
|
const compression = require('compression');
|
|
@@ -8,8 +8,8 @@ const bodyParser = require('body-parser');
|
|
|
8
8
|
const methodOverride = require('method-override');
|
|
9
9
|
const cookieParser = require('cookie-parser');
|
|
10
10
|
const monitor = require('@codefresh-io/cf-monitor');
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
const { newDomainMiddleware } = require('@codefresh-io/http-infra');
|
|
11
|
+
// @ts-ignore Incorrectly typed package
|
|
12
|
+
const { newDomainMiddleware, getAuthenticatedEntity } = require('@codefresh-io/http-infra');
|
|
13
13
|
const { openapi } = require('@codefresh-io/cf-openapi');
|
|
14
14
|
const CFError = require('cf-errors');
|
|
15
15
|
const { globalLogger } = require('./global-logger');
|
|
@@ -24,6 +24,28 @@ const { globalLogger } = require('./global-logger');
|
|
|
24
24
|
* @typedef {import('node:http').Server} HttpServer
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
+
const authLogger = new Logger('auth');
|
|
28
|
+
/**
|
|
29
|
+
* @param {Request} _req
|
|
30
|
+
* @param {Response} _res
|
|
31
|
+
* @param {NextFunction} next
|
|
32
|
+
*/
|
|
33
|
+
const enrichLogsWithAuthMetadataMiddleware = (_req, _res, next) => {
|
|
34
|
+
try {
|
|
35
|
+
const authEntity = getAuthenticatedEntity();
|
|
36
|
+
const initiator = {
|
|
37
|
+
type: authEntity?.getType?.() ?? 'anonymous',
|
|
38
|
+
id: authEntity?.id,
|
|
39
|
+
};
|
|
40
|
+
const context = authEntity?.isSystemEntity?.() ? {} : { account_id: authEntity?.getActiveAccountId?.() };
|
|
41
|
+
// @ts-ignore rest parameters are optional
|
|
42
|
+
runInContext({ initiator, context }, next);
|
|
43
|
+
} catch (exception) {
|
|
44
|
+
authLogger.error('Failed to enrich logs with auth metadata', exception);
|
|
45
|
+
next();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
27
49
|
class Express {
|
|
28
50
|
#logger = globalLogger.child(this.constructor.name);
|
|
29
51
|
|
|
@@ -102,9 +124,11 @@ class Express {
|
|
|
102
124
|
}
|
|
103
125
|
});
|
|
104
126
|
|
|
127
|
+
this.expressApp.use(enrichLogsWithAuthMetadataMiddleware);
|
|
128
|
+
|
|
105
129
|
/** @type {ErrorRequestHandler} */
|
|
106
130
|
const errorHandler = (err, _req, res, next) => {
|
|
107
|
-
this.#logger.error(
|
|
131
|
+
this.#logger.error(err);
|
|
108
132
|
|
|
109
133
|
if (res.headersSent) {
|
|
110
134
|
next(err);
|