Fix user requirement in setup, cleanup.

This commit is contained in:
Niklas Keller
2015-12-21 00:01:25 +01:00
parent 5a4ab4a410
commit 37d054975c
6 changed files with 58 additions and 86 deletions

View File

@@ -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 = <<<EOT
____ __________ ___ ___
@@ -21,10 +32,7 @@ $help = <<<EOT
Usage: bin/acme command --args
Available Commands:
bin/acme setup
bin/acme issue
bin/acme revoke
bin/acme renew
{$help}
Get more help by appending --help to specific commands.
@@ -33,48 +41,44 @@ EOT;
$climate = new CLImate;
$injector = new Injector;
$commands = [
"setup",
"revoke",
"issue",
"renew",
];
if (PHP_SAPI !== "phpdbg" && PHP_SAPI !== "cli") {
$climate->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) {
if (count($argv) === 3 && in_array($argv[2], ["h", "-h", "--help", "help"], true)) {
$climate->usage(["bin/acme {$argv[1]}"]);
$climate->br();
if (count($argv) !== 3 || !in_array($argv[2], ["-h", "--help", "help"])) {
exit(0);
} else {
$climate->error($e->getMessage());
}
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();

View File

@@ -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"
]
}
}
}

View File

@@ -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) . " ...");

15
src/Config.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
namespace Kelunik\AcmeClient;
class Config {
private $config;
public function __construct(array $config) {
$this->config = $config;
}
public function get($key) {
return isset($this->config[$key]) ? $this->config[$key] : null;
}
}

View File

@@ -1,27 +0,0 @@
<?php
namespace Kelunik\AcmeClient;
use RuntimeException;
class Configuration {
private $config;
public function __construct($file) {
$json = file_get_contents($file);
if (!$json) {
throw new RuntimeException("Couldn't read config file: '{$file}'");
}
$this->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;
}
}

View File

@@ -1,16 +0,0 @@
<?php
namespace Kelunik\AcmeClient;
function commandToClass($command) {
return __NAMESPACE__ . "\\Commands\\" . ucfirst($command);
}
function getServer(Configuration $config = null) {
if ($config === null) {
$path = dirname(__DIR__) . "/data";
$config = new Configuration($path . "/account/config.json");
}
return $config->get("server");
}