forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecrets.ts
More file actions
347 lines (313 loc) · 11.5 KB
/
Copy pathsecrets.ts
File metadata and controls
347 lines (313 loc) · 11.5 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
import * as dotenv from 'dotenv';
import { logger } from '../logger';
// Load .env file
dotenv.config();
/**
* Represents a secret that can be retrieved and potentially refreshed.
* This interface allows for rotation-safe handling of secrets.
*/
export interface Secret<T> {
/**
* Get the current value of the secret.
*/
get(): T;
/**
* Refresh the secret value from its source (e.g., Environment, Vault, Secrets Manager).
*/
refresh(): Promise<void>;
}
/**
* An implementation of Secret that loads from environment variables.
*/
/** Known weak/placeholder secret values that must never be accepted outside development or test. */
const WEAK_SECRET_LITERALS = new Set<string>([
'dev-secret-keep-it-safe',
'postgresql://localhost:5432/talenttrust',
]);
/**
* Options controlling how a secret's default value and validation are
* applied depending on the current runtime environment.
*/
export interface EnvSecretOptions<T> {
/** Default value, only ever honored in development/test. */
defaultValue?: T;
/** Transform raw string -> T. */
transform?: (val: string) => T;
/**
* When true, this secret is treated as sensitive: its default is
* refused outside development/test, a minimum length is enforced,
* and known weak literal values are rejected — even if explicitly set.
*/
requireStrongInProd?: boolean;
/** Minimum character length when requireStrongInProd is true. Default: 32. */
minLength?: number;
}
/**
* An implementation of Secret that loads from environment variables.
*/
export class EnvSecret<T = string> implements Secret<T> {
private value!: T;
private readonly key: string;
private readonly defaultValue?: T;
private readonly transform?: (val: string) => T;
private readonly requireStrongInProd: boolean;
private readonly minLength: number;
/**
* @param key The environment variable key.
* @param defaultValue Optional default value if the environment variable is missing.
* For secrets registered with `requireStrongInProd: true`, this default is
* only ever honored in development/test — never in production/staging.
* @param transform Optional function to transform the raw string value to type T.
* @param options Optional extra validation behavior (see {@link EnvSecretOptions}).
*/
constructor(
key: string,
defaultValue?: T,
transform?: (val: string) => T,
options?: Omit<EnvSecretOptions<T>, 'defaultValue' | 'transform'>,
) {
this.key = key;
this.defaultValue = defaultValue;
this.transform = transform;
this.requireStrongInProd = options?.requireStrongInProd ?? false;
this.minLength = options?.minLength ?? 32;
this.load();
}
private isDevOrTest(): boolean {
const env = process.env.NODE_ENV ?? 'development';
return env === 'development' || env === 'test';
}
/**
* Loads the secret value from the environment variable.
*
* @remarks
* **Security — transform error redaction guarantee**:
* If the `transform` callback throws for any reason, the thrown error
* message contains **only the environment-variable key name** — never the
* raw protected value, any substring of it, nor any message derived from the
* original thrown value.
*
* This guarantee is unconditional:
* - If transform throws an `Error` whose `.message` echoes its input
* (e.g. a JSON/YAML parser), that message is discarded.
* - If transform throws a plain `string` (e.g. `throw rawValue`), that
* string is discarded.
* - If transform throws any other non-Error value, it is discarded.
*
* The catch block intentionally uses the catch-all `catch {` form
* (no binding) to make it impossible to accidentally reference the
* original error or the raw protected value.
*
* The resulting error message is safe to write to any log sink, including
* `src/logger.ts`, without further redaction.
*/
private load(): void {
const rawValue = process.env[this.key];
if (rawValue === undefined) {
// Defaults for sensitive secrets are dev/test-only, no matter what
// the caller passed in — this is the fail-fast guarantee that
// prevents a forgotten env var from silently falling back to a
// known, committed value in production.
if (this.defaultValue !== undefined && (!this.requireStrongInProd || this.isDevOrTest())) {
this.value = this.defaultValue;
return;
}
throw new Error(`Configuration Error: Missing required secret "${this.key}"`);
}
if (this.requireStrongInProd && !this.isDevOrTest()) {
if (WEAK_SECRET_LITERALS.has(rawValue)) {
logger.warn('SecretsManager: rejected known weak secret literal', { key: this.key });
throw new Error(`Configuration Error: Secret "${this.key}" is set to a known weak/placeholder value and must be changed`);
}
if (rawValue.length < this.minLength) {
logger.warn('SecretsManager: rejected secret below minimum length', { key: this.key, minLength: this.minLength });
throw new Error(`Configuration Error: Secret "${this.key}" must be at least ${this.minLength} characters`);
}
}
try {
this.value = this.transform ? this.transform(rawValue) : (rawValue as unknown as T);
} catch {
// Never include the original error message or any derivative of the raw
// secret value in the thrown error — a thrown parser error can echo its
// input. Only the key name is safe to surface here.
throw new Error(
`Configuration Error: Failed to transform credential "${this.key}" — details omitted`
);
}
}
/**
* Returns the current secret value.
*/
get(): T {
return this.value;
}
/**
* Refreshes the secret value by re-reading the environment variable.
* Note: In a production environment with rotation (like AWS Secrets Manager),
* this would involve an asynchronous API call to fetch the latest version.
*/
async refresh(): Promise<void> {
// For environment variables, we just re-load.
// If process.env was updated externally (e.g., via some watcher), this would pick it up.
this.load();
}
}
/**
* Manager class for handling multiple secrets and providing a unified interface.
*/
export class SecretsManager {
private secrets: Map<string, Secret<any>> = new Map();
/**
* Register a secret with the manager.
*/
register<T>(name: string, secret: Secret<T>): void {
if (this.secrets.has(name)) {
throw new Error(`SecretsManager Error: Secret "${name}" is already registered.`);
}
this.secrets.set(name, secret);
}
/**
* Get a registered secret by name.
*/
get<T>(name: string): Secret<T> {
const secret = this.secrets.get(name);
if (!secret) {
throw new Error(`SecretsManager Error: Secret "${name}" not found.`);
}
return secret;
}
/**
* Get the current value of a secret directly.
*/
getValue<T>(name: string): T {
return this.get<T>(name).get();
}
/**
* Refresh all registered secrets.
*/
async refreshAll(): Promise<void> {
const promises = Array.from(this.secrets.values()).map((s) => s.refresh());
await Promise.all(promises);
}
/**
* Clear all registered secrets (useful for testing).
*/
clear(): void {
this.secrets.clear();
}
}
/**
* Default instance of SecretsManager for the application.
*/
export const secretsManager = new SecretsManager();
/**
* Initialize core application secrets.
* This should be called early in the application lifecycle.
*
* @remarks
* - Secrets with defaults are for development only and must be overridden in production.
* - `DATABASE_URL` and `JWT_SECRET` are required in production.
*/
export function initializeSecrets(): void {
// Clear any existing registrations to avoid "already registered" errors on re-init
secretsManager.clear();
// Register common secrets
secretsManager.register('PORT', new EnvSecret<number>('PORT', 3001, (v) => parseInt(v, 10)));
secretsManager.register('NODE_ENV', new EnvSecret('NODE_ENV', 'development'));
// These have defaults for development/test only. Outside those environments,
// a missing value throws at boot (fail-fast) instead of silently falling
// back to a value that is committed to source control.
secretsManager.register(
'DATABASE_URL',
new EnvSecret('DATABASE_URL', 'postgresql://localhost:5432/talenttrust', undefined, {
requireStrongInProd: true,
minLength: 1, // DATABASE_URL just needs to be present outside dev/test, not "strong" per se
}),
);
secretsManager.register(
'JWT_SECRET',
new EnvSecret('JWT_SECRET', 'dev-secret-keep-it-safe', undefined, {
requireStrongInProd: true,
minLength: 32,
}),
);
}
// Self-initialize on module load for convenience, but can be called again if needed.
initializeSecrets();
/**
* RotatingSecret fetches a secret value from an asynchronous provider and
* caches the last successful value. It exposes the same synchronous
* `get()` contract as other `Secret` implementations while making
* `refresh()` perform the real asynchronous fetch.
*
* On refresh errors the previous value is retained (fail-safe) and no
* secret material is ever written to logs. A refresh interval can be
* supplied to enable background polling.
*/
export class RotatingSecret<T = string> implements Secret<T> {
private value?: T;
private readonly provider: () => Promise<string>;
private readonly transform?: (val: string) => T;
private timer?: NodeJS.Timeout;
private readonly name?: string;
/**
* @param opts.provider Async function that returns the raw secret string.
* @param opts.defaultValue Optional default value used until the first
* successful fetch.
* @param opts.transform Optional transform from raw string to `T`.
* @param opts.refreshIntervalMs Optional background refresh interval.
* @param opts.name Optional name used in non-sensitive logs/messages.
*/
constructor(opts: {
provider: () => Promise<string>;
defaultValue?: T;
transform?: (val: string) => T;
refreshIntervalMs?: number;
name?: string;
}) {
this.provider = opts.provider;
this.transform = opts.transform;
this.name = opts.name;
if (opts.defaultValue !== undefined) {
this.value = opts.defaultValue;
}
if (opts.refreshIntervalMs && opts.refreshIntervalMs > 0) {
this.timer = setInterval(() => {
// fire-and-forget background refresh; failures are tolerated
this.refresh().catch(() => {
// Intentionally quiet: refresh() already logs a minimal message
});
}, opts.refreshIntervalMs);
}
}
get(): T {
if (this.value === undefined) {
throw new Error(`Configuration Error: Missing rotated secret${this.name ? ` \"${this.name}\"` : ''}`);
}
return this.value as T;
}
async refresh(): Promise<void> {
try {
const raw = await this.provider();
const newVal = this.transform ? this.transform(raw) : (raw as unknown as T);
this.value = newVal;
} catch {
// Do not log secret values. Log only that refresh failed and include
// the secret name for context. Preserve previous value (fail-safe).
try {
logger.warn('SecretsManager: failed to refresh secret', { name: this.name });
} catch {
// Swallow any logging errors; we must not surface secrets here.
}
}
}
/**
* Stop any background refresh timer. Useful for tests/cleanup.
*/
stopAutoRefresh(): void {
if (this.timer) {
clearInterval(this.timer);
this.timer = undefined;
}
}
}