Skip to content

Commit 873992c

Browse files
author
Edwin Hoksberg
committed
Allow private key to be passed as a string
1 parent d6c8326 commit 873992c

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/SSHConnection.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class SSHConnection
1818
private $username;
1919
private $password;
2020
private $privateKeyPath;
21+
private $privateKeyContents;
2122
private $timeout;
2223
private $connected = false;
2324
private $ssh;
@@ -52,6 +53,12 @@ public function withPrivateKey(string $privateKeyPath): self
5253
return $this;
5354
}
5455

56+
public function withPrivateKeyString(string $privateKeyContents): self
57+
{
58+
$this->privateKeyContents = $privateKeyContents;
59+
return $this;
60+
}
61+
5562
public function timeout(int $timeout): self
5663
{
5764
$this->timeout = $timeout;
@@ -68,8 +75,8 @@ private function sanityCheck()
6875
throw new InvalidArgumentException('Username not specified.');
6976
}
7077

71-
if (!$this->password && (!$this->privateKeyPath)) {
72-
throw new InvalidArgumentException('No password or private key path specified.');
78+
if (!$this->password && !$this->privateKeyPath && !$this->privateKeyContents) {
79+
throw new InvalidArgumentException('No password or private key specified.');
7380
}
7481
}
7582

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

86-
if ($this->privateKeyPath) {
93+
if ($this->privateKeyPath || $this->privateKeyContents) {
8794
$key = new RSA();
88-
$key->loadKey(file_get_contents($this->privateKeyPath));
95+
96+
if ($this->privateKeyPath) {
97+
$key->loadKey(file_get_contents($this->privateKeyPath));
98+
} else if ($this->privateKeyContents) {
99+
$key->loadKey($this->privateKeyContents);
100+
}
101+
89102
$authenticated = $this->ssh->login($this->username, $key);
90103
if (!$authenticated) {
91104
throw new RuntimeException('Error authenticating with public-private key pair.');

tests/Integration/SSHConnectionTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ public function testSSHConnectionWithKeyPair()
5555
$this->assertEquals('', $command->getRawError());
5656
}
5757

58+
public function testSSHConnectionWithKeyContents()
59+
{
60+
$privateKeyContents = file_get_contents('/home/travis/.ssh/id_rsa');
61+
62+
$connection = (new SSHConnection())
63+
->to('localhost')
64+
->onPort(22)
65+
->as('travis')
66+
->withPrivateKeyString($privateKeyContents)
67+
->connect();
68+
69+
$command = $connection->run('echo "Hello world!"');
70+
71+
$this->assertEquals('Hello world!', $command->getOutput());
72+
$this->assertEquals('Hello world!'."\n", $command->getRawOutput());
73+
74+
$this->assertEquals('', $command->getError());
75+
$this->assertEquals('', $command->getRawError());
76+
}
77+
5878
public function testSSHConnectionWithPassword()
5979
{
6080
$connection = (new SSHConnection())

0 commit comments

Comments
 (0)