-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathruntime-env.ts
More file actions
108 lines (89 loc) · 3.5 KB
/
Copy pathruntime-env.ts
File metadata and controls
108 lines (89 loc) · 3.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
export const RUNTIME_ENV_NAME_PATTERN = /^[A-Z_][A-Z0-9_]*$/
export interface RuntimeEnvRedactionRegistrar {
registerSecretName(name: string): void
registerSecretValue(value: string): void
}
export interface NormalizeRuntimeEnvRecordOptions {
field?: string
invalid?: "throw" | "omit"
}
export interface ResolveSecretEnvNamesOptions {
source?: Record<string, string | undefined>
field?: string
}
export function isValidRuntimeEnvName(name: string): boolean {
return RUNTIME_ENV_NAME_PATTERN.test(name)
}
export function assertRuntimeEnvName(name: string, field = "env name"): void {
if (!isValidRuntimeEnvName(name)) {
throw new Error(`${field} must match ${RUNTIME_ENV_NAME_PATTERN.source}: ${name}`)
}
}
export function normalizeRuntimeEnvRecord(values: Record<string, unknown>, options: NormalizeRuntimeEnvRecordOptions = {}): Record<string, string> {
const runtimeEnv: Record<string, string> = {}
const invalid = options.invalid ?? "throw"
const field = options.field ?? "env"
for (const [name, value] of Object.entries(values)) {
const normalized = name.trim()
const valid = isValidRuntimeEnvName(normalized) && typeof value === "string"
if (!valid) {
if (invalid === "throw") {
throw new Error(`${field}.${name} must be a string value with a valid environment variable name`)
}
continue
}
runtimeEnv[normalized] = value
}
return runtimeEnv
}
export function resolveSecretEnvNames(names: readonly string[], options: ResolveSecretEnvNamesOptions = {}): Record<string, string> {
const source = options.source ?? process.env
const field = options.field ?? "secretEnv"
const secretEnv: Record<string, string> = {}
for (const name of names) {
const normalized = name.trim()
assertRuntimeEnvName(normalized, field)
const value = source[normalized]
if (value !== undefined) {
secretEnv[normalized] = value
}
}
return secretEnv
}
export function resolveRuntimeSecretEnvTargets(secretEnv: Record<string, string>, targets: Record<string, string> = {}): Record<string, string> {
const resolved: Record<string, string> = {}
for (const [target, source] of Object.entries(targets)) {
assertRuntimeEnvName(target, "secret env target")
assertRuntimeEnvName(source, "secret env source")
if (!(source in secretEnv)) throw new Error(`Secret env target ${target} references unavailable source: ${source}`)
resolved[target] = secretEnv[source] as string
}
return resolved
}
export function assertRuntimeSecretEnvTargetsAvailable(targets: Record<string, string> = {}, ...environmentSources: Array<Record<string, unknown>>): void {
for (const target of Object.keys(targets)) {
assertRuntimeEnvName(target, "secret env target")
if (environmentSources.some((source) => target in source)) throw new Error(`Secret env target collides with injected environment: ${target}`)
}
}
export function registerRuntimeSecretRedactions(secretEnv: Record<string, string>, registrar: RuntimeEnvRedactionRegistrar): void {
for (const [name, value] of Object.entries(secretEnv)) {
registrar.registerSecretName(name)
if (shouldRedactRuntimeSecretValue(value)) {
registrar.registerSecretValue(value)
}
}
}
export function shouldRedactRuntimeSecretValue(value: string): boolean {
const trimmed = value.trim()
if (trimmed.length < 8) {
return false
}
if (["true", "false", "null", "undefined"].includes(trimmed.toLowerCase())) {
return false
}
if (/^[0-9]+$/.test(trimmed)) {
return false
}
return true
}