Add password getter system for pass phrase (with use public key for connection) #298

This commit is contained in:
Vitaliy Zhuk 2015-05-27 13:47:56 +03:00
parent 3c036c659b
commit dc6b348d73
4 changed files with 112 additions and 24 deletions

View File

@ -68,24 +68,7 @@ class Builder
*/
public function password($password = null)
{
if (is_null($password)) {
// Ask password before connection
$password = AskPasswordGetter::createLazyGetter();
} else if (is_object($password)) {
// Invalid password
if (!$password instanceof PasswordGetterInterface) {
throw new \InvalidArgumentException(sprintf(
'The password should be a string or PasswordGetterInterface instances, but "%s" given.',
get_class($password)
));
}
} else if (!is_scalar($password)) {
// Invalid password
throw new \InvalidArgumentException(sprintf(
'The password should be a string or PasswordGetterInterface instances, but "%s" given.',
gettype($password)
));
}
$password = $this->checkPassword($password);
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_PASSWORD);
$this->config->setPassword($password);
@ -147,6 +130,23 @@ class Builder
*/
public function identityFile($publicKeyFile = '~/.ssh/id_rsa.pub', $privateKeyFile = '~/.ssh/id_rsa', $passPhrase = '')
{
$passPhrase = $this->checkPassword($passPhrase);
if (is_null($publicKeyFile)) {
// Use default value
$publicKeyFile = '~/.ssh/id_rsa.pub';
}
if (is_null($privateKeyFile)) {
// Use default value
$privateKeyFile = '~/.ssh/id_rsa';
}
if (is_null($passPhrase)) {
// Ask pass phrase before connection
$passPhrase = AskPasswordGetter::createLazyGetter();
}
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_IDENTITY_FILE);
$this->config->setPublicKey($publicKeyFile);
$this->config->setPrivateKey($privateKeyFile);
@ -210,4 +210,32 @@ class Builder
return $this;
}
/**
* Check password valid
*
* @param mixed $password
*
* @return mixed
*/
private function checkPassword($password)
{
if (is_null($password)) {
return AskPasswordGetter::createLazyGetter();
}
if (is_scalar($password)) {
return $password;
}
if (is_object($password) && $password instanceof PasswordGetterInterface) {
return $password;
}
// Invalid password
throw new \InvalidArgumentException(sprintf(
'The password should be a string or PasswordGetterInterface instances, but "%s" given.',
is_object($password) ? get_class($password) : gettype($password)
));
}
}

View File

@ -208,7 +208,7 @@ class Configuration
/**
* Set password for connection
*
* @param string|PasswordGetterInterface $password
* @param PasswordGetterInterface|string $password
*
* @return Configuration
*/
@ -274,13 +274,21 @@ class Configuration
*/
public function getPassPhrase()
{
return $this->passPhrase;
if ($this->passPhrase instanceof PasswordGetterInterface) {
$host = $this->getHost();
$user = $this->getUser();
$passPhrase = $this->passPhrase->getPassword($host, $user);
} else {
$passPhrase = $this->passPhrase;
}
return $passPhrase;
}
/**
* Set pass phrase
*
* @param string $passPhrase
* @param PasswordGetterInterface|string $passPhrase
*
* @return Configuration
*/
@ -383,6 +391,7 @@ class Configuration
public function setPemFile($pemFile)
{
$this->pemFile = $this->parseHome($pemFile);
return $this;
}

View File

@ -222,11 +222,43 @@ class BuilderTest extends \PHPUnit_Framework_TestCase
$config->expects($this->once())
->method('setPassPhrase')
->with('')
->with('pass-phrase')
->will($this->returnSelf());
$b = new Builder($config, $env);
$b->identityFile();
$b->identityFile(null, null, 'pass-phrase');
}
/**
* Test set public key for connection with use password getter for pass phrase
*/
public function testPublicKeyWithNullPassPhrase()
{
$config = $this->getMockBuilder('Deployer\Server\Configuration')->disableOriginalConstructor()->getMock();
$env = $this->getMock('Deployer\Server\Environment');
$config->expects($this->once())
->method('setAuthenticationMethod')
->with(Configuration::AUTH_BY_IDENTITY_FILE)
->will($this->returnSelf());
$config->expects($this->once())
->method('setPublicKey')
->with('~/.ssh/id_rsa.pub')
->will($this->returnSelf());
$config->expects($this->once())
->method('setPrivateKey')
->with('~/.ssh/id_rsa')
->will($this->returnSelf());
$config->expects($this->once())
->method('setPassPhrase')
->with($this->isInstanceOf('Deployer\Server\Password\CallablePasswordGetter'))
->will($this->returnSelf());
$b = new Builder($config, $env);
$b->identityFile(null, null, null);
}
/**

View File

@ -53,6 +53,25 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
$configuration->setPassword($passwordGetter);
$password = $configuration->getPassword();
$this->assertEquals('some-password', $password);
$this->assertEquals('some-password', $password, 'Password mismatch');
}
/**
* Test get pass phrase with use PasswordGetter system
*/
public function testGetPassPhraseWithUsePasswordGetter()
{
$configuration = new Configuration('name', 'localhost', 80);
$configuration->setUser('user');
$passwordGetter = $this->getMockForAbstractClass('Deployer\Server\Password\PasswordGetterInterface');
$passwordGetter->expects($this->once())->method('getPassword')
->with('localhost', 'user')
->will($this->returnValue('some-pass-phrase'));
$configuration->setPassPhrase($passwordGetter);
$passPhrase = $configuration->getPassPhrase();
$this->assertEquals('some-pass-phrase', $passPhrase, 'Pass phrases mismatch');
}
}