Implement storage defaulting to the old one and required when using as PHAR

This commit is contained in:
Niklas Keller
2016-03-23 19:33:26 +01:00
parent 1a06a5cadf
commit 9ea97f18e1
6 changed files with 74 additions and 16 deletions

View File

@@ -26,7 +26,7 @@ class Check implements Command {
$server = \Kelunik\AcmeClient\resolveServer($args->get("server")); $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$server = \Kelunik\AcmeClient\serverToKeyname($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); $certificateStore = new CertificateStore($path);
$pem = (yield $certificateStore->get($args->get("name"))); $pem = (yield $certificateStore->get($args->get("name")));
@@ -35,20 +35,22 @@ class Check implements Command {
$this->climate->info("Certificate is valid until " . date("d.m.Y", $cert->getValidTo())); $this->climate->info("Certificate is valid until " . date("d.m.Y", $cert->getValidTo()));
if ($cert->getValidTo() > time() + $args->get("ttl") * 24 * 60 * 60) { if ($cert->getValidTo() > time() + $args->get("ttl") * 24 * 60 * 60) {
exit(0); return 0;
} }
$this->climate->comment("Certificate is going to expire within the specified " . $args->get("ttl") . " days."); $this->climate->comment("Certificate is going to expire within the specified " . $args->get("ttl") . " days.");
exit(1); return 1;
} }
public static function getDefinition() { public static function getDefinition() {
$isPhar = \Kelunik\AcmeClient\isPhar();
return [ return [
"server" => [ "server" => [
"prefix" => "s", "prefix" => "s",
"longPrefix" => "server", "longPrefix" => "server",
"description" => "", "description" => "ACME server to use for registration and issuance of certificates.",
"required" => true, "required" => true,
], ],
"name" => [ "name" => [
@@ -62,6 +64,12 @@ class Check implements Command {
"defaultValue" => 30, "defaultValue" => 30,
"castTo" => "int", "castTo" => "int",
], ],
"storage" => [
"longPrefix" => "storage",
"description" => "Storage directory for account keys and certificates.",
"required" => $isPhar,
"defaultValue" => $isPhar ? null : (__DIR__ . "/../../data")
]
]; ];
} }
} }

View File

@@ -63,7 +63,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")); $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$keyFile = \Kelunik\AcmeClient\serverToKeyname($server); $keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
@@ -71,9 +71,7 @@ class Issue implements Command {
try { try {
$keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem")); $keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem"));
} catch (KeyStoreException $e) { } catch (KeyStoreException $e) {
$this->climate->error("Account key not found, did you run 'bin/acme setup'?"); throw new AcmeException("Account key not found, did you run 'bin/acme setup'?", 0, $e);
exit(1);
} }
$acme = new AcmeService(new AcmeClient($server, $keyPair)); $acme = new AcmeService(new AcmeClient($server, $keyPair));
@@ -109,11 +107,13 @@ class Issue implements Command {
$location = (yield $acme->requestCertificate($keyPair, $domains)); $location = (yield $acme->requestCertificate($keyPair, $domains));
$certificates = (yield $acme->pollForCertificate($location)); $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); $certificateStore = new CertificateStore($path);
yield $certificateStore->put($certificates); yield $certificateStore->put($certificates);
$this->climate->info("Successfully issued certificate, see {$path}/" . reset($domains)); $this->climate->info("Successfully issued certificate, see {$path}/" . reset($domains));
return 0;
} }
private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain, $path) { private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain, $path) {
@@ -196,11 +196,13 @@ class Issue implements Command {
} }
public static function getDefinition() { public static function getDefinition() {
$isPhar = \Kelunik\AcmeClient\isPhar();
return [ return [
"server" => [ "server" => [
"prefix" => "s", "prefix" => "s",
"longPrefix" => "server", "longPrefix" => "server",
"description" => "Server to use for issuance, see also 'bin/acme setup'.", "description" => "ACME server to use for registration and issuance of certificates.",
"required" => true, "required" => true,
], ],
"domains" => [ "domains" => [
@@ -226,6 +228,12 @@ class Issue implements Command {
"defaultValue" => 2048, "defaultValue" => 2048,
"castTo" => "int", "castTo" => "int",
], ],
"storage" => [
"longPrefix" => "storage",
"description" => "Storage directory for account keys and certificates.",
"required" => $isPhar,
"defaultValue" => $isPhar ? null : (__DIR__ . "/../../data")
]
]; ];
} }
} }

