grada-run 0.49.8 → 0.49.10

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",
3
- "version": "0.49.8",
3
+ "version": "0.49.10",
4
4
  "description": "Provision production-ready AWS infrastructure and CI/CD pipelines in seconds.",
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
@@ -15,7 +15,7 @@ import { readInitManifest, recordAppliedTarget } from '../utils/backup.js';
15
15
  import { requireManagedTree, parseStateBucketBackend, upsertAutoTfvar } from '../utils/terraform-metadata.js';
16
16
  import { readTerraformComputeTarget, resolveRegion, readRenderedContainerEnvNames } from '../utils/resolvers.js';
17
17
  import { requireAwakeEnvironment, resolveSleepTarget, guardSleepingEnv } from '../utils/sleep-state.js';
18
- import { promptStateBucketRecovery, recreateStateBucket } from '../utils/recovery.js';
18
+ import { promptStateBucketRecovery, recreateStateBucket, offerInFlowRecovery, confirmRecoveryAction } from '../utils/recovery.js';
19
19
  import { resolveProjectName, readResourceSuffix } from '../utils/resolvers.js';
20
20
  import { ensureLambdaSeedImage } from '../utils/lambda-ecr.js';
21
21
  import { checkAwsCredentials, ensureStateBucketExists, isAuthError, printAwsAuthGuidance, resolveClient } from '../utils/aws.js';
@@ -406,6 +406,18 @@ function preflightSecretEnvCollisions({ targetDir }) {
406
406
  return { kind: 'collision', keys: collisions };
407
407
  }
