@grada-run/grada 0.40.10 → 0.40.12
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 +1 -1
- package/src/commands/secrets.js +66 -9
package/package.json
CHANGED
package/src/commands/secrets.js
CHANGED
|
@@ -92,12 +92,34 @@ export async function resolveSecretsRegion(options = {}) {
|
|
|
92
92
|
return 'us-east-2';
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
// Quote one value so `dotenv.parse` reads it back byte-identical. Plain
|
|
96
|
+
// values keep the legacy double-quote style; values holding a double
|
|
97
|
+
// quote switch to a literal style (single or backtick) because dotenv
|
|
98
|
+
// keeps `\"` verbatim instead of unescaping it. Returns null when no
|
|
99
|
+
// style round-trips — the caller must refuse to write rather than
|
|
100
|
+
// corrupt the file.
|
|
101
|
+
export function quoteEnvValue(value) {
|
|
102
|
+
const text = String(value ?? '');
|
|
103
|
+
const hasSingle = text.includes("'");
|
|
104
|
+
const hasDouble = text.includes('"');
|
|
105
|
+
const hasBacktick = text.includes('`');
|
|
106
|
+
if (hasDouble) {
|
|
107
|
+
if (hasSingle && hasBacktick) return null;
|
|
108
|
+
if (hasSingle) return `\`${text}\``;
|
|
109
|
+
return `'${text}'`;
|
|
110
|
+
}
|
|
111
|
+
if (hasSingle) {
|
|
112
|
+
// Literal backslash-n/r sequences are indistinguishable from escaped
|
|
113
|
+
// newlines inside double quotes, so they force the literal styles.
|
|
114
|
+
if (/\\[nr]/.test(text)) return hasBacktick ? null : `\`${text}\``;
|
|
115
|
+
return `"${text.replace(/\n/g, '\\n').replace(/\r/g, '\\r')}"`;
|
|
116
|
+
}
|
|
117
|
+
if (text.includes('\\')) return `'${text}'`;
|
|
118
|
+
return `"${text.replace(/\n/g, '\\n').replace(/\r/g, '\\r')}"`;
|
|
97
119
|
}
|
|
98
120
|
|
|
99
121
|
function serializeEnv(orderedKeys, values) {
|
|
100
|
-
return orderedKeys.map((key) => `${key}
|
|
122
|
+
return orderedKeys.map((key) => `${key}=${quoteEnvValue(values[key])}`).join('\n') + '\n';
|
|
101
123
|
}
|
|
102
124
|
|
|
103
125
|
function normalizeSecretsArgs(projectName, options) {
|
|
@@ -117,10 +139,12 @@ function resolvePullAuditArgs(projectName, options) {
|
|
|
117
139
|
}
|
|
118
140
|
|
|
119
141
|
export function diffSecretsEnvs(localSecrets = {}, remoteSecrets = {}) {
|
|
120
|
-
|
|
121
|
-
|
|
142
|
+
// Own-property checks: `in` matches inherited names (toString,
|
|
143
|
+
// __proto__), which would misclassify real vault keys.
|
|
144
|
+
const missingLocally = Object.keys(remoteSecrets).filter((k) => !Object.hasOwn(localSecrets, k));
|
|
145
|
+
const untrackedLocally = Object.keys(localSecrets).filter((k) => !Object.hasOwn(remoteSecrets, k));
|
|
122
146
|
const mismatched = Object.keys(remoteSecrets).filter(
|
|
123
|
-
(k) => k
|
|
147
|
+
(k) => Object.hasOwn(localSecrets, k) && String(localSecrets[k]) !== String(remoteSecrets[k])
|
|
124
148
|
);
|
|
125
149
|
return { missingLocally, mismatched, untrackedLocally };
|
|
126
150
|
}
|
|
@@ -298,6 +322,19 @@ async function pushSecretsMain(envFilePath, projectName, options = {}) {
|
|
|
298
322
|
});
|
|
299
323
|
return { keysChanged: false, restarted: false };
|
|
300
324
|
}
|
|
325
|
+
// Automated runs cannot answer prompts: skip the restart (fresh
|
|
326
|
+
// deploys pick up the values) instead of hanging on confirm.
|
|
327
|
+
if (opts.isHeadless || process.env.CI) {
|
|
328
|
+
console.log(color.dim('Headless environment detected: skipping the ECS restart prompt — new values apply on the next deployment or restart.'));
|
|
329
|
+
console.log(color.blue(`\n📘 Learn how secrets reach your app: ${color.underline('https://github.com/grada-run/grada/blob/main/apps/docs/src/content/docs/guides/secrets-management.md')}`));
|
|
330
|
+
await trackSuccess('secrets_pushed', {
|
|
331
|
+
projectName: resolvedProjectName,
|
|
332
|
+
secret_count: Object.keys(parsedSecrets).length,
|
|
333
|
+
keys_changed: false,
|
|
334
|
+
ecs_restart: false
|
|
335
|
+
});
|
|
336
|
+
return { keysChanged: false, restarted: false };
|
|
337
|
+
}
|
|
301
338
|
const shouldRestart = await confirm({
|
|
302
339
|
message: 'Keys are unchanged. Trigger a rolling ECS restart to apply new values immediately?',
|
|
303
340
|
initialValue: false,
|
|
@@ -385,6 +422,7 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
|
|
|
385
422
|
|
|
386
423
|
const s = spinner();
|
|
387
424
|
s.start('Fetching secrets from AWS...');
|
|
425
|
+
let remoteFetched = false;
|
|
388
426
|
|
|
389
427
|
try {
|
|
390
428
|
const targetRegion = await resolveSecretsRegion(opts);
|
|
@@ -406,6 +444,7 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
|
|
|
406
444
|
}
|
|
407
445
|
const remoteKeys = Object.keys(remoteSecrets);
|
|
408
446
|
s.stop('Remote secrets fetched.');
|
|
447
|
+
remoteFetched = true;
|
|
409
448
|
|
|
410
449
|
const envPath = path.resolve(process.cwd(), resolvedFilePath);
|
|
411
450
|
let localSecrets = {};
|
|
@@ -436,17 +475,31 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
|
|
|
436
475
|
}
|
|
437
476
|
}
|
|
438
477
|
|
|
439
|
-
|
|
478
|
+
// Null-prototype map: a `__proto__` vault key lands as an own data
|
|
479
|
+
// property instead of mutating the merge target's prototype.
|
|
480
|
+
const merged = Object.assign(Object.create(null), localSecrets);
|
|
440
481
|
for (const [key, value] of Object.entries(remoteSecrets)) {
|
|
441
|
-
if (!(key
|
|
482
|
+
if (!Object.hasOwn(merged, key) || overwrite) {
|
|
442
483
|
merged[key] = String(value);
|
|
443
484
|
}
|
|
444
485
|
}
|
|
445
486
|
|
|
446
487
|
const orderedKeys = [
|
|
447
488
|
...Object.keys(localSecrets),
|
|
448
|
-
...remoteKeys.filter((k) => !(k
|
|
489
|
+
...remoteKeys.filter((k) => !Object.hasOwn(localSecrets, k)),
|
|
449
490
|
];
|
|
491
|
+
const unrepresentable = orderedKeys.filter((key) =>
|
|
492
|
+
key === '__proto__' || quoteEnvValue(merged[key]) === null
|
|
493
|
+
);
|
|
494
|
+
if (unrepresentable.length > 0) {
|
|
495
|
+
const abort = new Error(
|
|
496
|
+
`Refusing to write ${resolvedFilePath}: ${unrepresentable.length} entr${unrepresentable.length === 1 ? 'y' : 'ies'} ` +
|
|
497
|
+
`cannot round-trip through .env quoting (${unrepresentable.join(', ')}). ` +
|
|
498
|
+
'Rename or re-encode them in the vault and retry.'
|
|
499
|
+
);
|
|
500
|
+
abort.code = 'SECRETS_PULL_UNREPRESENTABLE_VALUE';
|
|
501
|
+
throw abort;
|
|
502
|
+
}
|
|
450
503
|
await fs.writeFile(envPath, serializeEnv(orderedKeys, merged));
|
|
451
504
|
|
|
452
505
|
outro(`Successfully synced ${remoteKeys.length} secrets to ${resolvedFilePath}`);
|
|
@@ -470,6 +523,10 @@ async function pullSecretsMain(envFilePath, projectName, options = {}) {
|
|
|
470
523
|
console.log(`Run ${color.cyan(`npx grada-run secrets push ${resolvedFilePath}`)} first to upload your environment variables.\n`);
|
|
471
524
|
} else if (handleAuthErrorBranch(error, s, opts)) {
|
|
472
525
|
return;
|
|
526
|
+
} else if (remoteFetched) {
|
|
527
|
+
// The spinner already stopped, so its stop is a no-op —
|
|
528
|
+
// route the message through the failure stamp instead.
|
|
529
|
+
return failCommand({ message: `❌ Failed to pull secrets: ${error.message}`, exitCode: 1 });
|
|
473
530
|
} else {
|
|
474
531
|
s.stop(`❌ Failed to pull secrets: ${error.message}`);
|
|
475
532
|
}
|