@apolitical/server 4.1.0-pla-305-0 → 4.1.0-pla-305.0
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/package.json +7 -3
- package/src/config.js +3 -1
- package/src/container.js +0 -2
- package/src/secrets.js +10 -0
- package/src/services/secrets.service.js +28 -14
- package/src/helpers/impersonation.helper.js +0 -22
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/server",
|
|
3
|
-
"version": "4.1.0-pla-305
|
|
3
|
+
"version": "4.1.0-pla-305.0",
|
|
4
4
|
"description": "Node.js module to encapsulate Apolitical's express server setup",
|
|
5
5
|
"author": "Apolitical Group Limited <engineering@apolitical.co>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js",
|
|
10
|
+
"./secrets": "./src/secrets.js"
|
|
11
|
+
},
|
|
8
12
|
"files": [
|
|
9
13
|
"src"
|
|
10
14
|
],
|
|
@@ -24,8 +28,8 @@
|
|
|
24
28
|
],
|
|
25
29
|
"dependencies": {
|
|
26
30
|
"@apolitical/logger": "2.1.0",
|
|
27
|
-
"@google-cloud/secret-manager": "5.6.0",
|
|
28
31
|
"@cloudnative/health-connect": "2.1.0",
|
|
32
|
+
"@google-cloud/secret-manager": "^6.1.1",
|
|
29
33
|
"@opentelemetry/api": "1.9.0",
|
|
30
34
|
"@opentelemetry/auto-instrumentations-node": "0.57.1",
|
|
31
35
|
"@opentelemetry/exporter-trace-otlp-grpc": "0.200.0",
|
|
@@ -40,7 +44,7 @@
|
|
|
40
44
|
"cookie-parser": "1.4.6",
|
|
41
45
|
"cors": "2.8.5",
|
|
42
46
|
"dotenv": "16.0.3",
|
|
43
|
-
"express": "4.
|
|
47
|
+
"express": "4.22.0",
|
|
44
48
|
"express-jwt": "8.3.0",
|
|
45
49
|
"http-status-codes": "2.2.0",
|
|
46
50
|
"http-terminator": "3.2.0",
|
package/src/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const { NODE_ENV, LOG_LEVEL } = process.env;
|
|
3
|
+
const { NODE_ENV, LOG_LEVEL, GOOGLE_CLOUD_PROJECT, GOOGLE_IMPERSONATE_SERVICE_ACCOUNT } = process.env;
|
|
4
4
|
|
|
5
5
|
const NAME = 'apolitical-server';
|
|
6
6
|
const UUID = '00000000-0000-0000-0000-000000000000';
|
|
@@ -10,6 +10,8 @@ const ADMIN_ROLE = 'administrator';
|
|
|
10
10
|
module.exports = {
|
|
11
11
|
NODE_ENV,
|
|
12
12
|
LOG_LEVEL,
|
|
13
|
+
GOOGLE_CLOUD_PROJECT,
|
|
14
|
+
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT,
|
|
13
15
|
LOGGER_OPTIONS: {
|
|
14
16
|
logLevel: LOG_LEVEL,
|
|
15
17
|
labels: {
|
package/src/container.js
CHANGED
|
@@ -37,7 +37,6 @@ const jwtEncodeHelper = require('./helpers/jwt/encode.helper');
|
|
|
37
37
|
const jwtPassportHelper = require('./helpers/jwt/passport.helper');
|
|
38
38
|
const loggerHelper = require('./helpers/logger.helper');
|
|
39
39
|
const requestHelper = require('./helpers/request.helper');
|
|
40
|
-
const impersonationHelper = require('./helpers/impersonation.helper');
|
|
41
40
|
// Loaders
|
|
42
41
|
const documentationLoader = require('./loaders/documentation.loader');
|
|
43
42
|
const expressLoader = require('./loaders/express.loader');
|
|
@@ -98,7 +97,6 @@ container.register({
|
|
|
98
97
|
jwtPassportHelper: asFunction(jwtPassportHelper).singleton(),
|
|
99
98
|
loggerHelper: asFunction(loggerHelper).singleton(),
|
|
100
99
|
requestHelper: asFunction(requestHelper).singleton(),
|
|
101
|
-
impersonationHelper: asFunction(impersonationHelper).singleton(),
|
|
102
100
|
// Loaders
|
|
103
101
|
documentationLoader: asFunction(documentationLoader).singleton(),
|
|
104
102
|
expressLoader: asFunction(expressLoader).singleton(),
|
package/src/secrets.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const secretManager = require('@google-cloud/secret-manager');
|
|
4
|
+
const secretsServiceFactory = require('./services/secrets.service');
|
|
5
|
+
|
|
6
|
+
module.exports = secretsServiceFactory({
|
|
7
|
+
secretManager,
|
|
8
|
+
config: process.env,
|
|
9
|
+
logger: console,
|
|
10
|
+
});
|
|
@@ -1,26 +1,39 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
module.exports = ({ secretManager,
|
|
3
|
+
module.exports = ({ secretManager, config, logger }) => {
|
|
4
4
|
const DEV_DEFAULTS = {
|
|
5
|
-
projectId: 'hazel-tea-194609',
|
|
6
5
|
impersonateAccount: 'development-platform@hazel-tea-194609.iam.gserviceaccount.com',
|
|
7
6
|
};
|
|
8
7
|
|
|
9
8
|
const cache = {};
|
|
10
9
|
let client = null;
|
|
11
10
|
|
|
11
|
+
// Resolved once at first client creation; stored in closure so subsequent
|
|
12
|
+
// calls to isImpersonationEnabled() reflect the dev default without
|
|
13
|
+
// re-reading process.env.
|
|
14
|
+
let resolvedImpersonationTarget = config.GOOGLE_IMPERSONATE_SERVICE_ACCOUNT?.trim() || null;
|
|
15
|
+
|
|
16
|
+
function isImpersonationEnabled() {
|
|
17
|
+
return resolvedImpersonationTarget !== null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function impersonationLogContext() {
|
|
21
|
+
return resolvedImpersonationTarget ? { targetServiceAccount: resolvedImpersonationTarget } : {};
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
function getClient() {
|
|
13
25
|
if (!client) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
process.env
|
|
26
|
+
if (config.NODE_ENV !== 'production' && !isImpersonationEnabled()) {
|
|
27
|
+
resolvedImpersonationTarget = DEV_DEFAULTS.impersonateAccount;
|
|
28
|
+
// Set on process.env so the Google Cloud SDK picks it up via ADC
|
|
29
|
+
process.env['GOOGLE_IMPERSONATE_SERVICE_ACCOUNT'] = resolvedImpersonationTarget;
|
|
17
30
|
logger.info('[secrets] set default impersonation for development', {
|
|
18
|
-
targetServiceAccount:
|
|
31
|
+
targetServiceAccount: resolvedImpersonationTarget,
|
|
19
32
|
});
|
|
20
33
|
}
|
|
21
34
|
|
|
22
|
-
if (
|
|
23
|
-
logger.info('[secrets] using impersonation',
|
|
35
|
+
if (isImpersonationEnabled()) {
|
|
36
|
+
logger.info('[secrets] using impersonation', impersonationLogContext());
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
client = new secretManager.SecretManagerServiceClient();
|
|
@@ -30,10 +43,7 @@ module.exports = ({ secretManager, impersonationHelper, logger }) => {
|
|
|
30
43
|
|
|
31
44
|
async function loadSecrets(opts) {
|
|
32
45
|
const { secrets } = opts;
|
|
33
|
-
const projectId =
|
|
34
|
-
opts.projectId ??
|
|
35
|
-
process.env['GOOGLE_CLOUD_PROJECT'] ??
|
|
36
|
-
(process.env['NODE_ENV'] !== 'production' ? DEV_DEFAULTS.projectId : undefined);
|
|
46
|
+
const projectId = opts.projectId ?? config.GOOGLE_CLOUD_PROJECT;
|
|
37
47
|
|
|
38
48
|
if (!projectId) {
|
|
39
49
|
throw new Error('Missing projectId for Secret Manager');
|
|
@@ -57,12 +67,17 @@ module.exports = ({ secretManager, impersonationHelper, logger }) => {
|
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
cache[spec.envVar] = payload;
|
|
60
|
-
|
|
70
|
+
|
|
71
|
+
if (spec.exportToEnv) {
|
|
72
|
+
process.env[spec.envVar] = payload;
|
|
73
|
+
}
|
|
61
74
|
|
|
62
75
|
logger.info('[secrets] loaded', {
|
|
63
76
|
name: spec.name,
|
|
64
77
|
envVar: spec.envVar,
|
|
65
78
|
version,
|
|
79
|
+
cached: true,
|
|
80
|
+
exportedToEnv: !!spec.exportToEnv,
|
|
66
81
|
});
|
|
67
82
|
} catch (err) {
|
|
68
83
|
throw new Error(`Failed to load secret "${spec.name}": ${err.message}`);
|
|
@@ -81,6 +96,5 @@ module.exports = ({ secretManager, impersonationHelper, logger }) => {
|
|
|
81
96
|
return {
|
|
82
97
|
loadSecrets,
|
|
83
98
|
getSecret,
|
|
84
|
-
impersonation: impersonationHelper,
|
|
85
99
|
};
|
|
86
100
|
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
module.exports = () => {
|
|
4
|
-
// Returns the target service account to impersonate if configured via env
|
|
5
|
-
function getImpersonationTarget() {
|
|
6
|
-
const target = process.env['GOOGLE_IMPERSONATE_SERVICE_ACCOUNT'];
|
|
7
|
-
return target && target.trim().length > 0 ? target.trim() : null;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// Whether impersonation is enabled for the current process
|
|
11
|
-
function isImpersonationEnabled() {
|
|
12
|
-
return getImpersonationTarget() !== null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Produce a safe log payload for audit visibility
|
|
16
|
-
function impersonationLogContext() {
|
|
17
|
-
const target = getImpersonationTarget();
|
|
18
|
-
return target ? { targetServiceAccount: target } : {};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return { getImpersonationTarget, isImpersonationEnabled, impersonationLogContext };
|
|
22
|
-
};
|