Exemplo n.º 1
0
 /**
  * @param Manager $args
  * @return \Generator
  */
 private function doExecute(Manager $args)
 {
     $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
     $server = \Kelunik\AcmeClient\serverToKeyname($server);
     $path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $server;
     $certificateStore = new CertificateStore($path);
     try {
         $pem = (yield $certificateStore->get($args->get("name")));
     } catch (CertificateStoreException $e) {
         $this->climate->br()->error("    Certificate not found.")->br();
         (yield new CoroutineResult(1));
         return;
     }
     $cert = new Certificate($pem);
     $this->climate->br();
     $this->climate->whisper("    Certificate is valid until " . date("d.m.Y", $cert->getValidTo()))->br();
     if ($args->defined("names")) {
         $names = array_map("trim", explode(",", $args->get("names")));
         $missingNames = array_diff($names, $cert->getNames());
         if ($missingNames) {
             $this->climate->comment("    The following names are not covered: " . implode(", ", $missingNames))->br();
             (yield new CoroutineResult(1));
             return;
         }
     }
     if ($cert->getValidTo() > time() + $args->get("ttl") * 24 * 60 * 60) {
         (yield new CoroutineResult(0));
         return;
     }
     $this->climate->comment("    Certificate is going to expire within the specified " . $args->get("ttl") . " days.")->br();
     (yield new CoroutineResult(1));
 }
Exemplo n.º 2
0
 private function doExecute(Manager $args)
 {
     $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
     $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
     $keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
     $keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem"));
     $acme = $this->acmeFactory->build($server, $keyPair);
     $this->climate->br();
     $this->climate->whisper("    Revoking certificate ...");
     $path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile . "/" . $args->get("name") . "/cert.pem";
     try {
         $pem = (yield \Amp\File\get($path));
         $cert = new Certificate($pem);
     } catch (FilesystemException $e) {
         throw new \RuntimeException("There's no such certificate (" . $path . ")");
     }
     if ($cert->getValidTo() < time()) {
         $this->climate->comment("    Certificate did already expire, no need to revoke it.");
     }
     $names = $cert->getNames();
     $this->climate->whisper("    Certificate was valid for " . count($names) . " domains.");
     $this->climate->whisper("     - " . implode(PHP_EOL . "     - ", $names) . PHP_EOL);
     (yield $acme->revokeCertificate($pem));
     $this->climate->br();
     $this->climate->info("    Certificate has been revoked.");
     (yield (new CertificateStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile))->delete($args->get("name")));
     (yield new CoroutineResult(0));
 }
Exemplo n.º 3
0
 public function doExecute(Manager $args)
 {
     $email = $args->get("email");
     (yield \Amp\resolve($this->checkEmail($email)));
     $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
     $keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
     $path = "accounts/{$keyFile}.pem";
     $bits = 4096;
     $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
     $this->climate->br();
     try {
         $keyPair = (yield $keyStore->get($path));
         $this->climate->whisper("    Using existing private key ...");
     } catch (KeyStoreException $e) {
         $this->climate->whisper("    No private key found, generating new one ...");
         $keyPair = (new OpenSSLKeyGenerator())->generate($bits);
         $keyPair = (yield $keyStore->put($path, $keyPair));
         $this->climate->whisper("    Generated new private key with {$bits} bits.");
     }
     $acme = $this->acmeFactory->build($server, $keyPair);
     $this->climate->whisper("    Registering with " . substr($server, 8) . " ...");
     /** @var Registration $registration */
     $registration = (yield $acme->register($email));
     $this->climate->info("    Registration successful. Contacts: " . implode(", ", $registration->getContact()));
     $this->climate->br();
     (yield new CoroutineResult(0));
 }
