From 37d054975c364d6388f8b9b68ea1da6ed88e8d5a Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Mon, 21 Dec 2015 00:01:25 +0100 Subject: [PATCH] Fix user requirement in setup, cleanup. --- bin/acme | 66 ++++++++++++++++++++++++------------------ composer.json | 13 ++++----- src/Commands/Setup.php | 7 ----- src/Config.php | 15 ++++++++++ src/Configuration.php | 27 ----------------- src/functions.php | 16 ---------- 6 files changed, 58 insertions(+), 86 deletions(-) create mode 100644 src/Config.php delete mode 100644 src/Configuration.php delete mode 100644 src/functions.php diff --git a/bin/acme b/bin/acme index 9662ea4..5c7cd30 100755 --- a/bin/acme +++ b/bin/acme @@ -11,6 +11,17 @@ use Psr\Log\LoggerInterface; require __DIR__ . "/../vendor/autoload.php"; +$commands = [ + "setup", + "issue", + "renew", + "revoke", +]; + +$help = implode("\n ", array_map(function($command) { + return "bin/acme {$command}"; +}, $commands)); + $help = <<error("Please run this script as command line script!"); +if (!in_array(PHP_SAPI, ["cli", "phpdbg"], true)) { + $climate->error("Please run this script via CLI!"); exit(1); } -if (count($argv) === 1 || $argv[1] === "-h" || $argv[1] === "--help" || $argv[1] === "help") { +if (count($argv) === 1 || in_array($argv[1], ["h", "-h", "help", "--help"], true)) { print $help; exit(0); } if (!in_array($argv[1], $commands)) { - $climate->error("Unknown command: '{$argv[1]}'"); - + $climate->error("Unknown command '{$argv[1]}', use --help for a list of available commands."); exit(1); } -$class = \Kelunik\AcmeClient\commandToClass($argv[1]); +/** @var \Kelunik\AcmeClient\Commands\Command $class */ +$class = "Kelunik\\AcmeClient\\Commands\\" . ucfirst($argv[1]); $definition = $class::getDefinition(); try { + $args = $argv; + unset($args[1]); + $climate->arguments->add($definition); - $climate->arguments->parse(); + $climate->arguments->parse(array_values($args)); } catch (Exception $e) { - $climate->usage(["bin/acme {$argv[1]}"]); - $climate->br(); - - if (count($argv) !== 3 || !in_array($argv[2], ["-h", "--help", "help"])) { + if (count($argv) === 3 && in_array($argv[2], ["h", "-h", "--help", "help"], true)) { + $climate->usage(["bin/acme {$argv[1]}"]); + exit(0); + } else { $climate->error($e->getMessage()); + exit(1); } - - exit(1); } -if (posix_geteuid() !== 0) { - $climate->error("Please run this script as root!"); +if (posix_geteuid() !== 0) { // TODO: Windows? + $climate->error("Please run this script as root."); exit(1); } @@ -91,9 +95,7 @@ $injector->share($logger); $command = $injector->make($class); Amp\run(function () use ($command, $climate, $logger) { - try { - yield $command->execute($climate->arguments); - } catch (Throwable $e) { + $handler = function($e) use ($logger) { $error = (string) $e; $lines = explode("\n", $error); $lines = array_filter($lines, function ($line) { @@ -105,6 +107,14 @@ Amp\run(function () use ($command, $climate, $logger) { } exit(1); + }; + + try { + yield $command->execute($climate->arguments); + } catch (Throwable $e) { + $handler($e); + } catch (Exception $e) { + $handler($e); } Amp\stop(); diff --git a/composer.json b/composer.json index f56ccca..776396d 100644 --- a/composer.json +++ b/composer.json @@ -2,17 +2,17 @@ "name": "kelunik/acme-client", "description": "Standalone PHP ACME client.", "require": { - "php": ">=7.0.0", + "amphp/process": "^0.1.1", + "bramus/monolog-colored-line-formatter": "^2", "ext-posix": "*", "ext-openssl": "*", - "bramus/monolog-colored-line-formatter": "^2", "kelunik/acme": "^0.2", "kelunik/certificate": "^0.2", "league/climate": "^3", "monolog/monolog": "^1.17", + "php": ">=5.5", "psr/log": "^1", - "rdlowrey/auryn": "^1", - "amphp/process": "^0.1.1" + "rdlowrey/auryn": "^1" }, "license": "MIT", "authors": [ @@ -26,9 +26,6 @@ "autoload": { "psr-4": { "Kelunik\\AcmeClient\\": "src" - }, - "files": [ - "src/functions.php" - ] + } } } diff --git a/src/Commands/Setup.php b/src/Commands/Setup.php index 283c56f..038deb5 100644 --- a/src/Commands/Setup.php +++ b/src/Commands/Setup.php @@ -61,13 +61,6 @@ class Setup implements Command { $this->logger->info("New private key successfully saved."); } - $user = $args->get("user") ?: "www-data"; - $userInfo = posix_getpwnam($user); - - if (!$userInfo) { - throw new RuntimeException("User doesn't exist: '{$user}'"); - } - $acme = new AcmeService(new AcmeClient($server, $keyPair), $keyPair); $this->logger->info("Registering with ACME server " . substr($server, 8) . " ..."); diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..112bc92 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,15 @@ +config = $config; + } + + public function get($key) { + return isset($this->config[$key]) ? $this->config[$key] : null; + } +} \ No newline at end of file diff --git a/src/Configuration.php b/src/Configuration.php deleted file mode 100644 index 9c4d075..0000000 --- a/src/Configuration.php +++ /dev/null @@ -1,27 +0,0 @@ -config = json_decode($json); - - if (!$this->config) { - throw new RuntimeException("Couldn't read JSON: '{$json}'"); - } - } - - public function get($key) { - return isset($this->config->{$key}) ? $this->config->{$key} : null; - } -} \ No newline at end of file diff --git a/src/functions.php b/src/functions.php deleted file mode 100644 index 656ac31..0000000 --- a/src/functions.php +++ /dev/null @@ -1,16 +0,0 @@ -get("server"); -} \ No newline at end of file