Skip to content

Commit

Permalink
Merge pull request #18 from netsells/bugfix/mfa-and-env-issues
Browse files Browse the repository at this point in the history
Fixes for MFA login and env issues
  • Loading branch information
Jakub Gawron authored Mar 3, 2022
2 parents c8b9d4a + 88d4234 commit 3d5493a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/Commands/AwsAssumeRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 6 additions & 1 deletion app/Commands/AwsMfaLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
Expand Down
8 changes: 6 additions & 2 deletions app/Commands/EditEnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class EditEnvironmentVariables extends BaseCommand

private string $fileContents;

private string $editor;

public function configure()
{
$this->setDefinition(array_merge([
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 9 additions & 5 deletions app/Helpers/Aws/Iam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,4 @@
'providers' => [
App\Providers\AppServiceProvider::class,
],

'editor' => env('EDITOR', '/usr/bin/vi'),
'shell' => env('SHELL', 'bash')
];

0 comments on commit 3d5493a

Please sign in to comment.