1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-07 01:00:09 +01:00

♻️ Refactoring resecureForNew* methods to resecureForNewConfiguration.

This commit is contained in:
Mikaël Popowicz
2021-02-23 22:32:50 +01:00
parent 7bb5326a27
commit a876175c6a
5 changed files with 48 additions and 60 deletions

View File

@@ -98,7 +98,7 @@ function replaceLoopback($siteConf)
return $siteConf;
}
$str = '#listen VALET_LOOPBACK:80 default_server; # valet loopback';
$str = '#listen VALET_LOOPBACK:80; # valet loopback';
return str_replace(
$str,
@@ -150,8 +150,9 @@ function rewriteSecureNginxFiles()
$tld = $this->configuration->read()['tld'];
$loopback = $this->configuration->read()['loopback'];
$this->site->resecureForNewTld($tld, $tld);
$this->site->resecureForNewLoopback($loopback, $loopback);
$config = compact('tld', 'loopback');
$this->site->resecureForNewConfiguration($config, $config);
}
/**

View File

@@ -328,13 +328,13 @@ function pruneLinks()
}
/**
* Resecure all currently secured sites with a fresh tld.
* Resecure all currently secured sites with a fresh configuration.
*
* @param string $oldTld
* @param string $tld
* @param array $old
* @param array $new
* @return void
*/
function resecureForNewTld($oldTld, $tld)
function resecureForNewConfiguration(array $old, array $new)
{
if (! $this->files->exists($this->certificatesPath())) {
return;
@@ -342,6 +342,14 @@ function resecureForNewTld($oldTld, $tld)
$secured = $this->secured();
$defaultTld = $this->config->read()['tld'];
$oldTld = isset($old['tld']) && ! empty($old['tld']) ? $old['tld'] : $defaultTld;
$tld = isset($new['tld']) && ! empty($new['tld']) ? $new['tld'] : $defaultTld;
$defaultLoopback = $this->config->read()['loopback'];
$oldLoopback = isset($old['loopback']) && ! empty($old['loopback']) ? $old['loopback'] : $defaultLoopback;
$loopback = isset($new['loopback']) && ! empty($new['loopback']) ? $new['loopback'] : $defaultLoopback;
foreach ($secured as $url) {
$newUrl = str_replace('.'.$oldTld, '.'.$tld, $url);
$siteConf = $this->getSiteConfigFileContents($url, '.'.$oldTld);
@@ -349,7 +357,14 @@ function resecureForNewTld($oldTld, $tld)
if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) {
// proxy config
$this->unsecure($url);
$this->secure($newUrl, $this->replaceOldDomainWithNew($siteConf, $url, $newUrl));
$this->secure(
$newUrl,
$this->replaceOldLoopbackWithNew(
$this->replaceOldDomainWithNew($siteConf, $url, $newUrl),
$oldLoopback,
$loopback
)
);
} else {
// normal config
$this->unsecure($url);
@@ -384,36 +399,6 @@ function replaceOldDomainWithNew($siteConf, $old, $new)
return $siteConf;
}
/**
* Resecure all currently secured sites with a fresh loopback address.
*
* @param string $oldLoopback
* @param string $loopback
* @return void
*/
function resecureForNewLoopback($oldLoopback, $loopback)
{
if (! $this->files->exists($this->certificatesPath())) {
return;
}
$secured = $this->secured();
foreach ($secured as $url) {
$siteConf = $this->getSiteConfigFileContents($url);
if (!empty($siteConf) && strpos($siteConf, '# valet stub: proxy.valet.conf') === 0) {
// proxy config
$this->unsecure($url);
$this->secure($url, $this->replaceOldLoopbackWithNew($siteConf, $oldLoopback, $loopback));
} else {
// normal config
$this->unsecure($url);
$this->secure($url);
}
}
}
/**
* Parse Nginx site config file contents to swap old loopback address to new.
*
@@ -639,13 +624,16 @@ function buildCertificateConf($path, $url)
function buildSecureNginxServer($url, $siteConf = null)
{
if ($siteConf === null) {
$siteConf = $this->files->get(__DIR__.'/../stubs/secure.valet.conf');
$siteConf = $this->replaceOldLoopbackWithNew(
$this->files->get(__DIR__.'/../stubs/secure.valet.conf'),
'VALET_LOOPBACK',
$this->valetLoopback()
);
}
return str_replace(
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_CERT', 'VALET_KEY'],
[
$this->valetLoopback(),
$this->valetHomePath(),
VALET_SERVER_PATH,
VALET_STATIC_PREFIX,
@@ -731,11 +719,15 @@ function proxyCreate($url, $host)
$url .= '.'.$tld;
}
$siteConf = $this->files->get(__DIR__.'/../stubs/proxy.valet.conf');
$siteConf = $this->replaceOldLoopbackWithNew(
$this->files->get(__DIR__.'/../stubs/proxy.valet.conf'),
'VALET_LOOPBACK',
$this->valetLoopback()
);
$siteConf = str_replace(
['VALET_LOOPBACK', 'VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'],
[$this->valetLoopback(), $this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host],
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PROXY_HOST'],
[$this->valetHomePath(), VALET_SERVER_PATH, VALET_STATIC_PREFIX, $url, $host],
$siteConf
);
@@ -811,7 +803,7 @@ function updatePlist($loopback)
str_replace(
'VALET_LOOPBACK',
$loopback,
$this->files->get(__DIR__.'/../stubs/ifconfig.plist')
$this->files->get(__DIR__.'/../stubs/loopback.plist')
)
);
@@ -836,7 +828,7 @@ function valetLoopback()
*/
function plistPath()
{
return '/Library/LaunchDaemons/com.laravel.valet.ifconfig.plist';
return '/Library/LaunchDaemons/com.laravel.valet.loopback.plist';
}
/**

View File

@@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.laravel.valet.ifconfig</string>
<string>com.laravel.valet.loopback</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>

View File

@@ -66,16 +66,9 @@
*/
if (is_dir(VALET_HOME_PATH)) {
/**
* Upgrade helper: ensure the tld config exists
* Upgrade helper: ensure the tld config exists or the loopback config exists
*/
if (empty(Configuration::read()['tld'])) {
Configuration::writeBaseConfiguration();
}
/**
* Upgrade helper: ensure the loopback config exists
*/
if (empty(Configuration::read()['loopback'])) {
if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) {
Configuration::writeBaseConfiguration();
}
@@ -93,7 +86,7 @@
Configuration::updateKey('tld', $tld);
Site::resecureForNewTld($oldTld, $tld);
Site::resecureForNewConfiguration(['tld' => $oldTld], ['tld' => $tld]);
PhpFpm::restart();
Nginx::restart();
@@ -114,9 +107,9 @@
DnsMasq::refreshConfiguration();
Site::aliasLoopback($oldLoopback, $loopback);
Site::resecureForNewLoopback($oldLoopback, $loopback);
Nginx::installServer();
Site::resecureForNewConfiguration(['loopback' => $oldLoopback], ['loopback' => $loopback]);
PhpFpm::restart();
Nginx::installServer();
Nginx::restart();
info('Your valet loopback address has been updated to ['.$loopback.']');

View File

@@ -82,6 +82,8 @@ public function test_install_nginx_directories_rewrites_secure_nginx_files()
$nginx = resolve(Nginx::class);
$nginx->installNginxDirectory();
$site->shouldHaveReceived('resecureForNewTld', ['test', 'test']);
$data = ['tld' => 'test', 'loopback' => '127.0.0.1'];
$site->shouldHaveReceived('resecureForNewConfiguration', [$data, $data]);
}
}