Skip to content

Commit f69cd8b

Browse files
authored
Merge pull request #20 from adpeyre/login_rsa_key
Authentication with ssh key
2 parents f3f11db + 5e42388 commit f69cd8b

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

src/Automate/Loader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ private function getSchema()
137137
],
138138
'password' => [
139139
'_type' => 'text',
140-
'_required' => true,
140+
'_required' => false,
141+
'_not_empty' => false,
142+
],
143+
'ssh_key' => [
144+
'_type' => 'text',
145+
'_required' => false,
141146
'_not_empty' => true,
142147
],
143148
'path' => [

src/Automate/Model/Server.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class Server
3131
*/
3232
private $user;
3333

34+
35+
/**
36+
* @var string
37+
*/
38+
private $sshKey;
39+
3440
/**
3541
* @var int
3642
*/
@@ -106,6 +112,26 @@ public function setUser($user)
106112
return $this;
107113
}
108114

115+
/**
116+
* @return string
117+
*/
118+
public function getSshKey()
119+
{
120+
return $this->sshKey;
121+
}
122+
123+
/**
124+
* @param string $sshKey
125+
*
126+
* @return Server
127+
*/
128+
public function setSshKey($sshKey)
129+
{
130+
$this->sshKey = $sshKey;
131+
132+
return $this;
133+
}
134+
109135
/**
110136
* @return int
111137
*/
@@ -165,6 +191,4 @@ public function setPort($port)
165191

166192
return $this;
167193
}
168-
169-
170194
}

src/Automate/Serializer/ServerDenormalizer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public function denormalize($data, $class, $format = null, array $context = arra
2828
$server = new Server();
2929

3030
$server
31-
->setName($this->extractValue($data, 'name'))
32-
->setHost($this->extractValue($data, 'host'))
33-
->setUser($this->extractValue($data, 'user'))
34-
->setPassword($this->extractValue($data, 'password'))
35-
->setPath($this->extractValue($data, 'path'))
36-
->setPort($this->extractValue($data, 'port', 22))
31+
->setName($this->extractValue($data, 'name'))
32+
->setHost($this->extractValue($data, 'host'))
33+
->setUser($this->extractValue($data, 'user'))
34+
->setSshKey($this->extractValue($data, 'ssh_key'))
35+
->setPassword($this->extractValue($data, 'password', ""))
36+
->setPath($this->extractValue($data, 'path'))
37+
->setPort($this->extractValue($data, 'port', 22))
3738
;
3839

3940
return $server;

src/Automate/SessionFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Automate\Model\Server;
1515
use phpseclib\Net\SSH2;
16+
use phpseclib\Crypt\RSA;
1617

1718
class SessionFactory
1819
{
@@ -29,8 +30,23 @@ public function create(Server $server)
2930
{
3031
$ssh = new SSH2($server->getHost(), $server->getPort());
3132

32-
if (!$ssh->login($server->getUser(), $server->getPassword())) {
33-
throw new \Exception(sprintf('[%s] Invalid user or password', $server->getName()));
33+
// Connection with ssh key and optional
34+
if (!empty($server->getSshKey())) {
35+
if (!file_exists($server->getSshKey())) {
36+
throw new \Exception(sprintf('[%s] File "'.$server->getSshKey().'" not found', $server->getName()));
37+
}
38+
39+
$key = new RSA();
40+
$key->setPassword($server->getPassword());
41+
$key->loadKey(file_get_contents($server->getSshKey()));
42+
43+
if (!$ssh->login($server->getUser(), $key)) {
44+
throw new \Exception(sprintf('[%s] SSH key or passphrase is invalid', $server->getName()));
45+
}
46+
} else {
47+
if (!$ssh->login($server->getUser(), $server->getPassword())) {
48+
throw new \Exception(sprintf('[%s] Invalid user or password', $server->getName()));
49+
}
3450
}
3551

3652
return new Session($ssh);

0 commit comments

Comments
 (0)