Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for specifying remote user when calling the ssh command #67

Merged
merged 5 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/Commands/SshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SshCommand extends Command
*
* @var string
*/
protected $signature = 'ssh {server? : The server name}';
protected $signature = 'ssh {server? : The server name} {--u|user= : The user to connect to the server as}';

/**
* The description of the command.
Expand All @@ -35,13 +35,15 @@ public function handle()

$server = $this->currentServer();

$username = $this->option('user') ?: 'forge';

$this->step('Establishing secure connection');

$this->remote->ensureSshIsConfigured();

$this->successfulStep('Connected To <comment>['.$server->name.']</comment>');

$exitCode = $this->remote->passthru();
$exitCode = $this->remote->passthru(null, $username);

abort_if($exitCode == 255, $exitCode, 'Unable to connect to remote server. Have you configured an SSH key?');

Expand Down
15 changes: 9 additions & 6 deletions app/Repositories/RemoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ public function __construct($socketsPath)
* Execute a command against the shell, and displays the output.
*
* @param string|null $command
* @param string $user
* @return int
*/
public function passthru($command = null)
public function passthru($command = null, $user = 'forge')
{
$this->ensureSshIsConfigured();

passthru($this->ssh('"'.$command.'"'), $exitCode);
passthru($this->ssh('"'.$command.'"', $user), $exitCode);

return (int) $exitCode;
}
Expand Down Expand Up @@ -195,12 +196,13 @@ protected function ensuresFilesAreTailable($files)
}

/**
* Returns the "ssh" sheel command to be run.
* Returns the "ssh" shell command to be run.
*
* @param string $command|null
* @param string|null $command
* @param string $user
* @return string
*/
protected function ssh($command = null)
protected function ssh($command = null, $user = 'forge')
{
$options = collect([
'ConnectTimeout' => 5,
Expand All @@ -221,8 +223,9 @@ protected function ssh($command = null)
}

return trim(sprintf(
'ssh %s -t forge@%s %s',
'ssh %s -t %s@%s %s',
$options,
$user,
$this->server->ipAddress,
$command,
));
Expand Down
2 changes: 1 addition & 1 deletion app/Support/Panic.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public static function abort($message)
- Operating System: %s
- Error Message: %s.
EOF
, config('app.version'), phpversion(), PHP_OS, $message));
, config('app.version'), phpversion(), PHP_OS, $message));
}
}
42 changes: 42 additions & 0 deletions tests/Feature/SshCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,48 @@
$this->artisan('ssh')->assertExitCode(0);
});

it('can connect to a specific ssh user', function () {
$this->config->set('server', 1);

$this->forge->shouldReceive('server')
->once()
->with(1)
->andReturn((object) [
'name' => 'production',
'ipAddress' => '123.456.789.000',
]);

$this->remote->shouldReceive('ensureSshIsConfigured');

$this->remote->shouldReceive('passthru')
->with(null, 'testuser')
->andReturn(0);

$this->artisan('ssh', [
'--user' => 'testuser',
])->assertExitCode(0);
});

it('defaults to the forge user', function () {
$this->config->set('server', 1);

$this->forge->shouldReceive('server')
->once()
->with(1)
->andReturn((object) [
'name' => 'production',
'ipAddress' => '123.456.789.000',
]);

$this->remote->shouldReceive('ensureSshIsConfigured');

$this->remote->shouldReceive('passthru')
->with(null, 'forge')
->andReturn(0);

$this->artisan('ssh')->assertExitCode(0);
});

it('can not create ssh connections when ssh key is missing', function () {
$this->config->set('server', 1);

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Repositories/RemoteRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct($sanitizableOutput)
$this->sanitizableOutput = $sanitizableOutput;
}

protected function ssh($command = null)
protected function ssh($command = null, $user = null)
{
return $command;
}
Expand Down