@grada-run/grada 0.49.7 → 0.49.9

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grada-run/grada",
3
- "version": "0.49.7",
3
+ "version": "0.49.9",
4
4
  "description": "Provision production-ready AWS infrastructure and CI/CD pipelines in seconds.",
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
@@ -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
 
@@ -405,6 +406,18 @@ function preflightSecretEnvCollisions({ targetDir }) {
405
406
  return { kind: 'collision', keys: collisions };
406
407
  }
407
408
 
409
+ // Picks the live AWSCURRENT version id out of a DescribeSecret
410
+ // response — null when none is staged current (a missing or
411
+ // stageless map means there is nothing safe to adopt).
412
+ function currentLiveVersionId(described) {
413
+ const stages = described?.VersionIdsToStages;
414
+ if (!stages || typeof stages !== 'object') return null;
415
+ for (const [versionId, versionStages] of Object.entries(stages)) {
416
+ if (Array.isArray(versionStages) && versionStages.includes('AWSCURRENT')) return versionId;
417
+ }
418
+ return null;
419
+ }
420
+
408
421
  export async function applyStack(input = {}) {
409
422
  setActiveCommandName('apply');
410
423
  try {
@@ -944,12 +957,93 @@ async function applyStackMain(input = {}) {
944
957
  }
945
958
  }
946
959
  }
960
+ let secretRecoveryError = null;
961
+ if (code === 'SECRET_PENDING_DELETION' && secretName && options._secretRecovered !== true) {
962
+ // Offer the recovery in-flow instead of shelling the user
963
+ // out to pasted AWS CLI + raw terraform; declined, failed,
964
+ // dismissed, or unparseable runs fall through to the
965
+ // runbook below. Restore is non-destructive (auto under
966
+ // --auto-approve); force-delete always needs an explicit
967
+ // interactive choice, never auto.
968
+ const refused = await refuseIfNonInteractive(options, {
969
+ message: '\n✖ apply needs approval to recover the deleted secret vault.',
970
+ hint: ' Re-run with --auto-approve to restore non-interactively.\n',
971
+ event: 'infrastructure_applied',
972
+ telemetry: { projectName: actualProjectName },
973
+ errorCode: 'SECRET_RECOVERY_NON_INTERACTIVE',
974
+ });
975
+ if (refused) return refused;
976
+ let method = shouldAutoApprove(options) ? 'restore' : null;
977
+ if (!method) {
978
+ const choice = await select({
979
+ message: `Secret "${secretName}" is scheduled for deletion — recover it and retry?`,
980
+ options: [
981
+ { value: 'restore', label: 'Restore the vault, import, and retry' },
982
+ { value: 'force-delete', label: 'Force-delete without recovery and retry' },
983
+ { value: 'cancel', label: 'Cancel (show manual steps)' },
984
+ ],
985
+ });
986
+ if (!isCancel(choice) && (choice === 'restore' || choice === 'force-delete')) method = choice;
987
+ }
988
+ if (method) {
989
+ let versionAdopted = null;
990
+ try {
991
+ const targetRegion = detectedConfig.region || process.env.AWS_REGION || 'us-east-2';
992
+ const client = resolveClient(options.secretsClient, SecretsManagerClient, { region: targetRegion });
993
+ if (method === 'restore') {
994
+ await client.send(new RestoreSecretCommand({ SecretId: secretName }));
995
+ const described = await client.send(new DescribeSecretCommand({ SecretId: secretName }));
996
+ if (!described || !described.ARN) {
997
+ throw new Error(`DescribeSecret returned no ARN for "${secretName}".`);
998
+ }
999
+ // Argv-spawned (no shell), so the count index
1000
+ // needs no quoting — unlike the printed runbook.
1001
+ await runTerraform(['import', 'aws_secretsmanager_secret.app_secrets[0]', described.ARN], tfDir, null, `Importing ${secretName}`);
1002
+ // Adopt the live current version too: without
1003
+ // it the retry recreates the placeholder
1004
+ // version over the restored real values
1005
+ // (ignore_changes never stops creation).
1006
+ const liveVersion = currentLiveVersionId(described);
1007
+ versionAdopted = liveVersion !== null;
1008
+ if (liveVersion) {
1009
+ await runTerraform(['import', 'aws_secretsmanager_secret_version.app_secrets_initial[0]', `${described.ARN}|${liveVersion}`], tfDir, null, `Importing ${secretName} live values`);
1010
+ }
1011
+ } else {
1012
+ await client.send(new DeleteSecretCommand({ SecretId: secretName, ForceDeleteWithoutRecovery: true }));
1013
+ }
1014
+ } catch (error) {
1015
+ secretRecoveryError = error;
1016
+ }
1017
+ if (secretRecoveryError === null) {
1018
+ trackEvent('secret_deletion_recovered', {
1019
+ projectName: actualProjectName,
1020
+ method,
1021
+ ...(method === 'restore' ? { adopted: versionAdopted === true } : {}),
1022
+ });
1023
+ if (method === 'restore') {
1024
+ if (versionAdopted) {
1025
+ log.message(color.cyan('Restored the secret vault and adopted its live values — retrying apply...'));
1026
+ } else {
1027
+ log.message(color.cyan('Restored the secret vault and imported it — retrying apply...'));
1028
+ log.message(color.yellow(' Live values could not be adopted: run secrets audit after apply and re-push if they look wrong.'));
1029
+ }
1030
+ } else {
1031
+ log.message(color.cyan('Force-deleted the secret vault — retrying apply...'));
1032
+ log.message(color.dim(' Re-push your secrets after apply (secrets push): the vault is recreated empty.'));
1033
+ }
1034
+ return applyStack({ ...options, _skipPreview: true, _secretRecovered: true });
1035
+ }
1036
+ }
1037
+ }
947
1038
  const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId, secretName });
948
1039
  log.error(color.red(`✖ ${runbook.message}`));
949
1040
  log.message(` ${runbook.hint}\n`);
950
1041
  if (reconfigureError) {
951
1042
  log.message(color.dim(` (Automatic reconfigure failed: ${reconfigureError.message})`));
952
1043
  }
1044
+ if (secretRecoveryError) {
1045
+ log.message(color.dim(` (Automatic recovery failed: ${secretRecoveryError.message})`));
1046
+ }
953
1047
  log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
954
1048
  } else {
955
1049
  // Generic fallback for unknown errors
@@ -161,8 +161,8 @@ const RUNBOOKS = {
161
161
  },
162
162
  SECRET_PENDING_DELETION: {
163
163
  message: 'A Secrets Manager secret is scheduled for deletion.',
164
- headless: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import aws_secretsmanager_secret.app_secrets <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 <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.',
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 only force-deletes when you explicitly choose it: 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 only force-deletes when you explicitly choose it: the window protects against accidental loss.',
166
166
  },
167
167
  };
168
168