Merge pull request #18 from kelunik/issue-17

Provide possibility to specify storage directory
This commit is contained in:
Niklas Keller
2016-03-23 23:39:27 +01:00
9 changed files with 101 additions and 46 deletions

View File

@@ -99,7 +99,7 @@ bin/acme issue -s letsencrypt -d example.com:www.example.com -p /var/www/example
To revoke a certificate, you need a valid account key currently, just like for issuance.
```
bin/acme revoke --name example.com
bin/acme revoke --name example.com -s letsencrypt
```
For renewal, there's the `bin/acme check` subcommand.

View File

@@ -125,7 +125,13 @@ Amp\run(function () use ($command, $climate) {
};
try {
yield $command->execute($climate->arguments);
$exitCode = (yield $command->execute($climate->arguments));
if ($exitCode === null) {
$logger->warning("Invalid exit code: null, falling back to 0. Please consider reporting this as bug.");
}
exit($exitCode);
} catch (Throwable $e) {
$handler($e);
} catch (Exception $e) {

View File

@@ -23,7 +23,7 @@
"require-dev": {
"phpunit/phpunit": "^5",
"fabpot/php-cs-fixer": "^1.9",
"macfja/phar-builder": "^0.2.3"
"macfja/phar-builder": "dev-master#bf48160bd6bdf702557ff213e6837710c1dbf572"
},
"license": "MIT",
"authors": [
@@ -45,9 +45,9 @@
"extra": {
"phar-builder": {
"compression": "GZip",
"name": "acme.phar",
"name": "acme-client.phar",
"output-dir": "build",
"include": ["bin", "src", "vendor"],
"include": ["src", "vendor/kelunik/acme/res"],
"entry-point": "bin/acme"
}
}

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient\Commands;
use Amp\CoroutineResult;
use Kelunik\AcmeClient\Stores\CertificateStore;
use Kelunik\Certificate\Certificate;
use League\CLImate\Argument\Manager;
@@ -26,7 +27,7 @@ class Check implements Command {
$server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$server = \Kelunik\AcmeClient\serverToKeyname($server);
$path = dirname(dirname(__DIR__)) . "/data/certs/" . $server;
$path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $server;
$certificateStore = new CertificateStore($path);
$pem = (yield $certificateStore->get($args->get("name")));
@@ -35,22 +36,19 @@ class Check implements Command {
$this->climate->info("Certificate is valid until " . date("d.m.Y", $cert->getValidTo()));
if ($cert->getValidTo() > time() + $args->get("ttl") * 24 * 60 * 60) {
exit(0);
yield new CoroutineResult(0);
return;
}
$this->climate->comment("Certificate is going to expire within the specified " . $args->get("ttl") . " days.");
exit(1);
yield new CoroutineResult(1);
}
public static function getDefinition() {
return [
"server" => [
"prefix" => "s",
"longPrefix" => "server",
"description" => "",
"required" => true,
],
"server" => \Kelunik\AcmeClient\getArgumentDescription("server"),
"storage" => \Kelunik\AcmeClient\getArgumentDescription("storage"),
"name" => [
"longPrefix" => "name",
"description" => "Common name of the certificate to check.",

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient\Commands;
use Amp\CoroutineResult;
use Amp\Dns\Record;
use Exception;
use Kelunik\Acme\AcmeClient;
@@ -44,10 +45,10 @@ class Issue implements Command {
}
}
$domains = array_map("trim", explode(":", str_replace(",", ":", $args->get("domains"))));
$domains = array_map("trim", explode(":", str_replace([",", ";"], ":", $args->get("domains"))));
yield \Amp\resolve($this->checkDnsRecords($domains));
$docRoots = explode(":", str_replace("\\", "/", $args->get("path")));
$docRoots = explode(PATH_SEPARATOR, str_replace("\\", "/", $args->get("path")));
$docRoots = array_map(function ($root) {
return rtrim($root, "/");
}, $docRoots);
@@ -63,7 +64,7 @@ class Issue implements Command {
);
}
$keyStore = new KeyStore(dirname(dirname(__DIR__)) . "/data");
$keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
$server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
@@ -71,9 +72,7 @@ class Issue implements Command {
try {
$keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem"));
} catch (KeyStoreException $e) {
$this->climate->error("Account key not found, did you run 'bin/acme setup'?");
exit(1);
throw new AcmeException("Account key not found, did you run 'bin/acme setup'?", 0, $e);
}
$acme = new AcmeService(new AcmeClient($server, $keyPair));
@@ -109,11 +108,13 @@ class Issue implements Command {
$location = (yield $acme->requestCertificate($keyPair, $domains));
$certificates = (yield $acme->pollForCertificate($location));
$path = dirname(dirname(__DIR__)) . "/data/certs/" . $keyFile;
$path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile;
$certificateStore = new CertificateStore($path);
yield $certificateStore->put($certificates);
$this->climate->info("Successfully issued certificate, see {$path}/" . reset($domains));
yield new CoroutineResult(0);
}
private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain, $path) {
@@ -197,22 +198,18 @@ class Issue implements Command {
public static function getDefinition() {
return [
"server" => [
"prefix" => "s",
"longPrefix" => "server",
"description" => "Server to use for issuance, see also 'bin/acme setup'.",
"required" => true,
],
"server" => \Kelunik\AcmeClient\getArgumentDescription("server"),
"storage" => \Kelunik\AcmeClient\getArgumentDescription("storage"),
"domains" => [
"prefix" => "d",
"longPrefix" => "domains",
"description" => "Colon separated list of domains to request a certificate for.",
"description" => "Colon / Semicolon / Comma separated list of domains to request a certificate for.",
"required" => true,
],
"path" => [
"prefix" => "p",
"longPrefix" => "path",
"description" => "Colon separated list of paths to the document roots. The last one will be used for all remaining ones if fewer than the amount of domains is given.",
"description" => "Colon (Unix) / Semicolon (Windows) separated list of paths to the document roots. The last one will be used for all remaining ones if fewer than the amount of domains is given.",
"required" => true,
],
"user" => [

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient\Commands;
use Amp\CoroutineResult;
use Amp\File\FilesystemException;
use Kelunik\Acme\AcmeClient;
use Kelunik\Acme\AcmeService;
@@ -23,7 +24,7 @@ class Revoke implements Command {
}
private function doExecute(Manager $args) {
$keyStore = new KeyStore(dirname(dirname(__DIR__)) . "/data");
$keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
$server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
@@ -33,7 +34,7 @@ class Revoke implements Command {
$this->climate->info("Revoking certificate ...");
$path = dirname(dirname(__DIR__)) . "/data/certs/" . $keyFile . "/" . $args->get("name") . "/cert.pem";
$path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile . "/" . $args->get("name") . "/cert.pem";
try {
$pem = (yield \Amp\File\get($path));
@@ -50,17 +51,15 @@ class Revoke implements Command {
yield $acme->revokeCertificate($pem);
$this->climate->info("Certificate has been revoked.");
yield (new CertificateStore(dirname(dirname(__DIR__)) . "/data/certs/" . $keyFile))->delete($args->get("name"));
yield (new CertificateStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")). "/certs/" . $keyFile))->delete($args->get("name"));
yield new CoroutineResult(0);
}
public static function getDefinition() {
return [
"server" => [
"prefix" => "s",
"longPrefix" => "server",
"description" => "",
"required" => true,
],
"server" => \Kelunik\AcmeClient\getArgumentDescription("server"),
"storage" => \Kelunik\AcmeClient\getArgumentDescription("storage"),
"name" => [
"longPrefix" => "name",
"description" => "Common name of the certificate to be revoked.",

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient\Commands;
use Amp\CoroutineResult;
use Amp\Dns\Record;
use Amp\Dns\ResolutionException;
use InvalidArgumentException;
@@ -36,7 +37,7 @@ class Setup implements Command {
$path = "accounts/{$keyFile}.pem";
$bits = 4096;
$keyStore = new KeyStore(dirname(dirname(__DIR__)) . "/data");
$keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
try {
$keyPair = (yield $keyStore->get($path));
@@ -57,6 +58,8 @@ class Setup implements Command {
/** @var Registration $registration */
$registration = (yield $acme->register($email));
$this->climate->whisper("Registration successful with the following contact information: " . implode(", ", $registration->getContact()));
yield new CoroutineResult(0);
}
private function checkEmail($email) {
@@ -78,16 +81,14 @@ class Setup implements Command {
}
public static function getDefinition() {
return [
"server" => [
"prefix" => "s",
"longPrefix" => "server",
"description" => "ACME server to use for registration and issuance of certificates.",
"required" => true,
],
"server" => \Kelunik\AcmeClient\getArgumentDescription("server"),
"storage" => \Kelunik\AcmeClient\getArgumentDescription("storage"),
"email" => [
"longPrefix" => "email",
"description" => "Email for important issues, will be sent to the ACME server.",
"description" => "E-mail for important issues, will be sent to the ACME server.",
"required" => true,
],
];

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient;
use Phar;
use Webmozart\Assert\Assert;
function suggestCommand($badCommand, array $commands, $suggestThreshold = 70) {
@@ -56,4 +57,46 @@ function serverToKeyname($server) {
$keyFile = preg_replace("@\\.+@", ".", $keyFile);
return $keyFile;
}
function isPhar() {
if (!class_exists("Phar")) {
return false;
}
return Phar::running(true) !== "";
}
function normalizePath($path) {
return rtrim(str_replace("\\", "/", $path), "/");
}
function getArgumentDescription($argument) {
$isPhar = \Kelunik\AcmeClient\isPhar();
switch ($argument) {
case "server":
return [
"prefix" => "s",
"longPrefix" => "server",
"description" => "ACME server to use for registration and issuance of certificates.",
"required" => true,
];
case "storage":
$argument = [
"longPrefix" => "storage",
"description" => "Storage directory for account keys and certificates.",
"required" => $isPhar,
];
if (!$isPhar) {
$argument["defaultValue"] = dirname(__DIR__) . "/data";
}
return $argument;
default:
throw new \InvalidArgumentException("Unknown argument: " . $argument);
}
}

View File

@@ -15,4 +15,15 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
$this->assertSame("acme", suggestCommand("acme!", ["acme"]));
$this->assertSame("", suggestCommand("issue", ["acme"]));
}
public function testIsPhar() {
$this->assertFalse(isPhar());
}
public function testNormalizePath() {
$this->assertSame("/etc/foobar", normalizePath("/etc/foobar"));
$this->assertSame("/etc/foobar", normalizePath("/etc/foobar/"));
$this->assertSame("/etc/foobar", normalizePath("/etc/foobar/"));
$this->assertSame("C:/etc/foobar", normalizePath("C:\\etc\\foobar\\"));
}
}