From 403e75d58020c717951db0a01d80000425242ac5 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 23 Sep 2016 09:50:09 -0400 Subject: [PATCH] Rewrite TLS Caddy files on install Porting this from master branch for now, makes it a lot easier for people to go from master to 1.1.* since the master branch has a different Caddyfile structure. Since Caddy 0.9.* still has the X-Accel-Redirect issue re: Content-Type, we can't tag on master yet, so this will help people get back to a tagged release without having to manually fix a bunch of Caddyfiles. --- cli/Valet/Caddy.php | 21 ++++++++++++++++++++- tests/CaddyTest.php | 18 +++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cli/Valet/Caddy.php b/cli/Valet/Caddy.php index 5c6e7ae..8065552 100644 --- a/cli/Valet/Caddy.php +++ b/cli/Valet/Caddy.php @@ -6,6 +6,8 @@ class Caddy { var $cli; var $files; + var $configuration; + var $site; var $daemonPath = '/Library/LaunchDaemons/com.laravel.valetServer.plist'; /** @@ -14,10 +16,12 @@ class Caddy * @param CommandLine $cli * @param Filesystem $files */ - function __construct(CommandLine $cli, Filesystem $files) + function __construct(CommandLine $cli, Filesystem $files, Configuration $configuration, Site $site) { $this->cli = $cli; $this->files = $files; + $this->configuration = $configuration; + $this->site = $site; } /** @@ -61,6 +65,21 @@ function installCaddyDirectory() } $this->files->putAsUser($caddyDirectory.'/.keep', "\n"); + + $this->rewriteSecureCaddyFiles(); + } + + /** + * Generate fresh Caddyfiles for existing secure sites. + * + * This simplifies upgrading when the Caddyfile structure changes. + * + * @return void + */ + function rewriteSecureCaddyFiles() + { + $domain = $this->configuration->read()['domain']; + $this->site->resecureForNewDomain($domain, $domain); } /** diff --git a/tests/CaddyTest.php b/tests/CaddyTest.php index 487e1aa..b8de7a0 100644 --- a/tests/CaddyTest.php +++ b/tests/CaddyTest.php @@ -1,7 +1,9 @@ shouldReceive('putAsUser')->andReturnUsing(function ($path, $contents) { $this->assertSame(VALET_HOME_PATH.'/Caddyfile', $path); $this->assertTrue(strpos($contents, 'import '.VALET_HOME_PATH.'/Caddy/*') !== false); - }); + })->once(); swap(Filesystem::class, $files); $caddy = resolve(Caddy::class); @@ -39,10 +41,12 @@ public function test_install_caddy_directories_creates_location_for_site_specifi { $files = Mockery::mock(Filesystem::class); $files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(false); - $files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Caddy'); - $files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n"); + $files->shouldReceive('mkdirAsUser')->with(VALET_HOME_PATH.'/Caddy')->once(); + $files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n")->once(); swap(Filesystem::class, $files); + swap(Configuration::class, Mockery::spy(Configuration::class)); + swap(Site::class, Mockery::spy(Site::class)); $caddy = resolve(Caddy::class); $caddy->installCaddyDirectory(); @@ -54,9 +58,11 @@ public function test_caddy_directory_is_never_created_if_it_already_exists() $files = Mockery::mock(Filesystem::class); $files->shouldReceive('isDir')->with(VALET_HOME_PATH.'/Caddy')->andReturn(true); $files->shouldReceive('mkdirAsUser')->never(); - $files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n"); + $files->shouldReceive('putAsUser')->with(VALET_HOME_PATH.'/Caddy/.keep', "\n")->once(); swap(Filesystem::class, $files); + swap(Configuration::class, Mockery::spy(Configuration::class)); + swap(Site::class, Mockery::spy(Site::class)); $caddy = resolve(Caddy::class); $caddy->installCaddyDirectory(); @@ -68,12 +74,14 @@ public function test_caddy_daemon_is_placed_in_correct_location() $files = Mockery::mock(Filesystem::class.'[put]'); swap(Filesystem::class, $files); + swap(Configuration::class, Mockery::spy(Configuration::class)); + swap(Site::class, Mockery::spy(Site::class)); $caddy = resolve(Caddy::class); $files->shouldReceive('put')->andReturnUsing(function ($path, $contents) use ($caddy) { $this->assertSame($caddy->daemonPath, $path); $this->assertTrue(strpos($contents, VALET_HOME_PATH) !== false); - }); + })->once(); $caddy->installCaddyDaemon(); }