Improvements

This commit is contained in:
Christopher Vagnetoft 2016-04-19 22:55:09 +02:00
parent fceb4c4966
commit b63260533f
12 changed files with 101 additions and 13 deletions

View File

@ -1,3 +1,3 @@
.phony: phar .phony: phar
phar: phar:
makephar -c makephar.conf ./makephar -c makephar.conf

View File

@ -2,6 +2,8 @@
require_once __DIR__."/../vendor/autoload.php"; require_once __DIR__."/../vendor/autoload.php";
require_once __DIR__."/systemtest.php"; require_once __DIR__."/systemtest.php";
if (file_exists(__DIR__."/banner.php"))
require_once __DIR__."/banner.php";
use NoccyLabs\Hotfix\HotfixApplication; use NoccyLabs\Hotfix\HotfixApplication;

0
bin/hotfix Normal file → Executable file
View File

BIN
makephar Executable file

Binary file not shown.

View File

@ -9,6 +9,7 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\ChoiceQuestion;
use NoccyLabs\Hotfix\Hotfix\Loader; use NoccyLabs\Hotfix\Hotfix\Loader;
class ApplyCommand extends Command class ApplyCommand extends Command
@ -20,6 +21,7 @@ class ApplyCommand extends Command
$this->setDescription("Apply a hotfix from a file or the Internet"); $this->setDescription("Apply a hotfix from a file or the Internet");
$this->addOption("insecure", "I", InputOption::VALUE_NONE, "Disable signature checks. Don't use unless you know what you are doing!"); $this->addOption("insecure", "I", InputOption::VALUE_NONE, "Disable signature checks. Don't use unless you know what you are doing!");
$this->addOption("preview", "p", InputOption::VALUE_NONE, "Only preview the hotfix script, don't apply anything");
$this->addArgument("hotfix", InputArgument::OPTIONAL, "The identifier, path or filename of the hotfix"); $this->addArgument("hotfix", InputArgument::OPTIONAL, "The identifier, path or filename of the hotfix");
} }
@ -30,10 +32,20 @@ class ApplyCommand extends Command
$fix = $input->getArgument("hotfix"); $fix = $input->getArgument("hotfix");
$insecure = $input->getOption("insecure"); $insecure = $input->getOption("insecure");
$loader = new Loader();
if (!$fix) {
$loaders = $loader->getLoaders();
$output->writeln("Supported loaders:\n");
foreach ($loaders as $loader) {
$output->writeln(" * ".$loader->getInfo());
}
return;
}
$output->writeln("Reading hotfix <comment>{$fix}</comment>..."); $output->writeln("Reading hotfix <comment>{$fix}</comment>...");
$loader = new Loader();
try { try {
$hotfix = $loader->load($fix, $insecure); $hotfix = $loader->load($fix, $insecure);
} catch (\Exception $e) { } catch (\Exception $e) {
@ -47,20 +59,27 @@ class ApplyCommand extends Command
$output->writeln(""); $output->writeln("");
if (($signer = $hotfix->getSignedBy())) { if (($signer = $hotfix->getSignedBy())) {
$output->writeln("<info>Hotfix has good signature by {$signer}</info>"); $keyid = $hotfix->getKeyId();
$output->writeln("Hotfix has good signature from <fg=green;options=bold>{$signer}</fg=green;options=bold> (keyid <info>{$keyid}</info>)");
} else { } else {
$output->writeln("<fg=red;options=bold>Warning: Hotfix is not signed or signature not valid!</fg=red;options=bold>"); $output->writeln("<fg=red;options=bold>Warning: Hotfix is not signed or signature not valid!</fg=red;options=bold>");
} }
$output->writeln(""); $output->writeln("");
$output->writeln(" Hotfix: <comment>".$hotfix->getName()."</comment>"); $output->writeln(" Hotfix: <fg=yellow;options=bold>".$hotfix->getName()."</fg=yellow;options=bold>");
$output->writeln(" Author: <comment>".$hotfix->getAuthor()."</comment>"); $output->writeln(" Author: <comment>".$hotfix->getAuthor()."</comment>");
$output->writeln(" Info:"); $info = explode("\n",wordwrap(trim($hotfix->getInfo()), 60));
$info = explode("\n",wordwrap(trim($hotfix->getInfo()), 70)); $info = "<comment>".join("</comment>\n <comment>", $info)."</comment>";
foreach ($info as $line) { $output->writeln(" Info: ".$info);
$output->writeln(" | <info>{$line}</info>");
}
$output->writeln(""); $output->writeln("");
if ($input->getOption("preview")) {
$output->writeln("This is the script that will be executed:");
$body = $hotfix->getBody();
$body = "<fg=cyan> ".join("\n ", explode("\n",$body))."</fg=cyan>";
$output->writeln($body);
return;
}
$helper = $this->getHelper("question"); $helper = $this->getHelper("question");
$question = new ConfirmationQuestion("Do you want to apply this hotfix? [y/N] ", false); $question = new ConfirmationQuestion("Do you want to apply this hotfix? [y/N] ", false);

View File

@ -9,6 +9,8 @@ class Hotfix
protected $signer; protected $signer;
protected $keyId;
protected $header; protected $header;
protected $body; protected $body;
@ -16,7 +18,8 @@ class Hotfix
public function __construct($hotfix, $signer) public function __construct($hotfix, $signer)
{ {
$this->load($hotfix); $this->load($hotfix);
$this->signer = $signer; $this->signer = $signer[0];
$this->keyId = $signer[1];
} }
protected function load($hotfix) protected function load($hotfix)
@ -31,6 +34,11 @@ class Hotfix
$this->body = $body; $this->body = $body;
} }
public function getBody()
{
return $this->body;
}
public function apply() public function apply()
{ {
if (!array_key_exists('lang', $this->header)) { if (!array_key_exists('lang', $this->header)) {
@ -40,9 +48,12 @@ class Hotfix
} }
$script = null; $script = null;
$head = null;
$foot = null;
switch ($lang) { switch ($lang) {
case 'bash': case 'bash':
$exec = "/bin/bash"; $exec = "/bin/bash";
$head = file_get_contents(__DIR__."/../stubs/bash.stub");
break; break;
case 'php': case 'php':
$exec = "/usr/bin/env php"; $exec = "/usr/bin/env php";
@ -53,7 +64,7 @@ class Hotfix
} }
$tmpfile = sys_get_temp_dir()."/hotfix_".sha1($this->header['hotfix']); $tmpfile = sys_get_temp_dir()."/hotfix_".sha1($this->header['hotfix']);
file_put_contents($tmpfile, $this->body); file_put_contents($tmpfile, $head."\n".$this->body."\n".$foot);
passthru($exec." ".$tmpfile); passthru($exec." ".$tmpfile);
unlink($tmpfile); unlink($tmpfile);
} }
@ -69,6 +80,11 @@ class Hotfix
); );
} }
public function getKeyId()
{
return $this->keyId;
}
public function getName() public function getName()
{ {
return $this->header['hotfix']; return $this->header['hotfix'];

View File

@ -22,6 +22,11 @@ class Loader
$this->loaders[] = $loader; $this->loaders[] = $loader;
} }
public function getLoaders()
{
return $this->loaders;
}
public function load($fix, $insecure=false) public function load($fix, $insecure=false)
{ {
foreach ($this->loaders as $loader) { foreach ($this->loaders as $loader) {
@ -56,12 +61,22 @@ class Loader
throw new \Exception("Hotfix signature is not valid!"); throw new \Exception("Hotfix signature is not valid!");
} }
$keyInfo = gnupg_keyinfo($gpg, $sigInfo[0]['fingerprint']); $fingerprint = $sigInfo[0]['fingerprint'];
$keyInfo = gnupg_keyinfo($gpg, $fingerprint);
$subKeys = $keyInfo[0]['subkeys'];
$keyId = null;
foreach ($subKeys as $subKey) {
if ($subKey['fingerprint'] == $fingerprint) {
$keyId = $subKey['keyid'];
break;
}
}
if (empty($keyInfo)) { if (empty($keyInfo)) {
throw new \Exception("Unknown signer (key id {$sigInfo[0]['fingerprint']})"); throw new \Exception("Unknown signer (key id {$sigInfo[0]['fingerprint']})");
} }
return $keyInfo[0]; return [ $keyInfo[0], $keyId ];
} }
} }

