Skip to content

Commit

Permalink
Merge pull request #20 from adpeyre/#_rsa_key
Browse files Browse the repository at this point in the history
Authentication with ssh key
  • Loading branch information
julienj authored Feb 14, 2018
2 parents f3f11db + 5e42388 commit f69cd8b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/Automate/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ private function getSchema()
],
'password' => [
'_type' => 'text',
'_required' => true,
'_required' => false,
'_not_empty' => false,
],
'ssh_key' => [
'_type' => 'text',
'_required' => false,
'_not_empty' => true,
],
'path' => [
Expand Down
28 changes: 26 additions & 2 deletions src/Automate/Model/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class Server
*/
private $user;


/**
* @var string
*/
private $sshKey;

/**
* @var int
*/
Expand Down Expand Up @@ -106,6 +112,26 @@ public function setUser($user)
return $this;
}

/**
* @return string
*/
public function getSshKey()
{
return $this->sshKey;
}

/**
* @param string $sshKey
*
* @return Server
*/
public function setSshKey($sshKey)
{
$this->sshKey = $sshKey;

return $this;
}

/**
* @return int
*/
Expand Down Expand Up @@ -165,6 +191,4 @@ public function setPort($port)

return $this;
}


}
13 changes: 7 additions & 6 deletions src/Automate/Serializer/ServerDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public function denormalize($data, $class, $format = null, array $context = arra
$server = new Server();

$server
->setName($this->extractValue($data, 'name'))
->setHost($this->extractValue($data, 'host'))
->setUser($this->extractValue($data, 'user'))
->setPassword($this->extractValue($data, 'password'))
->setPath($this->extractValue($data, 'path'))
->setPort($this->extractValue($data, 'port', 22))
->setName($this->extractValue($data, 'name'))
->setHost($this->extractValue($data, 'host'))
->setUser($this->extractValue($data, 'user'))
->setSshKey($this->extractValue($data, 'ssh_key'))
->setPassword($this->extractValue($data, 'password', ""))
->setPath($this->extractValue($data, 'path'))
->setPort($this->extractValue($data, 'port', 22))
;

return $server;
Expand Down
20 changes: 18 additions & 2 deletions src/Automate/SessionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Automate\Model\Server;
use phpseclib\Net\SSH2;
use phpseclib\Crypt\RSA;

class SessionFactory
{
Expand All @@ -29,8 +30,23 @@ public function create(Server $server)
{
$ssh = new SSH2($server->getHost(), $server->getPort());

if (!$ssh->login($server->getUser(), $server->getPassword())) {
throw new \Exception(sprintf('[%s] Invalid user or password', $server->getName()));
// Connection with ssh key and optional
if (!empty($server->getSshKey())) {
if (!file_exists($server->getSshKey())) {
throw new \Exception(sprintf('[%s] File "'.$server->getSshKey().'" not found', $server->getName()));
}

$key = new RSA();
$key->setPassword($server->getPassword());
$key->loadKey(file_get_contents($server->getSshKey()));

if (!$ssh->login($server->getUser(), $key)) {
throw new \Exception(sprintf('[%s] SSH key or passphrase is invalid', $server->getName()));
}
} else {
if (!$ssh->login($server->getUser(), $server->getPassword())) {
throw new \Exception(sprintf('[%s] Invalid user or password', $server->getName()));
}
}

return new Session($ssh);
Expand Down

0 comments on commit f69cd8b

Please # to comment.