-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathuseCdkPreflight.ts
More file actions
970 lines (887 loc) · 37.2 KB
/
useCdkPreflight.ts
File metadata and controls
970 lines (887 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
import { ConfigIO, SecureCredentials } from '../../../lib';
import { AwsCredentialsError } from '../../../lib/errors/types';
import type { AwsDeploymentTarget, DeployedState } from '../../../schema';
import { applyTargetRegionToEnv } from '../../aws';
import { validateAwsCredentials } from '../../aws/account';
import { type CdkToolkitWrapper, type SwitchableIoHost, createSwitchableIoHost } from '../../cdk/toolkit-lib';
import { getErrorMessage, isExpiredTokenError, isNoCredentialsError } from '../../errors';
import type { ExecLogger } from '../../logging';
import {
type MissingCredential,
type PreflightContext,
bootstrapEnvironment,
buildCdkProject,
checkBootstrapNeeded,
checkDependencyVersions,
checkStackDeployability,
formatError,
getAllCredentials,
hasIdentityApiProviders,
hasIdentityOAuthProviders,
setupApiKeyProviders,
setupOAuth2Providers,
synthesizeCdk,
validateProject,
} from '../../operations/deploy';
import type { Step } from '../components';
import * as path from 'node:path';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
type PreflightPhase =
| 'idle'
| 'running'
| 'teardown-confirm'
| 'credentials-prompt'
| 'identity-setup'
| 'bootstrap-confirm'
| 'bootstrapping'
| 'complete'
| 'error';
export type { PreflightContext, MissingCredential };
interface BootstrapContext {
toolkitWrapper: CdkToolkitWrapper;
target: NonNullable<PreflightContext['awsTargets'][0]>;
}
export interface PreflightOptions {
/** Logger for capturing step execution */
logger: ExecLogger;
/** Whether running in interactive TUI mode - affects error message verbosity */
isInteractive?: boolean;
/** Skip identity provider check (for plan command which only synthesizes) */
skipIdentityCheck?: boolean;
/**
* Subset of targets the user picked in the TUI target selector. When provided,
* the preflight context's awsTargets is filtered to only these targets, so
* downstream deploy steps and the displayed Target header reflect what was
* actually selected. When omitted, all configured targets are used (CLI mode
* already filters via --target before reaching here).
*/
selectedTargets?: AwsDeploymentTarget[];
}
export interface PreflightResult {
phase: PreflightPhase;
steps: Step[];
context: PreflightContext | null;
cdkToolkitWrapper: CdkToolkitWrapper | null;
stackNames: string[];
/** Switchable ioHost - call setVerbose(true) before deploy for CLI output */
switchableIoHost: SwitchableIoHost;
/** True if preflight failed due to expired/invalid AWS credentials */
hasTokenExpiredError: boolean;
/** True if preflight failed due to missing AWS credentials (not configured) */
hasCredentialsError: boolean;
/** Missing credentials that need to be provided */
missingCredentials: MissingCredential[];
/** KMS key ARN used for identity token vault encryption */
identityKmsKeyArn?: string;
/** Credential ARNs (API key + OAuth) from pre-deploy setup */
allCredentials: Record<string, { credentialProviderArn: string; clientSecretArn?: string; callbackUrl?: string }>;
startPreflight: () => Promise<void>;
confirmTeardown: () => void;
cancelTeardown: () => void;
confirmBootstrap: () => void;
skipBootstrap: () => void;
/** Clear the token expired error state */
clearTokenExpiredError: () => void;
/** Clear the credentials error state */
clearCredentialsError: () => void;
/** Called when user chooses to use credentials from .env.local */
useEnvLocalCredentials: () => void;
/** Called when user enters credentials manually */
useManualCredentials: (credentials: Record<string, string>) => void;
/** Called when user chooses to skip credential setup */
skipCredentials: () => void;
}
// Step indices for base preflight steps (always present)
const STEP_VALIDATE = 0;
const STEP_DEPS = 1;
const STEP_BUILD = 2;
// Note: Identity steps are inserted at index 3+ when needed, shifting synth and stack status down.
// Use findStepIndex() to locate synth and stack status dynamically.
const BASE_PREFLIGHT_STEPS: Step[] = [
{ label: 'Validate project', status: 'pending' },
{ label: 'Check dependencies', status: 'pending' },
{ label: 'Build CDK project', status: 'pending' },
{ label: 'Synthesize CloudFormation', status: 'pending' },
{ label: 'Check stack status', status: 'pending' },
];
const LABEL_SYNTH = 'Synthesize CloudFormation';
const LABEL_STACK_STATUS = 'Check stack status';
const LABEL_API_KEY = 'Set up API key providers';
const LABEL_OAUTH = 'Set up OAuth providers';
const IDENTITY_STEP: Step = { label: LABEL_API_KEY, status: 'pending' };
const BOOTSTRAP_STEP: Step = { label: 'Bootstrap AWS environment', status: 'pending' };
export function useCdkPreflight(options: PreflightOptions): PreflightResult {
const { logger, isInteractive = false, skipIdentityCheck = false, selectedTargets } = options;
// Create switchable ioHost - starts silent, can be flipped to verbose for deploy
const switchableIoHost = useMemo(() => createSwitchableIoHost(), []);
const [phase, setPhase] = useState<PreflightPhase>('idle');
const [steps, setSteps] = useState<Step[]>(BASE_PREFLIGHT_STEPS);
const [context, setContext] = useState<PreflightContext | null>(null);
const [cdkToolkitWrapper, setCdkToolkitWrapper] = useState<CdkToolkitWrapper | null>(null);
const [stackNames, setStackNames] = useState<string[]>([]);
const [bootstrapContext, setBootstrapContext] = useState<BootstrapContext | null>(null);
const [hasTokenExpiredError, setHasTokenExpiredError] = useState(false);
const [hasCredentialsError, setHasCredentialsError] = useState(false);
const [missingCredentials, setMissingCredentials] = useState<MissingCredential[]>([]);
const [runtimeCredentials, setRuntimeCredentials] = useState<SecureCredentials | null>(null);
const [skipIdentitySetup, setSkipIdentitySetup] = useState(false);
const [identityKmsKeyArn, setIdentityKmsKeyArn] = useState<string | undefined>(undefined);
const [allCredentials, setAllCredentials] = useState<
Record<string, { credentialProviderArn: string; clientSecretArn?: string; callbackUrl?: string }>
>({});
const [teardownConfirmed, setTeardownConfirmed] = useState(false);
// Guard against concurrent runs (React StrictMode, re-renders, etc.)
const isRunningRef = useRef(false);
// Keep a ref to the wrapper so we can dispose it when starting a new run
const wrapperRef = useRef<CdkToolkitWrapper | null>(null);
// Restore function for AWS_REGION / AWS_DEFAULT_REGION overrides, applied
// after target resolution so downstream SDK / CDK toolkit-lib clients use the
// aws-targets.json region rather than whatever the SDK default chain resolves.
// See https://github.com/aws/agentcore-cli/issues/924.
const restoreRegionEnvRef = useRef<(() => void) | null>(null);
const updateStep = (index: number, update: Partial<Step>) => {
setSteps(prev => prev.map((s, i) => (i === index ? { ...s, ...update } : s)));
};
const updateStepByLabel = (label: string, update: Partial<Step>) => {
setSteps(prev => prev.map(s => (s.label === label ? { ...s, ...update } : s)));
};
const resetSteps = () => {
setSteps(BASE_PREFLIGHT_STEPS.map(s => ({ ...s, status: 'pending' as const })));
};
// Dispose wrapper and clear ref
const disposeWrapper = useCallback(async () => {
if (wrapperRef.current) {
await wrapperRef.current.dispose();
wrapperRef.current = null;
}
}, []);
// Restore AWS_REGION / AWS_DEFAULT_REGION (no-op when nothing was applied)
const restoreRegionEnv = useCallback(() => {
restoreRegionEnvRef.current?.();
restoreRegionEnvRef.current = null;
}, []);
const startPreflight = useCallback(async () => {
if (isRunningRef.current) return;
// Dispose any existing wrapper before starting a new run
await disposeWrapper();
// Restore any previously-applied region env override before re-running
restoreRegionEnv();
resetSteps();
setCdkToolkitWrapper(null);
setStackNames([]);
setBootstrapContext(null);
setHasTokenExpiredError(false); // Reset token expired state when retrying
setHasCredentialsError(false); // Reset credentials error state when retrying
setPhase('running');
}, [disposeWrapper, restoreRegionEnv]);
const clearTokenExpiredError = useCallback(() => {
setHasTokenExpiredError(false);
}, []);
const clearCredentialsError = useCallback(() => {
setHasCredentialsError(false);
}, []);
// Cleanup on unmount or interruption
useEffect(() => {
const handleInterrupt = () => {
void disposeWrapper();
restoreRegionEnv();
};
process.on('SIGINT', handleInterrupt);
process.on('SIGTERM', handleInterrupt);
return () => {
process.off('SIGINT', handleInterrupt);
process.off('SIGTERM', handleInterrupt);
// Dispose on unmount (user navigated away)
void disposeWrapper();
restoreRegionEnv();
};
}, [disposeWrapper, restoreRegionEnv]);
// Restore region env override when any preflight stage lands in 'error'.
// Individual error branches inside the stage effects only call setPhase('error')
// without cleanup, so this hook is the single place that guarantees restore
// happens on every error path without threading the call into every branch.
useEffect(() => {
if (phase === 'error') {
restoreRegionEnv();
}
}, [phase, restoreRegionEnv]);
const confirmTeardown = useCallback(() => {
// Mark teardown as confirmed and restart the preflight flow
setTeardownConfirmed(true);
setPhase('running');
isRunningRef.current = false; // Allow the run to restart
}, []);
const cancelTeardown = useCallback(() => {
setPhase('error');
isRunningRef.current = false;
restoreRegionEnv();
}, [restoreRegionEnv]);
const confirmBootstrap = useCallback(() => {
setPhase('bootstrapping');
}, []);
const skipBootstrap = useCallback(() => {
setPhase('complete');
isRunningRef.current = false;
}, []);
// Credential prompt callbacks
const useEnvLocalCredentials = useCallback(() => {
// Use credentials from .env.local (no runtime override needed)
setRuntimeCredentials(null);
setPhase('identity-setup');
}, []);
const useManualCredentials = useCallback((credentials: Record<string, string>) => {
// Use manually entered credentials (runtime override) - wrap in SecureCredentials for safe handling
setRuntimeCredentials(new SecureCredentials(credentials));
setPhase('identity-setup');
}, []);
const skipCredentials = useCallback(() => {
// Skip identity setup entirely
setSkipIdentitySetup(true);
setPhase('identity-setup');
}, []);
// Main preflight effect
useEffect(() => {
if (phase !== 'running') return;
if (isRunningRef.current) return;
isRunningRef.current = true;
const handleUnhandledRejection = (reason: unknown) => {
const error = formatError(reason);
setSteps(prev => {
const runningIndex = prev.findIndex(s => s.status === 'running');
if (runningIndex >= 0) {
return prev.map((s, i) =>
i === runningIndex ? { ...s, status: 'error' as const, error: `Unhandled error: ${error}` } : s
);
}
const pendingIndex = prev.findIndex(s => s.status === 'pending');
if (pendingIndex >= 0) {
return prev.map((s, i) =>
i === pendingIndex ? { ...s, status: 'error' as const, error: `Unhandled error: ${error}` } : s
);
}
return prev;
});
setPhase('error');
isRunningRef.current = false;
};
process.on('unhandledRejection', handleUnhandledRejection);
const run = async () => {
try {
// Step: Validate project
updateStep(STEP_VALIDATE, { status: 'running' });
logger.startStep('Validate project');
let preflightContext: PreflightContext;
try {
preflightContext = await validateProject();
// Filter to only the targets the user picked in the TUI selector,
// if provided. Without this, the deploy header shows every target
// in aws-targets.json and downstream `awsTargets[0]` reads can
// resolve to a target the user did not select. See issue #1267.
if (selectedTargets && selectedTargets.length > 0) {
const selectedNames = new Set(selectedTargets.map(t => t.name));
preflightContext = {
...preflightContext,
awsTargets: preflightContext.awsTargets.filter(t => selectedNames.has(t.name)),
};
}
setContext(preflightContext);
// Make aws-targets.json region authoritative for downstream SDK / CDK
// toolkit-lib clients that bypass explicit region options. Restored on
// unmount, teardown rejection, or subsequent preflight start.
// See https://github.com/aws/agentcore-cli/issues/924.
const firstTarget = preflightContext.awsTargets[0];
if (firstTarget) {
restoreRegionEnv();
restoreRegionEnvRef.current = applyTargetRegionToEnv(firstTarget.region);
}
logger.endStep('success');
updateStep(STEP_VALIDATE, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
// Check if this is a credentials error (no AWS credentials configured)
if (isNoCredentialsError(err)) {
setHasCredentialsError(true);
}
// In interactive mode with credentials error, use short message (UI handles recovery)
// In non-interactive mode, show full message with fix instructions
let userMessage: string;
if (isInteractive && err instanceof AwsCredentialsError) {
userMessage = err.shortMessage;
} else {
userMessage = getErrorMessage(err);
}
updateStep(STEP_VALIDATE, { status: 'error', error: userMessage });
setPhase('error');
isRunningRef.current = false;
return;
}
// Teardown confirmation: pause for user confirmation before proceeding
if (preflightContext.isTeardownDeploy && !teardownConfirmed) {
setPhase('teardown-confirm');
isRunningRef.current = false;
return;
}
// Validate AWS credentials (deferred for teardown deploys until after confirmation)
if (preflightContext.isTeardownDeploy) {
try {
await validateAwsCredentials();
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isNoCredentialsError(err)) {
setHasCredentialsError(true);
}
const userMessage =
isInteractive && err instanceof AwsCredentialsError ? err.shortMessage : getErrorMessage(err);
updateStep(STEP_VALIDATE, { status: 'error', error: userMessage });
setPhase('error');
isRunningRef.current = false;
return;
}
}
// Step: Check dependencies (Node >= 18, uv for Python CodeZip)
updateStep(STEP_DEPS, { status: 'running' });
logger.startStep('Check dependencies');
try {
const depsResult = await checkDependencyVersions(preflightContext.projectSpec);
if (!depsResult.passed) {
const errorMsg = depsResult.errors.join('\n');
logger.endStep('error', errorMsg);
updateStep(STEP_DEPS, { status: 'error', error: errorMsg });
setPhase('error');
isRunningRef.current = false;
return;
}
// Log version info
if (depsResult.nodeCheck.current) {
logger.log(`Node.js: ${depsResult.nodeCheck.current}`);
}
if (depsResult.uvCheck?.current) {
logger.log(`uv: ${depsResult.uvCheck.current}`);
}
logger.endStep('success');
updateStep(STEP_DEPS, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
updateStep(STEP_DEPS, { status: 'error', error: logger.getFailureMessage('Check dependencies') });
setPhase('error');
isRunningRef.current = false;
return;
}
// Step: Build CDK project
updateStep(STEP_BUILD, { status: 'running' });
logger.startStep('Build CDK project');
try {
await buildCdkProject(preflightContext.cdkProject);
logger.endStep('success');
updateStep(STEP_BUILD, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
updateStep(STEP_BUILD, { status: 'error', error: logger.getFailureMessage('Build CDK project') });
setPhase('error');
isRunningRef.current = false;
return;
}
// Check if API key providers need setup before CDK synth (CDK needs credential ARNs)
// Skip this check if skipIdentityCheck is true (e.g., plan command only synthesizes)
const needsCredentialSetup =
!skipIdentityCheck &&
(hasIdentityApiProviders(preflightContext.projectSpec) ||
hasIdentityOAuthProviders(preflightContext.projectSpec));
if (needsCredentialSetup) {
// Get all credentials for the prompt (not just missing ones)
const allCredentials = getAllCredentials(preflightContext.projectSpec);
// Always show dialog when credentials exist
setMissingCredentials(allCredentials);
setPhase('credentials-prompt');
isRunningRef.current = false; // Reset so identity-setup can run after user input
return;
}
// Step: Synthesize CloudFormation
updateStepByLabel(LABEL_SYNTH, { status: 'running' });
logger.startStep('Synthesize CloudFormation');
let synthStackNames: string[];
try {
const synthResult = await synthesizeCdk(preflightContext.cdkProject, {
ioHost: switchableIoHost.ioHost,
previousWrapper: wrapperRef.current,
});
wrapperRef.current = synthResult.toolkitWrapper;
setCdkToolkitWrapper(synthResult.toolkitWrapper);
setStackNames(synthResult.stackNames);
synthStackNames = synthResult.stackNames;
logger.log(`Stacks: ${synthResult.stackNames.join(', ')}`);
logger.endStep('success');
updateStepByLabel(LABEL_SYNTH, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
updateStepByLabel(LABEL_SYNTH, {
status: 'error',
error: logger.getFailureMessage('Synthesize CloudFormation'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
// Step: Check stack status (ensure stacks are not in UPDATE_IN_PROGRESS etc.)
const target = preflightContext.awsTargets[0];
if (target && synthStackNames.length > 0) {
updateStepByLabel(LABEL_STACK_STATUS, { status: 'running' });
logger.startStep('Check stack status');
try {
const stackStatus = await checkStackDeployability(target.region, synthStackNames);
if (!stackStatus.canDeploy) {
const errorMsg = stackStatus.message ?? `Stack ${stackStatus.blockingStack} is not in a deployable state`;
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_STACK_STATUS, { status: 'error', error: errorMsg });
setPhase('error');
isRunningRef.current = false;
return;
}
logger.endStep('success');
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
updateStepByLabel(LABEL_STACK_STATUS, {
status: 'error',
error: logger.getFailureMessage('Check stack status'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
} else {
// Skip stack status check if no target or no stacks
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
}
// Check if bootstrap is needed
const bootstrapCheck = await checkBootstrapNeeded(preflightContext.awsTargets);
if (bootstrapCheck.needsBootstrap && bootstrapCheck.target) {
setBootstrapContext({
toolkitWrapper: wrapperRef.current,
target: bootstrapCheck.target,
});
setPhase('bootstrap-confirm');
return;
}
setPhase('complete');
isRunningRef.current = false;
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
setSteps(prev => {
const runningIndex = prev.findIndex(s => s.status === 'running');
if (runningIndex >= 0) {
const stepName = prev[runningIndex]?.label ?? 'Unknown step';
return prev.map((s, i) =>
i === runningIndex ? { ...s, status: 'error' as const, error: logger.getFailureMessage(stepName) } : s
);
}
return prev;
});
setPhase('error');
isRunningRef.current = false;
}
};
void run();
return () => {
process.off('unhandledRejection', handleUnhandledRejection);
};
}, [
phase,
logger,
switchableIoHost,
isInteractive,
skipIdentityCheck,
teardownConfirmed,
restoreRegionEnv,
selectedTargets,
]);
// Handle identity-setup phase (after user provides credentials)
useEffect(() => {
if (phase !== 'identity-setup' || !context) return;
if (isRunningRef.current) return; // Prevent duplicate runs
isRunningRef.current = true;
const runIdentitySetup = async () => {
// If user chose to skip, go directly to synth
if (skipIdentitySetup) {
logger.log('Skipping identity provider setup (user choice)');
setSkipIdentitySetup(false); // Reset for next run
// Synthesize CloudFormation
updateStepByLabel(LABEL_SYNTH, { status: 'running' });
logger.startStep('Synthesize CloudFormation');
let synthStackNames: string[];
try {
const synthResult = await synthesizeCdk(context.cdkProject, {
ioHost: switchableIoHost.ioHost,
previousWrapper: wrapperRef.current,
});
wrapperRef.current = synthResult.toolkitWrapper;
setCdkToolkitWrapper(synthResult.toolkitWrapper);
setStackNames(synthResult.stackNames);
synthStackNames = synthResult.stackNames;
logger.endStep('success');
updateStepByLabel(LABEL_SYNTH, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_SYNTH, {
status: 'error',
error: logger.getFailureMessage('Synthesize CloudFormation'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
// Check stack status
const target = context.awsTargets[0];
if (target && synthStackNames.length > 0) {
updateStepByLabel(LABEL_STACK_STATUS, { status: 'running' });
logger.startStep('Check stack status');
try {
const stackStatus = await checkStackDeployability(target.region, synthStackNames);
if (!stackStatus.canDeploy) {
const errorMsg = stackStatus.message ?? `Stack ${stackStatus.blockingStack} is not in a deployable state`;
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_STACK_STATUS, { status: 'error', error: errorMsg });
setPhase('error');
isRunningRef.current = false;
return;
}
logger.endStep('success');
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
updateStepByLabel(LABEL_STACK_STATUS, {
status: 'error',
error: logger.getFailureMessage('Check stack status'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
} else {
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
}
// Check if bootstrap is needed
const bootstrapCheck = await checkBootstrapNeeded(context.awsTargets);
if (bootstrapCheck.needsBootstrap && bootstrapCheck.target) {
setBootstrapContext({
toolkitWrapper: wrapperRef.current,
target: bootstrapCheck.target,
});
setPhase('bootstrap-confirm');
return;
}
setPhase('complete');
isRunningRef.current = false;
return;
}
// Run identity setup with runtime credentials
// Insert identity steps before synthesize in the step list
const hasApiKeys = hasIdentityApiProviders(context.projectSpec);
const hasOAuth = hasIdentityOAuthProviders(context.projectSpec);
setSteps(prev => {
const synthIndex = prev.findIndex(s => s.label === LABEL_SYNTH);
const identitySteps: Step[] = [];
if (hasApiKeys) identitySteps.push({ ...IDENTITY_STEP, status: 'running' });
if (hasOAuth) identitySteps.push({ label: LABEL_OAUTH, status: hasApiKeys ? 'pending' : 'running' });
return [...prev.slice(0, synthIndex), ...identitySteps, ...prev.slice(synthIndex)];
});
if (hasApiKeys) {
logger.startStep('Set up API key providers');
}
const target = context.awsTargets[0];
if (!target) {
const errorMsg = 'No AWS target configured';
if (hasApiKeys) {
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_API_KEY, { status: 'error', error: errorMsg });
} else if (hasOAuth) {
updateStepByLabel(LABEL_OAUTH, { status: 'error', error: errorMsg });
}
setPhase('error');
isRunningRef.current = false;
return;
}
try {
const configBaseDir = path.dirname(context.cdkProject.projectDir);
// Collect credential ARNs for deployed state
const deployedCredentials: Record<
string,
{ credentialProviderArn: string; clientSecretArn?: string; callbackUrl?: string }
> = {};
let kmsKeyArn: string | undefined;
// Set up API key providers if needed
if (hasApiKeys) {
const identityResult = await setupApiKeyProviders({
projectSpec: context.projectSpec,
configBaseDir,
region: target.region,
runtimeCredentials: runtimeCredentials ?? undefined,
enableKmsEncryption: true,
});
// Log KMS setup
if (identityResult.kmsKeyArn) {
logger.log(`Token vault encrypted with KMS key: ${identityResult.kmsKeyArn}`);
kmsKeyArn = identityResult.kmsKeyArn;
setIdentityKmsKeyArn(identityResult.kmsKeyArn);
}
// Log results
for (const result of identityResult.results) {
if (result.status === 'created') {
logger.log(`Created API key provider: ${result.providerName}`);
} else if (result.status === 'updated') {
logger.log(`Updated API key provider: ${result.providerName}`);
} else if (result.status === 'exists') {
logger.log(`API key provider exists: ${result.providerName}`);
} else if (result.status === 'skipped') {
logger.log(`Skipped ${result.providerName}: ${result.error}`);
} else if (result.status === 'error') {
logger.log(`Error for ${result.providerName}: ${result.error}`);
}
}
if (identityResult.hasErrors) {
logger.endStep('error', 'Some API key providers failed to set up');
updateStepByLabel(LABEL_API_KEY, { status: 'error', error: 'Some API key providers failed' });
setPhase('error');
isRunningRef.current = false;
return;
}
logger.endStep('success');
updateStepByLabel(LABEL_API_KEY, { status: 'success' });
for (const result of identityResult.results) {
if (result.credentialProviderArn) {
deployedCredentials[result.providerName] = {
credentialProviderArn: result.credentialProviderArn,
};
}
}
}
// Set up OAuth credential providers if needed
if (hasOAuth) {
updateStepByLabel(LABEL_OAUTH, { status: 'running' });
logger.startStep('Set up OAuth providers');
const oauthResult = await setupOAuth2Providers({
projectSpec: context.projectSpec,
configBaseDir,
region: target.region,
runtimeCredentials: runtimeCredentials ?? undefined,
});
for (const result of oauthResult.results) {
if (result.status === 'created') {
logger.log(`Created OAuth provider: ${result.providerName}`);
} else if (result.status === 'updated') {
logger.log(`Updated OAuth provider: ${result.providerName}`);
} else if (result.status === 'skipped') {
logger.log(`Skipped ${result.providerName}: ${result.error}`);
} else if (result.status === 'error') {
logger.log(`Error for ${result.providerName}: ${result.error}`);
}
}
if (oauthResult.hasErrors) {
logger.endStep('error', 'Some OAuth providers failed to set up');
updateStepByLabel(LABEL_OAUTH, { status: 'error', error: 'Some OAuth providers failed' });
setPhase('error');
isRunningRef.current = false;
return;
}
// Collect credential ARNs for deployed state
const creds: Record<
string,
{ credentialProviderArn: string; clientSecretArn?: string; callbackUrl?: string }
> = {};
for (const result of oauthResult.results) {
if (result.credentialProviderArn) {
creds[result.providerName] = {
credentialProviderArn: result.credentialProviderArn,
clientSecretArn: result.clientSecretArn,
callbackUrl: result.callbackUrl,
};
}
}
Object.assign(deployedCredentials, creds);
logger.endStep('success');
updateStepByLabel(LABEL_OAUTH, { status: 'success' });
}
// Write partial deployed state with credential ARNs before CDK synth
if (Object.keys(deployedCredentials).length > 0) {
setAllCredentials(deployedCredentials);
const configIO = new ConfigIO();
const target = context.awsTargets[0];
const existingState = await configIO.readDeployedState().catch(() => ({ targets: {} }) as DeployedState);
const targetState = existingState.targets?.[target!.name] ?? { resources: {} };
targetState.resources ??= {};
targetState.resources.credentials = deployedCredentials;
if (kmsKeyArn) targetState.resources.identityKmsKeyArn = kmsKeyArn;
await configIO.writeDeployedState({
...existingState,
targets: { ...existingState.targets, [target!.name]: targetState },
});
}
// Clear runtime credentials
setRuntimeCredentials(null);
// Synthesize CloudFormation now that credentials are in deployed state
updateStepByLabel(LABEL_SYNTH, { status: 'running' });
logger.startStep('Synthesize CloudFormation');
let synthStackNames: string[];
try {
const synthResult = await synthesizeCdk(context.cdkProject, {
ioHost: switchableIoHost.ioHost,
previousWrapper: wrapperRef.current,
});
wrapperRef.current = synthResult.toolkitWrapper;
setCdkToolkitWrapper(synthResult.toolkitWrapper);
setStackNames(synthResult.stackNames);
synthStackNames = synthResult.stackNames;
logger.endStep('success');
updateStepByLabel(LABEL_SYNTH, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_SYNTH, {
status: 'error',
error: logger.getFailureMessage('Synthesize CloudFormation'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
// Check stack status
if (target && synthStackNames.length > 0) {
updateStepByLabel(LABEL_STACK_STATUS, { status: 'running' });
logger.startStep('Check stack status');
try {
const stackStatus = await checkStackDeployability(target.region, synthStackNames);
if (!stackStatus.canDeploy) {
const errorMsg = stackStatus.message ?? `Stack ${stackStatus.blockingStack} is not in a deployable state`;
logger.endStep('error', errorMsg);
updateStepByLabel(LABEL_STACK_STATUS, { status: 'error', error: errorMsg });
setPhase('error');
isRunningRef.current = false;
return;
}
logger.endStep('success');
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
updateStepByLabel(LABEL_STACK_STATUS, {
status: 'error',
error: logger.getFailureMessage('Check stack status'),
});
setPhase('error');
isRunningRef.current = false;
return;
}
} else {
updateStepByLabel(LABEL_STACK_STATUS, { status: 'success' });
}
// Check if bootstrap is needed
const bootstrapCheck = await checkBootstrapNeeded(context.awsTargets);
if (bootstrapCheck.needsBootstrap && bootstrapCheck.target) {
setBootstrapContext({
toolkitWrapper: wrapperRef.current,
target: bootstrapCheck.target,
});
setPhase('bootstrap-confirm');
return;
}
setPhase('complete');
isRunningRef.current = false;
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
setSteps(prev =>
prev.map((s, i) =>
i === prev.length - 1
? { ...s, status: 'error', error: logger.getFailureMessage('Set up API key providers') }
: s
)
);
setPhase('error');
isRunningRef.current = false;
}
};
void runIdentitySetup();
}, [phase, context, skipIdentitySetup, runtimeCredentials, logger, switchableIoHost.ioHost]);
// Handle bootstrapping phase
useEffect(() => {
if (phase !== 'bootstrapping' || !bootstrapContext) return;
const runBootstrap = async () => {
setSteps(prev => [...prev, { ...BOOTSTRAP_STEP, status: 'running' }]);
logger.startStep('Bootstrap AWS environment');
try {
await bootstrapEnvironment(bootstrapContext.toolkitWrapper, bootstrapContext.target);
logger.endStep('success');
// Update the last step (bootstrap step we just added)
setSteps(prev => prev.map((s, i) => (i === prev.length - 1 ? { ...s, status: 'success' } : s)));
setPhase('complete');
isRunningRef.current = false;
} catch (err) {
const errorMsg = formatError(err);
logger.endStep('error', errorMsg);
if (isExpiredTokenError(err)) {
setHasTokenExpiredError(true);
}
setSteps(prev =>
prev.map((s, i) =>
i === prev.length - 1
? { ...s, status: 'error', error: logger.getFailureMessage('Bootstrap AWS environment') }
: s
)
);
setPhase('error');
isRunningRef.current = false;
}
};
void runBootstrap();
}, [phase, bootstrapContext, logger]);
return {
phase,
steps,
context,
cdkToolkitWrapper,
stackNames,
switchableIoHost,
hasTokenExpiredError,
hasCredentialsError,
missingCredentials,
identityKmsKeyArn,
allCredentials,
startPreflight,
confirmTeardown,
cancelTeardown,
confirmBootstrap,
skipBootstrap,
clearTokenExpiredError,
clearCredentialsError,
useEnvLocalCredentials,
useManualCredentials,
skipCredentials,
};
}