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

Allow private key to be passed as a string #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions src/SSHConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SSHConnection
private $username;
private $password;
private $privateKeyPath;
private $privateKeyContents;
private $timeout;
private $connected = false;
private $ssh;
Expand Down Expand Up @@ -52,6 +53,12 @@ public function withPrivateKey(string $privateKeyPath): self
return $this;
}

public function withPrivateKeyString(string $privateKeyContents): self
{
$this->privateKeyContents = $privateKeyContents;
return $this;
}

public function timeout(int $timeout): self
{
$this->timeout = $timeout;
Expand All @@ -68,8 +75,8 @@ private function sanityCheck()
throw new InvalidArgumentException('Username not specified.');
}

if (!$this->password && (!$this->privateKeyPath)) {
throw new InvalidArgumentException('No password or private key path specified.');
if (!$this->password && !$this->privateKeyPath && !$this->privateKeyContents) {
throw new InvalidArgumentException('No password or private key specified.');
}
}

Expand All @@ -83,9 +90,15 @@ public function connect(): self
throw new RuntimeException('Error connecting to server.');
}

if ($this->privateKeyPath) {
if ($this->privateKeyPath || $this->privateKeyContents) {
$key = new RSA();
$key->loadKey(file_get_contents($this->privateKeyPath));

if ($this->privateKeyPath) {
$key->loadKey(file_get_contents($this->privateKeyPath));
} else if ($this->privateKeyContents) {
$key->loadKey($this->privateKeyContents);
}

$authenticated = $this->ssh->login($this->username, $key);
if (!$authenticated) {
throw new RuntimeException('Error authenticating with public-private key pair.');
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/SSHConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ public function testSSHConnectionWithKeyPair()
$this->assertEquals('', $command->getRawError());
}

public function testSSHConnectionWithKeyContents()
{
$privateKeyContents = file_get_contents('/home/travis/.ssh/id_rsa');

$connection = (new SSHConnection())
->to('localhost')
->onPort(22)
->as('travis')
->withPrivateKeyString($privateKeyContents)
->connect();

$command = $connection->run('echo "Hello world!"');

$this->assertEquals('Hello world!', $command->getOutput());
$this->assertEquals('Hello world!'."\n", $command->getRawOutput());

$this->assertEquals('', $command->getError());
$this->assertEquals('', $command->getRawError());
}

public function testSSHConnectionWithPassword()
{
$connection = (new SSHConnection())
Expand Down