diff --git a/app/Commands/AwsAssumeRole.php b/app/Commands/AwsAssumeRole.php index 3e174b5..20a1960 100644 --- a/app/Commands/AwsAssumeRole.php +++ b/app/Commands/AwsAssumeRole.php @@ -92,7 +92,7 @@ public function handle() $this->info("Now opening a session following you ({$sessionUser}) assuming the role {$role} on {$accountName} ({$accountId}) . Type `exit` to leave this shell."); - (new Process([config('app.shell')])) + (new Process([env('SHELL', 'bash')])) ->setEnv(array_merge($envVars, [ 'AWS_S3_ENV' => $account['s3env'], 'BASH_SILENCE_DEPRECATION_WARNING' => '1', diff --git a/app/Commands/AwsMfaLogin.php b/app/Commands/AwsMfaLogin.php index c1e40fd..3bbed52 100644 --- a/app/Commands/AwsMfaLogin.php +++ b/app/Commands/AwsMfaLogin.php @@ -50,9 +50,14 @@ 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')])) + (new Process([env('SHELL', 'bash')])) ->setEnv(array_merge($envVars, [ 'BASH_SILENCE_DEPRECATION_WARNING' => '1', 'PS1' => "\e[32mnscli\e[34m(mfa-authd)$\e[39m ", diff --git a/app/Commands/EditEnvironmentVariables.php b/app/Commands/EditEnvironmentVariables.php index 1e89601..dfaeaf4 100644 --- a/app/Commands/EditEnvironmentVariables.php +++ b/app/Commands/EditEnvironmentVariables.php @@ -36,6 +36,8 @@ class EditEnvironmentVariables extends BaseCommand private string $fileContents; + private string $editor; + public function configure() { $this->setDefinition(array_merge([ @@ -50,7 +52,9 @@ public function configure() */ public function handle() { - $requiredBinaries = ['aws', config('app.editor')]; + $this->editor = env('EDITOR', '/usr/bin/vi'); + + $requiredBinaries = ['aws', $this->editor]; if ($this->helpers->checks()->checkAndReportMissingBinaries($requiredBinaries)) { exit(1); @@ -143,7 +147,7 @@ private function editFile(): string case -1: die('Unable to run editor'); case 0: - pcntl_exec(config('app.editor'), ['/tmp/' . $this->fileName]); + pcntl_exec($this->editor, ['/tmp/' . $this->fileName]); break; default: pcntl_waitpid($pid, $status); 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 diff --git a/config/app.php b/config/app.php index 2a04f31..4ce73b7 100644 --- a/config/app.php +++ b/config/app.php @@ -56,7 +56,4 @@ 'providers' => [ App\Providers\AppServiceProvider::class, ], - - 'editor' => env('EDITOR', '/usr/bin/vi'), - 'shell' => env('SHELL', 'bash') ];