Page MenuHomeDevCentral

D1419.id3614.diff
No OneTemporary

D1419.id3614.diff

diff --git a/lib/ArcConduitQuery.php b/lib/ArcConduitQuery.php
--- a/lib/ArcConduitQuery.php
+++ b/lib/ArcConduitQuery.php
@@ -12,124 +12,124 @@
* Class ArcConduitQuery
*/
class ArcConduitQuery {
- ///
- /// Private members
- ///
-
- /**
- * The request to send through conduit
- *
- * @var ConduitRequest
- */
- private $request;
-
- /**
- * The conduit reply
- *
- * @var object
- */
- private $reply = null;
-
- ///
- /// Constructor
- ///
-
- /**
- * Initializes a new instance of the ArcConduitQuery class
- *
- * @param ConduitRequest $request The request to send through conduit
- */
- public function __construct ($request) {
- $this->request = $request;
- }
-
- ///
- /// Helper functions to run software
- ///
-
- /**
- * Gets an array of descriptors for proc_open
- *
- * @return array descriptors for stdin, stdout and stderr
- */
- private static function getDescriptors () {
- return [
- 0 => ["pipe", "r"], //stdin
- 1 => ["pipe", "w"], //stdout
- 2 => ["file", static::class . "-error.log", "a"] //stderr
- ];
- }
-
- /**
- * Runs a command, passing a string to stdin and returns stdout
- *
- * @param $command The command to run
- * @param $input The string to pass to stdin
- * @return string The stdout content returned by the application
- *
- * @throws Exception on command process spawning failure
- */
- private static function runCommand ($command, $input) {
- $process = proc_open(
- $command,
- self::getDescriptors(),
- $pipes
- );
-
- if (!is_resource($process)) {
- throw new Exception("Can't call $command");
- }
-
- // -> stdin
- fwrite($pipes[0], $input);
- fclose($pipes[0]);
-
- // <- stdout
- $output = stream_get_contents($pipes[1]);
- fclose($pipes[1]);
-
- proc_close($process);
- return $output;
- }
-
- ///
- /// Methods to call arc call-conduit
- ///
-
- /**
- * Gets arc command with the relevant parameters
- *
- * @return string The arc call-conduit <method> command
- */
- private function getArcCommand () {
- return "arc call-conduit " . $this->request->getMethod();
- }
-
- /**
- * Runs the arc command and stores the reply
- *
- * @return $this
- * @throws Exception on command process spawning failure
- */
- public function run () {
- $reply = self::runCommand(
- $this->getArcCommand(),
- $this->request->getJSONRepresentation()
- );
- $this->reply = json_decode($reply);
- return $this;
- }
-
- ///
- /// Methods to access to properties
- ///
-
- /**
- * Gets the reply of the conduit query
- *
- * @return object The conduit reply
- */
- public function getReply () {
- return $this->reply;
- }
+ ///
+ /// Private members
+ ///
+
+ /**
+ * The request to send through conduit
+ *
+ * @var ConduitRequest
+ */
+ private $request;
+
+ /**
+ * The conduit reply
+ *
+ * @var object
+ */
+ private $reply = null;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of the ArcConduitQuery class
+ *
+ * @param ConduitRequest $request The request to send through conduit
+ */
+ public function __construct ($request) {
+ $this->request = $request;
+ }
+
+ ///
+ /// Helper functions to run software
+ ///
+
+ /**
+ * Gets an array of descriptors for proc_open
+ *
+ * @return array descriptors for stdin, stdout and stderr
+ */
+ private static function getDescriptors () {
+ return [
+ 0 => ["pipe", "r"], //stdin
+ 1 => ["pipe", "w"], //stdout
+ 2 => ["file", static::class . "-error.log", "a"] //stderr
+ ];
+ }
+
+ /**
+ * Runs a command, passing a string to stdin and returns stdout
+ *
+ * @param $command The command to run
+ * @param $input The string to pass to stdin
+ * @return string The stdout content returned by the application
+ *
+ * @throws Exception on command process spawning failure
+ */
+ private static function runCommand ($command, $input) {
+ $process = proc_open(
+ $command,
+ self::getDescriptors(),
+ $pipes
+ );
+
+ if (!is_resource($process)) {
+ throw new Exception("Can't call $command");
+ }
+
+ // -> stdin
+ fwrite($pipes[0], $input);
+ fclose($pipes[0]);
+
+ // <- stdout
+ $output = stream_get_contents($pipes[1]);
+ fclose($pipes[1]);
+
+ proc_close($process);
+ return $output;
+ }
+
+ ///
+ /// Methods to call arc call-conduit
+ ///
+
+ /**
+ * Gets arc command with the relevant parameters
+ *
+ * @return string The arc call-conduit <method> command
+ */
+ private function getArcCommand () {
+ return "arc call-conduit " . $this->request->getMethod();
+ }
+
+ /**
+ * Runs the arc command and stores the reply
+ *
+ * @return $this
+ * @throws Exception on command process spawning failure
+ */
+ public function run () {
+ $reply = self::runCommand(
+ $this->getArcCommand(),
+ $this->request->getJSONRepresentation()
+ );
+ $this->reply = json_decode($reply);
+ return $this;
+ }
+
+ ///
+ /// Methods to access to properties
+ ///
+
+ /**
+ * Gets the reply of the conduit query
+ *
+ * @return object The conduit reply
+ */
+ public function getReply () {
+ return $this->reply;
+ }
}
diff --git a/lib/ConduitPassphraseQueryRequest.php b/lib/ConduitPassphraseQueryRequest.php
--- a/lib/ConduitPassphraseQueryRequest.php
+++ b/lib/ConduitPassphraseQueryRequest.php
@@ -16,39 +16,39 @@
* Represents a request to send to conduit
*/
class ConduitPassphraseQueryRequest extends ConduitRequest {
- /**
- * The IDs of the credentials to fetch
- *
- * @var array
- */
- public $ids;
+ /**
+ * The IDs of the credentials to fetch
+ *
+ * @var array
+ */
+ public $ids;
- /**
- * Indicates if the public keys are to be retrieved
- *
- * @var bool
- */
- public $needPublicKeys = false;
+ /**
+ * Indicates if the public keys are to be retrieved
+ *
+ * @var bool
+ */
+ public $needPublicKeys = false;
- /**
- * Indicates if the private keys or passwords are to be retrieved
- *
- * @var bool
- */
- public $needSecrets = false;
+ /**
+ * Indicates if the private keys or passwords are to be retrieved
+ *
+ * @var bool
+ */
+ public $needSecrets = false;
- /**
- * Initializes a new instance of the ConduitPassphraseQueryRequest class
- *
- * @param array $ids The IDs of the credentials to fetch
- * @param bool $needPublicKeys If true, retrieves the public keys
- * @param bool $needSecrets If true, retrieves the private keys and passwords
- */
- public function __construct ($ids, $needPublicKeys = false, $needSecrets = true) {
- $this->ids = $ids;
- $this->needPublicKeys = $needPublicKeys;
- $this->needSecrets = $needSecrets;
+ /**
+ * Initializes a new instance of the ConduitPassphraseQueryRequest class
+ *
+ * @param array $ids The IDs of the credentials to fetch
+ * @param bool $needPublicKeys If true, retrieves the public keys
+ * @param bool $needSecrets If true, retrieves the private keys and passwords
+ */
+ public function __construct ($ids, $needPublicKeys = false, $needSecrets = true) {
+ $this->ids = $ids;
+ $this->needPublicKeys = $needPublicKeys;
+ $this->needSecrets = $needSecrets;
- parent::__construct('passphrase.query');
- }
+ parent::__construct('passphrase.query');
+ }
}
diff --git a/lib/ConduitRequest.php b/lib/ConduitRequest.php
--- a/lib/ConduitRequest.php
+++ b/lib/ConduitRequest.php
@@ -16,46 +16,46 @@
* Represents a Conduit request
*/
class ConduitRequest {
- /**
- * The conduit method to call
- *
- * @var string
- */
- private $method;
+ /**
+ * The conduit method to call
+ *
+ * @var string
+ */
+ private $method;
- /**
- * Initializes a new instance of the ConduitRequest class
- *
- * @param $method The conduit method to call
- */
- public function __construct ($method) {
- $this->method = $method;
- }
+ /**
+ * Initializes a new instance of the ConduitRequest class
+ *
+ * @param $method The conduit method to call
+ */
+ public function __construct ($method) {
+ $this->method = $method;
+ }
- /**
- * Queries conduit
- *
- * @return $this
- */
- public function query () {
- return (new ArcConduitQuery($this))->run();
- }
+ /**
+ * Queries conduit
+ *
+ * @return $this
+ */
+ public function query () {
+ return (new ArcConduitQuery($this))->run();
+ }
- /**
- * Gets the conduit method to call
- *
- * @return string The conduit method
- */
- public function getMethod () {
- return $this->method;
- }
+ /**
+ * Gets the conduit method to call
+ *
+ * @return string The conduit method
+ */
+ public function getMethod () {
+ return $this->method;
+ }
- /**
- * Gets the JSON representation of the request
- *
- * @return string The JSON representation
- */
- public function getJSONRepresentation () {
- return json_encode($this);
- }
+ /**
+ * Gets the JSON representation of the request
+ *
+ * @return string The JSON representation
+ */
+ public function getJSONRepresentation () {
+ return json_encode($this);
+ }
}
diff --git a/lib/GetConfiguration.php b/lib/GetConfiguration.php
--- a/lib/GetConfiguration.php
+++ b/lib/GetConfiguration.php
@@ -14,67 +14,67 @@
* Allows to get configuration from a template
*/
class GetConfiguration {
- ///
- /// Properties
- ///
+ ///
+ /// Properties
+ ///
- /**
- * The configuration template content
- * @var string
- */
- public $template;
+ /**
+ * The configuration template content
+ * @var string
+ */
+ public $template;
- ///
- /// Templating methods
- ///
+ ///
+ /// Templating methods
+ ///
- /**
- * Prints configuration from specified configuration template
- * replacing expressions by relevant variables and credentials.
- *
- * @return string the configuration
- */
+ /**
+ * Prints configuration from specified configuration template
+ * replacing expressions by relevant variables and credentials.
+ *
+ * @return string the configuration
+ */
public function printConfiguration () {
- echo static::substituteTemplateVariables($this->template);
+ echo static::substituteTemplateVariables($this->template);
}
- /**
- * Substitutes template variables in the specified text
- *
- * @param string $text The template text
- * @return string The substitued text
- */
- public static function substituteTemplateVariables ($text) {
- $text = static::substituteExec($text);
- return $text;
- }
+ /**
+ * Substitutes template variables in the specified text
+ *
+ * @param string $text The template text
+ * @return string The substitued text
+ */
+ public static function substituteTemplateVariables ($text) {
+ $text = static::substituteExec($text);
+ return $text;
+ }
- /**
- * Substitutes %%`command`%% by the output of the command.
- *
- * @param string $text The template text
- * @return string The substitued text
- */
- public static function substituteExec ($text) {
- return preg_replace_callback(
- '/%%`(.*)`%%/',
- function ($matches) {
- return trim(`$matches[1]`);
- },
- $text
- );
- }
+ /**
+ * Substitutes %%`command`%% by the output of the command.
+ *
+ * @param string $text The template text
+ * @return string The substitued text
+ */
+ public static function substituteExec ($text) {
+ return preg_replace_callback(
+ '/%%`(.*)`%%/',
+ function ($matches) {
+ return trim(`$matches[1]`);
+ },
+ $text
+ );
+ }
- ///
- /// Script procedural code
- ///
+ ///
+ /// Script procedural code
+ ///
- /**
- * Runs the script
- */
- public static function run ($file) {
+ /**
+ * Runs the script
+ */
+ public static function run ($file) {
$instance = new self;
$instance->template = file_get_contents($file);
- $instance->printConfiguration();
- }
+ $instance->printConfiguration();
+ }
}
diff --git a/lib/GetPublicKeys.php b/lib/GetPublicKeys.php
--- a/lib/GetPublicKeys.php
+++ b/lib/GetPublicKeys.php
@@ -16,152 +16,152 @@
* Allows to get public keys allowed to connect to the account
*/
class GetPublicKeys {
- ///
- /// Constant and helper methods to retrieves filename paths
- ///
-
- /**
- * The directory where data is stored
- *
- * @const string
- */
- const DATA_DIRECTORY = 'data';
-
- /**
- * Gets the path to the servers data file
- *
- * @return string The path to the servers data file
- */
- public static function getServersDataFile () {
- return self::getDataFile('servers.json');
- }
-
- /**
- * Gets the path to the specified data file
- *
- * @param string $filename The file name to get the path of
- * @return string The path to the data file
- */
- public static function getDataFile ($filename) {
- return getenv("HOME")
- . '/' . self::DATA_DIRECTORY
- . '/' . $filename;
- }
-
- ///
- /// SSH authorized_keys helper methods
- ///
-
- /**
- * Gets SSH public key from DevCentral
- *
- * @param int $id The id to the credential stored in passphrase
- * @return string The SSH public key
- */
- public static function getPublicKey ($id) {
- //Calls conduit with a passphrase.query request
- $request = new ConduitPassphraseQueryRequest(
- [$id], true, false
- );
- $reply = $request
- ->query()
- ->getReply();
-
- //Gets the first property of the response.data object,
- //then the material.publicKey property
- $data = (Array)$reply->response->data;
- $firstDataItem = array_shift($data);
-
- $key = $firstDataItem->material->publicKey;
- return trim($key);
- }
-
- /**
- * Gets a .ssh/authorized_keys line to give access to the specified server
- *
- * @param object $server The server to give access to
- * @return string The line to add in authorized_keys file to give access to this server
- *
- * The expected object has the following properties:
- * - int key: the id of the SSH key to fetch from DevCentral
- * - array allowedConnectionFrom: an array of the hosts masks to allow to connect, each item a string (mandatory)
- * - string|null restrictCommand: if not null, restrict the connection to a single command, automatically executed
- * - string comment: the SSH key comment
- */
- public static function getAuthorizedKey ($server) {
- $line = "";
-
- //Options
- if ($server->restrictCommand !== null) {
- $line .= self::getAuthorizedKeyCommandOption($server->restrictCommand);
- $line .= ',';
- }
- $line .= self::getAuthorizedKeyFrom($server->allowedConnectionFrom);
- $line .= ',';
- $line .= self::getAuthorizedKeyRestrictOptions();
- $line .= ' ';
-
- //Key
- $line .= self::getPublicKey($server->key);
- $line .= ' ';
- $line .= $server->comment;
-
- return $line;
- }
-
- /**
- * Gets command authorized_keys option
- *
- * @param string The command to execute on login
- * @return string The command option
- */
- public static function getAuthorizedKeyCommandOption ($command) {
- return 'command="' . $command . '"';
- }
-
- /**
- * Gets restriction authorized_keys options
- *
- * @return string A list of options restricting access
- */
- public static function getAuthorizedKeyRestrictOptions () {
- return "no-port-forwarding,no-x11-forwarding,no-agent-forwarding";
- }
-
- /**
- * Gets authorized_keys from expression
- *
- * @param array $patterns a list of pattern matching the authorized_keys from format
- * @return string the from authorized_keys expression
- */
- public static function getAuthorizedKeyFrom ($patterns) {
- return "from=\""
- . implode(',', $patterns)
- . '"';
- }
-
- /**
- * Prints a .ssh/authorized_keys file to give access to the specified servers
- *
- * @param array $servers The list of the servers needing to access this account
- */
- public static function printAuthorizedKeys ($servers) {
- foreach ($servers as $server) {
- echo self::getAuthorizedKey($server);
- echo "\n";
- }
- }
-
- ///
- /// Script procedural code
- ///
-
- /**
- * Runs the script
- */
- public static function run () {
- $file = self::getServersDataFile();
- $data = json_decode(file_get_contents($file));
- self::printAuthorizedKeys($data);
- }
+ ///
+ /// Constant and helper methods to retrieves filename paths
+ ///
+
+ /**
+ * The directory where data is stored
+ *
+ * @const string
+ */
+ const DATA_DIRECTORY = 'data';
+
+ /**
+ * Gets the path to the servers data file
+ *
+ * @return string The path to the servers data file
+ */
+ public static function getServersDataFile () {
+ return self::getDataFile('servers.json');
+ }
+
+ /**
+ * Gets the path to the specified data file
+ *
+ * @param string $filename The file name to get the path of
+ * @return string The path to the data file
+ */
+ public static function getDataFile ($filename) {
+ return getenv("HOME")
+ . '/' . self::DATA_DIRECTORY
+ . '/' . $filename;
+ }
+
+ ///
+ /// SSH authorized_keys helper methods
+ ///
+
+ /**
+ * Gets SSH public key from DevCentral
+ *
+ * @param int $id The id to the credential stored in passphrase
+ * @return string The SSH public key
+ */
+ public static function getPublicKey ($id) {
+ //Calls conduit with a passphrase.query request
+ $request = new ConduitPassphraseQueryRequest(
+ [$id], true, false
+ );
+ $reply = $request
+ ->query()
+ ->getReply();
+
+ //Gets the first property of the response.data object,
+ //then the material.publicKey property
+ $data = (Array)$reply->response->data;
+ $firstDataItem = array_shift($data);
+
+ $key = $firstDataItem->material->publicKey;
+ return trim($key);
+ }
+
+ /**
+ * Gets a .ssh/authorized_keys line to give access to the specified server
+ *
+ * @param object $server The server to give access to
+ * @return string The line to add in authorized_keys file to give access to this server
+ *
+ * The expected object has the following properties:
+ * - int key: the id of the SSH key to fetch from DevCentral
+ * - array allowedConnectionFrom: an array of the hosts masks to allow to connect, each item a string (mandatory)
+ * - string|null restrictCommand: if not null, restrict the connection to a single command, automatically executed
+ * - string comment: the SSH key comment
+ */
+ public static function getAuthorizedKey ($server) {
+ $line = "";
+
+ //Options
+ if ($server->restrictCommand !== null) {
+ $line .= self::getAuthorizedKeyCommandOption($server->restrictCommand);
+ $line .= ',';
+ }
+ $line .= self::getAuthorizedKeyFrom($server->allowedConnectionFrom);
+ $line .= ',';
+ $line .= self::getAuthorizedKeyRestrictOptions();
+ $line .= ' ';
+
+ //Key
+ $line .= self::getPublicKey($server->key);
+ $line .= ' ';
+ $line .= $server->comment;
+
+ return $line;
+ }
+
+ /**
+ * Gets command authorized_keys option
+ *
+ * @param string The command to execute on login
+ * @return string The command option
+ */
+ public static function getAuthorizedKeyCommandOption ($command) {
+ return 'command="' . $command . '"';
+ }
+
+ /**
+ * Gets restriction authorized_keys options
+ *
+ * @return string A list of options restricting access
+ */
+ public static function getAuthorizedKeyRestrictOptions () {
+ return "no-port-forwarding,no-x11-forwarding,no-agent-forwarding";
+ }
+
+ /**
+ * Gets authorized_keys from expression
+ *
+ * @param array $patterns a list of pattern matching the authorized_keys from format
+ * @return string the from authorized_keys expression
+ */
+ public static function getAuthorizedKeyFrom ($patterns) {
+ return "from=\""
+ . implode(',', $patterns)
+ . '"';
+ }
+
+ /**
+ * Prints a .ssh/authorized_keys file to give access to the specified servers
+ *
+ * @param array $servers The list of the servers needing to access this account
+ */
+ public static function printAuthorizedKeys ($servers) {
+ foreach ($servers as $server) {
+ echo self::getAuthorizedKey($server);
+ echo "\n";
+ }
+ }
+
+ ///
+ /// Script procedural code
+ ///
+
+ /**
+ * Runs the script
+ */
+ public static function run () {
+ $file = self::getServersDataFile();
+ $data = json_decode(file_get_contents($file));
+ self::printAuthorizedKeys($data);
+ }
}
diff --git a/tests/GetConfigurationTest.php b/tests/GetConfigurationTest.php
--- a/tests/GetConfigurationTest.php
+++ b/tests/GetConfigurationTest.php
@@ -4,39 +4,39 @@
class GetConfigurationTest extends PHPUnit_Framework_TestCase {
- public function testPrintConfiguration () {
- $this->expectOutputString('foo');
- $instance = new GetConfiguration();
- $instance->template = 'foo';
- $instance->printConfiguration();
- }
-
- public function testSubstituteTemplateVariables () {
- //Empty string
- $this->assertEquals('', GetConfiguration::substituteTemplateVariables(''));
-
- //Equivalent to an empty string
- $this->assertEquals('', GetConfiguration::substituteTemplateVariables(null));
- $this->assertEquals('', GetConfiguration::substituteTemplateVariables(false));
-
- //Nothing to substitute
- $this->assertEquals('foo', GetConfiguration::substituteTemplateVariables('foo'));
- }
-
- /**
- * @requires OS Linux|BSD|CYGWIN|Darwin
- */
- public function testSubstituteExec () {
- $this->assertEquals('', GetConfiguration::substituteExec(''));
-
- $template = '%%`echo -n ""`%%';
- $this->assertEquals('', GetConfiguration::substituteExec($template));
-
- $template = '%%`echo ""`%%';
- $this->assertEquals('', GetConfiguration::substituteExec($template));
-
- $template = 'File is %%`ls GetConfigurationTest.php`%%.';
- $this->assertEquals('File is GetConfigurationTest.php.', GetConfiguration::substituteExec($template));
- }
+ public function testPrintConfiguration () {
+ $this->expectOutputString('foo');
+ $instance = new GetConfiguration();
+ $instance->template = 'foo';
+ $instance->printConfiguration();
+ }
+
+ public function testSubstituteTemplateVariables () {
+ //Empty string
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(''));
+
+ //Equivalent to an empty string
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(null));
+ $this->assertEquals('', GetConfiguration::substituteTemplateVariables(false));
+
+ //Nothing to substitute
+ $this->assertEquals('foo', GetConfiguration::substituteTemplateVariables('foo'));
+ }
+
+ /**
+ * @requires OS Linux|BSD|CYGWIN|Darwin
+ */
+ public function testSubstituteExec () {
+ $this->assertEquals('', GetConfiguration::substituteExec(''));
+
+ $template = '%%`echo -n ""`%%';
+ $this->assertEquals('', GetConfiguration::substituteExec($template));
+
+ $template = '%%`echo ""`%%';
+ $this->assertEquals('', GetConfiguration::substituteExec($template));
+
+ $template = 'File is %%`ls GetConfigurationTest.php`%%.';
+ $this->assertEquals('File is GetConfigurationTest.php.', GetConfiguration::substituteExec($template));
+ }
}

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 13:28 (8 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264660
Default Alt Text
D1419.id3614.diff (23 KB)

Event Timeline