View File

@@ -23,7 +23,7 @@ class Revoke implements Command {
} }
private function doExecute(Manager $args) { 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")); $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
$keyFile = \Kelunik\AcmeClient\serverToKeyname($server); $keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
@@ -33,7 +33,7 @@ class Revoke implements Command {
$this->climate->info("Revoking certificate ..."); $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 { try {
$pem = (yield \Amp\File\get($path)); $pem = (yield \Amp\File\get($path));
@@ -50,15 +50,19 @@ class Revoke implements Command {
yield $acme->revokeCertificate($pem); yield $acme->revokeCertificate($pem);
$this->climate->info("Certificate has been revoked."); $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"));
return 0;
} }
public static function getDefinition() { public static function getDefinition() {
$isPhar = \Kelunik\AcmeClient\isPhar();
return [ return [
"server" => [ "server" => [
"prefix" => "s", "prefix" => "s",
"longPrefix" => "server", "longPrefix" => "server",
"description" => "", "description" => "ACME server to use for registration and issuance of certificates.",
"required" => true, "required" => true,
], ],
"name" => [ "name" => [
@@ -66,6 +70,12 @@ class Revoke implements Command {
"description" => "Common name of the certificate to be revoked.", "description" => "Common name of the certificate to be revoked.",
"required" => true, "required" => true,
], ],
"storage" => [
"longPrefix" => "storage",
"description" => "Storage directory for account keys and certificates.",
"required" => $isPhar,
"defaultValue" => $isPhar ? null : (__DIR__ . "/../../data")
]
]; ];
} }
} }

View File

@@ -36,7 +36,7 @@ class Setup implements Command {
$path = "accounts/{$keyFile}.pem"; $path = "accounts/{$keyFile}.pem";
$bits = 4096; $bits = 4096;
$keyStore = new KeyStore(dirname(dirname(__DIR__)) . "/data"); $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/data");
try { try {
$keyPair = (yield $keyStore->get($path)); $keyPair = (yield $keyStore->get($path));
@@ -78,6 +78,8 @@ class Setup implements Command {
} }
public static function getDefinition() { public static function getDefinition() {
$isPhar = \Kelunik\AcmeClient\isPhar();
return [ return [
"server" => [ "server" => [
"prefix" => "s", "prefix" => "s",
@@ -87,9 +89,15 @@ class Setup implements Command {
], ],
"email" => [ "email" => [
"longPrefix" => "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, "required" => true,
], ],
"storage" => [
"longPrefix" => "storage",
"description" => "Storage directory for account keys and certificates.",
"required" => $isPhar,
"defaultValue" => $isPhar ? null : (__DIR__ . "/../../data")
]
]; ];
} }
} }

View File

@@ -2,6 +2,7 @@
namespace Kelunik\AcmeClient; namespace Kelunik\AcmeClient;
use Phar;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
function suggestCommand($badCommand, array $commands, $suggestThreshold = 70) { function suggestCommand($badCommand, array $commands, $suggestThreshold = 70) {
@@ -57,3 +58,15 @@ function serverToKeyname($server) {
return $keyFile; return $keyFile;
} }
function isPhar() {
if (!class_exists("Phar")) {
return false;
}
return Phar::running(true) !== "";
}
function normalizePath($path) {
return rtrim(str_replace("\\", "/", $path), "/");
}

View File

@@ -15,4 +15,15 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
$this->assertSame("acme", suggestCommand("acme!", ["acme"])); $this->assertSame("acme", suggestCommand("acme!", ["acme"]));
$this->assertSame("", suggestCommand("issue", ["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\\"));
}
} }