From 11dc8c730dda2c5bd38cb386f96331c5ce3cac9c Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 20 Apr 2021 12:44:20 +0200 Subject: Adds a bunch of new Modules --- src/AptModule.php | 38 ++++++++++++-------------- src/CopyModule.php | 50 ++++++++++++++++++++++++++++++++++ src/FileModule.php | 43 +++++++++++++++++++++++++++++ src/GitModule.php | 45 +++++++++++++++++++++++++++++++ src/Support/HasPermissions.php | 15 +++++++++++ src/Support/Permissions.php | 28 +++++++++++++++++++ src/TemplateModule.php | 42 +++++++++++++++++++++++++++++ src/UfwModule.php | 39 +++++++++++++++++++++++++++ src/UserModule.php | 61 +++++++++++++++++------------------------- 9 files changed, 304 insertions(+), 57 deletions(-) create mode 100644 src/CopyModule.php create mode 100644 src/FileModule.php create mode 100644 src/GitModule.php create mode 100644 src/Support/HasPermissions.php create mode 100644 src/Support/Permissions.php create mode 100644 src/TemplateModule.php create mode 100644 src/UfwModule.php (limited to 'src') diff --git a/src/AptModule.php b/src/AptModule.php index 9b882e8..c133521 100644 --- a/src/AptModule.php +++ b/src/AptModule.php @@ -2,41 +2,33 @@ namespace PHPIAC\Modules; +use PHPIAC\Connection; use PHPIAC\Module\Module; use PHPIAC\Module\State; -use phpseclib3\Net\SSH2; class AptModule extends Module { - /** - * AptModule constructor. - * - * @param string $package - * @param string $state - */ - public function __construct( - private string $package, - private string $state = State::PRESENT - ) {} + protected string $package; + + protected string $state = State::PRESENT; + protected bool $updateCache = false; /** * @inheritDoc */ public function checkState(): bool { - global $ssh; - /**@var SSH2 $ssh*/ - $ssh->enablePTY(); + Connection::enablePty(); - $ssh->exec("dpkg -l $this->package | grep 'ii'"); - $dpkg = $ssh->read(); + Connection::exec("dpkg -l $this->package | grep 'ii'"); + $dpkg = Connection::read(); $state = match ($this->state) { State::PRESENT => str_starts_with($dpkg, 'ii'), State::ABSENT => str_contains($dpkg, 'no packages found'), }; - $ssh->disablePTY(); + Connection::disablePty(); return $state; } @@ -46,9 +38,13 @@ class AptModule extends Module */ public function getCommands(): array { - return match ($this->state) { - State::PRESENT => ["sudo apt install -y $this->package"], - State::ABSENT => ["sudo apt remove -y $this->package"], - }; + if ($this->state === State::PRESENT) { + Connection::exec("sudo apt install -y $this->package"); + } + else if ($this->state === State::ABSENT) { + Connection::exec("sudo apt remove -y $this->package"); + } + + return []; } } diff --git a/src/CopyModule.php b/src/CopyModule.php new file mode 100644 index 0000000..069f594 --- /dev/null +++ b/src/CopyModule.php @@ -0,0 +1,50 @@ +remoteSrc) { + Connection::exec("sudo cp -r $this->src $this->dest"); + } + else { + Connection::put($this->dest, $this->src); + } + + Connection::exec($this->getPermissions($this->dest)); + + return []; + } +} diff --git a/src/FileModule.php b/src/FileModule.php new file mode 100644 index 0000000..95ba771 --- /dev/null +++ b/src/FileModule.php @@ -0,0 +1,43 @@ +state === State::PRESENT) { + Connection::exec( + "sudo touch $this->path" . PHP_EOL . + $this->getPermissions($this->path) + ); + } + else { + Connection::exec("sudo rm -rf $this->path"); + } + + return []; + } +} diff --git a/src/GitModule.php b/src/GitModule.php new file mode 100644 index 0000000..3927992 --- /dev/null +++ b/src/GitModule.php @@ -0,0 +1,45 @@ +dest"); + $ls = Connection::read(); + + $state = ! str_contains($ls, 'No such file or directory'); + + Connection::disablePty(); + + return $state; + } + + /** + * @inheritDoc + */ + public function getCommands(): array + { + Connection::exec( + "sudo git clone $this->repo $this->dest" . PHP_EOL . + $this->getPermissions($this->dest) + ); + + return []; + } +} diff --git a/src/Support/HasPermissions.php b/src/Support/HasPermissions.php new file mode 100644 index 0000000..c42d71d --- /dev/null +++ b/src/Support/HasPermissions.php @@ -0,0 +1,15 @@ +owner, $this->group, $this->mode); + } +} diff --git a/src/Support/Permissions.php b/src/Support/Permissions.php new file mode 100644 index 0000000..daabf5a --- /dev/null +++ b/src/Support/Permissions.php @@ -0,0 +1,28 @@ +owner) || ! empty($this->group)) { + $permissions[] = "sudo chown -R $this->owner:$this->group $this->path"; + } + + if (! empty($this->mode)) { + $permissions[] = "sudo chmod -R $this->mode $this->path"; + } + + return implode(PHP_EOL, $permissions); + } +} diff --git a/src/TemplateModule.php b/src/TemplateModule.php new file mode 100644 index 0000000..515d891 --- /dev/null +++ b/src/TemplateModule.php @@ -0,0 +1,42 @@ +src)); + $twig = new Environment($loader); + $rendered = $twig->render(basename($this->src), $this->vars); + + Connection::put($this->dest, $rendered); + + Connection::exec($this->getPermissions($this->dest)); + + return []; + } +} diff --git a/src/UfwModule.php b/src/UfwModule.php new file mode 100644 index 0000000..1ec4452 --- /dev/null +++ b/src/UfwModule.php @@ -0,0 +1,39 @@ +rule $this->name", + match ($this->state) { + State::ENABLED => "sudo ufw --force enable", + State::DISABLED => "sudo ufw disable", + }, + ])); + + return []; + } +} diff --git a/src/UserModule.php b/src/UserModule.php index fcab5a8..c230976 100644 --- a/src/UserModule.php +++ b/src/UserModule.php @@ -2,50 +2,37 @@ namespace PHPIAC\Modules; +use PHPIAC\Connection; use PHPIAC\Module\Module; use PHPIAC\Module\State; -use phpseclib3\Net\SSH2; class UserModule extends Module { - /** - * UserModule constructor. - * - * @param string $username - * @param array $options - * @param string $state - */ - public function __construct( - private string $username, - private array $options = [], - private string $state = State::PRESENT - ) { - $this->options = array_replace([ - 'append' => false, - 'create_home' => true, - 'groups' => [], - 'shell' => '/bin/bash', - ], $options); - } + protected string $username; + protected string $password; + + protected bool $append = false; + protected bool $createHome = true; + protected array $groups = []; + protected string $shell = '/bin/bash'; + protected string $state = State::PRESENT; /** * @inheritDoc */ public function checkState(): bool { - global $ssh; - /**@var SSH2 $ssh*/ - $ssh->enablePTY(); + Connection::enablePty(); - $ssh->exec("cat /etc/passwd | grep $this->username:"); - $hasUser = $ssh->read(); + Connection::exec("cat /etc/passwd | grep $this->username:"); + $hasUser = Connection::read(); $state = match ($this->state) { State::PRESENT => str_starts_with($hasUser, "$this->username:"), State::ABSENT => empty($hasUser), }; - $ssh->disablePTY(); + Connection::disablePty(); return $state; } @@ -55,16 +42,18 @@ class UserModule extends Module */ public function getCommands(): array { - return match ($this->state) { - State::PRESENT => [ + if ($this->state === State::PRESENT) { + Connection::exec(implode(PHP_EOL, [ "sudo adduser $this->username --quiet" . - " --shell " . $this->options['shell'] . - ($this->options['create_home'] ? '' : ' --no-create-home'), - "sudo usermod -" . ($this->options['append'] ? 'a' : '') . "G " . implode(',', $this->options['groups']) . " $this->username" - ], - State::ABSENT => [ - "sudo userdel $this->username", - ], - }; + " --shell " . $this->shell . + ($this->createHome ? '' : ' --no-create-home'), + "sudo usermod -" . ($this->append ? 'a' : '') . "G " . implode(',', $this->groups) . " $this->username" + ])); + } + else if ($this->state === State::ABSENT) { + Connection::exec("sudo userdel $this->username"); + } + + return []; } } -- cgit v1.2.3