diff --git a/app/Commands/AwsMfaLogin.php b/app/Commands/AwsMfaLogin.php index c1e40fd..541b244 100644 --- a/app/Commands/AwsMfaLogin.php +++ b/app/Commands/AwsMfaLogin.php @@ -50,6 +50,11 @@ public function handle() $envVars = $this->helpers->aws()->iam()->authenticateWithMfaDevice($mfaDevice, $code); + if (empty($envVars)) { + $this->error("Unable to authenicate MFA"); + return 1; + } + $this->info("Now opening a session following your MFA authentication. Type `exit` to leave this shell."); (new Process([config('app.shell')])) diff --git a/app/Helpers/Aws/Iam.php b/app/Helpers/Aws/Iam.php index ee5d6eb..e819bf0 100644 --- a/app/Helpers/Aws/Iam.php +++ b/app/Helpers/Aws/Iam.php @@ -59,14 +59,18 @@ public function authenticateWithMfaDevice($mfaDeviceArn, $code): array ]; try { - $processOutput = $this->aws->newProcess($commandOptions) - ->run(); + $processOutput = $this->aws->newProcess($commandOptions)->run(); } catch (ProcessFailed $e) { - $this->command->error("Unable to authenicate MFA"); - return null; + return []; } - return json_decode($processOutput, true); + $result = json_decode($processOutput, true); + + return [ + 'AWS_ACCESS_KEY_ID' => $result['Credentials']['AccessKeyId'], + 'AWS_SECRET_ACCESS_KEY' => $result['Credentials']['SecretAccessKey'], + 'AWS_SESSION_TOKEN' => $result['Credentials']['SessionToken'], + ]; } public function assumeRole(int $accountId, string $role, string $sessionUser, ?string $mfaDevice = null, ?string $mfaCode = null): array