Skip to content

Commit 4db3af6

Browse files
authored
Merge pull request #13 from EdwinHoksberg/allow-private-key-as-string
Allow private key to be passed as a string
2 parents 1a8d9e5 + 92452cc commit 4db3af6

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/SSHConnection.php

Lines changed: 17 additions & 4 deletions
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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public function testSSHConnectionWithKeyPair()
6464
$this->assertEquals('', $command->getRawError());
6565
}
6666

67+
public function testSSHConnectionWithKeyContents()
68+
{
69+
$privateKeyContents = file_get_contents('/home/travis/.ssh/id_rsa');
70+
71+
$connection = (new SSHConnection())
72+
->to('localhost')
73+
->onPort(22)
74+
->as('travis')
75+
->withPrivateKeyString($privateKeyContents)
76+
->connect();
77+
78+
$command = $connection->run('echo "Hello world!"');
79+
80+
$this->assertEquals('Hello world!', $command->getOutput());
81+
$this->assertEquals('Hello world!'."\n", $command->getRawOutput());
82+
83+
$this->assertEquals('', $command->getError());
84+
$this->assertEquals('', $command->getRawError());
85+
}
86+
6787
public function testSSHConnectionWithPassword()
6888
{
6989
$connection = (new SSHConnection())

0 commit comments

Comments
 (0)