Exemplo n.º 4
0
 /**
  * @param Manager $args
  * @return \Generator
  */
 private function doExecute(Manager $args)
 {
     $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
     $keyName = \Kelunik\AcmeClient\serverToKeyname($server);
     $storage = \Kelunik\AcmeClient\normalizePath($args->get("storage"));
     try {
         $keyStore = new KeyStore($storage);
         (yield $keyStore->get("accounts/{$keyName}.pem"));
         $setup = true;
     } catch (KeyStoreException $e) {
         $setup = false;
     }
     $this->climate->br();
     $this->climate->out("  [" . ($setup ? "<green> ✓ </green>" : "<red> ✗ </red>") . "] " . ($setup ? "Registered on " : "Not yet registered on ") . $server);
     $this->climate->br();
     if ((yield \Amp\File\exists($storage . "/certs/{$keyName}"))) {
         $certificateStore = new CertificateStore($storage . "/certs/{$keyName}");
         $domains = (yield \Amp\File\scandir($storage . "/certs/{$keyName}"));
         foreach ($domains as $domain) {
             $pem = (yield $certificateStore->get($domain));
             $cert = new Certificate($pem);
             $symbol = time() > $cert->getValidTo() ? "<red> ✗ </red>" : "<green> ✓ </green>";
             if (time() < $cert->getValidTo() && time() + $args->get("ttl") * 24 * 60 * 60 > $cert->getValidTo()) {
                 $symbol = "<yellow> ⭮ </yellow>";
             }
             $this->climate->out("  [" . $symbol . "] " . implode(", ", $cert->getNames()));
         }
         $this->climate->br();
     }
 }
Exemplo n.º 5
0
 private function doExecute(Manager $args)
 {
     if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
         if (posix_geteuid() !== 0) {
             $processUser = posix_getpwnam(posix_geteuid());
             $currentUsername = $processUser["name"];
             $user = $args->get("user") ?: $currentUsername;
             if ($currentUsername !== $user) {
                 throw new AcmeException("Running this script with --user only works as root!");
             }
         } else {
             $user = $args->get("user") ?: "www-data";
         }
     }
     $domains = array_map("trim", explode(":", str_replace([",", ";"], ":", $args->get("domains"))));
     (yield \Amp\resolve($this->checkDnsRecords($domains)));
     $docRoots = explode(PATH_SEPARATOR, str_replace("\\", "/", $args->get("path")));
     $docRoots = array_map(function ($root) {
         return rtrim($root, "/");
     }, $docRoots);
     if (count($domains) < count($docRoots)) {
         throw new AcmeException("Specified more document roots than domains.");
     }
     if (count($domains) > count($docRoots)) {
         $docRoots = array_merge($docRoots, array_fill(count($docRoots), count($domains) - count($docRoots), end($docRoots)));
     }
     $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage")));
     $server = \Kelunik\AcmeClient\resolveServer($args->get("server"));
     $keyFile = \Kelunik\AcmeClient\serverToKeyname($server);
     try {
         $keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem"));
     } catch (KeyStoreException $e) {
         throw new AcmeException("Account key not found, did you run 'bin/acme setup'?", 0, $e);
     }
     $this->climate->br();
     $acme = $this->acmeFactory->build($server, $keyPair);
     $errors = [];
     $domainChunks = array_chunk($domains, 10, true);
     foreach ($domainChunks as $domainChunk) {
         $promises = [];
         foreach ($domainChunk as $i => $domain) {
             $promises[] = \Amp\resolve($this->solveChallenge($acme, $keyPair, $domain, $docRoots[$i]));
         }
         list($chunkErrors) = (yield \Amp\any($promises));
         $errors += $chunkErrors;
     }
     if (!empty($errors)) {
         foreach ($errors as $error) {
             $this->climate->error($error->getMessage());
         }
         throw new AcmeException("Issuance failed, not all challenges could be solved.");
     }
     $path = "certs/" . $keyFile . "/" . reset($domains) . "/key.pem";
     $bits = $args->get("bits");
     try {
         $keyPair = (yield $keyStore->get($path));
     } catch (KeyStoreException $e) {
         $keyPair = (new OpenSSLKeyGenerator())->generate($bits);
         $keyPair = (yield $keyStore->put($path, $keyPair));
     }
     $this->climate->br();
     $this->climate->whisper("    Requesting certificate ...");
     $location = (yield $acme->requestCertificate($keyPair, $domains));
     $certificates = (yield $acme->pollForCertificate($location));
     $path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile;
     $certificateStore = new CertificateStore($path);
     (yield $certificateStore->put($certificates));
     $this->climate->info("    Successfully issued certificate.");
     $this->climate->info("    See {$path}/" . reset($domains));
     $this->climate->br();
     (yield new CoroutineResult(0));
 }