dotsec 1.0.0-alpha.2 → 1.0.0-alpha.20
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/README.md +191 -39
- package/bin/dotsec.js +1 -1
- package/dist/cli/index.js +37 -0
- package/dist/cli/index.js.map +7 -0
- package/dist/cli/index.mjs +37 -0
- package/dist/cli/index.mjs.map +7 -0
- package/dist/index.d.ts +141 -11
- package/dist/index.js +1 -43
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +26 -31
- package/src/templates/dotsec.config.ts +15 -0
- package/bin/ds.js +0 -3
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +0 -2237
- package/dist/cli.js.map +0 -7
- package/dist/ds/cli.js +0 -1111
- package/dist/ds/cli.js.map +0 -7
- package/dist/esm/cli.js +0 -2245
- package/dist/esm/cli.js.map +0 -7
- package/dist/esm/ds/cli.js +0 -1116
- package/dist/esm/ds/cli.js.map +0 -7
- package/dist/esm/index.js +0 -16
- package/dist/esm/index.js.map +0 -7
package/dist/ds/cli.js
DELETED
|
@@ -1,1111 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
25
|
-
var __reExport = (target, module2, desc) => {
|
|
26
|
-
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
27
|
-
for (let key of __getOwnPropNames(module2))
|
|
28
|
-
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
29
|
-
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
30
|
-
}
|
|
31
|
-
return target;
|
|
32
|
-
};
|
|
33
|
-
var __toModule = (module2) => {
|
|
34
|
-
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// src/ds/cli.ts
|
|
38
|
-
var import_commander2 = __toModule(require("commander"));
|
|
39
|
-
|
|
40
|
-
// src/ds/commands/run.ts
|
|
41
|
-
var import_node_fs = __toModule(require("node:fs"));
|
|
42
|
-
var import_commander = __toModule(require("commander"));
|
|
43
|
-
var import_cross_spawn = __toModule(require("cross-spawn"));
|
|
44
|
-
var import_dotenv = __toModule(require("dotenv"));
|
|
45
|
-
|
|
46
|
-
// src/utils/getCredentialsProfileRegion.ts
|
|
47
|
-
var import_credential_providers = __toModule(require("@aws-sdk/credential-providers"));
|
|
48
|
-
var import_shared_ini_file_loader = __toModule(require("@aws-sdk/shared-ini-file-loader"));
|
|
49
|
-
|
|
50
|
-
// src/utils/logger.ts
|
|
51
|
-
var import_chalk = __toModule(require("chalk"));
|
|
52
|
-
var import_cli_highlight = __toModule(require("cli-highlight"));
|
|
53
|
-
var _logger;
|
|
54
|
-
var getLogger = () => {
|
|
55
|
-
if (!_logger) {
|
|
56
|
-
_logger = console;
|
|
57
|
-
}
|
|
58
|
-
return _logger;
|
|
59
|
-
};
|
|
60
|
-
var emphasis = (str) => import_chalk.default.yellowBright(str);
|
|
61
|
-
var strong = (str) => import_chalk.default.yellow.bold(str);
|
|
62
|
-
var myTheme = {
|
|
63
|
-
attr: import_chalk.default.yellow.bold,
|
|
64
|
-
string: import_chalk.default.yellowBright.dim,
|
|
65
|
-
params: import_chalk.default.red,
|
|
66
|
-
deletion: import_chalk.default.red.strikethrough,
|
|
67
|
-
number: import_cli_highlight.plain
|
|
68
|
-
};
|
|
69
|
-
var prettyCode = (str) => {
|
|
70
|
-
return (0, import_cli_highlight.highlight)(str, { theme: myTheme });
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// src/utils/getCredentialsProfileRegion.ts
|
|
74
|
-
var getCredentialsProfileRegion = async ({
|
|
75
|
-
argv,
|
|
76
|
-
env
|
|
77
|
-
}) => {
|
|
78
|
-
var _a, _b, _c;
|
|
79
|
-
const sharedConfigFiles = await (0, import_shared_ini_file_loader.loadSharedConfigFiles)();
|
|
80
|
-
let credentialsAndOrigin = void 0;
|
|
81
|
-
let profileAndOrigin = void 0;
|
|
82
|
-
let regionAndOrigin = void 0;
|
|
83
|
-
if (argv.profile) {
|
|
84
|
-
profileAndOrigin = {
|
|
85
|
-
value: argv.profile,
|
|
86
|
-
origin: `command line option: ${emphasis(argv.profile)}`
|
|
87
|
-
};
|
|
88
|
-
credentialsAndOrigin = {
|
|
89
|
-
value: await (0, import_credential_providers.fromIni)({
|
|
90
|
-
profile: argv.profile
|
|
91
|
-
})(),
|
|
92
|
-
origin: `${emphasis(`[${argv.profile}]`)} in credentials file`
|
|
93
|
-
};
|
|
94
|
-
} else if (env.AWS_PROFILE) {
|
|
95
|
-
profileAndOrigin = {
|
|
96
|
-
value: env.AWS_PROFILE,
|
|
97
|
-
origin: `env variable ${emphasis("AWS_PROFILE")}: ${strong(env.AWS_PROFILE)}`
|
|
98
|
-
};
|
|
99
|
-
credentialsAndOrigin = {
|
|
100
|
-
value: await (0, import_credential_providers.fromIni)({
|
|
101
|
-
profile: env.AWS_PROFILE
|
|
102
|
-
})(),
|
|
103
|
-
origin: `env variable ${emphasis("AWS_PROFILE")}: ${strong(env.AWS_PROFILE)}`
|
|
104
|
-
};
|
|
105
|
-
} else if (env.AWS_ACCESS_KEY_ID && env.AWS_SECRET_ACCESS_KEY) {
|
|
106
|
-
credentialsAndOrigin = {
|
|
107
|
-
value: await (0, import_credential_providers.fromEnv)()(),
|
|
108
|
-
origin: `env variables ${emphasis("AWS_ACCESS_KEY_ID")} and ${emphasis("AWS_SECRET_ACCESS_KEY")}`
|
|
109
|
-
};
|
|
110
|
-
} else if ((_a = sharedConfigFiles.credentialsFile) == null ? void 0 : _a.default) {
|
|
111
|
-
profileAndOrigin = {
|
|
112
|
-
value: "default",
|
|
113
|
-
origin: `${emphasis("[default]")} in credentials file`
|
|
114
|
-
};
|
|
115
|
-
credentialsAndOrigin = {
|
|
116
|
-
value: await (0, import_credential_providers.fromIni)({
|
|
117
|
-
profile: "default"
|
|
118
|
-
})(),
|
|
119
|
-
origin: `profile ${emphasis("[default]")}`
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
if (argv.region) {
|
|
123
|
-
regionAndOrigin = {
|
|
124
|
-
value: argv.region,
|
|
125
|
-
origin: `command line option: ${emphasis(argv.region)}`
|
|
126
|
-
};
|
|
127
|
-
} else if (env.AWS_REGION) {
|
|
128
|
-
regionAndOrigin = {
|
|
129
|
-
value: env.AWS_REGION,
|
|
130
|
-
origin: `env variable ${emphasis("AWS_REGION")}: ${strong(env.AWS_REGION)}`
|
|
131
|
-
};
|
|
132
|
-
} else if (env.AWS_DEFAULT_REGION) {
|
|
133
|
-
regionAndOrigin = {
|
|
134
|
-
value: env.AWS_DEFAULT_REGION,
|
|
135
|
-
origin: `env variable ${emphasis("AWS_DEFAULT_REGION")}: ${strong(env.AWS_DEFAULT_REGION)}`
|
|
136
|
-
};
|
|
137
|
-
} else if (profileAndOrigin) {
|
|
138
|
-
const foundRegion = (_c = (_b = sharedConfigFiles == null ? void 0 : sharedConfigFiles.configFile) == null ? void 0 : _b[profileAndOrigin.value]) == null ? void 0 : _c.region;
|
|
139
|
-
if (foundRegion) {
|
|
140
|
-
regionAndOrigin = {
|
|
141
|
-
value: foundRegion,
|
|
142
|
-
origin: `${emphasis(`[profile ${profileAndOrigin.value}]`)} in config file`
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const assumedRole = argv.assumeRoleArn || env.AWS_ASSUME_ROLE_ARN;
|
|
147
|
-
if (assumedRole) {
|
|
148
|
-
const origin = argv.assumeRoleArn ? "command line option" : "env variable";
|
|
149
|
-
credentialsAndOrigin = {
|
|
150
|
-
value: await (0, import_credential_providers.fromTemporaryCredentials)({
|
|
151
|
-
masterCredentials: credentialsAndOrigin == null ? void 0 : credentialsAndOrigin.value,
|
|
152
|
-
params: {
|
|
153
|
-
DurationSeconds: argv.assumeRoleSessionDuration || Number(env.AWS_ASSUME_ROLE_SESSION_DURATION) || 3600,
|
|
154
|
-
RoleArn: assumedRole
|
|
155
|
-
},
|
|
156
|
-
clientConfig: {
|
|
157
|
-
region: regionAndOrigin == null ? void 0 : regionAndOrigin.value
|
|
158
|
-
}
|
|
159
|
-
})(),
|
|
160
|
-
origin: `${origin} ${emphasis(`[${assumedRole}]`)}`
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
return { credentialsAndOrigin, regionAndOrigin, profileAndOrigin };
|
|
164
|
-
};
|
|
165
|
-
var printVerboseCredentialsProfileRegion = ({
|
|
166
|
-
credentialsAndOrigin,
|
|
167
|
-
regionAndOrigin,
|
|
168
|
-
profileAndOrigin
|
|
169
|
-
}) => {
|
|
170
|
-
const out = [];
|
|
171
|
-
if (profileAndOrigin) {
|
|
172
|
-
out.push(`Got profile name from ${profileAndOrigin.origin}`);
|
|
173
|
-
}
|
|
174
|
-
if (credentialsAndOrigin) {
|
|
175
|
-
out.push(`Resolved credentials from ${credentialsAndOrigin.origin}`);
|
|
176
|
-
}
|
|
177
|
-
if (regionAndOrigin) {
|
|
178
|
-
out.push(`Resolved region from ${regionAndOrigin.origin}`);
|
|
179
|
-
}
|
|
180
|
-
return out.join("\n");
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
// src/lib/partial-commands/handleCredentialsAndRegion.ts
|
|
184
|
-
var handleCredentialsAndRegion = async ({
|
|
185
|
-
argv,
|
|
186
|
-
env
|
|
187
|
-
}) => {
|
|
188
|
-
const { credentialsAndOrigin, regionAndOrigin, profileAndOrigin } = await getCredentialsProfileRegion({
|
|
189
|
-
argv: {
|
|
190
|
-
region: argv.awsRegion,
|
|
191
|
-
profile: argv.awsProfile,
|
|
192
|
-
assumeRoleArn: argv.awsAssumeRoleArn,
|
|
193
|
-
assumeRoleSessionDuration: argv.awsAssumeRoleSessionDuration
|
|
194
|
-
},
|
|
195
|
-
env: __spreadValues({}, env)
|
|
196
|
-
});
|
|
197
|
-
if (argv.verbose === true) {
|
|
198
|
-
console.log(printVerboseCredentialsProfileRegion({
|
|
199
|
-
credentialsAndOrigin,
|
|
200
|
-
regionAndOrigin,
|
|
201
|
-
profileAndOrigin
|
|
202
|
-
}));
|
|
203
|
-
}
|
|
204
|
-
if (!credentialsAndOrigin || !regionAndOrigin) {
|
|
205
|
-
if (!credentialsAndOrigin) {
|
|
206
|
-
console.error("Could not find credentials");
|
|
207
|
-
throw new Error("Could not find credentials");
|
|
208
|
-
}
|
|
209
|
-
if (!regionAndOrigin) {
|
|
210
|
-
console.error("Could not find region");
|
|
211
|
-
throw new Error("Could not find region");
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return { credentialsAndOrigin, regionAndOrigin };
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
// src/lib/wtf/crypto.ts
|
|
218
|
-
var import_client_kms2 = __toModule(require("@aws-sdk/client-kms"));
|
|
219
|
-
var import_client_secrets_manager2 = __toModule(require("@aws-sdk/client-secrets-manager"));
|
|
220
|
-
var import_client_ssm2 = __toModule(require("@aws-sdk/client-ssm"));
|
|
221
|
-
var import_constant_case = __toModule(require("constant-case"));
|
|
222
|
-
|
|
223
|
-
// src/utils/kms.ts
|
|
224
|
-
var import_client_kms = __toModule(require("@aws-sdk/client-kms"));
|
|
225
|
-
var getKMSClient = ({
|
|
226
|
-
configuration
|
|
227
|
-
}) => {
|
|
228
|
-
const kmsClient = new import_client_kms.KMSClient(configuration);
|
|
229
|
-
return kmsClient;
|
|
230
|
-
};
|
|
231
|
-
var getEncryptionAlgorithm = async (kmsClient, awsKeyAlias) => {
|
|
232
|
-
var _a, _b;
|
|
233
|
-
const describeKeyCommand = new import_client_kms.DescribeKeyCommand({
|
|
234
|
-
KeyId: awsKeyAlias
|
|
235
|
-
});
|
|
236
|
-
const describeKeyResult = await kmsClient.send(describeKeyCommand);
|
|
237
|
-
const encryptionAlgorithm = (_b = (_a = describeKeyResult.KeyMetadata) == null ? void 0 : _a.EncryptionAlgorithms) == null ? void 0 : _b[0];
|
|
238
|
-
if (encryptionAlgorithm === void 0) {
|
|
239
|
-
throw new Error(`Could not determine encryption algorithm`);
|
|
240
|
-
}
|
|
241
|
-
return encryptionAlgorithm;
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
// src/utils/secretsManager.ts
|
|
245
|
-
var import_client_secrets_manager = __toModule(require("@aws-sdk/client-secrets-manager"));
|
|
246
|
-
|
|
247
|
-
// src/utils/ssm.ts
|
|
248
|
-
var import_client_ssm = __toModule(require("@aws-sdk/client-ssm"));
|
|
249
|
-
|
|
250
|
-
// src/lib/wtf/types.ts
|
|
251
|
-
var isString = (value) => {
|
|
252
|
-
return typeof value === "string";
|
|
253
|
-
};
|
|
254
|
-
var isNumber = (value) => {
|
|
255
|
-
return typeof value === "number";
|
|
256
|
-
};
|
|
257
|
-
var isBoolean = (value) => {
|
|
258
|
-
return typeof value === "boolean";
|
|
259
|
-
};
|
|
260
|
-
var isSSMParameter = (leafOrTree) => {
|
|
261
|
-
const ssmParameter = leafOrTree;
|
|
262
|
-
return typeof ssmParameter === "object" && ssmParameter !== null && "type" in ssmParameter && ssmParameter.type === "ssm";
|
|
263
|
-
};
|
|
264
|
-
var isRegularParameterObject = (value) => {
|
|
265
|
-
const regularParameter = value;
|
|
266
|
-
return typeof regularParameter === "object" && regularParameter !== null && "type" in regularParameter && regularParameter.type === "standard";
|
|
267
|
-
};
|
|
268
|
-
var isRegularParameter = (leafOrTree) => {
|
|
269
|
-
const leaf = leafOrTree;
|
|
270
|
-
return isString(leaf) || isNumber(leaf) || isBoolean(leaf) || isRegularParameterObject(leaf);
|
|
271
|
-
};
|
|
272
|
-
var isEncryptedSSMParameter = (leafOrTree) => {
|
|
273
|
-
const leaf = leafOrTree;
|
|
274
|
-
return leaf.type !== void 0 && leaf.type === "ssm" && leaf.encryptedValue !== void 0;
|
|
275
|
-
};
|
|
276
|
-
var isEncryptedRegularParameter = (leafOrTree) => {
|
|
277
|
-
const leaf = leafOrTree;
|
|
278
|
-
return leaf.type !== void 0 && leaf.type === "standard" && leaf.encryptedValue !== void 0;
|
|
279
|
-
};
|
|
280
|
-
var isSecretsManagerParameter = (leafOrTree) => {
|
|
281
|
-
const leaf = leafOrTree;
|
|
282
|
-
return leaf.type !== void 0 && leaf.type === "secretsManager" && !(isString(leaf) || isNumber(leaf) || isBoolean(leaf));
|
|
283
|
-
};
|
|
284
|
-
var isDotSecTree = (leafOrTree) => {
|
|
285
|
-
if (typeof leafOrTree === "object" && !Array.isArray(leafOrTree) && leafOrTree !== null && !isSSMParameter(leafOrTree) && !isRegularParameter(leafOrTree) && !isEncryptedSSMParameter(leafOrTree) && !isEncryptedRegularParameter(leafOrTree) && !isSecretsManagerParameter(leafOrTree)) {
|
|
286
|
-
return true;
|
|
287
|
-
}
|
|
288
|
-
return false;
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
// src/lib/wtf/flat.ts
|
|
292
|
-
var flattenTree = (tree) => {
|
|
293
|
-
const lazy = {};
|
|
294
|
-
const innerParser = (leafOrTree, paths = []) => {
|
|
295
|
-
if (isDotSecTree(leafOrTree)) {
|
|
296
|
-
Object.entries(leafOrTree).map(([key, value]) => {
|
|
297
|
-
innerParser(value, [...paths, key]);
|
|
298
|
-
});
|
|
299
|
-
} else {
|
|
300
|
-
lazy[paths.join("/")] = leafOrTree;
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
innerParser(tree);
|
|
304
|
-
return lazy;
|
|
305
|
-
};
|
|
306
|
-
var flattenPlainText = (dotSec) => {
|
|
307
|
-
return __spreadProps(__spreadValues({}, dotSec), { plaintext: flattenTree(dotSec.plaintext) });
|
|
308
|
-
};
|
|
309
|
-
var flattenEncrypted = (dotSec) => {
|
|
310
|
-
return __spreadProps(__spreadValues({}, dotSec), { encrypted: flattenTree(dotSec.encrypted) });
|
|
311
|
-
};
|
|
312
|
-
var expandTree = (tree) => {
|
|
313
|
-
const lazy = {};
|
|
314
|
-
Object.entries(tree).map(([key, value]) => {
|
|
315
|
-
const paths = key.split("/");
|
|
316
|
-
let current = lazy;
|
|
317
|
-
paths.forEach((pathKey, index) => {
|
|
318
|
-
if (!current[pathKey]) {
|
|
319
|
-
if (index === paths.length - 1) {
|
|
320
|
-
current[pathKey] = value;
|
|
321
|
-
} else {
|
|
322
|
-
current[pathKey] = {};
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
current = current[pathKey];
|
|
326
|
-
});
|
|
327
|
-
});
|
|
328
|
-
return lazy;
|
|
329
|
-
};
|
|
330
|
-
var expandPlainText = (dotSec) => {
|
|
331
|
-
return __spreadProps(__spreadValues({}, dotSec), { plaintext: expandTree(dotSec.plaintext) });
|
|
332
|
-
};
|
|
333
|
-
var expandEncrypted = (dotSec) => {
|
|
334
|
-
return __spreadProps(__spreadValues({}, dotSec), { encrypted: expandTree(dotSec.encrypted) });
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
// src/lib/wtf/crypto.ts
|
|
338
|
-
var maybeJson = (value) => {
|
|
339
|
-
try {
|
|
340
|
-
return JSON.parse(value);
|
|
341
|
-
} catch (e) {
|
|
342
|
-
return value;
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
var decryptedEncrypted = async (options) => {
|
|
346
|
-
var _a, _b;
|
|
347
|
-
const { dotSecEncrypted, credentials, region, verbose, keyAlias } = options;
|
|
348
|
-
const dotSecEncryptedFlattened = flattenEncrypted(dotSecEncrypted);
|
|
349
|
-
const { info, table } = getLogger();
|
|
350
|
-
const kmsClient = getKMSClient({
|
|
351
|
-
configuration: {
|
|
352
|
-
credentials,
|
|
353
|
-
region
|
|
354
|
-
},
|
|
355
|
-
verbose
|
|
356
|
-
});
|
|
357
|
-
const awsKeyAlias = keyAlias || ((_b = (_a = dotSecEncrypted.config) == null ? void 0 : _a.aws) == null ? void 0 : _b.keyAlias);
|
|
358
|
-
if (!awsKeyAlias) {
|
|
359
|
-
throw new Error("No key alias specified");
|
|
360
|
-
}
|
|
361
|
-
if (verbose) {
|
|
362
|
-
info(`Encrypting using key alias ${emphasis(awsKeyAlias)} in ${emphasis(await kmsClient.config.region())}`);
|
|
363
|
-
const describeKeyCommand = new import_client_kms2.DescribeKeyCommand({
|
|
364
|
-
KeyId: awsKeyAlias
|
|
365
|
-
});
|
|
366
|
-
const describeKeyResult = await kmsClient.send(describeKeyCommand);
|
|
367
|
-
info("keyMetaData", __spreadValues({}, describeKeyResult.KeyMetadata));
|
|
368
|
-
}
|
|
369
|
-
const encryptionAlgorithm = await getEncryptionAlgorithm(kmsClient, awsKeyAlias);
|
|
370
|
-
const dotSecFlattened = {
|
|
371
|
-
config: __spreadValues({}, dotSecEncrypted.config),
|
|
372
|
-
plaintext: {}
|
|
373
|
-
};
|
|
374
|
-
for (const [key, encryptedValue] of Object.entries(dotSecEncryptedFlattened.encrypted)) {
|
|
375
|
-
const decryptCommand = new import_client_kms2.DecryptCommand({
|
|
376
|
-
KeyId: awsKeyAlias,
|
|
377
|
-
CiphertextBlob: Buffer.from(encryptedValue.encryptedValue, "base64"),
|
|
378
|
-
EncryptionAlgorithm: encryptionAlgorithm
|
|
379
|
-
});
|
|
380
|
-
const decryptionResult = await kmsClient.send(decryptCommand);
|
|
381
|
-
if (!decryptionResult.Plaintext) {
|
|
382
|
-
throw new Error(`Something bad happened: ${JSON.stringify({
|
|
383
|
-
key,
|
|
384
|
-
cipherText: encryptedValue,
|
|
385
|
-
decryptCommand
|
|
386
|
-
})}`);
|
|
387
|
-
}
|
|
388
|
-
if (verbose) {
|
|
389
|
-
info(`Decrypting key ${emphasis(key)} ${strong("ok")}`);
|
|
390
|
-
}
|
|
391
|
-
const decryptedValue = Buffer.from(decryptionResult.Plaintext).toString();
|
|
392
|
-
const decryptedKeyValue = JSON.parse(decryptedValue);
|
|
393
|
-
dotSecFlattened.plaintext[key] = maybeJson(decryptedKeyValue.value);
|
|
394
|
-
}
|
|
395
|
-
return expandPlainText(dotSecFlattened);
|
|
396
|
-
};
|
|
397
|
-
var encryptPlainText = async (options) => {
|
|
398
|
-
var _a, _b;
|
|
399
|
-
const { dotSecPlainText, credentials, region, verbose, keyAlias } = options;
|
|
400
|
-
const dotSecFlattened = flattenPlainText(dotSecPlainText);
|
|
401
|
-
const { info } = getLogger();
|
|
402
|
-
const kmsClient = getKMSClient({
|
|
403
|
-
configuration: {
|
|
404
|
-
credentials,
|
|
405
|
-
region
|
|
406
|
-
},
|
|
407
|
-
verbose
|
|
408
|
-
});
|
|
409
|
-
const awsKeyAlias = keyAlias || ((_b = (_a = dotSecFlattened.config) == null ? void 0 : _a.aws) == null ? void 0 : _b.keyAlias);
|
|
410
|
-
if (!awsKeyAlias) {
|
|
411
|
-
throw new Error("No key alias specified");
|
|
412
|
-
}
|
|
413
|
-
if (verbose) {
|
|
414
|
-
info(`Encrypting using key alias ${emphasis(awsKeyAlias)} in ${emphasis(await kmsClient.config.region())}`);
|
|
415
|
-
const describeKeyCommand = new import_client_kms2.DescribeKeyCommand({
|
|
416
|
-
KeyId: awsKeyAlias
|
|
417
|
-
});
|
|
418
|
-
const describeKeyResult = await kmsClient.send(describeKeyCommand);
|
|
419
|
-
info("keyMetaData", __spreadValues({}, describeKeyResult.KeyMetadata));
|
|
420
|
-
}
|
|
421
|
-
const encryptionAlgorithm = await getEncryptionAlgorithm(kmsClient, awsKeyAlias);
|
|
422
|
-
const encryptedDotSecFlattened = {
|
|
423
|
-
config: __spreadValues({}, dotSecFlattened.config),
|
|
424
|
-
encrypted: {}
|
|
425
|
-
};
|
|
426
|
-
for (const [key, plainTextValue] of Object.entries(dotSecFlattened.plaintext)) {
|
|
427
|
-
let plainTextValueCopy = plainTextValue;
|
|
428
|
-
if (typeof plainTextValueCopy !== "string" && typeof plainTextValueCopy !== "number" && typeof plainTextValueCopy !== "boolean") {
|
|
429
|
-
plainTextValueCopy = JSON.stringify(plainTextValue);
|
|
430
|
-
}
|
|
431
|
-
const damn = JSON.stringify({ key, value: plainTextValueCopy });
|
|
432
|
-
const encryptCommand = new import_client_kms2.EncryptCommand({
|
|
433
|
-
KeyId: awsKeyAlias,
|
|
434
|
-
Plaintext: Buffer.from(String(damn)),
|
|
435
|
-
EncryptionAlgorithm: encryptionAlgorithm
|
|
436
|
-
});
|
|
437
|
-
const encryptionResult = await kmsClient.send(encryptCommand);
|
|
438
|
-
if (!encryptionResult.CiphertextBlob) {
|
|
439
|
-
throw new Error(`Something bad happened: ${JSON.stringify({
|
|
440
|
-
key,
|
|
441
|
-
value: plainTextValue,
|
|
442
|
-
encryptCommand
|
|
443
|
-
})}`);
|
|
444
|
-
}
|
|
445
|
-
if (verbose) {
|
|
446
|
-
info(`Encrypting key ${emphasis(key)} ${strong("ok")}`);
|
|
447
|
-
}
|
|
448
|
-
const cipherText = Buffer.from(encryptionResult.CiphertextBlob).toString("base64");
|
|
449
|
-
if (isRegularParameter(plainTextValue)) {
|
|
450
|
-
encryptedDotSecFlattened.encrypted[key] = {
|
|
451
|
-
type: "standard",
|
|
452
|
-
encryptedValue: cipherText
|
|
453
|
-
};
|
|
454
|
-
} else if (isSSMParameter(plainTextValue)) {
|
|
455
|
-
encryptedDotSecFlattened.encrypted[key] = {
|
|
456
|
-
type: "ssm",
|
|
457
|
-
encryptedValue: cipherText
|
|
458
|
-
};
|
|
459
|
-
} else if (isSecretsManagerParameter(plainTextValue)) {
|
|
460
|
-
encryptedDotSecFlattened.encrypted[key] = {
|
|
461
|
-
type: "secretsManager",
|
|
462
|
-
encryptedValue: cipherText
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
return expandEncrypted(encryptedDotSecFlattened);
|
|
467
|
-
};
|
|
468
|
-
var decryptRawDotSecValues = async (options) => {
|
|
469
|
-
const { info } = getLogger();
|
|
470
|
-
const {
|
|
471
|
-
dotSecKeysValues: rawDotSec,
|
|
472
|
-
credentials,
|
|
473
|
-
region,
|
|
474
|
-
verbose,
|
|
475
|
-
keyAlias,
|
|
476
|
-
searchPath
|
|
477
|
-
} = options;
|
|
478
|
-
const kmsClient = getKMSClient({
|
|
479
|
-
configuration: {
|
|
480
|
-
credentials,
|
|
481
|
-
region
|
|
482
|
-
},
|
|
483
|
-
verbose
|
|
484
|
-
});
|
|
485
|
-
const s = searchPath == null ? void 0 : searchPath.split(".").map((part) => `${(0, import_constant_case.constantCase)(part)}_`).join("");
|
|
486
|
-
const awsKeyAlias = keyAlias;
|
|
487
|
-
if (!keyAlias) {
|
|
488
|
-
throw new Error("No key alias specified");
|
|
489
|
-
}
|
|
490
|
-
const encryptionAlgorithm = await getEncryptionAlgorithm(kmsClient, keyAlias);
|
|
491
|
-
const dotEnvLines = [];
|
|
492
|
-
const filtered = s ? Object.fromEntries(Object.entries(rawDotSec).filter(([key]) => key.startsWith(s)).map(([key, value]) => [key.replace(s, ""), value])) : rawDotSec;
|
|
493
|
-
for (const [key, encryptedValue] of Object.entries(filtered)) {
|
|
494
|
-
const decryptCommand = new import_client_kms2.DecryptCommand({
|
|
495
|
-
KeyId: awsKeyAlias,
|
|
496
|
-
CiphertextBlob: Buffer.from(encryptedValue, "base64"),
|
|
497
|
-
EncryptionAlgorithm: encryptionAlgorithm
|
|
498
|
-
});
|
|
499
|
-
const decryptionResult = await kmsClient.send(decryptCommand);
|
|
500
|
-
if (!decryptionResult.Plaintext) {
|
|
501
|
-
throw new Error(`Something bad happened: ${JSON.stringify({
|
|
502
|
-
key,
|
|
503
|
-
cipherText: encryptedValue,
|
|
504
|
-
decryptCommand
|
|
505
|
-
})}`);
|
|
506
|
-
}
|
|
507
|
-
if (verbose) {
|
|
508
|
-
info(`Decrypting key ${emphasis(key)} ${strong("ok")}`);
|
|
509
|
-
}
|
|
510
|
-
const decryptedValue = Buffer.from(decryptionResult.Plaintext).toString();
|
|
511
|
-
const parsedValue = JSON.parse(decryptedValue);
|
|
512
|
-
const stringOrJson = maybeJson(parsedValue.value);
|
|
513
|
-
if (isRegularParameter(stringOrJson)) {
|
|
514
|
-
if (isRegularParameterObject(stringOrJson)) {
|
|
515
|
-
dotEnvLines.push(`${key}=${JSON.stringify(stringOrJson.value)}`);
|
|
516
|
-
} else {
|
|
517
|
-
dotEnvLines.push(`${key}=${String(stringOrJson)}`);
|
|
518
|
-
}
|
|
519
|
-
} else if (isSSMParameter(stringOrJson)) {
|
|
520
|
-
dotEnvLines.push(`${key}=${JSON.stringify(stringOrJson.value)}`);
|
|
521
|
-
} else if (isSecretsManagerParameter(stringOrJson)) {
|
|
522
|
-
dotEnvLines.push(`${key}=${JSON.stringify(stringOrJson.value)}`);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
return dotEnvLines.join("\n");
|
|
526
|
-
};
|
|
527
|
-
|
|
528
|
-
// src/lib/wtf/dotenv.ts
|
|
529
|
-
var import_constant_case2 = __toModule(require("constant-case"));
|
|
530
|
-
var fromPlainTextLeafsToEnvEntries = (leafs) => {
|
|
531
|
-
return Object.entries(leafs).map(([key, plainTextValue]) => {
|
|
532
|
-
const parts = key.split("/");
|
|
533
|
-
const dotEnvKeyPath = parts.map((k) => (0, import_constant_case2.constantCase)(k)).join("_");
|
|
534
|
-
let storageValue;
|
|
535
|
-
if (isRegularParameter(plainTextValue)) {
|
|
536
|
-
if (isRegularParameterObject(plainTextValue)) {
|
|
537
|
-
storageValue = plainTextValue.value;
|
|
538
|
-
} else {
|
|
539
|
-
storageValue = plainTextValue;
|
|
540
|
-
}
|
|
541
|
-
} else if (isSSMParameter(plainTextValue)) {
|
|
542
|
-
storageValue = plainTextValue.value;
|
|
543
|
-
} else if (isSecretsManagerParameter(plainTextValue)) {
|
|
544
|
-
storageValue = plainTextValue.value;
|
|
545
|
-
} else {
|
|
546
|
-
throw new Error("Invalid parameter type");
|
|
547
|
-
}
|
|
548
|
-
if (!isString(storageValue) && !isNumber(storageValue) && !isBoolean(storageValue)) {
|
|
549
|
-
storageValue = JSON.stringify(storageValue);
|
|
550
|
-
}
|
|
551
|
-
return `${dotEnvKeyPath}=${String(storageValue)}`;
|
|
552
|
-
});
|
|
553
|
-
};
|
|
554
|
-
var toDotEnv = (options) => {
|
|
555
|
-
const { info } = getLogger();
|
|
556
|
-
const { dotSecPlainText, searchPath, verbose } = options;
|
|
557
|
-
let tree = dotSecPlainText.plaintext;
|
|
558
|
-
if (searchPath) {
|
|
559
|
-
if (verbose) {
|
|
560
|
-
info(`Searching for path: ${strong(searchPath)}`);
|
|
561
|
-
}
|
|
562
|
-
const pathParts = searchPath.split("/");
|
|
563
|
-
for (const pathPart of pathParts) {
|
|
564
|
-
tree = tree[pathPart];
|
|
565
|
-
if (tree === void 0) {
|
|
566
|
-
throw new Error(`Invalid search path: '${searchPath}', part: '${pathPart}' could not be found`);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
const flattenedTree = flattenTree(tree);
|
|
571
|
-
return fromPlainTextLeafsToEnvEntries(flattenedTree).join("\n");
|
|
572
|
-
};
|
|
573
|
-
|
|
574
|
-
// src/ds/commands/run.ts
|
|
575
|
-
var run_default = (program2) => {
|
|
576
|
-
const subProgram = program2.command("run").description("run a command with the decrypted .sec file as environment variables: dotsec run --sec .sec command npm run start").summary(`Spawns a process with (decrypted) environment variables.
|
|
577
|
-
Works with .env, .sec, secrets.{json|yml|ts} and secrets.encrypted.{json|yml|ts}.
|
|
578
|
-
|
|
579
|
-
Examples:
|
|
580
|
-
|
|
581
|
-
// run npm start with .sec file
|
|
582
|
-
dotsec run --sec .sec command npm start
|
|
583
|
-
dotsec run command npm start
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
// run npm start with .env file
|
|
587
|
-
dotsec run --env .env command npm start
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
// run npm start with secrets.json file
|
|
591
|
-
dotsec run --secrets secrets.json command npm start
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
// run npm start with secrets.encrypted.json file
|
|
595
|
-
dotsec run --encrypted-secrets secrets.encrypted.json command npm start
|
|
596
|
-
`).option("--env <env>", "Run command with .env file").option("--sec <sec>", "Run command with .sec file").option("--secrets <secrets>", "Run command with secrets.json file").option("--encryptedSecrets,--encrypted-secrets <encryptedSecrets>", "Run command with encrypted.secrets.json file").option("--awsKeyAlias, --aws-key-alias <awsKeyAlias>").option("--awsRegion, --aws-region <awsRegion>").option("--searchPath, --search-path <searchPath>").action((_options, command) => {
|
|
597
|
-
command.help();
|
|
598
|
-
});
|
|
599
|
-
subProgram.command("command <command...>").allowUnknownOption().passThroughOptions().action(async (commands, _options, command) => {
|
|
600
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
601
|
-
const config = (_a = command.parent) == null ? void 0 : _a.getOptionValue("dotsecConfig");
|
|
602
|
-
const verbose = Boolean((_b = command.parent) == null ? void 0 : _b.getOptionValue("verbose"));
|
|
603
|
-
const awsKeyAlias = ((_c = command.parent) == null ? void 0 : _c.getOptionValue("awsKeyAlias")) || config.aws.keyAlias;
|
|
604
|
-
const awsRegion = ((_d = command.parent) == null ? void 0 : _d.getOptionValue("awsRegion")) || config.aws.region;
|
|
605
|
-
const searchPath = (_e = command.parent) == null ? void 0 : _e.getOptionValue("searchPath");
|
|
606
|
-
const sec = (_f = command.parent) == null ? void 0 : _f.getOptionValue("sec");
|
|
607
|
-
const env = (_g = command.parent) == null ? void 0 : _g.getOptionValue("env");
|
|
608
|
-
console.log("awsKeyAlias", {
|
|
609
|
-
awsRegion,
|
|
610
|
-
awsKeyAlias,
|
|
611
|
-
searchPath,
|
|
612
|
-
sec,
|
|
613
|
-
env,
|
|
614
|
-
commands
|
|
615
|
-
});
|
|
616
|
-
const inputFiles = ["env", "sec", "secrets", "encryptedSecrets"].map((fileType) => {
|
|
617
|
-
var _a2;
|
|
618
|
-
const filename = (_a2 = command.parent) == null ? void 0 : _a2.getOptionValue(fileType);
|
|
619
|
-
if (filename) {
|
|
620
|
-
return [fileType, filename];
|
|
621
|
-
}
|
|
622
|
-
}).filter((v) => !!v);
|
|
623
|
-
let rawDotenv;
|
|
624
|
-
if (inputFiles.length <= 1) {
|
|
625
|
-
try {
|
|
626
|
-
const { credentialsAndOrigin, regionAndOrigin } = await handleCredentialsAndRegion({
|
|
627
|
-
argv: {},
|
|
628
|
-
env: __spreadValues({}, process.env)
|
|
629
|
-
});
|
|
630
|
-
const fileType = inputFiles.length === 0 ? "sec" : (_h = inputFiles[0]) == null ? void 0 : _h[0];
|
|
631
|
-
const filename = inputFiles.length === 0 ? ".sec" : (_i = inputFiles[0]) == null ? void 0 : _i[1];
|
|
632
|
-
console.log("filename", filename);
|
|
633
|
-
if (filename && fileType) {
|
|
634
|
-
const raw = import_node_fs.default.readFileSync(filename, "utf8");
|
|
635
|
-
if (fileType === "sec") {
|
|
636
|
-
rawDotenv = await decryptRawDotSecValues({
|
|
637
|
-
dotSecKeysValues: (0, import_dotenv.parse)(raw),
|
|
638
|
-
credentials: credentialsAndOrigin.value,
|
|
639
|
-
region: awsRegion || regionAndOrigin.value,
|
|
640
|
-
keyAlias: awsKeyAlias,
|
|
641
|
-
verbose,
|
|
642
|
-
searchPath
|
|
643
|
-
});
|
|
644
|
-
} else if (fileType === "env") {
|
|
645
|
-
rawDotenv = raw;
|
|
646
|
-
} else if (fileType === "secrets") {
|
|
647
|
-
const dotSecPlainText = JSON.parse(raw);
|
|
648
|
-
rawDotenv = toDotEnv({
|
|
649
|
-
dotSecPlainText,
|
|
650
|
-
verbose,
|
|
651
|
-
searchPath
|
|
652
|
-
});
|
|
653
|
-
} else if (fileType === "encryptedSecrets") {
|
|
654
|
-
rawDotenv = raw;
|
|
655
|
-
const dotSecEncrypted = JSON.parse(raw);
|
|
656
|
-
const dotSecPlainText = await decryptedEncrypted({
|
|
657
|
-
dotSecEncrypted,
|
|
658
|
-
credentials: credentialsAndOrigin.value,
|
|
659
|
-
region: awsRegion || regionAndOrigin.value,
|
|
660
|
-
keyAlias: awsKeyAlias,
|
|
661
|
-
verbose
|
|
662
|
-
});
|
|
663
|
-
rawDotenv = toDotEnv({
|
|
664
|
-
dotSecPlainText,
|
|
665
|
-
verbose,
|
|
666
|
-
searchPath
|
|
667
|
-
});
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
if (rawDotenv) {
|
|
671
|
-
const [userCommand, ...userCommandArgs] = commands;
|
|
672
|
-
const parsedDotEnv = (0, import_dotenv.parse)(rawDotenv);
|
|
673
|
-
(0, import_cross_spawn.default)(userCommand, [...userCommandArgs], {
|
|
674
|
-
stdio: "inherit",
|
|
675
|
-
shell: false,
|
|
676
|
-
env: __spreadProps(__spreadValues(__spreadValues({}, process.env), parsedDotEnv), {
|
|
677
|
-
__DOTSEC_VARS: JSON.stringify(Object.keys(parsedDotEnv))
|
|
678
|
-
})
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
} catch (e) {
|
|
682
|
-
if (e instanceof Error) {
|
|
683
|
-
console.error(e.message);
|
|
684
|
-
if (verbose) {
|
|
685
|
-
console.error(e.name, e.stack);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
} else {
|
|
690
|
-
throw new import_commander.default.InvalidOptionArgumentError("Can only pick one of --sec. --env, --secrets or --encryptedSecrets");
|
|
691
|
-
}
|
|
692
|
-
});
|
|
693
|
-
return subProgram;
|
|
694
|
-
};
|
|
695
|
-
|
|
696
|
-
// src/ds/commands/encrypt.ts
|
|
697
|
-
var import_node_fs2 = __toModule(require("node:fs"));
|
|
698
|
-
var import_node_path2 = __toModule(require("node:path"));
|
|
699
|
-
|
|
700
|
-
// src/utils/io.ts
|
|
701
|
-
var import_promises = __toModule(require("fs/promises"));
|
|
702
|
-
var import_prompts = __toModule(require("prompts"));
|
|
703
|
-
var import_node_path = __toModule(require("node:path"));
|
|
704
|
-
var fileExists = async (source) => {
|
|
705
|
-
try {
|
|
706
|
-
await (0, import_promises.stat)(source);
|
|
707
|
-
return true;
|
|
708
|
-
} catch {
|
|
709
|
-
return false;
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
var promptOverwriteIfFileExists = async ({
|
|
713
|
-
filePath,
|
|
714
|
-
skip
|
|
715
|
-
}) => {
|
|
716
|
-
let overwriteResponse;
|
|
717
|
-
if (await fileExists(filePath) && skip !== true) {
|
|
718
|
-
overwriteResponse = await (0, import_prompts.default)({
|
|
719
|
-
type: "confirm",
|
|
720
|
-
name: "overwrite",
|
|
721
|
-
message: () => {
|
|
722
|
-
return `Overwrite './${import_node_path.default.relative(process.cwd(), filePath)}' ?`;
|
|
723
|
-
}
|
|
724
|
-
});
|
|
725
|
-
} else {
|
|
726
|
-
overwriteResponse = void 0;
|
|
727
|
-
}
|
|
728
|
-
return overwriteResponse;
|
|
729
|
-
};
|
|
730
|
-
|
|
731
|
-
// src/lib/wtf/dotsec.ts
|
|
732
|
-
var import_constant_case3 = __toModule(require("constant-case"));
|
|
733
|
-
var fromEncryptedLeafsToEnvEntries = (leafs) => {
|
|
734
|
-
return Object.entries(leafs).map(([key, plainTextValue]) => {
|
|
735
|
-
const parts = key.split("/");
|
|
736
|
-
const dotEnvKeyPath = parts.map((k) => (0, import_constant_case3.constantCase)(k)).join("_");
|
|
737
|
-
let storageValue;
|
|
738
|
-
if (isEncryptedRegularParameter(plainTextValue)) {
|
|
739
|
-
storageValue = plainTextValue.encryptedValue;
|
|
740
|
-
} else if (isEncryptedSSMParameter(plainTextValue)) {
|
|
741
|
-
storageValue = plainTextValue.encryptedValue;
|
|
742
|
-
} else if (isSecretsManagerParameter(plainTextValue)) {
|
|
743
|
-
storageValue = plainTextValue.encryptedValue;
|
|
744
|
-
} else {
|
|
745
|
-
throw new Error("Invalid parameter type");
|
|
746
|
-
}
|
|
747
|
-
return `${dotEnvKeyPath}=${String(storageValue)}`;
|
|
748
|
-
});
|
|
749
|
-
};
|
|
750
|
-
var toDotSec = (options) => {
|
|
751
|
-
const { info } = getLogger();
|
|
752
|
-
const { dotSecEncrypted, searchPath, verbose } = options;
|
|
753
|
-
let tree = dotSecEncrypted.encrypted;
|
|
754
|
-
if (searchPath) {
|
|
755
|
-
if (verbose) {
|
|
756
|
-
info(`Searching for path: ${strong(searchPath)}`);
|
|
757
|
-
}
|
|
758
|
-
const pathParts = searchPath.split("/");
|
|
759
|
-
for (const pathPart of pathParts) {
|
|
760
|
-
tree = tree[pathPart];
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
const flattenedTree = flattenTree(tree);
|
|
764
|
-
return fromEncryptedLeafsToEnvEntries(flattenedTree).join("\n");
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
// src/ds/commands/encrypt.ts
|
|
768
|
-
var writeOut = async (options) => {
|
|
769
|
-
const { info } = getLogger();
|
|
770
|
-
const {
|
|
771
|
-
targetFile: encryptedSecretsPath,
|
|
772
|
-
converted,
|
|
773
|
-
skipPromptForOverride
|
|
774
|
-
} = options;
|
|
775
|
-
info(`target: ${strong(encryptedSecretsPath)}
|
|
776
|
-
`);
|
|
777
|
-
info(prettyCode(converted));
|
|
778
|
-
info("\n");
|
|
779
|
-
const overwriteResponse = await promptOverwriteIfFileExists({
|
|
780
|
-
filePath: encryptedSecretsPath,
|
|
781
|
-
skip: skipPromptForOverride
|
|
782
|
-
});
|
|
783
|
-
if (overwriteResponse === void 0 || overwriteResponse.overwrite === true) {
|
|
784
|
-
import_node_fs2.default.writeFileSync(encryptedSecretsPath, converted);
|
|
785
|
-
info(`Wrote encrypted secrets to ${strong(`./${import_node_path2.default.relative(process.cwd(), encryptedSecretsPath)}`)}`);
|
|
786
|
-
}
|
|
787
|
-
};
|
|
788
|
-
var encrypt = async (options) => {
|
|
789
|
-
console.log("options", options);
|
|
790
|
-
const { credentialsAndOrigin, regionAndOrigin } = await handleCredentialsAndRegion({
|
|
791
|
-
argv: {},
|
|
792
|
-
env: __spreadValues({}, process.env)
|
|
793
|
-
});
|
|
794
|
-
const {
|
|
795
|
-
secrets,
|
|
796
|
-
awsKeyAlias,
|
|
797
|
-
awsRegion,
|
|
798
|
-
verbose,
|
|
799
|
-
encryptedSecrets,
|
|
800
|
-
sec,
|
|
801
|
-
skipPromptForOverride
|
|
802
|
-
} = options;
|
|
803
|
-
const dotSecPlainText = JSON.parse(import_node_fs2.default.readFileSync(secrets, "utf8"));
|
|
804
|
-
const dotSecEncrypted = await encryptPlainText({
|
|
805
|
-
dotSecPlainText,
|
|
806
|
-
credentials: credentialsAndOrigin.value,
|
|
807
|
-
region: awsRegion || regionAndOrigin.value,
|
|
808
|
-
keyAlias: awsKeyAlias,
|
|
809
|
-
verbose
|
|
810
|
-
});
|
|
811
|
-
if (encryptedSecrets) {
|
|
812
|
-
const targetFile = import_node_path2.default.resolve(process.cwd(), encryptedSecrets);
|
|
813
|
-
const converted = JSON.stringify(dotSecEncrypted, null, 2);
|
|
814
|
-
await writeOut({
|
|
815
|
-
targetFile,
|
|
816
|
-
converted,
|
|
817
|
-
skipPromptForOverride
|
|
818
|
-
});
|
|
819
|
-
} else if (sec) {
|
|
820
|
-
const dotSec = toDotSec({
|
|
821
|
-
dotSecEncrypted,
|
|
822
|
-
verbose
|
|
823
|
-
});
|
|
824
|
-
const targetFile = import_node_path2.default.resolve(process.cwd(), sec);
|
|
825
|
-
await writeOut({
|
|
826
|
-
targetFile,
|
|
827
|
-
converted: dotSec,
|
|
828
|
-
skipPromptForOverride
|
|
829
|
-
});
|
|
830
|
-
} else {
|
|
831
|
-
throw new Error("Must provide either encryptedSecrets or sec");
|
|
832
|
-
}
|
|
833
|
-
};
|
|
834
|
-
var encrypt_default = (program2) => {
|
|
835
|
-
const encryptProgram = program2.enablePositionalOptions().command("encrypt").option("--secrets [secrets]", "Run command with secrets.json file", "secrets.json").option("--awsKeyAlias, --aws-key-alias [awsKeyAlias]").option("--awsRegion, --aws-region [awsRegion]").usage("encrypt [--secrets secrets.json] [to]").summary("Encrypt secrets.json file").description("Encrypts secrets.json to secrets.encrypted.json.\n1123").action(async (_options, command) => {
|
|
836
|
-
const verbose = Boolean(command.getOptionValue("verbose"));
|
|
837
|
-
const secrets = command.getOptionValue("secrets");
|
|
838
|
-
const config = command.getOptionValue("dotsecConfig");
|
|
839
|
-
const awsKeyAlias = command.getOptionValue("awsKeyAlias") || config.aws.keyAlias;
|
|
840
|
-
const awsRegion = command.getOptionValue("awsRegion") || config.aws.region;
|
|
841
|
-
await encrypt({
|
|
842
|
-
config,
|
|
843
|
-
verbose,
|
|
844
|
-
secrets,
|
|
845
|
-
awsKeyAlias,
|
|
846
|
-
awsRegion,
|
|
847
|
-
encryptedSecrets: "encrypted.secrets.json",
|
|
848
|
-
skipPromptForOverride: false
|
|
849
|
-
});
|
|
850
|
-
});
|
|
851
|
-
const toProgram = encryptProgram.command("to").usage("[--encryptedSecrets [encrypted.secrets.json]] [--sec [.sec]]").summary("specifies encryption output format").action(async (_options, command) => {
|
|
852
|
-
command.help();
|
|
853
|
-
});
|
|
854
|
-
toProgram.command("dotsec").option("--sec, [sec]", "Target dotsec file", ".sec").action(async (_options, command) => {
|
|
855
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
856
|
-
const config = (_b = (_a = command.parent) == null ? void 0 : _a.parent) == null ? void 0 : _b.getOptionValue("dotsecConfig");
|
|
857
|
-
const verbose = Boolean((_e = (_d = (_c = command.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.parent) == null ? void 0 : _e.getOptionValue("verbose"));
|
|
858
|
-
const secrets = (_g = (_f = command.parent) == null ? void 0 : _f.parent) == null ? void 0 : _g.getOptionValue("secrets");
|
|
859
|
-
const sec = command.getOptionValue("sec");
|
|
860
|
-
const awsKeyAlias = ((_j = (_i = (_h = command.parent) == null ? void 0 : _h.parent) == null ? void 0 : _i.parent) == null ? void 0 : _j.getOptionValue("awsKeyAlias")) || config.aws.keyAlias;
|
|
861
|
-
const awsRegion = ((_m = (_l = (_k = command.parent) == null ? void 0 : _k.parent) == null ? void 0 : _l.parent) == null ? void 0 : _m.getOptionValue("awsRegion")) || config.aws.region;
|
|
862
|
-
await encrypt({
|
|
863
|
-
config,
|
|
864
|
-
verbose,
|
|
865
|
-
secrets,
|
|
866
|
-
awsKeyAlias,
|
|
867
|
-
awsRegion,
|
|
868
|
-
sec,
|
|
869
|
-
skipPromptForOverride: false
|
|
870
|
-
});
|
|
871
|
-
});
|
|
872
|
-
toProgram.command("encryptedSecrets").option("--encryptedSecrets,--encrypted-secrets [encryptedSecrets]", "Target encrypted secrets file", "encrypted.secrets.json").action(async (_options, command) => {
|
|
873
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
874
|
-
const config = (_b = (_a = command.parent) == null ? void 0 : _a.parent) == null ? void 0 : _b.getOptionValue("dotsecConfig");
|
|
875
|
-
const verbose = Boolean((_e = (_d = (_c = command.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.parent) == null ? void 0 : _e.getOptionValue("verbose"));
|
|
876
|
-
const secrets = (_g = (_f = command.parent) == null ? void 0 : _f.parent) == null ? void 0 : _g.getOptionValue("secrets");
|
|
877
|
-
const encryptedSecrets = command.getOptionValue("encryptedSecrets");
|
|
878
|
-
const awsKeyAlias = ((_j = (_i = (_h = command.parent) == null ? void 0 : _h.parent) == null ? void 0 : _i.parent) == null ? void 0 : _j.getOptionValue("awsKeyAlias")) || config.aws.keyAlias;
|
|
879
|
-
const awsRegion = ((_m = (_l = (_k = command.parent) == null ? void 0 : _k.parent) == null ? void 0 : _l.parent) == null ? void 0 : _m.getOptionValue("awsRegion")) || config.aws.region;
|
|
880
|
-
await encrypt({
|
|
881
|
-
config,
|
|
882
|
-
verbose,
|
|
883
|
-
secrets,
|
|
884
|
-
awsKeyAlias,
|
|
885
|
-
awsRegion,
|
|
886
|
-
encryptedSecrets,
|
|
887
|
-
skipPromptForOverride: false
|
|
888
|
-
});
|
|
889
|
-
});
|
|
890
|
-
return encryptProgram;
|
|
891
|
-
};
|
|
892
|
-
|
|
893
|
-
// src/ds/commands/decrypt.ts
|
|
894
|
-
var import_node_fs3 = __toModule(require("node:fs"));
|
|
895
|
-
var import_node_path3 = __toModule(require("node:path"));
|
|
896
|
-
var writeOut2 = async (options) => {
|
|
897
|
-
const { info } = getLogger();
|
|
898
|
-
const {
|
|
899
|
-
targetFile: encryptedSecretsPath,
|
|
900
|
-
converted,
|
|
901
|
-
skipPromptForOverride
|
|
902
|
-
} = options;
|
|
903
|
-
info(`target: ${strong(encryptedSecretsPath)}
|
|
904
|
-
`);
|
|
905
|
-
info(prettyCode(converted));
|
|
906
|
-
info("\n");
|
|
907
|
-
const overwriteResponse = await promptOverwriteIfFileExists({
|
|
908
|
-
filePath: encryptedSecretsPath,
|
|
909
|
-
skip: skipPromptForOverride
|
|
910
|
-
});
|
|
911
|
-
if (overwriteResponse === void 0 || overwriteResponse.overwrite === true) {
|
|
912
|
-
import_node_fs3.default.writeFileSync(encryptedSecretsPath, converted);
|
|
913
|
-
info(`Wrote encrypted secrets to ${strong(`./${import_node_path3.default.relative(process.cwd(), encryptedSecretsPath)}`)}`);
|
|
914
|
-
}
|
|
915
|
-
};
|
|
916
|
-
var decrypt = async (options) => {
|
|
917
|
-
console.log("options", options);
|
|
918
|
-
const { credentialsAndOrigin, regionAndOrigin } = await handleCredentialsAndRegion({
|
|
919
|
-
argv: {},
|
|
920
|
-
env: __spreadValues({}, process.env)
|
|
921
|
-
});
|
|
922
|
-
const {
|
|
923
|
-
secrets,
|
|
924
|
-
awsKeyAlias,
|
|
925
|
-
awsRegion,
|
|
926
|
-
verbose,
|
|
927
|
-
encryptedSecrets,
|
|
928
|
-
sec,
|
|
929
|
-
skipPromptForOverride
|
|
930
|
-
} = options;
|
|
931
|
-
const dotSecPlainText = JSON.parse(import_node_fs3.default.readFileSync(secrets, "utf8"));
|
|
932
|
-
const dotSecEncrypted = await encryptPlainText({
|
|
933
|
-
dotSecPlainText,
|
|
934
|
-
credentials: credentialsAndOrigin.value,
|
|
935
|
-
region: awsRegion || regionAndOrigin.value,
|
|
936
|
-
keyAlias: awsKeyAlias,
|
|
937
|
-
verbose
|
|
938
|
-
});
|
|
939
|
-
if (encryptedSecrets) {
|
|
940
|
-
const targetFile = import_node_path3.default.resolve(process.cwd(), encryptedSecrets);
|
|
941
|
-
const converted = JSON.stringify(dotSecEncrypted, null, 2);
|
|
942
|
-
await writeOut2({
|
|
943
|
-
targetFile,
|
|
944
|
-
converted,
|
|
945
|
-
skipPromptForOverride
|
|
946
|
-
});
|
|
947
|
-
} else if (sec) {
|
|
948
|
-
const dotSec = toDotSec({
|
|
949
|
-
dotSecEncrypted,
|
|
950
|
-
verbose
|
|
951
|
-
});
|
|
952
|
-
const targetFile = import_node_path3.default.resolve(process.cwd(), sec);
|
|
953
|
-
await writeOut2({
|
|
954
|
-
targetFile,
|
|
955
|
-
converted: dotSec,
|
|
956
|
-
skipPromptForOverride
|
|
957
|
-
});
|
|
958
|
-
} else {
|
|
959
|
-
throw new Error("Must provide either encryptedSecrets or sec");
|
|
960
|
-
}
|
|
961
|
-
};
|
|
962
|
-
var decrypt_default = (program2) => {
|
|
963
|
-
const encryptProgram = program2.enablePositionalOptions().command("decrypt").option("--secrets [secrets]", "Run command with secrets.json file", "secrets.json").option("--awsKeyAlias, --aws-key-alias [awsKeyAlias]").option("--awsRegion, --aws-region [awsRegion]").usage("encrypt [--secrets secrets.json] [to]").summary("Encrypt secrets.json file").description("Encrypts secrets.json to secrets.encrypted.json.\n1123").action(async (_options, command) => {
|
|
964
|
-
const verbose = Boolean(command.getOptionValue("verbose"));
|
|
965
|
-
const secrets = command.getOptionValue("secrets");
|
|
966
|
-
const config = command.getOptionValue("dotsecConfig");
|
|
967
|
-
const awsKeyAlias = command.getOptionValue("awsKeyAlias") || config.aws.keyAlias;
|
|
968
|
-
const awsRegion = command.getOptionValue("awsRegion") || config.aws.region;
|
|
969
|
-
await decrypt({
|
|
970
|
-
config,
|
|
971
|
-
verbose,
|
|
972
|
-
secrets,
|
|
973
|
-
awsKeyAlias,
|
|
974
|
-
awsRegion,
|
|
975
|
-
encryptedSecrets: "encrypted.secrets.json",
|
|
976
|
-
skipPromptForOverride: false
|
|
977
|
-
});
|
|
978
|
-
});
|
|
979
|
-
const toProgram = encryptProgram.command("to").usage("[--encryptedSecrets [encrypted.secrets.json]] [--sec [.sec]]").summary("specifies encryption output format").action(async (_options, command) => {
|
|
980
|
-
command.help();
|
|
981
|
-
});
|
|
982
|
-
toProgram.command("dotsec").option("--sec, [sec]", "Target dotsec file", ".sec").action(async (_options, command) => {
|
|
983
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
984
|
-
const config = (_b = (_a = command.parent) == null ? void 0 : _a.parent) == null ? void 0 : _b.getOptionValue("dotsecConfig");
|
|
985
|
-
const verbose = Boolean((_e = (_d = (_c = command.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.parent) == null ? void 0 : _e.getOptionValue("verbose"));
|
|
986
|
-
const secrets = (_g = (_f = command.parent) == null ? void 0 : _f.parent) == null ? void 0 : _g.getOptionValue("secrets");
|
|
987
|
-
const sec = command.getOptionValue("sec");
|
|
988
|
-
const awsKeyAlias = ((_j = (_i = (_h = command.parent) == null ? void 0 : _h.parent) == null ? void 0 : _i.parent) == null ? void 0 : _j.getOptionValue("awsKeyAlias")) || config.aws.keyAlias;
|
|
989
|
-
const awsRegion = ((_m = (_l = (_k = command.parent) == null ? void 0 : _k.parent) == null ? void 0 : _l.parent) == null ? void 0 : _m.getOptionValue("awsRegion")) || config.aws.region;
|
|
990
|
-
await decrypt({
|
|
991
|
-
config,
|
|
992
|
-
verbose,
|
|
993
|
-
secrets,
|
|
994
|
-
awsKeyAlias,
|
|
995
|
-
awsRegion,
|
|
996
|
-
sec,
|
|
997
|
-
skipPromptForOverride: false
|
|
998
|
-
});
|
|
999
|
-
});
|
|
1000
|
-
toProgram.command("encryptedSecrets").option("--encryptedSecrets,--encrypted-secrets [encryptedSecrets]", "Target encrypted secrets file", "encrypted.secrets.json").action(async (_options, command) => {
|
|
1001
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
1002
|
-
const config = (_b = (_a = command.parent) == null ? void 0 : _a.parent) == null ? void 0 : _b.getOptionValue("dotsecConfig");
|
|
1003
|
-
const verbose = Boolean((_e = (_d = (_c = command.parent) == null ? void 0 : _c.parent) == null ? void 0 : _d.parent) == null ? void 0 : _e.getOptionValue("verbose"));
|
|
1004
|
-
const secrets = (_g = (_f = command.parent) == null ? void 0 : _f.parent) == null ? void 0 : _g.getOptionValue("secrets");
|
|
1005
|
-
const encryptedSecrets = command.getOptionValue("encryptedSecrets");
|
|
1006
|
-
const awsKeyAlias = ((_j = (_i = (_h = command.parent) == null ? void 0 : _h.parent) == null ? void 0 : _i.parent) == null ? void 0 : _j.getOptionValue("awsKeyAlias")) || config.aws.keyAlias;
|
|
1007
|
-
const awsRegion = ((_m = (_l = (_k = command.parent) == null ? void 0 : _k.parent) == null ? void 0 : _l.parent) == null ? void 0 : _m.getOptionValue("awsRegion")) || config.aws.region;
|
|
1008
|
-
await decrypt({
|
|
1009
|
-
config,
|
|
1010
|
-
verbose,
|
|
1011
|
-
secrets,
|
|
1012
|
-
awsKeyAlias,
|
|
1013
|
-
awsRegion,
|
|
1014
|
-
encryptedSecrets,
|
|
1015
|
-
skipPromptForOverride: false
|
|
1016
|
-
});
|
|
1017
|
-
});
|
|
1018
|
-
return encryptProgram;
|
|
1019
|
-
};
|
|
1020
|
-
|
|
1021
|
-
// src/lib/config/index.ts
|
|
1022
|
-
var import_node_path5 = __toModule(require("node:path"));
|
|
1023
|
-
var import_bundle_require = __toModule(require("bundle-require"));
|
|
1024
|
-
var import_joycon = __toModule(require("joycon"));
|
|
1025
|
-
|
|
1026
|
-
// src/lib/json.ts
|
|
1027
|
-
var import_fs = __toModule(require("fs"));
|
|
1028
|
-
var import_node_path4 = __toModule(require("node:path"));
|
|
1029
|
-
function jsoncParse(data) {
|
|
1030
|
-
try {
|
|
1031
|
-
return new Function("return " + data.trim())();
|
|
1032
|
-
} catch {
|
|
1033
|
-
return {};
|
|
1034
|
-
}
|
|
1035
|
-
}
|
|
1036
|
-
var loadJson = async (filepath) => {
|
|
1037
|
-
try {
|
|
1038
|
-
return jsoncParse(await import_fs.default.promises.readFile(filepath, "utf8"));
|
|
1039
|
-
} catch (error) {
|
|
1040
|
-
if (error instanceof Error) {
|
|
1041
|
-
throw new Error(`Failed to parse ${import_node_path4.default.relative(process.cwd(), filepath)}: ${error.message}`);
|
|
1042
|
-
} else {
|
|
1043
|
-
throw error;
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
};
|
|
1047
|
-
|
|
1048
|
-
// src/lib/config/constants.ts
|
|
1049
|
-
var defaultConfig = {
|
|
1050
|
-
aws: {
|
|
1051
|
-
keyAlias: "alias/top-secret"
|
|
1052
|
-
}
|
|
1053
|
-
};
|
|
1054
|
-
|
|
1055
|
-
// src/lib/config/index.ts
|
|
1056
|
-
var getConfig = async (filename) => {
|
|
1057
|
-
const cwd = process.cwd();
|
|
1058
|
-
const configJoycon = new import_joycon.default();
|
|
1059
|
-
const configPath = await configJoycon.resolve({
|
|
1060
|
-
files: filename ? [filename] : [
|
|
1061
|
-
"dotsec.config.ts",
|
|
1062
|
-
"dotsec.config.js",
|
|
1063
|
-
"dotsec.config.cjs",
|
|
1064
|
-
"dotsec.config.mjs",
|
|
1065
|
-
"dotsec.config.json",
|
|
1066
|
-
"package.json"
|
|
1067
|
-
],
|
|
1068
|
-
cwd,
|
|
1069
|
-
stopDir: import_node_path5.default.parse(cwd).root,
|
|
1070
|
-
packageKey: "dotsec"
|
|
1071
|
-
});
|
|
1072
|
-
if (filename && configPath === null) {
|
|
1073
|
-
throw new Error(`Could not find config file ${filename}`);
|
|
1074
|
-
}
|
|
1075
|
-
if (configPath) {
|
|
1076
|
-
if (configPath.endsWith(".json")) {
|
|
1077
|
-
const rawData = await loadJson(configPath);
|
|
1078
|
-
let data;
|
|
1079
|
-
if (configPath.endsWith("package.json") && rawData.dotsec !== void 0) {
|
|
1080
|
-
data = rawData.dotsec;
|
|
1081
|
-
} else {
|
|
1082
|
-
data = rawData;
|
|
1083
|
-
}
|
|
1084
|
-
return __spreadProps(__spreadValues(__spreadValues({}, defaultConfig), data), {
|
|
1085
|
-
aws: __spreadValues(__spreadValues({}, defaultConfig.aws), data.aws)
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
const config = await (0, import_bundle_require.bundleRequire)({
|
|
1089
|
-
filepath: configPath
|
|
1090
|
-
});
|
|
1091
|
-
const retrievedConfig = config.mod.dotsec || config.mod.default || config.mod;
|
|
1092
|
-
return __spreadValues(__spreadValues({}, defaultConfig), retrievedConfig);
|
|
1093
|
-
}
|
|
1094
|
-
return __spreadValues({}, defaultConfig);
|
|
1095
|
-
};
|
|
1096
|
-
|
|
1097
|
-
// src/ds/cli.ts
|
|
1098
|
-
var program = new import_commander2.Command();
|
|
1099
|
-
program.name("dotsec").description(".env, but secure").version("1.0.0").enablePositionalOptions().option("--verbose").option("--config, <dotsec config file>").action((_options, other) => {
|
|
1100
|
-
other.help();
|
|
1101
|
-
}).hook("preSubcommand", async (thisCommand, actionCommand) => {
|
|
1102
|
-
const configfile = thisCommand.getOptionValue("config");
|
|
1103
|
-
const dotsecConfig = await getConfig(configfile);
|
|
1104
|
-
actionCommand.setOptionValue("dotsecConfig", dotsecConfig);
|
|
1105
|
-
actionCommand.setOptionValue("verbose", Boolean(thisCommand.getOptionValue("verbose")));
|
|
1106
|
-
});
|
|
1107
|
-
run_default(program);
|
|
1108
|
-
encrypt_default(program);
|
|
1109
|
-
decrypt_default(program);
|
|
1110
|
-
program.parse();
|
|
1111
|
-
//# sourceMappingURL=cli.js.map
|