@apolitical/server 4.0.0 → 4.1.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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apolitical/server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.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",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@apolitical/logger": "2.1.0",
|
|
27
|
+
"@google-cloud/secret-manager": "5.6.0",
|
|
27
28
|
"@cloudnative/health-connect": "2.1.0",
|
|
28
29
|
"@opentelemetry/api": "1.9.0",
|
|
29
30
|
"@opentelemetry/auto-instrumentations-node": "0.57.1",
|
package/src/container.js
CHANGED
|
@@ -36,6 +36,7 @@ const jwtEncodeHelper = require('./helpers/jwt/encode.helper');
|
|
|
36
36
|
const jwtPassportHelper = require('./helpers/jwt/passport.helper');
|
|
37
37
|
const loggerHelper = require('./helpers/logger.helper');
|
|
38
38
|
const requestHelper = require('./helpers/request.helper');
|
|
39
|
+
const impersonationHelper = require('./helpers/impersonation.helper');
|
|
39
40
|
// Loaders
|
|
40
41
|
const documentationLoader = require('./loaders/documentation.loader');
|
|
41
42
|
const expressLoader = require('./loaders/express.loader');
|
|
@@ -55,6 +56,7 @@ const expressService = require('./services/express.service');
|
|
|
55
56
|
const healthService = require('./services/health.service');
|
|
56
57
|
const jwtService = require('./services/jwt.service');
|
|
57
58
|
const serverService = require('./services/server.service');
|
|
59
|
+
const secretsService = require('./services/secrets.service');
|
|
58
60
|
|
|
59
61
|
// DI container
|
|
60
62
|
const container = createContainer();
|
|
@@ -94,6 +96,7 @@ container.register({
|
|
|
94
96
|
jwtPassportHelper: asFunction(jwtPassportHelper).singleton(),
|
|
95
97
|
loggerHelper: asFunction(loggerHelper).singleton(),
|
|
96
98
|
requestHelper: asFunction(requestHelper).singleton(),
|
|
99
|
+
impersonationHelper: asFunction(impersonationHelper).singleton(),
|
|
97
100
|
// Loaders
|
|
98
101
|
documentationLoader: asFunction(documentationLoader).singleton(),
|
|
99
102
|
expressLoader: asFunction(expressLoader).singleton(),
|
|
@@ -113,6 +116,7 @@ container.register({
|
|
|
113
116
|
healthService: asFunction(healthService).singleton(),
|
|
114
117
|
jwtService: asFunction(jwtService).singleton(),
|
|
115
118
|
serverService: asFunction(serverService).singleton(),
|
|
119
|
+
secretsService: asFunction(secretsService).singleton(),
|
|
116
120
|
});
|
|
117
121
|
|
|
118
122
|
module.exports = container;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Helpers for Application Default Credentials (ADC) impersonation detection.
|
|
5
|
+
*
|
|
6
|
+
* These utilities do not acquire credentials; they only surface intent
|
|
7
|
+
* based on environment variables so applications can log and enforce policy.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns the target service account to impersonate if configured via env.
|
|
12
|
+
* GOOGLE_IMPERSONATE_SERVICE_ACCOUNT is respected by Google client libraries
|
|
13
|
+
* when acquiring credentials via ADC.
|
|
14
|
+
* @returns {string|null}
|
|
15
|
+
*/
|
|
16
|
+
function getImpersonationTarget() {
|
|
17
|
+
const target = process.env['GOOGLE_IMPERSONATE_SERVICE_ACCOUNT'];
|
|
18
|
+
return target && target.trim().length > 0 ? target.trim() : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether impersonation is enabled for the current process.
|
|
23
|
+
* @returns {boolean}
|
|
24
|
+
*/
|
|
25
|
+
function isImpersonationEnabled() {
|
|
26
|
+
return getImpersonationTarget() !== null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Produce a safe log payload for audit visibility.
|
|
31
|
+
* @returns {{ targetServiceAccount: string } | {}}
|
|
32
|
+
*/
|
|
33
|
+
function impersonationLogContext() {
|
|
34
|
+
const target = getImpersonationTarget();
|
|
35
|
+
return target ? { targetServiceAccount: target } : {};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = () => ({
|
|
39
|
+
getImpersonationTarget,
|
|
40
|
+
isImpersonationEnabled,
|
|
41
|
+
impersonationLogContext,
|
|
42
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { SecretManagerServiceClient } = require('@google-cloud/secret-manager');
|
|
4
|
+
|
|
5
|
+
const DEV_DEFAULTS = {
|
|
6
|
+
projectId: 'hazel-tea-194609',
|
|
7
|
+
impersonateAccount: 'development-platform@hazel-tea-194609.iam.gserviceaccount.com',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* GCP Secret Manager service for loading secrets at runtime.
|
|
12
|
+
* Uses Application Default Credentials (ADC) with service account impersonation
|
|
13
|
+
* for local development.
|
|
14
|
+
*/
|
|
15
|
+
module.exports = ({ impersonationHelper, logger }) => {
|
|
16
|
+
const cache = {};
|
|
17
|
+
let client = null;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get or create the Secret Manager client.
|
|
21
|
+
* In non-production, auto-configures impersonation if not already set.
|
|
22
|
+
* @returns {SecretManagerServiceClient}
|
|
23
|
+
*/
|
|
24
|
+
function getClient() {
|
|
25
|
+
if (!client) {
|
|
26
|
+
if (process.env['NODE_ENV'] !== 'production' && !impersonationHelper.isImpersonationEnabled()) {
|
|
27
|
+
process.env['GOOGLE_IMPERSONATE_SERVICE_ACCOUNT'] = DEV_DEFAULTS.impersonateAccount;
|
|
28
|
+
logger.info('[secrets] set default impersonation for development', {
|
|
29
|
+
targetServiceAccount: DEV_DEFAULTS.impersonateAccount,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (impersonationHelper.isImpersonationEnabled()) {
|
|
34
|
+
logger.info('[secrets] using impersonation', impersonationHelper.impersonationLogContext());
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
client = new SecretManagerServiceClient();
|
|
38
|
+
}
|
|
39
|
+
return client;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Load secrets from Google Secret Manager and set them as environment variables.
|
|
44
|
+
* @param {Object} opts
|
|
45
|
+
* @param {string} [opts.projectId] - GCP project ID (defaults to DEV_DEFAULTS in non-production)
|
|
46
|
+
* @param {Array<{name: string, envVar: string, version?: string}>} opts.secrets - Secrets to load
|
|
47
|
+
* @throws {Error} If projectId is missing or secret cannot be loaded
|
|
48
|
+
*/
|
|
49
|
+
async function loadSecrets(opts) {
|
|
50
|
+
const { secrets } = opts;
|
|
51
|
+
const projectId =
|
|
52
|
+
opts.projectId ??
|
|
53
|
+
process.env['GOOGLE_CLOUD_PROJECT'] ??
|
|
54
|
+
(process.env['NODE_ENV'] !== 'production' ? DEV_DEFAULTS.projectId : undefined);
|
|
55
|
+
|
|
56
|
+
if (!projectId) {
|
|
57
|
+
throw new Error('Missing projectId for Secret Manager');
|
|
58
|
+
}
|
|
59
|
+
if (!secrets || secrets.length === 0) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const secretClient = getClient();
|
|
64
|
+
|
|
65
|
+
for (const spec of secrets) {
|
|
66
|
+
const version = spec.version ?? 'latest';
|
|
67
|
+
const name = `projects/${projectId}/secrets/${spec.name}/versions/${version}`;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
const [resp] = await secretClient.accessSecretVersion({ name });
|
|
71
|
+
const payload = resp.payload?.data?.toString('utf8');
|
|
72
|
+
|
|
73
|
+
if (!payload) {
|
|
74
|
+
throw new Error(`Empty payload for secret ${spec.name}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
cache[spec.envVar] = payload;
|
|
78
|
+
process.env[spec.envVar] = payload;
|
|
79
|
+
|
|
80
|
+
logger.info('[secrets] loaded', {
|
|
81
|
+
name: spec.name,
|
|
82
|
+
envVar: spec.envVar,
|
|
83
|
+
version,
|
|
84
|
+
});
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw new Error(`Failed to load secret "${spec.name}": ${err.message}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get a secret value from cache or environment.
|
|
93
|
+
* @param {string} envVar - The environment variable name
|
|
94
|
+
* @returns {string} The secret value
|
|
95
|
+
* @throws {Error} If secret is not loaded
|
|
96
|
+
*/
|
|
97
|
+
function getSecret(envVar) {
|
|
98
|
+
const val = cache[envVar] ?? process.env[envVar];
|
|
99
|
+
if (!val) {
|
|
100
|
+
throw new Error(`Secret "${envVar}" not loaded`);
|
|
101
|
+
}
|
|
102
|
+
return val;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
loadSecrets,
|
|
107
|
+
getSecret,
|
|
108
|
+
impersonation: impersonationHelper,
|
|
109
|
+
};
|
|
110
|
+
};
|
|
@@ -8,6 +8,7 @@ module.exports = ({
|
|
|
8
8
|
jwtService,
|
|
9
9
|
serverError,
|
|
10
10
|
requestHelper,
|
|
11
|
+
secretsService,
|
|
11
12
|
}) => {
|
|
12
13
|
// Register shutdown to clean up any resources used by the express app
|
|
13
14
|
registerShutdown(shutdown);
|
|
@@ -22,5 +23,6 @@ module.exports = ({
|
|
|
22
23
|
authorisation: authorisationMiddleware,
|
|
23
24
|
},
|
|
24
25
|
request: requestHelper,
|
|
26
|
+
secrets: secretsService,
|
|
25
27
|
};
|
|
26
28
|
};
|