| | | 1 | | import { spawn } from 'node:child_process'; |
| | | 2 | | import { createInterface } from 'node:readline'; |
| | | 3 | | import { SsoSessionExpiredError } from '../../../core/domain/errors/DomainErrors.js'; |
| | | 4 | | import { presentError } from '../errors/CliErrorPresenter.js'; |
| | | 5 | | import { SilentExitError } from '../errors/SilentExitError.js'; |
| | | 6 | | |
| | 2 | 7 | | const AWS_CLI_NOT_FOUND = -1; |
| | 2 | 8 | | const LOGIN_FAILED = 1; |
| | | 9 | | |
| | | 10 | | export type RecoveryDeps = { |
| | | 11 | | isInteractive(): boolean; |
| | | 12 | | confirm(question: string): Promise<boolean>; |
| | | 13 | | runLogin(profileName?: string): Promise<number>; |
| | | 14 | | write(message: string): void; |
| | | 15 | | }; |
| | | 16 | | |
| | | 17 | | export function buildLoginArgs(profileName?: string): string[] { |
| | 3 | 18 | | if (profileName) { |
| | 2 | 19 | | return ['sso', 'login', '--profile', profileName]; |
| | | 20 | | } |
| | 1 | 21 | | return ['sso', 'login']; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | function defaultIsInteractive(): boolean { |
| | 0 | 25 | | return [process.stdout.isTTY, process.stdin.isTTY].every(Boolean); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | function defaultConfirm(question: string): Promise<boolean> { |
| | 0 | 29 | | const prompt = createInterface({ |
| | | 30 | | input: process.stdin, |
| | | 31 | | output: process.stdout, |
| | | 32 | | }); |
| | 0 | 33 | | return new Promise((resolve) => { |
| | 0 | 34 | | prompt.question(`${question} `, (answer) => { |
| | 0 | 35 | | prompt.close(); |
| | 0 | 36 | | resolve(answer.trim().toLowerCase() === 'y'); |
| | | 37 | | }); |
| | | 38 | | }); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | function defaultRunLogin(profileName?: string): Promise<number> { |
| | 0 | 42 | | return new Promise((resolve) => { |
| | 0 | 43 | | const child = spawn('aws', buildLoginArgs(profileName), { |
| | | 44 | | stdio: 'inherit', |
| | | 45 | | }); |
| | 0 | 46 | | child.on('error', (error: NodeJS.ErrnoException) => { |
| | 0 | 47 | | resolve(error.code === 'ENOENT' ? AWS_CLI_NOT_FOUND : LOGIN_FAILED); |
| | | 48 | | }); |
| | 0 | 49 | | child.on('close', (code) => { |
| | 0 | 50 | | resolve(code ?? LOGIN_FAILED); |
| | | 51 | | }); |
| | | 52 | | }); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | function defaultWrite(message: string): void { |
| | 0 | 56 | | console.error(message); |
| | | 57 | | } |
| | | 58 | | |
| | 2 | 59 | | const defaultDeps: RecoveryDeps = { |
| | | 60 | | isInteractive: defaultIsInteractive, |
| | | 61 | | confirm: defaultConfirm, |
| | | 62 | | runLogin: defaultRunLogin, |
| | | 63 | | write: defaultWrite, |
| | | 64 | | }; |
| | | 65 | | |
| | | 66 | | export async function executeWithSsoRecovery( |
| | | 67 | | run: () => Promise<void>, |
| | | 68 | | deps: Partial<RecoveryDeps> = {}, |
| | | 69 | | ): Promise<void> { |
| | 10 | 70 | | const resolved: RecoveryDeps = { ...defaultDeps, ...deps }; |
| | 10 | 71 | | try { |
| | 10 | 72 | | return await run(); |
| | | 73 | | } catch (error) { |
| | 7 | 74 | | if (!(error instanceof SsoSessionExpiredError)) { |
| | 1 | 75 | | throw error; |
| | | 76 | | } |
| | 6 | 77 | | if (!resolved.isInteractive()) { |
| | 1 | 78 | | throw error; |
| | | 79 | | } |
| | 5 | 80 | | await recoverInteractively(run, error, resolved); |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | async function recoverInteractively( |
| | | 85 | | run: () => Promise<void>, |
| | | 86 | | error: SsoSessionExpiredError, |
| | | 87 | | deps: RecoveryDeps, |
| | | 88 | | ): Promise<void> { |
| | 5 | 89 | | deps.write(presentError(error)); |
| | | 90 | | |
| | 5 | 91 | | const command = error.profileName |
| | | 92 | | ? `aws sso login --profile ${error.profileName}` |
| | | 93 | | : 'aws sso login'; |
| | 5 | 94 | | const accepted = await deps.confirm(`Run '${command}' now? [y/N]`); |
| | 5 | 95 | | if (!accepted) { |
| | 1 | 96 | | deps.write( |
| | | 97 | | 'Skipped. Run the command above when ready, then re-run envilder.', |
| | | 98 | | ); |
| | 1 | 99 | | throw new SilentExitError(LOGIN_FAILED); |
| | | 100 | | } |
| | | 101 | | |
| | 4 | 102 | | const exitCode = await deps.runLogin(error.profileName); |
| | 4 | 103 | | ensureLoginSucceeded(exitCode, deps); |
| | 4 | 104 | | await retryOnce(run, deps); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | function ensureLoginSucceeded(exitCode: number, deps: RecoveryDeps): void { |
| | 4 | 108 | | if (exitCode === AWS_CLI_NOT_FOUND) { |
| | 1 | 109 | | deps.write( |
| | | 110 | | 'The AWS CLI was not found on your PATH. Install it, then run the command above.', |
| | | 111 | | ); |
| | 1 | 112 | | throw new SilentExitError(LOGIN_FAILED); |
| | | 113 | | } |
| | 3 | 114 | | if (exitCode !== 0) { |
| | 1 | 115 | | deps.write('Login did not complete. Try again, then re-run envilder.'); |
| | 1 | 116 | | throw new SilentExitError(LOGIN_FAILED); |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | async function retryOnce( |
| | | 121 | | run: () => Promise<void>, |
| | | 122 | | deps: RecoveryDeps, |
| | | 123 | | ): Promise<void> { |
| | 2 | 124 | | deps.write('\uD83C\uDF44 1-UP! SSO session restored.'); |
| | 2 | 125 | | deps.write('\u21bb Warping back for your secrets\u2026'); |
| | 2 | 126 | | try { |
| | 2 | 127 | | await run(); |
| | 1 | 128 | | deps.write('\u2b50 Level cleared.'); |
| | | 129 | | } catch (retryError) { |
| | 1 | 130 | | if (retryError instanceof SsoSessionExpiredError) { |
| | 1 | 131 | | deps.write(presentError(retryError)); |
| | 1 | 132 | | throw new SilentExitError(LOGIN_FAILED); |
| | | 133 | | } |
| | 0 | 134 | | throw retryError; |
| | | 135 | | } |
| | | 136 | | } |