Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add document root check for vcl generation #25360

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions app/code/Magento/PageCache/Model/Varnish/VclGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PageCache\Model\Varnish;

use Magento\Framework\UrlInterface;
use Magento\PageCache\Model\VclGeneratorInterface;
use Magento\PageCache\Model\VclTemplateLocatorInterface;

Expand Down Expand Up @@ -49,16 +51,22 @@ class VclGenerator implements VclGeneratorInterface
*/
private $designExceptions;

/**
* @var Url
*/
private $url;

/**
* VclGenerator constructor.
*
* @param VclTemplateLocatorInterface $vclTemplateLocator
* @param string $backendHost
* @param int $backendPort
* @param array $accessList
* @param int $gracePeriod
* @param string $sslOffloadedHeader
* @param array $designExceptions
* @param string $backendHost
* @param int $backendPort
* @param array $accessList
* @param int $gracePeriod
* @param string $sslOffloadedHeader
* @param array $designExceptions
* @param UrlInterface|null $url
*/
public function __construct(
VclTemplateLocatorInterface $vclTemplateLocator,
Expand All @@ -67,7 +75,8 @@ public function __construct(
$accessList,
$gracePeriod,
$sslOffloadedHeader,
$designExceptions = []
$designExceptions = [],
UrlInterface $url = null
) {
$this->backendHost = $backendHost;
$this->backendPort = $backendPort;
Expand All @@ -76,6 +85,7 @@ public function __construct(
$this->vclTemplateLocator = $vclTemplateLocator;
$this->sslOffloadedHeader = $sslOffloadedHeader;
$this->designExceptions = $designExceptions;
$this->url = $url ?: \Magento\Framework\App\ObjectManager::getInstance()->get(UrlInterface::class);
}

/**
Expand All @@ -101,6 +111,7 @@ private function getReplacements()
return [
'/* {{ host }} */' => $this->getBackendHost(),
'/* {{ port }} */' => $this->getBackendPort(),
'/* {{ health_check }} */' => $this->getHealthCheck(),
'/* {{ ips }} */' => $this->getTransformedAccessList(),
'/* {{ design_exceptions_code }} */' => $this->getRegexForDesignExceptions(),
// http headers get transformed by php `X-Forwarded-Proto: https`
Expand Down Expand Up @@ -228,4 +239,18 @@ private function getDesignExceptions()
{
return $this->designExceptions;
}

/**
* Check if document root contains pub
*
* @return string
*/
private function getHealthCheck() : string
{
if (strpos($this->url->getBaseUrl(), 'pub') !== false) {
return '/pub/health_check.php';
}

return '/health_check.php';
}
}
44 changes: 33 additions & 11 deletions app/code/Magento/PageCache/Test/Unit/Model/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PageCache\Test\Unit\Model;

use Magento\Framework\Serialize\Serializer\Json;
Expand Down Expand Up @@ -102,6 +104,7 @@ protected function setUp()
null,
'X_Forwarded_Proto: https'
],

