From d28d496264c105b80878f3567994adbc38b9684a Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Fri, 23 Feb 2018 11:27:33 +0700 Subject: [PATCH] Added options 'php-censor.ssh.strength' and 'php-censor.ssh.comment' for SSH keys generation. Issue #154. --- docs/en/configuring.md | 3 +++ src/PHPCensor/Command/InstallCommand.php | 4 ++++ src/PHPCensor/Controller/ProjectController.php | 6 ++---- src/PHPCensor/Helper/SshKey.php | 15 ++++++++++++++- src/PHPCensor/Plugin/PhpCodeSniffer.php | 14 +++++++------- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs/en/configuring.md b/docs/en/configuring.md index f95eeeb0..85833b44 100644 --- a/docs/en/configuring.md +++ b/docs/en/configuring.md @@ -39,6 +39,9 @@ php-censor: log: rotate: true max_files: 10 + ssh: + strength: 4096 # SSH keys strength (default: 2048) + comment: admin@php-censor.info # SSH keys comment (default: admin@php-censor) bitbucket: username: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx app_password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index 1f3b86e5..70a8bc15 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -265,6 +265,10 @@ class InstallCommand extends Command 'smtp_password' => null, 'smtp_encryption' => false, ], + 'ssh' => [ + 'strength' => 2048, + 'comment' => 'admin@php-censor', + ], 'bitbucket' => [ 'username' => null, 'app_password' => null, diff --git a/src/PHPCensor/Controller/ProjectController.php b/src/PHPCensor/Controller/ProjectController.php index 7bb5a75f..99f038a9 100644 --- a/src/PHPCensor/Controller/ProjectController.php +++ b/src/PHPCensor/Controller/ProjectController.php @@ -297,17 +297,15 @@ class ProjectController extends PHPCensor\Controller $this->requireAdmin(); $method = $this->request->getMethod(); - $pub = null; $values = $this->getParams(); $values['branch'] = ''; - if ($method != 'POST') { + if ($method !== 'POST') { $sshKey = new SshKey(); $key = $sshKey->generate(); $values['key'] = $key['private_key']; $values['pubkey'] = $key['public_key']; - $pub = $key['public_key']; } $form = $this->projectForm($values); @@ -317,7 +315,7 @@ class ProjectController extends PHPCensor\Controller $view->type = 'add'; $view->project = null; $view->form = $form; - $view->key = $pub; + $view->key = $values['pubkey']; return $view->render(); } else { diff --git a/src/PHPCensor/Helper/SshKey.php b/src/PHPCensor/Helper/SshKey.php index 86e30cce..b532f181 100644 --- a/src/PHPCensor/Helper/SshKey.php +++ b/src/PHPCensor/Helper/SshKey.php @@ -2,6 +2,8 @@ namespace PHPCensor\Helper; +use b8\Config; + /** * Helper class for dealing with SSH keys. */ @@ -9,6 +11,7 @@ class SshKey { /** * Uses ssh-keygen to generate a public/private key pair. + * * @return array */ public function generate() @@ -22,7 +25,17 @@ class SshKey $return = ['private_key' => '', 'public_key' => '']; - $output = @shell_exec('ssh-keygen -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@php-censor"'); + $sshStrength = Config::getInstance()->get('php-censor.ssh.strength', 2048); + $sshComment = Config::getInstance()->get('php-censor.ssh.comment', 'admin@php-censor'); + + $output = @shell_exec( + sprintf( + 'ssh-keygen -t rsa -b %s -f %s -N "" -C "%s"', + $sshStrength, + $keyFile, + $sshComment + ) + ); if (!empty($output)) { $pub = file_get_contents($keyFile . '.pub'); diff --git a/src/PHPCensor/Plugin/PhpCodeSniffer.php b/src/PHPCensor/Plugin/PhpCodeSniffer.php index 1e565182..a5e8cce5 100644 --- a/src/PHPCensor/Plugin/PhpCodeSniffer.php +++ b/src/PHPCensor/Plugin/PhpCodeSniffer.php @@ -93,12 +93,12 @@ class PhpCodeSniffer extends Plugin implements ZeroConfigPluginInterface $this->suffixes = ['php']; $this->directory = $this->builder->buildPath; $this->standard = 'PSR2'; - $this->tabWidth = ''; + $this->tabWidth = ''; $this->encoding = ''; $this->path = ''; $this->ignore = $this->builder->ignore; - $this->allowedWarnings = 0; - $this->allowedErrors = 0; + $this->allowedWarnings = 0; + $this->allowedErrors = 0; if (isset($options['zero_config']) && $options['zero_config']) { $this->allowedWarnings = -1; @@ -133,6 +133,10 @@ class PhpCodeSniffer extends Plugin implements ZeroConfigPluginInterface $this->standard = $options['standard']; } + if (!empty($options['path'])) { + $this->path = $options['path']; + } + if (isset($options['severity']) && is_int($options['severity'])) { $this->severity = $options['severity']; } @@ -144,10 +148,6 @@ class PhpCodeSniffer extends Plugin implements ZeroConfigPluginInterface if (isset($options['warning_severity']) && is_int($options['warning_severity'])) { $this->warningSeverity = $options['warning_severity']; } - - if (isset($options['path'])) { - $this->path = $options['path']; - } } /**