From 450a942c38a533a3b1e4cdf89a092d145c856280 Mon Sep 17 00:00:00 2001 From: "J.Morales" Date: Tue, 27 Dec 2022 15:06:06 -0400 Subject: [PATCH 1/4] add user option/prompt when using ssh:configure --- app/Commands/SshConfigureCommand.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/app/Commands/SshConfigureCommand.php b/app/Commands/SshConfigureCommand.php index 208bd94..68fa750 100644 --- a/app/Commands/SshConfigureCommand.php +++ b/app/Commands/SshConfigureCommand.php @@ -12,7 +12,8 @@ class SshConfigureCommand extends Command protected $signature = 'ssh:configure {server? : The server name} {--key= : The path to the public key} - {--name= : The key name on Forge}'; + {--name= : The key name on Forge} + {--user= : The server username}'; /** * The description of the command. @@ -38,7 +39,7 @@ public function handle() $key = $this->getKey(); - $privateKey = $this->ensureKeyExists($this->getKeyName($key), $key); + $privateKey = $this->ensureKeyExists($this->getKeyName($key), $key, $this->getServerUsername()); $this->callSilently('ssh:test', ['--key' => $privateKey]); @@ -50,9 +51,10 @@ public function handle() * * @param string $name * @param string|null $key + * @param string $username * @return string */ - protected function ensureKeyExists($name, $key = null) + protected function ensureKeyExists($name, $key = null, $username = 'forge') { $server = $this->currentServer(); @@ -66,7 +68,7 @@ protected function ensureKeyExists($name, $key = null) $this->step('Adding Key ['.$localName.']'.' With The Name ['.$name.'] To Server ['.$server->name.']'); - $this->forge->createSSHKey($server->id, ['key' => $key, 'name' => $name], true); + $this->forge->createSSHKey($server->id, ['key' => $key, 'name' => $name, 'username' => $username], true); return $this->keys->keysPath().'/'.basename($localName, '.pub'); } @@ -111,4 +113,16 @@ protected function getKeyName($key) return $this->option('name') ?: $this->askStep($question, get_current_user()); } -} + + /** + * Prompt for a "server user" + * + * @return string + */ + protected function getServerUsername() + { + $question = 'What username should we use for the selected server'; + + return $this->option('user') ?: $this->askStep($question, 'forge'); + } +} \ No newline at end of file From a835e6640e5f479bf0aa67a77e14ffcb82dd50ff Mon Sep 17 00:00:00 2001 From: "J.Morales" Date: Tue, 27 Dec 2022 15:34:02 -0400 Subject: [PATCH 2/4] update tests reflecting new option update for ssh:configure command --- tests/Feature/SshConfigureCommandTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Feature/SshConfigureCommandTest.php b/tests/Feature/SshConfigureCommandTest.php index b110771..06acb5e 100644 --- a/tests/Feature/SshConfigureCommandTest.php +++ b/tests/Feature/SshConfigureCommandTest.php @@ -32,6 +32,7 @@ $this->forge->shouldReceive('createSSHKey')->with(1, [ 'name' => 'driesvints', 'key' => 'MY KEY Content', + 'username' => 'morales2k' ], true)->once(); $this->remote->shouldReceive('resolvePrivateKeyUsing')->once(); @@ -41,6 +42,7 @@ ->expectsQuestion('Which Server Would You Like To Configure The SSH Key Based Secure Authentication', 1) ->expectsQuestion('Which Key Would You Like To Use', 0) ->expectsQuestion('What Should The SSH Key Be Named', 'driesvints') + ->expectsQuestion('What Username Should We Use For The Selected Server', 'morales2k') ->expectsOutput('==> Creating Key [driesvints_rsa.pub]') ->expectsOutput('==> Adding Key [driesvints_rsa.pub] With The Name [driesvints] To Server [production]') ->expectsOutput('==> SSH Key Based Secure Authentication Configured Successfully'); @@ -78,6 +80,7 @@ $this->forge->shouldReceive('createSSHKey')->with(2, [ 'name' => 'driesvints', 'key' => 'MY KEY Content', + 'username' => 'morales2k' ], true)->once(); $this->remote->shouldReceive('resolvePrivateKeyUsing')->once(); @@ -86,6 +89,7 @@ $this->artisan('ssh:configure', ['server' => 2]) ->expectsQuestion('Which Key Would You Like To Use', 1) ->expectsQuestion('What Should The SSH Key Be Named In Forge', 'driesvints') + ->expectsQuestion('What Username Should We Use For The Selected Server', 'morales2k') ->expectsOutput('==> Adding Key [id_rsa.pub] With The Name [driesvints] To Server [production]') ->expectsOutput('==> SSH Key Based Secure Authentication Configured Successfully'); -}); +}); \ No newline at end of file From 46e09818a2ad37195a82b0597f93ccc42ee06a0c Mon Sep 17 00:00:00 2001 From: "J.Morales" Date: Tue, 27 Dec 2022 15:49:10 -0400 Subject: [PATCH 3/4] add style-ci fixes --- tests/Feature/SshConfigureCommandTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/SshConfigureCommandTest.php b/tests/Feature/SshConfigureCommandTest.php index 06acb5e..e96abb4 100644 --- a/tests/Feature/SshConfigureCommandTest.php +++ b/tests/Feature/SshConfigureCommandTest.php @@ -32,7 +32,7 @@ $this->forge->shouldReceive('createSSHKey')->with(1, [ 'name' => 'driesvints', 'key' => 'MY KEY Content', - 'username' => 'morales2k' + 'username' => 'morales2k', ], true)->once(); $this->remote->shouldReceive('resolvePrivateKeyUsing')->once(); @@ -80,7 +80,7 @@ $this->forge->shouldReceive('createSSHKey')->with(2, [ 'name' => 'driesvints', 'key' => 'MY KEY Content', - 'username' => 'morales2k' + 'username' => 'morales2k', ], true)->once(); $this->remote->shouldReceive('resolvePrivateKeyUsing')->once(); From 04abee2c74f1845e18ca2d62503f8bb017ab7fbd Mon Sep 17 00:00:00 2001 From: "J.Morales" Date: Thu, 29 Dec 2022 13:00:55 -0400 Subject: [PATCH 4/4] add missing dot in docblock --- app/Commands/SshConfigureCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/SshConfigureCommand.php b/app/Commands/SshConfigureCommand.php index 68fa750..ae23bcc 100644 --- a/app/Commands/SshConfigureCommand.php +++ b/app/Commands/SshConfigureCommand.php @@ -115,7 +115,7 @@ protected function getKeyName($key) } /** - * Prompt for a "server user" + * Prompt for a "server user". * * @return string */