Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add password getter system for customize getting password. (#298) #299

Merged
merged 8 commits into from
Jun 4, 2015
124 changes: 107 additions & 17 deletions src/Server/Builder.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
<?php
/* (c) Anton Medvedev <anton@elfet.ru>

/**
* (c) Anton Medvedev <anton@elfet.ru>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Deployer\Server;

use Deployer\Server\Password\AskPasswordGetter;
use Deployer\Server\Password\PasswordGetterInterface;

/**
* Build server configuration
*/
class Builder
{
/**
Expand All @@ -20,8 +28,10 @@ class Builder
protected $env;

/**
* Construct
*
* @param Configuration $config
* @param Environment $env
* @param Environment $env
*/
public function __construct(Configuration $config, Environment $env)
{
Expand All @@ -37,115 +47,195 @@ public function __construct(Configuration $config, Environment $env)

/**
* Define user name for authentication.
*
* @param string $name
* @return $this
*
* @return Builder
*/
public function user($name)
{
$this->config->setUser($name);

return $this;
}

/**
* @param string $password If you did not define password it will be asked on connection.
* @return $this
* Set password for connection
*
* @param string|PasswordGetterInterface $password If you did not define password it will be asked on connection.
*
* @return Builder
*/
public function password($password)
public function password($password = null)
{
$password = $this->checkPassword($password);

$this->config->setAuthenticationMethod(Configuration::AUTH_BY_PASSWORD);
$this->config->setPassword($password);

return $this;
}

/**
* Define server host
*
* @param string $host
* @return $this
*
* @return Builder
*/
public function host($host)
{
$this->config->setHost($host);

return $this;
}

/**
* Define server port
*
* @param int $port
* @return $this
*
* @return Builder
*/
public function port($port)
{
$this->config->setPort($port);

return $this;
}

/**
* If you use an ssh config file you can user it.
*
* @param string $file Config file path
* @return $this
*
* @return Builder
*/
public function configFile($file)
{
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_CONFIG);
$this->config->setConfigFile($file);

return $this;
}

/**
* Authenticate with public key
*
* @param string $publicKeyFile
* @param string $privateKeyFile
* @param string $passPhrase
* @return $this
*
* @return 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);
$this->config->setPassPhrase($passPhrase);

return $this;
}

/**
* @param $pemFile
* @return $this
* Authenticate with pem file
*
* @param string $pemFile
*
* @return Builder
*/
public function pemFile($pemFile)
{
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_PEM_FILE);
$this->config->setPemFile($pemFile);

return $this;
}

/**
* Using forward agent to authentication
*
* @return $this
* @return Builder
*/
public function forwardAgent()
{
$this->config->setAuthenticationMethod(Configuration::AUTH_BY_AGENT);

return $this;
}

/**
* @param string $name
* Set env variable
*
* @param string $name
* @param array|int|string $value
* @return $this
*
* @return Builder
*/
public function env($name, $value)
{
$this->env->set($name, $value);

return $this;
}

/**
* Indicate stage
*
* @param string|array $stages Name or array on server stages.
* @return $this
*
* @return Builder
*/
public function stage($stages)
{
$this->env->set('stages', (array)$stages);
$this->env->set('stages', (array) $stages);

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)
));
}
}
Loading