408
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
+
409
421
  export async function applyStack(input = {}) {
410
422
  setActiveCommandName('apply');
411
423
  try {
@@ -809,36 +821,31 @@ async function applyStackMain(input = {}) {
809
821
  const pairs = parseCollisionImports(errString)
810
822
  .filter((pair) => !pair.address.startsWith('aws_secretsmanager_secret.'));
811
823
  if (pairs.length > 0) {
812
- const refused = await refuseIfNonInteractive(options, {
813
- message: '\n✖ apply needs approval to import collided resources into state.',
814
- hint: ' Re-run with --auto-approve to import non-interactively.\n',
815
- event: 'infrastructure_applied',
816
- telemetry: { projectName: actualProjectName },
817
- errorCode: 'COLLISION_IMPORT_NON_INTERACTIVE',
818
- });
819
- if (refused) return refused;
820
- let proceed = shouldAutoApprove(options);
821
- if (!proceed) {
822
- const confirmed = await confirm({
823
- message: `Import ${pairs.length} collided resource${pairs.length === 1 ? '' : 's'} into state and retry?`,
824
- initialValue: true,
825
- });
826
- proceed = !isCancel(confirmed) && confirmed === true;
827
- }
828
- if (proceed) {
829
- try {
824
+ const recovery = await offerInFlowRecovery({
825
+ options,
826
+ refusal: {
827
+ message: '\n✖ apply needs approval to import collided resources into state.',
828
+ hint: ' Re-run with --auto-approve to import non-interactively.\n',
829
+ event: 'infrastructure_applied',
830
+ telemetry: { projectName: actualProjectName },
831
+ errorCode: 'COLLISION_IMPORT_NON_INTERACTIVE',
832
+ },
833
+ approve: () => confirmRecoveryAction(
834
+ `Import ${pairs.length} collided resource${pairs.length === 1 ? '' : 's'} into state and retry?`
835
+ ),
836
+ act: async () => {
830
837
  for (const pair of pairs) {
831
838
  await runTerraform(['import', pair.address, pair.id], tfDir, null, `Importing ${pair.address}`);
832
839
  }
833
- } catch (error) {
834
- collisionImportError = error;
835
- }
836
- if (collisionImportError === null) {
837
- trackEvent('collision_resources_imported', { projectName: actualProjectName, count: pairs.length });
838
- log.message(color.cyan('Imported collided resources — retrying apply...'));
839
- return applyStack({ ...options, _skipPreview: true, _collisionImported: true });
840
- }
840
+ },
841
+ });
842
+ if (recovery.status === 'refused') return recovery.refusal;
843
+ if (recovery.status === 'recovered') {
844
+ trackEvent('collision_resources_imported', { projectName: actualProjectName, count: pairs.length });
845
+ log.message(color.cyan('Imported collided resources — retrying apply...'));
846
+ return applyStack({ ...options, _skipPreview: true, _collisionImported: true });
841
847
  }
848
+ if (recovery.status === 'failed') collisionImportError = recovery.error;
842
849
  }
843
850
  }
844
851
  trackEvent('bucket_recovery_probed', { projectName: actualProjectName, collisions });
@@ -860,38 +867,32 @@ async function applyStackMain(input = {}) {
860
867
  // Adopt the shared provider instead of hand-editing: the
861
868
  // GitHub provider URL is account-singleton, so the existing
862
869
  // provider IS the match — flip the var and retry.
863
- const refused = await refuseIfNonInteractive(options, {
864
- message: '\n✖ apply needs approval to adopt the existing GitHub OIDC provider.',
865
- hint: ' Re-run with --auto-approve to adopt non-interactively.\n',
866
- event: 'infrastructure_applied',
867
- telemetry: { projectName: actualProjectName },
868
- errorCode: 'OIDC_ADOPT_NON_INTERACTIVE',
869
- });
870
- if (refused) return refused;
871
- let adopt = shouldAutoApprove(options);
872
- if (!adopt) {
873
- const confirmed = await confirm({
874
- message: 'Adopt the existing GitHub OIDC provider (writes create_oidc_provider = false) and retry?',
875
- initialValue: true,
876
- });
877
- adopt = !isCancel(confirmed) && confirmed === true;
878
- }
879
- if (adopt) {
880
- try {
870
+ const recovery = await offerInFlowRecovery({
871
+ options,
872
+ refusal: {
873
+ message: '\n✖ apply needs approval to adopt the existing GitHub OIDC provider.',
874
+ hint: ' Re-run with --auto-approve to adopt non-interactively.\n',
875
+ event: 'infrastructure_applied',
876
+ telemetry: { projectName: actualProjectName },
877
+ errorCode: 'OIDC_ADOPT_NON_INTERACTIVE',
878
+ },
879
+ approve: () => confirmRecoveryAction(
880
+ 'Adopt the existing GitHub OIDC provider (writes create_oidc_provider = false) and retry?'
881
+ ),
882
+ act: async () => {
881
883
  upsertAutoTfvar(tfDir, 'create_oidc_provider', false);
882
- } catch (error) {
883
- // A failed write must not crash inside the error
884
- // handler: fall through to the manual guidance with
885
- // the write error appended.
886
- adoptUpsertError = error;
887
- }
888
- if (adoptUpsertError === null) {
889
- trackEvent('oidc_provider_adopted', { projectName: actualProjectName });
890
- log.message(color.cyan('Adopted the existing OIDC provider — retrying apply...'));
891
- return applyStack({ ...options, _skipPreview: true, _oidcAdopted: true });
892
- }
884
+ },
885
+ });
886
+ if (recovery.status === 'refused') return recovery.refusal;
887
+ if (recovery.status === 'recovered') {
888
+ trackEvent('oidc_provider_adopted', { projectName: actualProjectName });
889
+ log.message(color.cyan('Adopted the existing OIDC provider — retrying apply...'));
890
+ return applyStack({ ...options, _skipPreview: true, _oidcAdopted: true });
893
891
  }
894
- // Declined (or unwritable): fall through to the manual guidance below.
892
+ // A failed write must not crash inside the error handler:
893
+ // fall through to the manual guidance with the write error
894
+ // appended. Declined runs fall through the same way.
895
+ if (recovery.status === 'failed') adoptUpsertError = recovery.error;
895
896
  }
896
897
 
897
898
  if (code === 'OIDC_PROVIDER_CONFLICT') {
@@ -916,36 +917,32 @@ async function applyStackMain(input = {}) {
916
917
  // Offer the reconfigure in-flow instead of shelling the
917
918
  // user out to raw terraform; declined or failed runs
918
919
  // fall through to the runbook below.
919
- const refused = await refuseIfNonInteractive(options, {
920
- message: '\n✖ apply needs approval to reconfigure the Terraform backend.',
921
- hint: ' Re-run with --auto-approve to reconfigure non-interactively.\n',
922
- event: 'infrastructure_applied',
923
- telemetry: { projectName: actualProjectName },
924
- errorCode: 'BACKEND_RECONFIGURE_NON_INTERACTIVE',
925
- });
926
- if (refused) return refused;
927
- let proceed = shouldAutoApprove(options);
928
- if (!proceed) {
929
- const confirmed = await confirm({
930
- message: 'Reconfigure the Terraform backend (init -reconfigure) and retry?',
931
- initialValue: true,
932
- });
933
- proceed = !isCancel(confirmed) && confirmed === true;
934
- }
935
- if (proceed) {
936
- try {
920
+ const recovery = await offerInFlowRecovery({
921
+ options,
922
+ refusal: {
923
+ message: '\n✖ apply needs approval to reconfigure the Terraform backend.',
924
+ hint: ' Re-run with --auto-approve to reconfigure non-interactively.\n',
925
+ event: 'infrastructure_applied',
926
+ telemetry: { projectName: actualProjectName },
927
+ errorCode: 'BACKEND_RECONFIGURE_NON_INTERACTIVE',
928
+ },
929
+ approve: () => confirmRecoveryAction(
930
+ 'Reconfigure the Terraform backend (init -reconfigure) and retry?'
931
+ ),
932
+ act: async () => {
937
933
  await runTerraform(['init', '-reconfigure'], tfDir, null, 'Reconfiguring backend');
938
- } catch (error) {
939
- reconfigureError = error;
940
- }
941
- if (reconfigureError === null) {
942
- trackEvent('backend_reconfigured', { projectName: actualProjectName });
943
- log.message(color.cyan('Reconfigured the backend — retrying apply...'));
944
- return applyStack({ ...options, _skipPreview: true, _backendReconfigured: true });
945
- }
934
+ },
935
+ });
936
+ if (recovery.status === 'refused') return recovery.refusal;
937
+ if (recovery.status === 'recovered') {
938
+ trackEvent('backend_reconfigured', { projectName: actualProjectName });
939
+ log.message(color.cyan('Reconfigured the backend — retrying apply...'));
940
+ return applyStack({ ...options, _skipPreview: true, _backendReconfigured: true });
946
941
  }
942
+ if (recovery.status === 'failed') reconfigureError = recovery.error;
947
943
  }
948
944
  let secretRecoveryError = null;
945
+ let secretImported = false;
949
946
  if (code === 'SECRET_PENDING_DELETION' && secretName && options._secretRecovered !== true) {
950
947
  // Offer the recovery in-flow instead of shelling the user
951
948
  // out to pasted AWS CLI + raw terraform; declined, failed,
@@ -953,28 +950,30 @@ async function applyStackMain(input = {}) {
953
950
  // runbook below. Restore is non-destructive (auto under
954
951
  // --auto-approve); force-delete always needs an explicit
955
952
  // 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 {
953
+ let versionAdopted = null;
954
+ const recovery = await offerInFlowRecovery({
955
+ options,
956
+ refusal: {
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
+ approve: async () => {
964
+ const choice = await select({
965
+ message: `Secret "${secretName}" is scheduled for deletion — recover it and retry?`,
966
+ options: [
967
+ { value: 'restore', label: 'Restore the vault, import, and retry' },
968
+ { value: 'force-delete', label: 'Force-delete without recovery and retry' },
969
+ { value: 'cancel', label: 'Cancel (show manual steps)' },
970
+ ],
971
+ });
972
+ if (!isCancel(choice) && (choice === 'restore' || choice === 'force-delete')) return choice;
973
+ return null;
974
+ },
975
+ autoMethod: 'restore',
976
+ act: async (method) => {
978
977
  const targetRegion = detectedConfig.region || process.env.AWS_REGION || 'us-east-2';
979
978
  const client = resolveClient(options.secretsClient, SecretsManagerClient, { region: targetRegion });
980
979
  if (method === 'restore') {
@@ -986,23 +985,43 @@ async function applyStackMain(input = {}) {
986
985
  // Argv-spawned (no shell), so the count index
987
986
  // needs no quoting — unlike the printed runbook.
988
987
  await runTerraform(['import', 'aws_secretsmanager_secret.app_secrets[0]', described.ARN], tfDir, null, `Importing ${secretName}`);
988
+ secretImported = true;
989
+ // Adopt the live current version too: without
990
+ // it the retry recreates the placeholder
991
+ // version over the restored real values
992
+ // (ignore_changes never stops creation).
993
+ const liveVersion = currentLiveVersionId(described);
994
+ versionAdopted = liveVersion !== null;
995
+ if (liveVersion) {
996
+ await runTerraform(['import', 'aws_secretsmanager_secret_version.app_secrets_initial[0]', `${described.ARN}|${liveVersion}`], tfDir, null, `Importing ${secretName} live values`);
997
+ }
989
998
  } else {
990
999
  await client.send(new DeleteSecretCommand({ SecretId: secretName, ForceDeleteWithoutRecovery: true }));
991
1000
  }
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...'));
1001
+ },
1002
+ });
1003
+ if (recovery.status === 'refused') return recovery.refusal;
1004
+ if (recovery.status === 'recovered') {
1005
+ const method = recovery.method;
1006
+ trackEvent('secret_deletion_recovered', {
1007
+ projectName: actualProjectName,
1008
+ method,
1009
+ ...(method === 'restore' ? { adopted: versionAdopted === true } : {}),
1010
+ });
1011
+ if (method === 'restore') {
1012
+ if (versionAdopted) {
1013
+ log.message(color.cyan('Restored the secret vault and adopted its live values — retrying apply...'));
999
1014
  } 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.'));
1015
+ log.message(color.cyan('Restored the secret vault and imported it — retrying apply...'));
1016
+ log.message(color.yellow(' Live values could not be adopted: run secrets audit after apply and re-push if they look wrong.'));
1002
1017
  }
1003
- return applyStack({ ...options, _skipPreview: true, _secretRecovered: true });
1018
+ } else {
1019
+ log.message(color.cyan('Force-deleted the secret vault — retrying apply...'));
1020
+ log.message(color.dim(' Re-push your secrets after apply (secrets push): the vault is recreated empty.'));
1004
1021
  }
1022
+ return applyStack({ ...options, _skipPreview: true, _secretRecovered: true });
1005
1023
  }
1024
+ if (recovery.status === 'failed') secretRecoveryError = recovery.error;
1006
1025
  }
1007
1026
  const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId, secretName });
1008
1027
  log.error(color.red(`✖ ${runbook.message}`));
@@ -1013,6 +1032,9 @@ async function applyStackMain(input = {}) {
1013
1032
  if (secretRecoveryError) {
1014
1033
  log.message(color.dim(` (Automatic recovery failed: ${secretRecoveryError.message})`));
1015
1034
  }
1035
+ if (secretRecoveryError && secretImported) {
1036
+ log.message(color.yellow(' The vault is already imported — run the version-adopt import from the steps above before re-applying, or the live values will be overwritten with placeholders.'));
1037
+ }
1016
1038
  log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
1017
1039
  } else {
1018
1040
  // Generic fallback for unknown errors