diff --git a/src/Commands/Check.php b/src/Commands/Check.php index d65f695..0c910ef 100644 --- a/src/Commands/Check.php +++ b/src/Commands/Check.php @@ -26,7 +26,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,20 +35,22 @@ 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); + return 0; } $this->climate->comment("Certificate is going to expire within the specified " . $args->get("ttl") . " days."); - exit(1); + return 1; } public static function getDefinition() { + $isPhar = \Kelunik\AcmeClient\isPhar(); + return [ "server" => [ "prefix" => "s", "longPrefix" => "server", - "description" => "", + "description" => "ACME server to use for registration and issuance of certificates.", "required" => true, ], "name" => [ @@ -62,6 +64,12 @@ class Check implements Command { "defaultValue" => 30, "castTo" => "int", ], + "storage" => [ + "longPrefix" => "storage", + "description" => "Storage directory for account keys and certificates.", + "required" => $isPhar, + "defaultValue" => $isPhar ? null : (__DIR__ . "/../../data") + ] ]; } } \ No newline at end of file diff --git a/src/Commands/Issue.php b/src/Commands/Issue.php index ae134f2..cf1e30e 100644 --- a/src/Commands/Issue.php +++ b/src/Commands/Issue.php @@ -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")); $keyFile = \Kelunik\AcmeClient\serverToKeyname($server); @@ -71,9 +71,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 +107,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)); + + return 0; } private function solveChallenge(AcmeService $acme, KeyPair $keyPair, $domain, $path) { @@ -196,11 +196,13 @@ class Issue implements Command { } public static function getDefinition() { + $isPhar = \Kelunik\AcmeClient\isPhar(); + return [ "server" => [ "prefix" => "s", "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, ], "domains" => [ @@ -226,6 +228,12 @@ class Issue implements Command { "defaultValue" => 2048, "castTo" => "int", ], + "storage" => [ + "longPrefix" => "storage", + "description" => "Storage directory for account keys and certificates.", + "required" => $isPhar, + "defaultValue" => $isPhar ? null : (__DIR__ . "/../../data") + ] ]; } } \ No newline at end of file diff --git a/src/Commands/Revoke.php b/src/Commands/Revoke.php index b537beb..602f47f 100644 --- a/src/Commands/Revoke.php +++ b/src/Commands/Revoke.php @@ -23,7 +23,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 +33,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,15 +50,19 @@ 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")); + + return 0; } public static function getDefinition() { + $isPhar = \Kelunik\AcmeClient\isPhar(); + return [ "server" => [ "prefix" => "s", "longPrefix" => "server", - "description" => "", + "description" => "ACME server to use for registration and issuance of certificates.", "required" => true, ], "name" => [ @@ -66,6 +70,12 @@ class Revoke implements Command { "description" => "Common name of the certificate to be revoked.", "required" => true, ], + "storage" => [ + "longPrefix" => "storage", + "description" => "Storage directory for account keys and certificates.", + "required" => $isPhar, + "defaultValue" => $isPhar ? null : (__DIR__ . "/../../data") + ] ]; } } \ No newline at end of file diff --git a/src/Commands/Setup.php b/src/Commands/Setup.php index 843c737..e9efc27 100644 --- a/src/Commands/Setup.php +++ b/src/Commands/Setup.php @@ -36,7 +36,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")) . "/data"); try { $keyPair = (yield $keyStore->get($path)); @@ -78,6 +78,8 @@ class Setup implements Command { } public static function getDefinition() { + $isPhar = \Kelunik\AcmeClient\isPhar(); + return [ "server" => [ "prefix" => "s", @@ -87,9 +89,15 @@ class Setup implements Command { ], "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, ], + "storage" => [ + "longPrefix" => "storage", + "description" => "Storage directory for account keys and certificates.", + "required" => $isPhar, + "defaultValue" => $isPhar ? null : (__DIR__ . "/../../data") + ] ]; } } \ No newline at end of file diff --git a/src/functions.php b/src/functions.php index 4f566ce..9592d0b 100644 --- a/src/functions.php +++ b/src/functions.php @@ -2,6 +2,7 @@ namespace Kelunik\AcmeClient; +use Phar; use Webmozart\Assert\Assert; function suggestCommand($badCommand, array $commands, $suggestThreshold = 70) { @@ -56,4 +57,16 @@ 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), "/"); } \ No newline at end of file diff --git a/test/FunctionsTest.php b/test/FunctionsTest.php index f48c771..4013eea 100644 --- a/test/FunctionsTest.php +++ b/test/FunctionsTest.php @@ -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\\")); + } } \ No newline at end of file