View File

@ -15,4 +15,9 @@ class FileLoader implements LoaderInterface
} }
return false; return false;
} }
public function getInfo()
{
return "<info>filename</info> - Read a hotfix from a file";
}
} }

View File

@ -15,4 +15,9 @@ class GistLoader implements LoaderInterface
return file_get_contents($url); return file_get_contents($url);
} }
public function getInfo()
{
return "gist:<info>user</info>/<info>gist-id</info> - Read hotfix from a Gist";
}
} }

View File

@ -12,4 +12,9 @@ class HttpLoader implements LoaderInterface
} }
return false; return false;
} }
public function getInfo()
{
return "http(s)://<info>url</info> - Read hotfix from a URL";
}
} }

View File

@ -20,4 +20,9 @@ class PastebinLoader implements LoaderInterface
return $body; return $body;
} }
public function getInfo()
{
return "pastebin:<info>paste-id</info> - Read hotfix from Pastebin";
}
} }

16
src/stubs/bash.stub Normal file
View File

@ -0,0 +1,16 @@
test -e /etc/lsb-release && source /etc/lsb-release
function exec() {
echo -e "\e[36;1m[exec]\e[36;21m $*\e[0m"
LOG=$(tempfile -p exec)
$@ &>$LOG
EC=$?
if [ $EC -gt 0 ]; then
echo -e "\e[31;1m[warn]\e[31;21m Command completed with exitcode $EC\e[0m"
cat $LOG
fi
rm $LOG
}
function info() {
echo -e "\e[32;1m[info]\e[32;21m $*\e[0m"
}