grada-run 0.49.6 → 0.49.8
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
CHANGED
package/src/commands/apply.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import fs from 'fs';
|
|
3
|
-
import { intro, outro, spinner, log, cancel, confirm, isCancel } from '@clack/prompts';
|
|
3
|
+
import { intro, outro, spinner, log, cancel, confirm, isCancel, select } from '@clack/prompts';
|
|
4
4
|
import color from 'picocolors';
|
|
5
|
+
import { SecretsManagerClient, RestoreSecretCommand, DescribeSecretCommand, DeleteSecretCommand } from '@aws-sdk/client-secrets-manager';
|
|
5
6
|
import { renderDryRunPreview, parseTerraformConfig, buildCostTelemetryProps } from '../utils/visualizer.js';
|
|
6
7
|
import { detectFramework } from '../utils/detector.js';
|
|
7
8
|
import { trackEvent, flushTelemetry, trackSuccess, trackFailure, setActiveCommandName, resetActiveCommandName, isCliEntry } from '../core/telemetry.js';
|
|
@@ -17,7 +18,7 @@ import { requireAwakeEnvironment, resolveSleepTarget, guardSleepingEnv } from '.
|
|
|
17
18
|
import { promptStateBucketRecovery, recreateStateBucket } from '../utils/recovery.js';
|
|
18
19
|
import { resolveProjectName, readResourceSuffix } from '../utils/resolvers.js';
|
|
19
20
|
import { ensureLambdaSeedImage } from '../utils/lambda-ecr.js';
|
|
20
|
-
import { checkAwsCredentials, ensureStateBucketExists, isAuthError, printAwsAuthGuidance } from '../utils/aws.js';
|
|
21
|
+
import { checkAwsCredentials, ensureStateBucketExists, isAuthError, printAwsAuthGuidance, resolveClient } from '../utils/aws.js';
|
|
21
22
|
import { findUnreconciledFiles, isValidAccountId, PLACEHOLDER_ACCOUNT_ID } from '../utils/reconcile.js';
|
|
22
23
|
import { runWake } from './wake.js';
|
|
23
24
|
|
|
@@ -708,6 +709,7 @@ async function applyStackMain(input = {}) {
|
|
|
708
709
|
const classified = classifyTerraformError(errString, { postRecovery: options._postRecovery === true });
|
|
709
710
|
const code = structuredCode || classified?.code || null;
|
|
710
711
|
const lockId = error.lockId || classified?.lockId || null;
|
|
712
|
+
const secretName = classified?.secretName || null;
|
|
711
713
|
|
|
712
714
|
if (code === 'STATE_BUCKET_MISSING' && options._postRecovery === true) {
|
|
713
715
|
// The bucket is still missing after a recreate: refuse instead
|
|
@@ -943,12 +945,74 @@ async function applyStackMain(input = {}) {
|
|
|
943
945
|
}
|
|
944
946
|
}
|
|
945
947
|
}
|
|
946
|
-
|
|
948
|
+
let secretRecoveryError = null;
|
|
949
|
+
if (code === 'SECRET_PENDING_DELETION' && secretName && options._secretRecovered !== true) {
|
|
950
|
+
// Offer the recovery in-flow instead of shelling the user
|
|
951
|
+
// out to pasted AWS CLI + raw terraform; declined, failed,
|
|
952
|
+
// dismissed, or unparseable runs fall through to the
|
|
953
|
+
// runbook below. Restore is non-destructive (auto under
|
|
954
|
+
// --auto-approve); force-delete always needs an explicit
|
|
955
|
+
// interactive choice, never auto.
|
|
956
|
+
const refused = await refuseIfNonInteractive(options, {
|
|
957
|
+
message: '\n✖ apply needs approval to recover the deleted secret vault.',
|
|
958
|
+
hint: ' Re-run with --auto-approve to restore non-interactively.\n',
|
|
959
|
+
event: 'infrastructure_applied',
|
|
960
|
+
telemetry: { projectName: actualProjectName },
|
|
961
|
+
errorCode: 'SECRET_RECOVERY_NON_INTERACTIVE',
|
|
962
|
+
});
|
|
963
|
+
if (refused) return refused;
|
|
964
|
+
let method = shouldAutoApprove(options) ? 'restore' : null;
|
|
965
|
+
if (!method) {
|
|
966
|
+
const choice = await select({
|
|
967
|
+
message: `Secret "${secretName}" is scheduled for deletion — recover it and retry?`,
|
|
968
|
+
options: [
|
|
969
|
+
{ value: 'restore', label: 'Restore the vault, import, and retry' },
|
|
970
|
+
{ value: 'force-delete', label: 'Force-delete without recovery and retry' },
|
|
971
|
+
{ value: 'cancel', label: 'Cancel (show manual steps)' },
|
|
972
|
+
],
|
|
973
|
+
});
|
|
974
|
+
if (!isCancel(choice) && (choice === 'restore' || choice === 'force-delete')) method = choice;
|
|
975
|
+
}
|
|
976
|
+
if (method) {
|
|
977
|
+
try {
|
|
978
|
+
const targetRegion = detectedConfig.region || process.env.AWS_REGION || 'us-east-2';
|
|
979
|
+
const client = resolveClient(options.secretsClient, SecretsManagerClient, { region: targetRegion });
|
|
980
|
+
if (method === 'restore') {
|
|
981
|
+
await client.send(new RestoreSecretCommand({ SecretId: secretName }));
|
|
982
|
+
const described = await client.send(new DescribeSecretCommand({ SecretId: secretName }));
|
|
983
|
+
if (!described || !described.ARN) {
|
|
984
|
+
throw new Error(`DescribeSecret returned no ARN for "${secretName}".`);
|
|
985
|
+
}
|
|
986
|
+
// Argv-spawned (no shell), so the count index
|
|
987
|
+
// needs no quoting — unlike the printed runbook.
|
|
988
|
+
await runTerraform(['import', 'aws_secretsmanager_secret.app_secrets[0]', described.ARN], tfDir, null, `Importing ${secretName}`);
|
|
989
|
+
} else {
|
|
990
|
+
await client.send(new DeleteSecretCommand({ SecretId: secretName, ForceDeleteWithoutRecovery: true }));
|
|
991
|
+
}
|
|
992
|
+
} catch (error) {
|
|
993
|
+
secretRecoveryError = error;
|
|
994
|
+
}
|
|
995
|
+
if (secretRecoveryError === null) {
|
|
996
|
+
trackEvent('secret_deletion_recovered', { projectName: actualProjectName, method });
|
|
997
|
+
if (method === 'restore') {
|
|
998
|
+
log.message(color.cyan('Restored the secret vault and imported it — retrying apply...'));
|
|
999
|
+
} else {
|
|
1000
|
+
log.message(color.cyan('Force-deleted the secret vault — retrying apply...'));
|
|
1001
|
+
log.message(color.dim(' Re-push your secrets after apply (secrets push): the vault is recreated empty.'));
|
|
1002
|
+
}
|
|
1003
|
+
return applyStack({ ...options, _skipPreview: true, _secretRecovered: true });
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId, secretName });
|
|
947
1008
|
log.error(color.red(`✖ ${runbook.message}`));
|
|
948
1009
|
log.message(` ${runbook.hint}\n`);
|
|
949
1010
|
if (reconfigureError) {
|
|
950
1011
|
log.message(color.dim(` (Automatic reconfigure failed: ${reconfigureError.message})`));
|
|
951
1012
|
}
|
|
1013
|
+
if (secretRecoveryError) {
|
|
1014
|
+
log.message(color.dim(` (Automatic recovery failed: ${secretRecoveryError.message})`));
|
|
1015
|
+
}
|
|
952
1016
|
log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
|
|
953
1017
|
} else {
|
|
954
1018
|
// Generic fallback for unknown errors
|
package/src/commands/destroy.js
CHANGED
|
@@ -15,7 +15,7 @@ import { findDbTarget, dbStatusOf, resolveDbIdentifier, resolveDbClusterIdentifi
|
|
|
15
15
|
import { requireManagedTree, parseStateBucketBackend } from '../utils/terraform-metadata.js';
|
|
16
16
|
import { guardSleepingEnv } from '../utils/sleep-state.js';
|
|
17
17
|
import { normalizeOptions, parseFlags, normalizeArgv } from '../utils/args.js';
|
|
18
|
-
import { resolveHeadless } from '../utils/resolvers.js';
|
|
18
|
+
import { resolveHeadless, readTerraformProjectName, readResourceSuffix } from '../utils/resolvers.js';
|
|
19
19
|
import { truncateSpinnerText } from '../utils/spinner-text.js';
|
|
20
20
|
|
|
21
21
|
export const DEFAULT_DESTROY_DB_TIMEOUT_MS = 600000;
|
|
@@ -59,13 +59,18 @@ export function parseDestroyArgs(argv = []) {
|
|
|
59
59
|
// Returns `{ retried: true }` when the destroy recovered so the caller
|
|
60
60
|
// continues to bucket cleanup, else `{ retried: false, result }`.
|
|
61
61
|
// Exported for unit tests: every branch must honor this contract.
|
|
62
|
-
async function describeDependencyBlockers(ec2, projectName) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
62
|
+
async function describeDependencyBlockers(ec2, projectName, vpcName) {
|
|
63
|
+
// Preview workspaces share the GradaProject tag, so the project
|
|
64
|
+
// filter alone matches every preview VPC too — the exact prod
|
|
65
|
+
// Name narrows to the destroy target. Without a readable tree
|
|
66
|
+
// there is no Name, and the single-VPC length check below keeps
|
|
67
|
+
// the legacy behavior.
|
|
68
|
+
const filters = [
|
|
69
|
+
{ Name: 'tag:ManagedBy', Values: ['grada'] },
|
|
70
|
+
{ Name: 'tag:GradaProject', Values: [projectName] },
|
|
71
|
+
];
|
|
72
|
+
if (vpcName) filters.push({ Name: 'tag:Name', Values: [vpcName] });
|
|
73
|
+
const { Vpcs = [] } = await ec2.send(new DescribeVpcsCommand({ Filters: filters }));
|
|
69
74
|
if (Vpcs.length !== 1 || !Vpcs[0].VpcId) return null;
|
|
70
75
|
const vpcId = Vpcs[0].VpcId;
|
|
71
76
|
const [niResp, sgResp] = await Promise.all([
|
|
@@ -181,7 +186,21 @@ export async function handleDestroyTerraformError({ options, tfDirPath, attemptD
|
|
|
181
186
|
let ec2 = null;
|
|
182
187
|
try {
|
|
183
188
|
ec2 = resolveClient(options.ec2Client, EC2Client, { region: region ?? process.env.AWS_REGION ?? 'us-east-2' });
|
|
184
|
-
|
|
189
|
+
// Rendered names beat the directory basename: the TF
|
|
190
|
+
// project + suffix pin the exact prod VPC Name, and
|
|
191
|
+
// unreadable trees fall back to basename matching.
|
|
192
|
+
let tagProject = actualProjectName;
|
|
193
|
+
let vpcName = null;
|
|
194
|
+
try {
|
|
195
|
+
const tfProject = readTerraformProjectName();
|
|
196
|
+
if (tfProject) {
|
|
197
|
+
tagProject = tfProject;
|
|
198
|
+
vpcName = `${tfProject}${readResourceSuffix()}-vpc`;
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
// fall through with basename matching
|
|
202
|
+
}
|
|
203
|
+
blockers = await describeDependencyBlockers(ec2, tagProject, vpcName);
|
|
185
204
|
} catch {
|
|
186
205
|
blockers = null;
|
|
187
206
|
}
|
|
@@ -89,7 +89,8 @@ export function classifyTerraformError(stderr, ctx = {}) {
|
|
|
89
89
|
// Disjoint from the collision family ("already scheduled for
|
|
90
90
|
// deletion" carries no AlreadyExists marker).
|
|
91
91
|
if (/scheduled for deletion/i.test(text)) {
|
|
92
|
-
|
|
92
|
+
const nameMatch = /Secrets Manager Secret \(([^)]+)\)/.exec(text);
|
|
93
|
+
return { code: 'SECRET_PENDING_DELETION', lockId: null, secretName: nameMatch ? nameMatch[1] : null };
|
|
93
94
|
}
|
|
94
95
|
if (/NoSuchBucket/i.test(text) || /does not exist/i.test(text)) {
|
|
95
96
|
return { code: 'STATE_BUCKET_MISSING', lockId: null };
|
|
@@ -160,8 +161,8 @@ const RUNBOOKS = {
|
|
|
160
161
|
},
|
|
161
162
|
SECRET_PENDING_DELETION: {
|
|
162
163
|
message: 'A Secrets Manager secret is scheduled for deletion.',
|
|
163
|
-
headless: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import
|
|
164
|
-
interactive: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import
|
|
164
|
+
headless: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import \'aws_secretsmanager_secret.app_secrets[0]\' <arn>) or force-delete without recovery (aws secretsmanager delete-secret --secret-id <name> --force-delete-without-recovery) and re-apply + push — fetch the ARN via aws secretsmanager describe-secret --secret-id <name> --query ARN --output text. Grada never force-deletes: the window protects against accidental loss.',
|
|
165
|
+
interactive: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import \'aws_secretsmanager_secret.app_secrets[0]\' <arn>) or force-delete without recovery (aws secretsmanager delete-secret --secret-id <name> --force-delete-without-recovery) and re-apply + push — fetch the ARN via aws secretsmanager describe-secret --secret-id <name> --query ARN --output text. Grada never force-deletes: the window protects against accidental loss.',
|
|
165
166
|
},
|
|
166
167
|
};
|
|
167
168
|
|
|
@@ -169,12 +170,15 @@ const RUNBOOKS = {
|
|
|
169
170
|
// codes. `headless` selects the variant; `lockId` fills the interactive
|
|
170
171
|
// lock runbook. Resource identifiers stay in the rendered hint (stdout),
|
|
171
172
|
// never in telemetry payloads.
|
|
172
|
-
export function getRunbook(code, { headless = false, lockId = null } = {}) {
|
|
173
|
+
export function getRunbook(code, { headless = false, lockId = null, secretName = null } = {}) {
|
|
173
174
|
const runbook = RUNBOOKS[code];
|
|
174
175
|
if (!runbook) return null;
|
|
175
176
|
let hint = headless ? runbook.headless : runbook.interactive;
|
|
176
177
|
if (code === 'STATE_LOCK_HELD' && lockId) {
|
|
177
178
|
hint = hint.replace(/<lock-id>/g, String(lockId));
|
|
178
179
|
}
|
|
180
|
+
if (code === 'SECRET_PENDING_DELETION' && secretName) {
|
|
181
|
+
hint = hint.replace(/<name>/g, String(secretName));
|
|
182
|
+
}
|
|
179
183
|
return { message: runbook.message, hint };
|
|
180
184
|
}
|