From b226ce322d9537f63e1fced5586ed6f18de07d4e Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Sun, 5 Oct 2014 14:04:48 -0500 Subject: [PATCH 1/6] follow the same behavior than console ssh client about config file --- src/Ssh/SshConfigFileConfiguration.php | 4 ++++ tests/Ssh/SshConfigFileConfigurationTest.php | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Ssh/SshConfigFileConfiguration.php b/src/Ssh/SshConfigFileConfiguration.php index 0cd63cb..21dca33 100644 --- a/src/Ssh/SshConfigFileConfiguration.php +++ b/src/Ssh/SshConfigFileConfiguration.php @@ -11,6 +11,7 @@ */ class SshConfigFileConfiguration extends Configuration { + const DEFAULT_SSH_IDENTITY = '~/.ssh/id_rsa'; protected $configs = array(); protected $config; @@ -126,7 +127,10 @@ public function getConfigForHost($host) unset($result['host']); if (isset($result['identityfile'])) { $result['identityfile'] = $this->processPath($result['identityfile']); + } else if (file_exists($file = $this->processPath(self::DEFAULT_SSH_IDENTITY))) { + $result['identityfile'] = $file; } + return $result; } diff --git a/tests/Ssh/SshConfigFileConfigurationTest.php b/tests/Ssh/SshConfigFileConfigurationTest.php index f885586..42bf0a9 100644 --- a/tests/Ssh/SshConfigFileConfigurationTest.php +++ b/tests/Ssh/SshConfigFileConfigurationTest.php @@ -96,9 +96,20 @@ public function testGetAuthentication() { $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'test'); - $this->assertEquals(new Authentication\None('test'), $config->getAuthentication(null, 'test')); + $identity = getenv('HOME') . "/.ssh/id_rsa"; - $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); + if (file_exists($identity)) { + $this->assertEquals(new Authentication\PublicKeyFile( + 'test', + "{$identity}.pub", + $identity, + null + ), $config->getAuthentication(null, 'test')); + + $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); + } else { + $this->assertEquals(new Authentication\None('test'), $config->getAuthentication(null, 'test')); + } $this->assertEquals( new Authentication\PublicKeyFile( From 9a07057ffe3ef388159f2d0449edb413a8fa52c3 Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Sun, 5 Oct 2014 14:32:38 -0500 Subject: [PATCH 2/6] fixed bug when copying --- tests/Ssh/SshConfigFileConfigurationTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Ssh/SshConfigFileConfigurationTest.php b/tests/Ssh/SshConfigFileConfigurationTest.php index 42bf0a9..26f9b0a 100644 --- a/tests/Ssh/SshConfigFileConfigurationTest.php +++ b/tests/Ssh/SshConfigFileConfigurationTest.php @@ -106,11 +106,12 @@ public function testGetAuthentication() null ), $config->getAuthentication(null, 'test')); - $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); } else { $this->assertEquals(new Authentication\None('test'), $config->getAuthentication(null, 'test')); } + $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); + $this->assertEquals( new Authentication\PublicKeyFile( 'test', From ad8c4926ed04fa25470a302e4c01ad0864ef293f Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Sun, 5 Oct 2014 14:45:17 -0500 Subject: [PATCH 3/6] Ensures replacing the ~ character only at the beginning of the route --- src/Ssh/SshConfigFileConfiguration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ssh/SshConfigFileConfiguration.php b/src/Ssh/SshConfigFileConfiguration.php index 21dca33..c53a9db 100644 --- a/src/Ssh/SshConfigFileConfiguration.php +++ b/src/Ssh/SshConfigFileConfiguration.php @@ -45,7 +45,7 @@ public function __construct($file, $host, $port = 22, array $methods = array(), */ protected function processPath($path) { - return str_replace('~', getenv('HOME'), $path); + return preg_replace('/^~/', getenv('HOME'), $path); } /** From 77c391337a8e8e54c0a474f364854540fb0fbc09 Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Mon, 6 Oct 2014 17:48:58 -0500 Subject: [PATCH 4/6] setter/getter identity configuration --- src/Ssh/Configuration.php | 21 ++++++++++++++++++++- src/Ssh/SshConfigFileConfiguration.php | 10 ++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Ssh/Configuration.php b/src/Ssh/Configuration.php index 8a5c9c5..d9156c2 100644 --- a/src/Ssh/Configuration.php +++ b/src/Ssh/Configuration.php @@ -13,6 +13,7 @@ class Configuration protected $port; protected $methods; protected $callbacks; + protected $identity; /** * Constructor @@ -21,13 +22,15 @@ class Configuration * @param integer $port * @param array $methods * @param array $callbacks + * @param string $identity */ - public function __construct($host, $port = 22, array $methods = array(), array $callbacks = array()) + public function __construct($host, $port = 22, array $methods = array(), array $callbacks = array(), $identity = null) { $this->host = $host; $this->port = $port; $this->methods = $methods; $this->callbacks = $callbacks; + $this->identity = $identity; } /** @@ -124,4 +127,20 @@ public function asArguments() $this->callbacks ); } + + /** + * @return string + */ + public function getIdentity() + { + return $this->identity; + } + + /** + * @param string $identity + */ + public function setIdentity($identity) + { + $this->identity = $identity; + } } diff --git a/src/Ssh/SshConfigFileConfiguration.php b/src/Ssh/SshConfigFileConfiguration.php index c53a9db..04e59fc 100644 --- a/src/Ssh/SshConfigFileConfiguration.php +++ b/src/Ssh/SshConfigFileConfiguration.php @@ -11,6 +11,7 @@ */ class SshConfigFileConfiguration extends Configuration { + const DEFAULT_SSH_IDENTITY = '~/.ssh/id_rsa'; protected $configs = array(); @@ -19,15 +20,20 @@ class SshConfigFileConfiguration extends Configuration /** * Constructor + * * @param string $file * @param string $host * @param integer $port * @param array $methods * @param array $callbacks + * @param string $itentity */ - public function __construct($file, $host, $port = 22, array $methods = array(), array $callbacks = array()) + public function __construct( + $file, $host, $port = 22, array $methods = array(), array $callbacks = array(), $itentity = null + ) { $this->parseSshConfigFile($this->processPath($file)); + $this->identity = !empty($identity) ? $identity : self::DEFAULT_SSH_IDENTITY; $this->config = $this->getConfigForHost($host); parent::__construct( @@ -127,7 +133,7 @@ public function getConfigForHost($host) unset($result['host']); if (isset($result['identityfile'])) { $result['identityfile'] = $this->processPath($result['identityfile']); - } else if (file_exists($file = $this->processPath(self::DEFAULT_SSH_IDENTITY))) { + } else if (file_exists($file = $this->processPath($this->getIdentity()))) { $result['identityfile'] = $file; } From aa76d43e207eab54c88073f23cf93939643921a4 Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Thu, 16 Oct 2014 16:28:06 -0500 Subject: [PATCH 5/6] fix typo and check only for null --- src/Ssh/SshConfigFileConfiguration.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Ssh/SshConfigFileConfiguration.php b/src/Ssh/SshConfigFileConfiguration.php index 04e59fc..fdfe18f 100644 --- a/src/Ssh/SshConfigFileConfiguration.php +++ b/src/Ssh/SshConfigFileConfiguration.php @@ -26,14 +26,14 @@ class SshConfigFileConfiguration extends Configuration * @param integer $port * @param array $methods * @param array $callbacks - * @param string $itentity + * @param string $identity */ public function __construct( - $file, $host, $port = 22, array $methods = array(), array $callbacks = array(), $itentity = null + $file, $host, $port = 22, array $methods = array(), array $callbacks = array(), $identity = null ) { $this->parseSshConfigFile($this->processPath($file)); - $this->identity = !empty($identity) ? $identity : self::DEFAULT_SSH_IDENTITY; + $this->identity = is_null($identity) ? self::DEFAULT_SSH_IDENTITY : $identity; $this->config = $this->getConfigForHost($host); parent::__construct( From 35c6278e9d89394864eea92c799574da3835f051 Mon Sep 17 00:00:00 2001 From: Julian Reyes Escrigas Date: Thu, 16 Oct 2014 16:28:24 -0500 Subject: [PATCH 6/6] this is a library --- .gitignore | 1 + composer.lock | 491 -------------------------------------------------- 2 files changed, 1 insertion(+), 491 deletions(-) delete mode 100644 composer.lock diff --git a/.gitignore b/.gitignore index 482af78..f69293a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor +*.lock tests/bootstrap.php \ No newline at end of file diff --git a/composer.lock b/composer.lock deleted file mode 100644 index 2a10718..0000000 --- a/composer.lock +++ /dev/null @@ -1,491 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" - ], - "hash": "a633d99ea64a6e7a38b75e2923250010", - "packages": [ - - ], - "packages-dev": [ - { - "name": "phpunit/php-code-coverage", - "version": "1.2.16", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "69e55e68481cf708a6db43aff0b504e31402fe27" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/69e55e68481cf708a6db43aff0b504e31402fe27", - "reference": "69e55e68481cf708a6db43aff0b504e31402fe27", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "phpunit/php-file-iterator": ">=1.3.0@stable", - "phpunit/php-text-template": ">=1.2.0@stable", - "phpunit/php-token-stream": ">=1.1.3@stable" - }, - "require-dev": { - "phpunit/phpunit": "3.7.*@dev" - }, - "suggest": { - "ext-dom": "*", - "ext-xdebug": ">=2.0.5" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2.x-dev" - } - }, - "autoload": { - "classmap": [ - "PHP/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", - "homepage": "https://github.com/sebastianbergmann/php-code-coverage", - "keywords": [ - "coverage", - "testing", - "xunit" - ], - "time": "2014-02-25 03:34:05" - }, - { - "name": "phpunit/php-file-iterator", - "version": "1.3.4", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/acd690379117b042d1c8af1fafd61bde001bf6bb", - "reference": "acd690379117b042d1c8af1fafd61bde001bf6bb", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "File/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", - "keywords": [ - "filesystem", - "iterator" - ], - "time": "2013-10-10 15:34:57" - }, - { - "name": "phpunit/php-text-template", - "version": "1.2.0", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-text-template.git", - "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", - "reference": "206dfefc0ffe9cebf65c413e3d0e809c82fbf00a", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "Text/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Simple template engine.", - "homepage": "https://github.com/sebastianbergmann/php-text-template/", - "keywords": [ - "template" - ], - "time": "2014-01-30 17:20:04" - }, - { - "name": "phpunit/php-timer", - "version": "1.0.5", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/19689d4354b295ee3d8c54b4f42c3efb69cbc17c", - "reference": "19689d4354b295ee3d8c54b4f42c3efb69cbc17c", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "PHP/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Utility class for timing", - "homepage": "https://github.com/sebastianbergmann/php-timer/", - "keywords": [ - "timer" - ], - "time": "2013-08-02 07:42:54" - }, - { - "name": "phpunit/php-token-stream", - "version": "1.2.2", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2-dev" - } - }, - "autoload": { - "classmap": [ - "PHP/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Wrapper around PHP's tokenizer extension.", - "homepage": "https://github.com/sebastianbergmann/php-token-stream/", - "keywords": [ - "tokenizer" - ], - "time": "2014-03-03 05:10:30" - }, - { - "name": "phpunit/phpunit", - "version": "3.7.32", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "2752cbb9ea5bd84c2811b34b6953f76965ec7a2f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2752cbb9ea5bd84c2811b34b6953f76965ec7a2f", - "reference": "2752cbb9ea5bd84c2811b34b6953f76965ec7a2f", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-pcre": "*", - "ext-reflection": "*", - "ext-spl": "*", - "php": ">=5.3.3", - "phpunit/php-code-coverage": "~1.2.1", - "phpunit/php-file-iterator": ">=1.3.1", - "phpunit/php-text-template": ">=1.1.1", - "phpunit/php-timer": ">=1.0.4", - "phpunit/phpunit-mock-objects": "~1.2.0", - "symfony/yaml": "~2.0" - }, - "require-dev": { - "pear-pear.php.net/pear": "1.9.4" - }, - "suggest": { - "ext-json": "*", - "ext-simplexml": "*", - "ext-tokenizer": "*", - "phpunit/php-invoker": ">=1.1.0,<1.2.0" - }, - "bin": [ - "composer/bin/phpunit" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.7.x-dev" - } - }, - "autoload": { - "classmap": [ - "PHPUnit/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "", - "../../symfony/yaml/" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "The PHP Unit Testing framework.", - "homepage": "http://www.phpunit.de/", - "keywords": [ - "phpunit", - "testing", - "xunit" - ], - "time": "2014-02-25 03:47:29" - }, - { - "name": "phpunit/phpunit-mock-objects", - "version": "1.2.3", - "source": { - "type": "git", - "url": "git://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "1.2.3" - }, - "dist": { - "type": "zip", - "url": "https://github.com/sebastianbergmann/phpunit-mock-objects/archive/1.2.3.zip", - "reference": "1.2.3", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "phpunit/php-text-template": ">=1.1.1@stable" - }, - "suggest": { - "ext-soap": "*" - }, - "type": "library", - "autoload": { - "classmap": [ - "PHPUnit/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Mock Object library for PHPUnit", - "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", - "keywords": [ - "mock", - "xunit" - ], - "time": "2013-01-13 10:24:48" - }, - { - "name": "symfony/filesystem", - "version": "v2.4.2", - "target-dir": "Symfony/Component/Filesystem", - "source": { - "type": "git", - "url": "https://github.com/symfony/Filesystem.git", - "reference": "7e65abb06d3b38f4be89266fe3fb4a759544e713" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/Filesystem/zipball/7e65abb06d3b38f4be89266fe3fb4a759544e713", - "reference": "7e65abb06d3b38f4be89266fe3fb4a759544e713", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.4-dev" - } - }, - "autoload": { - "psr-0": { - "Symfony\\Component\\Filesystem\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" - }, - { - "name": "Symfony Community", - "homepage": "http://symfony.com/contributors" - } - ], - "description": "Symfony Filesystem Component", - "homepage": "http://symfony.com", - "time": "2014-01-07 13:28:54" - }, - { - "name": "symfony/yaml", - "version": "v2.4.2", - "target-dir": "Symfony/Component/Yaml", - "source": { - "type": "git", - "url": "https://github.com/symfony/Yaml.git", - "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/bb6ddaf8956139d1b8c360b4b713ed0138e876b3", - "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.4-dev" - } - }, - "autoload": { - "psr-0": { - "Symfony\\Component\\Yaml\\": "" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com", - "homepage": "http://fabien.potencier.org", - "role": "Lead Developer" - }, - { - "name": "Symfony Community", - "homepage": "http://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "http://symfony.com", - "time": "2014-01-07 13:28:54" - } - ], - "aliases": [ - - ], - "minimum-stability": "stable", - "stability-flags": [ - - ], - "platform": { - "php": ">=5.3.2", - "ext-ssh2": "*" - }, - "platform-dev": [ - - ] -}