From 2a33d0ec8595c3cd21324b9c0ae9c32262bbe502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 2 Feb 2019 16:24:36 +0100 Subject: [PATCH] Add vhost_is_written test --- nginx/sites-available/example.conf | 2 - src/Interfaces/ServerConfigManager.php | 2 + .../NginxConfigManager.php | 29 +++++++++++ src/ServerManager.php | 9 +++- src/config/tenancy.php | 14 ++++- tests/ServerManagerTest.php | 52 +++++++++++++++++++ tests/TestCase.php | 4 +- 7 files changed, 105 insertions(+), 7 deletions(-) diff --git a/nginx/sites-available/example.conf b/nginx/sites-available/example.conf index cd7593b8..943b6263 100644 --- a/nginx/sites-available/example.conf +++ b/nginx/sites-available/example.conf @@ -1,5 +1,3 @@ -# certbot -n --staging --nginx -d tenancytest1.ml --preferred-challenges http --must-staple --agree-tos --email samuel.stancl@gmail.com - # Catch-all HTTP (:80) server server { listen 80 default_server; diff --git a/src/Interfaces/ServerConfigManager.php b/src/Interfaces/ServerConfigManager.php index aed294df..8216c538 100644 --- a/src/Interfaces/ServerConfigManager.php +++ b/src/Interfaces/ServerConfigManager.php @@ -4,4 +4,6 @@ namespace Stancl\Tenancy\Interfaces; interface ServerConfigManager { + public function addVhost(string $domain, string $file): bool; + public function deployCertificate(string $domain): bool; } diff --git a/src/ServerConfigManagers/NginxConfigManager.php b/src/ServerConfigManagers/NginxConfigManager.php index 6d698678..adf929b7 100644 --- a/src/ServerConfigManagers/NginxConfigManager.php +++ b/src/ServerConfigManagers/NginxConfigManager.php @@ -2,8 +2,37 @@ namespace Stancl\Tenancy\ServerConfigManagers; +use Symfony\Component\Process\Process; use Stancl\Tenancy\Interfaces\ServerConfigManager; class NginxConfigManager implements ServerConfigManager { + public function addVhost(string $domain, string $file): bool + { + $f = fopen($file, 'a'); + fwrite($f, $this->getVhostText($domain)); + fclose($f); + return true; + } + + public function getVhostText(string $domain) + { + return str_replace('%host%', $domain, config('tenancy.server.nginx.vhost')); + } + + public function deployCertificate(string $domain): bool + { + $process = new Process(array_merge([ + config('tenancy.server.certbot_path'), + '-n', + '--nginx', + '--agree-tos', + '-d', $domain, + '--preferred-challenges', 'http', + ], config('tenancy.server.nginx.extra_certbot_args'))); + + $process->run(); + + return $process->isSuccessful(); + } } diff --git a/src/ServerManager.php b/src/ServerManager.php index 251d36c2..f922d676 100644 --- a/src/ServerManager.php +++ b/src/ServerManager.php @@ -21,11 +21,16 @@ class ServerManager return config('tenancy.server.file.path.prefix') . $this->tenantManager->tenant['uuid'] . config('tenancy.server.file.path.suffix'); } - public function create() + public function createVhost(string $domain) { + $this->serverConfigManager->addVhost($domain, $this->getConfigFilePath()); + $this->serverConfigManager->deployCertificate($domain); + if (method_exists($this->serverConfigManager, 'postCertDeploymentChanges')) { + $this->serverConfigManager->postCertDeploymentChanges(); + } } - public function delete() + public function deleteVhost() { // todo } diff --git a/src/config/tenancy.php b/src/config/tenancy.php index 252d3e8d..2a53f7cc 100644 --- a/src/config/tenancy.php +++ b/src/config/tenancy.php @@ -43,6 +43,18 @@ return [ // results in: '/etc/nginx/sites-available/tenants/tenant' . $uuid . '.conf' ] */ - ] + ], + 'nginx' => [ + 'vhost' => " + server { + include includes/tenancy; + server_name %host%; + }", + 'extra_certbot_args' => [ + '--must-staple', + // '--staging', // obtains a fake cert intended for testing certbot + // '--email', 'your@email', // if you haven't created an account in certbot yet + ], + ], ] ]; diff --git a/tests/ServerManagerTest.php b/tests/ServerManagerTest.php index 269fc9ba..ac4da2a8 100644 --- a/tests/ServerManagerTest.php +++ b/tests/ServerManagerTest.php @@ -39,4 +39,56 @@ class ServerManagerTest extends TestCase $this->assertSame("/etc/foo{$uuid}bar", $this->serverManager->getConfigFilePath()); } + + /** @test */ + public function vhost_is_written() + { + [$tmpfile, $path, $vhost] = $this->setupCreateVhost(); + + $this->serverManager->createVhost('localhost'); + + $vhost = str_replace('%host%', 'localhost', $vhost); + + $this->assertContains($vhost, fread($tmpfile, filesize($path))); + } + + /** @test */ + public function cert_is_deployed() + { + [$tmpfile, $path, $vhost] = $this->setupCreateVhost(); + + $this->serverManager->createVhost('localhost'); + + dump(fread($tmpfile, filesize($path))); + // todo + // The following error was encountered:\n + // [Errno 13] Permission denied: '/var/log/letsencrypt/.certbot.lock'\n + // Either run as root, or set --config-dir, --work-dir, and --logs-dir to writeable paths.\n + } + + public function setupCreateVhost() + { + $tmpfile = tmpfile(); + $path = stream_get_meta_data($tmpfile)['uri']; + + $vhost = "server { + include includes/tenancy; + server_name %host%; +}"; + + config([ + 'tenancy.server.nginx' => [ + 'vhost' => $vhost, + 'extra_certbot_args' => [ + '--staging' + ], + ], + 'tenancy.server.file' => [ + 'single' => true, + 'path' => $path, + ], + ]); + + return [$tmpfile, $path, $vhost]; + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index e411b3a1..1a29c5b8 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,9 +17,9 @@ class TestCase extends \Orchestra\Testbench\TestCase Redis::connection('tenancy')->flushdb(); - tenant()->create('phpunit.localhost'); + tenant()->create('localhost'); - tenancy()->init('phpunit.localhost'); + tenancy()->init('localhost'); } /**