mirror of
https://github.com/laravel/valet.git
synced 2026-02-06 08:40:09 +01:00
Merge branch 'master' into tld-alias-for-domain-command
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
|
||||
class Brew
|
||||
{
|
||||
const SUPPORTED_PHP_VERSIONS = ['php', 'php@7.2', 'php@7.1', 'php@7.0', 'php@5.6', 'php72', 'php71', 'php70', 'php56'];
|
||||
const LATEST_PHP_VERSION = 'php@7.2';
|
||||
|
||||
var $cli, $files;
|
||||
|
||||
/**
|
||||
@@ -52,7 +55,7 @@ function hasInstalledPhp()
|
||||
*/
|
||||
function supportedPhpVersions()
|
||||
{
|
||||
return collect(['php72', 'php71', 'php70', 'php56']);
|
||||
return collect(static::SUPPORTED_PHP_VERSIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -182,7 +185,9 @@ function linkedPhp()
|
||||
$resolvedPath = $this->files->readLink('/usr/local/bin/php');
|
||||
|
||||
return $this->supportedPhpVersions()->first(function ($version) use ($resolvedPath) {
|
||||
return strpos($resolvedPath, $version) !== false;
|
||||
$resolvedPathNormalized= preg_replace('/([@|\.])/', '', $resolvedPath);
|
||||
$versionNormalized = preg_replace('/([@|\.])/', '', $version);
|
||||
return strpos($resolvedPathNormalized, "/$versionNormalized/") !== false;
|
||||
}, function () {
|
||||
throw new DomainException("Unable to determine linked PHP.");
|
||||
});
|
||||
@@ -197,4 +202,17 @@ function restartLinkedPhp()
|
||||
{
|
||||
$this->restartService($this->linkedPhp());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the "sudoers.d" entry for running Brew.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function createSudoersEntry()
|
||||
{
|
||||
$this->files->ensureDirExists('/etc/sudoers.d');
|
||||
|
||||
$this->files->put('/etc/sudoers.d/brew', 'Cmnd_Alias BREW = /usr/local/bin/brew *
|
||||
%admin ALL=(root) NOPASSWD: BREW'.PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,10 @@ function restart()
|
||||
*/
|
||||
function stop()
|
||||
{
|
||||
$this->brew->stopService('php56', 'php70', 'php71', 'php72');
|
||||
call_user_func_array(
|
||||
[$this->brew, 'stopService'],
|
||||
Brew::SUPPORTED_PHP_VERSIONS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,13 +108,16 @@ function stop()
|
||||
*/
|
||||
function fpmConfigPath()
|
||||
{
|
||||
$confLookup = [
|
||||
'php72' => '/usr/local/etc/php/7.2/php-fpm.d/www.conf',
|
||||
'php71' => '/usr/local/etc/php/7.1/php-fpm.d/www.conf',
|
||||
'php70' => '/usr/local/etc/php/7.0/php-fpm.d/www.conf',
|
||||
'php56' => '/usr/local/etc/php/5.6/php-fpm.conf',
|
||||
];
|
||||
$version = $this->brew->linkedPhp();
|
||||
|
||||
return $confLookup[$this->brew->linkedPhp()];
|
||||
$versionNormalized = preg_replace(
|
||||
'/php@?(\d)\.?(\d)/',
|
||||
'$1.$2',
|
||||
$version === 'php' ? Brew::LATEST_PHP_VERSION : $version
|
||||
);
|
||||
|
||||
return $versionNormalized === '5.6'
|
||||
? '/usr/local/etc/php/5.6/php-fpm.conf'
|
||||
: "/usr/local/etc/php/${versionNormalized}/php-fpm.d/www.conf";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,8 @@ function link($target, $link)
|
||||
*
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
function links() {
|
||||
function links()
|
||||
{
|
||||
$certsPath = VALET_HOME_PATH.'/Certificates';
|
||||
|
||||
$this->files->ensureDirExists($certsPath, user());
|
||||
@@ -82,11 +83,11 @@ function links() {
|
||||
*/
|
||||
function getCertificates($path)
|
||||
{
|
||||
return collect($this->files->scanDir($path))->filter(function ($value, $key) {
|
||||
return collect($this->files->scandir($path))->filter(function ($value, $key) {
|
||||
return ends_with($value, '.crt');
|
||||
})->map(function ($cert) {
|
||||
$tld = $this->config->read()['tld'];
|
||||
return substr($cert, 0, -(strlen($tld)+5));
|
||||
return substr($cert, 0, strripos($tld, '.', -5));
|
||||
})->flip();
|
||||
}
|
||||
|
||||
@@ -101,7 +102,7 @@ function getLinks($path, $certs)
|
||||
{
|
||||
$config = $this->config->read();
|
||||
|
||||
return collect($this->files->scanDir($path))->mapWithKeys(function ($site) use ($path) {
|
||||
return collect($this->files->scandir($path))->mapWithKeys(function ($site) use ($path) {
|
||||
return [$site => $this->files->readLink($path.'/'.$site)];
|
||||
})->map(function ($path, $site) use ($certs, $config) {
|
||||
$secured = $certs->has($site);
|
||||
@@ -183,8 +184,12 @@ function secure($url)
|
||||
{
|
||||
$this->unsecure($url);
|
||||
|
||||
$this->files->ensureDirExists($this->caPath(), user());
|
||||
|
||||
$this->files->ensureDirExists($this->certificatesPath(), user());
|
||||
|
||||
$this->createCa();
|
||||
|
||||
$this->createCertificate($url);
|
||||
|
||||
$this->files->putAsUser(
|
||||
@@ -192,6 +197,42 @@ function secure($url)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* If CA and root certificates are nonexistent, crete them and trust the root cert.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function createCa()
|
||||
{
|
||||
$caPemPath = $this->caPath().'/LaravelValetCASelfSigned.pem';
|
||||
$caKeyPath = $this->caPath().'/LaravelValetCASelfSigned.key';
|
||||
|
||||
if ($this->files->exists($caKeyPath) && $this->files->exists($caPemPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$oName = 'Laravel Valet CA Self Signed Organization';
|
||||
$cName = 'Laravel Valet CA Self Signed CN';
|
||||
|
||||
if ($this->files->exists($caKeyPath)) {
|
||||
$this->files->unlink($caKeyPath);
|
||||
}
|
||||
if ($this->files->exists($caPemPath)) {
|
||||
$this->files->unlink($caPemPath);
|
||||
}
|
||||
|
||||
$this->cli->run(sprintf(
|
||||
'sudo security delete-certificate -c "%s" /Library/Keychains/System.keychain',
|
||||
$cName
|
||||
));
|
||||
|
||||
$this->cli->runAsUser(sprintf(
|
||||
'openssl req -new -newkey rsa:2048 -days 730 -nodes -x509 -subj "/C=/ST=/O=%s/localityName=/commonName=%s/organizationalUnitName=Developers/emailAddress=%s/" -keyout %s -out %s',
|
||||
$oName, $cName, 'rootcertificate@laravel.valet', $caKeyPath, $caPemPath
|
||||
));
|
||||
$this->trustCa($caPemPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and trust a certificate for the given URL.
|
||||
*
|
||||
@@ -200,6 +241,9 @@ function secure($url)
|
||||
*/
|
||||
function createCertificate($url)
|
||||
{
|
||||
$caPemPath = $this->caPath().'/LaravelValetCASelfSigned.pem';
|
||||
$caKeyPath = $this->caPath().'/LaravelValetCASelfSigned.key';
|
||||
$caSrlPath = $this->caPath().'/LaravelValetCASelfSigned.srl';
|
||||
$keyPath = $this->certificatesPath().'/'.$url.'.key';
|
||||
$csrPath = $this->certificatesPath().'/'.$url.'.csr';
|
||||
$crtPath = $this->certificatesPath().'/'.$url.'.crt';
|
||||
@@ -209,9 +253,14 @@ function createCertificate($url)
|
||||
$this->createPrivateKey($keyPath);
|
||||
$this->createSigningRequest($url, $keyPath, $csrPath, $confPath);
|
||||
|
||||
$caSrlParam = ' -CAcreateserial';
|
||||
if ($this->files->exists($caSrlPath)) {
|
||||
$caSrlParam = ' -CAserial ' . $caSrlPath;
|
||||
}
|
||||
|
||||
$this->cli->runAsUser(sprintf(
|
||||
'openssl x509 -req -sha256 -days 365 -in %s -signkey %s -out %s -extensions v3_req -extfile %s',
|
||||
$csrPath, $keyPath, $crtPath, $confPath
|
||||
'openssl x509 -req -sha256 -days 730 -CA %s -CAkey %s%s -in %s -out %s -extensions v3_req -extfile %s',
|
||||
$caPemPath, $caKeyPath, $caSrlParam, $csrPath, $crtPath, $confPath
|
||||
));
|
||||
|
||||
$this->trustCertificate($crtPath);
|
||||
@@ -237,8 +286,21 @@ function createPrivateKey($keyPath)
|
||||
function createSigningRequest($url, $keyPath, $csrPath, $confPath)
|
||||
{
|
||||
$this->cli->runAsUser(sprintf(
|
||||
'openssl req -new -key %s -out %s -subj "/C=/ST=/O=/localityName=/commonName=*.%s/organizationalUnitName=/emailAddress=/" -config %s -passin pass:',
|
||||
$keyPath, $csrPath, $url, $confPath
|
||||
'openssl req -new -key %s -out %s -subj "/C=/ST=/O=/localityName=/commonName=%s/organizationalUnitName=/emailAddress=%s%s/" -config %s',
|
||||
$keyPath, $csrPath, $url, $url, '@laravel.valet', $confPath
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Trust the given root certificate file in the Mac Keychain.
|
||||
*
|
||||
* @param string $pemPath
|
||||
* @return void
|
||||
*/
|
||||
function trustCa($caPemPath)
|
||||
{
|
||||
$this->cli->run(sprintf(
|
||||
'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain %s', $caPemPath
|
||||
));
|
||||
}
|
||||
|
||||
@@ -251,7 +313,7 @@ function createSigningRequest($url, $keyPath, $csrPath, $confPath)
|
||||
function trustCertificate($crtPath)
|
||||
{
|
||||
$this->cli->run(sprintf(
|
||||
'sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain %s', $crtPath
|
||||
'sudo security add-trusted-cert -d -r trustAsRoot -k /Library/Keychains/System.keychain %s', $crtPath
|
||||
));
|
||||
}
|
||||
|
||||
@@ -299,9 +361,14 @@ function unsecure($url)
|
||||
$this->files->unlink($this->certificatesPath().'/'.$url.'.key');
|
||||
$this->files->unlink($this->certificatesPath().'/'.$url.'.csr');
|
||||
$this->files->unlink($this->certificatesPath().'/'.$url.'.crt');
|
||||
|
||||
$this->cli->run(sprintf('sudo security delete-certificate -c "%s" -t', $url));
|
||||
}
|
||||
|
||||
$this->cli->run(sprintf('sudo security delete-certificate -c "%s" /Library/Keychains/System.keychain', $url));
|
||||
$this->cli->run(sprintf('sudo security delete-certificate -c "*.%s" /Library/Keychains/System.keychain', $url));
|
||||
$this->cli->run(sprintf(
|
||||
'sudo security find-certificate -e "%s%s" -a -Z | grep SHA-1 | sudo awk \'{system("security delete-certificate -Z "$NF" /Library/Keychains/System.keychain")}\'',
|
||||
$url, '@laravel.valet'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,6 +381,16 @@ function sitesPath()
|
||||
return VALET_HOME_PATH.'/Sites';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the Valet CA certificates.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function caPath()
|
||||
{
|
||||
return VALET_HOME_PATH.'/CA';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the Valet TLS certificates.
|
||||
*
|
||||
|
||||
@@ -65,4 +65,17 @@ function onLatestVersion($currentVersion)
|
||||
|
||||
return version_compare($currentVersion, trim($response->body->tag_name, 'v'), '>=');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the "sudoers.d" entry for running Valet.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function createSudoersEntry()
|
||||
{
|
||||
$this->files->ensureDirExists('/etc/sudoers.d');
|
||||
|
||||
$this->files->put('/etc/sudoers.d/valet', 'Cmnd_Alias VALET = /usr/local/bin/valet *
|
||||
%admin ALL=(root) NOPASSWD: VALET'.PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user