forked from gov-suite/governed-service-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.ts
194 lines (169 loc) · 4.79 KB
/
vault.ts
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
import * as c from "https://deno.land/[email protected]/fmt/colors.ts";
import * as v from "https://denopkg.com/shah/[email protected]/mod.ts";
export type VaultAttrName = string;
export type VaultAttrQualifiedName = string;
export interface VaultAttrOptions {
readonly defaultValue?: unknown | VaultAttr;
readonly isOptional?: boolean;
readonly isSecret?: boolean;
readonly onUndefined?: (attr: VaultAttr) => unknown;
}
export interface VaultNamespace {
readonly commonNamespace: string;
readonly secretsNamespace: string;
}
export const isVaultNamespace = v.typeGuard<VaultNamespace>(
"commonNamespace",
"secretsNamespace",
);
export interface VaultAttr extends VaultAttrOptions {
readonly namespace?: VaultNamespace;
readonly baseName: VaultAttrName;
readonly qualifiedName: VaultAttrQualifiedName;
readonly value: () => unknown;
readonly valueFrom: (src: unknown | VaultAttr) => unknown;
}
export const isVaultAttr = v.typeGuard<VaultAttr>(
"baseName",
"qualifiedName",
"value",
"valueFrom",
);
export interface VaultTextAttr extends VaultAttr {
readonly value: () => string | undefined;
readonly valueFrom: (src: unknown | VaultAttr) => string | undefined;
readonly textValue: () => string | undefined;
}
export const isVaultTextAttr = v.typeGuard<VaultTextAttr>(
"baseName",
"qualifiedName",
"value",
"valueFrom",
"textValue",
);
export interface Vault {
readonly namespace: VaultNamespace;
readonly definedAttrs: () => Record<
VaultAttrQualifiedName,
VaultAttr
>;
readonly defineAttr: (attr: VaultAttr) => VaultAttr;
readonly qualifiedName: (
baseName: VaultAttrName,
options?: VaultAttrOptions,
) => VaultAttrQualifiedName;
}
export class EnvironmentVariable implements VaultTextAttr {
readonly namespace?: VaultNamespace;
readonly qualifiedName: VaultAttrQualifiedName;
constructor(
vault: Vault,
readonly baseName: VaultAttrName,
readonly options?: VaultAttrOptions,
) {
this.namespace = vault.namespace;
this.qualifiedName = vault.qualifiedName(baseName, options);
}
value(): string | undefined {
const value = Deno.env.get(this.qualifiedName);
if (value) return value;
if (this.options?.defaultValue) {
return this.valueFrom(this.options.defaultValue);
}
if (this.options?.onUndefined) {
return this.valueFrom(this.options.onUndefined(this));
}
}
valueFrom(src: unknown | VaultAttr): string | undefined {
if (isVaultTextAttr(src)) {
return src.value();
}
if (isVaultAttr(src)) {
return this.valueFrom(src.value());
}
if (typeof src === "string") return src;
return JSON.stringify(src);
}
textValue() {
return this.value() as string;
}
get defaulValue() {
return this.options?.defaultValue;
}
get isOptional() {
return this.options?.isOptional;
}
get isSecret() {
return this.options?.isSecret;
}
}
export class EnvironmentVault implements Vault {
readonly #definedAttrs: Record<VaultAttrQualifiedName, VaultAttr> = {};
constructor(
readonly namespace: VaultNamespace,
readonly options?: {
onUndefined?: (attr: VaultAttr) => unknown;
onDuplicateDefn?: (
newAttr: VaultAttr,
existingAttr: VaultAttr,
) => VaultAttr;
},
) {
}
prepareEnvVar(
baseName: VaultAttrName,
options?: VaultAttrOptions,
): VaultAttr {
return new EnvironmentVariable(
this,
baseName,
{ onUndefined: this.options?.onUndefined, ...options },
);
}
defineEnvVar(
baseName: VaultAttrName,
options?: VaultAttrOptions,
): VaultAttr {
return this.defineAttr(this.prepareEnvVar(baseName, options));
}
definedAttrs() {
return this.#definedAttrs;
}
defineAttr(attr: VaultAttr) {
const existing = this.#definedAttrs[attr.qualifiedName];
if (existing) {
if (this.options?.onDuplicateDefn) {
return this.options.onDuplicateDefn(attr, existing);
}
return existing;
}
this.#definedAttrs[attr.qualifiedName] = attr;
return attr;
}
qualifiedName(
baseName: VaultAttrName,
options?: VaultAttrOptions,
) {
const qName = options?.isSecret
? `${this.namespace.secretsNamespace}${baseName}`
: `${this.namespace.commonNamespace}${baseName}`;
return qName;
}
reportDefinedAttrs(): void {
const envVars = this.definedAttrs();
for (const envVar of Object.values(envVars)) {
const evValue = envVar.value();
console.log(
`${
evValue
? (envVar.isSecret
? c.brightGreen(envVar.qualifiedName)
: c.green(envVar.qualifiedName))
: (envVar.isSecret
? c.brightRed(envVar.qualifiedName)
: c.red(envVar.qualifiedName))
}${evValue ? "=" + c.yellow(evValue as string) : ""}`,
);
}
}
}