[
\Magento\PageCache\Model\Config::XML_VARNISH_PAGECACHE_GRACE_PERIOD,
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
Expand All @@ -115,15 +118,29 @@ protected function setUp()
$this->moduleReader = $this->createMock(\Magento\Framework\Module\Dir\Reader::class);
$this->serializerMock = $this->createMock(Json::class);

/** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
/**
* @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator
*/
$vclTemplateLocator = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclTemplateLocator::class)
->disableOriginalConstructor()
->setMethods(['getTemplate'])
->getMock();
$vclTemplateLocator->expects($this->any())
->method('getTemplate')
->will($this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl')));
/** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
/**
* @var \PHPUnit_Framework_MockObject_MockObject $request
*/
$url = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
->disableOriginalConstructor()
->setMethods(['getBaseUrl'])
->getMockForAbstractClass();
$url->expects($this->any())
->method('getBaseUrl')
->willReturn('/var/www/html/pub');
/**
* @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator
*/
$vclGeneratorFactory = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclGeneratorFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
Expand All @@ -139,15 +156,20 @@ protected function setUp()
$vclGeneratorFactory->expects($this->any())
->method('create')
->with($expectedParams)
->will($this->returnValue(new \Magento\PageCache\Model\Varnish\VclGenerator(
$vclTemplateLocator,
'example.com',
'8080',
explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
120,
'X_Forwarded_Proto: https',
[['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']]
)));
->will(
$this->returnValue(
new \Magento\PageCache\Model\Varnish\VclGenerator(
$vclTemplateLocator,
'example.com',
'8080',
explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
120,
'X_Forwarded_Proto: https',
[['regexp' => '(?i)pattern', 'value' => 'value_for_pattern']],
$url
)
)
);
$this->config = $objectManager->getObject(
\Magento\PageCache\Model\Config::class,
[
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/PageCache/etc/varnish4.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ backend default {
.port = "/* {{ port }} */";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.url = "/* {{ health_check }} */";
.timeout = 2s;
.interval = 5s;
.window = 10;
Expand Down Expand Up @@ -63,7 +63,7 @@ sub vcl_recv {
}

# Bypass health check requests
if (req.url ~ "/pub/health_check.php") {
if (req.url ~ "/* {{ health_check }} */") {
return (pass);
}

Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/PageCache/etc/varnish5.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ backend default {
.port = "/* {{ port }} */";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.url = "/* {{ health_check }} */";
.timeout = 2s;
.interval = 5s;
.window = 10;
Expand Down Expand Up @@ -64,7 +64,7 @@ sub vcl_recv {
}

# Bypass health check requests
if (req.url ~ "/pub/health_check.php") {
if (req.url ~ "/* {{ health_check }} */") {
return (pass);
}

Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/PageCache/etc/varnish6.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ backend default {
.port = "/* {{ port }} */";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.url = "/* {{ health_check }} */";
.timeout = 2s;
.interval = 5s;
.window = 10;
Expand Down Expand Up @@ -64,7 +64,7 @@ sub vcl_recv {
}

# Bypass health check requests
if (req.url ~ "/pub/health_check.php") {
if (req.url ~ "/* {{ health_check }} */") {
return (pass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PageCache\Model;

class ConfigTest extends \PHPUnit\Framework\TestCase
Expand Down Expand Up @@ -31,7 +33,9 @@ protected function setUp()
$this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl'))
);

/** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
/**
* @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator
*/
$vclTemplateLocator = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclTemplateLocator::class)
->disableOriginalConstructor()
->setMethods(['getTemplate'])
Expand All @@ -40,7 +44,20 @@ protected function setUp()
->method('getTemplate')
->will($this->returnValue(file_get_contents(__DIR__ . '/_files/test.vcl')));

/** @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator */
/**
* @var \PHPUnit_Framework_MockObject_MockObject $request
*/
$url = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
->disableOriginalConstructor()
->setMethods(['getBaseUrl'])
->getMockForAbstractClass();
$url->expects($this->any())
->method('getBaseUrl')
->willReturn('/var/www/html/pub');

/**
* @var \PHPUnit_Framework_MockObject_MockObject $vclTemplateLocator
*/
$vclGeneratorFactory = $this->getMockBuilder(\Magento\PageCache\Model\Varnish\VclGeneratorFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
Expand All @@ -56,15 +73,20 @@ protected function setUp()
$vclGeneratorFactory->expects($this->any())
->method('create')
->with($expectedParams)
->will($this->returnValue(new \Magento\PageCache\Model\Varnish\VclGenerator(
$vclTemplateLocator,
'example.com',
'8080',
explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
1234,
'X-Forwarded-Proto',
json_decode('{"_":{"regexp":"\/firefox\/i","value":"Magento\/blank"}}', true)
)));
->will(
$this->returnValue(
new \Magento\PageCache\Model\Varnish\VclGenerator(
$vclTemplateLocator,
'example.com',
'8080',
explode(',', '127.0.0.1,192.168.0.1,127.0.0.2'),
1234,
'X-Forwarded-Proto',
json_decode('{"_":{"regexp":"\/firefox\/i","value":"Magento\/blank"}}', true),
$url
)
)
);
$this->config = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\PageCache\Model\Config::class,
[
Expand Down