Implement SSH Agent Forwarding Feature

This commit is contained in:
oanhnn 2015-02-13 16:27:37 +07:00
parent ef209e2ba0
commit 9f05d7c94b
5 changed files with 38 additions and 0 deletions

View File

@ -81,6 +81,17 @@ class Builder
return $this;
}
/**
* Using forward agent to authentication
*
* @return \Deployer\Server\Builder
*/
public function forwardAgent()
{
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_AGENT);
return $this;
}
/**
* @param string $name
* @param array|int|string $value

View File

@ -17,6 +17,8 @@ class Configuration
const AUTH_BY_PEM_FILE = 3;
const AUTH_BY_AGENT = 4;
/**
* Type of authentication.
* @var int

View File

@ -11,6 +11,7 @@ use Deployer\Server\Configuration;
use Deployer\Server\ServerInterface;
use phpseclib\Crypt\RSA;
use phpseclib\Net\SFTP;
use \phpseclib\System\SSH\Agent;
class PhpSecLib implements ServerInterface
{
@ -71,6 +72,13 @@ class PhpSecLib implements ServerInterface
break;
case Configuration::AUTH_BY_AGENT:
$key = new Agent();
$this->sftp->login($serverConfig->getUser(), $key);
break;
default:
throw new \RuntimeException('You need to specify authentication method.');
}

View File

@ -82,6 +82,10 @@ class SshExtension implements ServerInterface
throw new \RuntimeException('If you want to use pem file, switch to using PhpSecLib.');
case Configuration::AUTH_BY_AGENT:
throw new \RuntimeException('If you want to use forward agent function, switch to using PhpSecLib.');
default:
throw new \RuntimeException('You need to specify authentication method.');
}

View File

@ -101,4 +101,17 @@ class BuilderTest extends \PHPUnit_Framework_TestCase
$b = new Builder($config, $env);
$b->env('name', 'value');
}
public function testForwardAgent()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_AGENT)
->will($this->returnSelf());
$env = $this->getMock('Deployer\Server\Environment');
$b = new Builder($config, $env);
$b->forwardAgent();
}
}