From cc1311527170c36781a0bdf0f4716bd180670a1c Mon Sep 17 00:00:00 2001 From: Domagoj Potkoc Date: Thu, 23 Jun 2022 10:08:26 +0200 Subject: [PATCH 1/5] fix/improvement for removing custom shippets after VCL upload action --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 40 +++++++++++++++- Model/Api.php | 15 ++++++ Model/Snippet/CheckIsAllowedForSync.php | 48 +++++++++++++++++++ etc/di.xml | 13 +++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 Model/Snippet/CheckIsAllowedForSync.php diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 3f0f2e02..86c5a421 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -36,6 +36,7 @@ use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Config\Model\ResourceModel\Config as CoreConfig; use Magento\Framework\App\Cache\TypeListInterface; +use Fastly\Cdn\Model\Snippet\CheckIsAllowedForSync; /** * Class Upload @@ -90,6 +91,11 @@ class Upload extends Action */ private $typeList; + /** + * @var CheckIsAllowedForSync + */ + private $checkIsAllowedForSync; + /** * Upload constructor. * @@ -105,6 +111,7 @@ class Upload extends Action * @param Filesystem $filesystem * @param CoreConfig $coreConfig * @param TypeListInterface $typeList + * @param CheckIsAllowedForSync $checkIsAllowedForSync */ public function __construct( Context $context, @@ -118,7 +125,8 @@ public function __construct( TimezoneInterface $timezone, Filesystem $filesystem, CoreConfig $coreConfig, - TypeListInterface $typeList + TypeListInterface $typeList, + CheckIsAllowedForSync $checkIsAllowedForSync ) { $this->request = $request; $this->resultJson = $resultJsonFactory; @@ -132,7 +140,7 @@ public function __construct( parent::__construct($context); $this->coreConfig = $coreConfig; $this->typeList = $typeList; - + $this->checkIsAllowedForSync = $checkIsAllowedForSync; } /** @@ -155,6 +163,7 @@ public function execute() $customSnippetPath = $read->getAbsolutePath(Config::CUSTOM_SNIPPET_PATH); $customSnippets = $this->config->getCustomSnippets($customSnippetPath); + $allowedSnippets = []; foreach ($snippets as $key => $value) { $priority = 50; if ($key == 'hash') { @@ -167,6 +176,7 @@ public function execute() 'priority' => $priority, 'content' => $value ]; + $allowedSnippets[] = $snippetData['name']; $this->api->uploadSnippet($clone->number, $snippetData); } @@ -183,9 +193,11 @@ public function execute() 'content' => $value, 'dynamic' => '0' ]; + $allowedSnippets[] = $customSnippetData['name']; $this->api->uploadSnippet($clone->number, $customSnippetData); } + $this->syncSnippets($allowedSnippets, $clone->number); $this->createGzipHeader($clone); $condition = [ @@ -334,4 +346,28 @@ private function createGzipHeader($clone) $this->api->createHeader($clone->number, $headerData); } + + /** + * Remove disabled snippets from current vcl file + * + * @param array $allowedSnippets + * @param int $version + * @throws LocalizedException + */ + private function syncSnippets(array $allowedSnippets, int $version): void + { + $snippets = $this->api->getSnippets($version); + + $currentActiveSnippets = []; + foreach ($snippets as $item) { + $currentActiveSnippets[] = $item->name; + } + $snippetsForDelete = array_diff($currentActiveSnippets, $allowedSnippets); + + foreach ($snippetsForDelete as $snippetName) { + if ($this->checkIsAllowedForSync->checkIsAllowed($snippetName)) { + $this->api->removeSnippet($version, $snippetName); + } + } + } } diff --git a/Model/Api.php b/Model/Api.php index 853e94e2..f963d4ed 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -1623,4 +1623,19 @@ private function extractErrorDetails($responseBody, $responseMessage) } return $responseMessage; } + + /** + * Get all VCL snippets + * + * @param int $version + * @return bool|mixed + * @throws LocalizedException + */ + public function getSnippets(int $version) + { + $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/snippet'; + $result = $this->_fetch($url, 'GET'); + + return $result; + } } diff --git a/Model/Snippet/CheckIsAllowedForSync.php b/Model/Snippet/CheckIsAllowedForSync.php new file mode 100644 index 00000000..2ae9aaf7 --- /dev/null +++ b/Model/Snippet/CheckIsAllowedForSync.php @@ -0,0 +1,48 @@ +predefinedList = $predefinedList; + } + + /** + * Check is snippet name allowed for sync / removing + * + * @param string $snippetName + * @return bool + */ + public function checkIsAllowed(string $snippetName): bool + { + if (strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { + return false; + } + + foreach ($this->predefinedList as $disableName) { + if (strpos($snippetName, $disableName) === 0) { + return false; + } + } + return true; + } +} diff --git a/etc/di.xml b/etc/di.xml index f2ee2e35..eeb5b9cd 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -91,4 +91,17 @@ + + + + + magentomodule_force_tls + magentomodule_gzip_safety + magentomodule_image_optimization + magentomodule_rate_limiting + magentomodule_error_page + magentomodule_waf + + + From 3acbe112bd628aefac995e98694cc58cde5be143 Mon Sep 17 00:00:00 2001 From: Domagoj Potkoc Date: Thu, 23 Jun 2022 10:43:15 +0200 Subject: [PATCH 2/5] fix/improvement for removing custom shippets after VCL upload action --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 2 +- Model/Snippet/CheckIsAllowedForSync.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 86c5a421..08820d5d 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -362,7 +362,7 @@ private function syncSnippets(array $allowedSnippets, int $version): void foreach ($snippets as $item) { $currentActiveSnippets[] = $item->name; } - $snippetsForDelete = array_diff($currentActiveSnippets, $allowedSnippets); + $snippetsForDelete = \array_diff($currentActiveSnippets, $allowedSnippets); foreach ($snippetsForDelete as $snippetName) { if ($this->checkIsAllowedForSync->checkIsAllowed($snippetName)) { diff --git a/Model/Snippet/CheckIsAllowedForSync.php b/Model/Snippet/CheckIsAllowedForSync.php index 2ae9aaf7..2a6ec226 100644 --- a/Model/Snippet/CheckIsAllowedForSync.php +++ b/Model/Snippet/CheckIsAllowedForSync.php @@ -34,12 +34,12 @@ public function __construct(array $predefinedList = []) */ public function checkIsAllowed(string $snippetName): bool { - if (strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { + if (\strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { return false; } foreach ($this->predefinedList as $disableName) { - if (strpos($snippetName, $disableName) === 0) { + if (\strpos($snippetName, $disableName) === 0) { return false; } } From 323c6c2318e7849cb7456669517787d2b761ea4a Mon Sep 17 00:00:00 2001 From: Domagoj Potkoc Date: Thu, 23 Jun 2022 12:15:53 +0200 Subject: [PATCH 3/5] renamed method name --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 2 +- Model/Snippet/CheckIsAllowedForSync.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 08820d5d..28f766a1 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -365,7 +365,7 @@ private function syncSnippets(array $allowedSnippets, int $version): void $snippetsForDelete = \array_diff($currentActiveSnippets, $allowedSnippets); foreach ($snippetsForDelete as $snippetName) { - if ($this->checkIsAllowedForSync->checkIsAllowed($snippetName)) { + if ($this->checkIsAllowedForSync->checkIsAllowedForRemoving($snippetName)) { $this->api->removeSnippet($version, $snippetName); } } diff --git a/Model/Snippet/CheckIsAllowedForSync.php b/Model/Snippet/CheckIsAllowedForSync.php index 2a6ec226..07a43ce3 100644 --- a/Model/Snippet/CheckIsAllowedForSync.php +++ b/Model/Snippet/CheckIsAllowedForSync.php @@ -32,7 +32,7 @@ public function __construct(array $predefinedList = []) * @param string $snippetName * @return bool */ - public function checkIsAllowed(string $snippetName): bool + public function checkIsAllowedForRemoving(string $snippetName): bool { if (\strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { return false; From 3445f36f1876e8a5c50559d1fc586ab520936ca4 Mon Sep 17 00:00:00 2001 From: Domagoj Potkoc Date: Tue, 28 Jun 2022 11:25:08 +0200 Subject: [PATCH 4/5] WP --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 14 +- Model/Api.php | 175 +++++++++--------- ...owedForSync.php => BuiltInSnippetList.php} | 16 +- etc/di.xml | 14 +- 4 files changed, 111 insertions(+), 108 deletions(-) rename Model/Snippet/{CheckIsAllowedForSync.php => BuiltInSnippetList.php} (65%) diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 28f766a1..54d857df 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -36,7 +36,7 @@ use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Config\Model\ResourceModel\Config as CoreConfig; use Magento\Framework\App\Cache\TypeListInterface; -use Fastly\Cdn\Model\Snippet\CheckIsAllowedForSync; +use Fastly\Cdn\Model\Snippet\BuiltInSnippetList; /** * Class Upload @@ -92,9 +92,9 @@ class Upload extends Action private $typeList; /** - * @var CheckIsAllowedForSync + * @var BuiltInSnippetList */ - private $checkIsAllowedForSync; + private $builtInSnippetList; /** * Upload constructor. @@ -111,7 +111,7 @@ class Upload extends Action * @param Filesystem $filesystem * @param CoreConfig $coreConfig * @param TypeListInterface $typeList - * @param CheckIsAllowedForSync $checkIsAllowedForSync + * @param BuiltInSnippetList $builtInSnippetList */ public function __construct( Context $context, @@ -126,7 +126,7 @@ public function __construct( Filesystem $filesystem, CoreConfig $coreConfig, TypeListInterface $typeList, - CheckIsAllowedForSync $checkIsAllowedForSync + BuiltInSnippetList $builtInSnippetList ) { $this->request = $request; $this->resultJson = $resultJsonFactory; @@ -140,7 +140,7 @@ public function __construct( parent::__construct($context); $this->coreConfig = $coreConfig; $this->typeList = $typeList; - $this->checkIsAllowedForSync = $checkIsAllowedForSync; + $this->builtInSnippetList = $builtInSnippetList; } /** @@ -365,7 +365,7 @@ private function syncSnippets(array $allowedSnippets, int $version): void $snippetsForDelete = \array_diff($currentActiveSnippets, $allowedSnippets); foreach ($snippetsForDelete as $snippetName) { - if ($this->checkIsAllowedForSync->checkIsAllowedForRemoving($snippetName)) { + if (!$this->builtInSnippetList->checkIsBuiltInSnippet($snippetName)) { $this->api->removeSnippet($version, $snippetName); } } diff --git a/Model/Api.php b/Model/Api.php index f963d4ed..ae57b555 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -18,6 +18,7 @@ * @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com) * @license BSD, see LICENSE_FASTLY_CDN.txt */ + namespace Fastly\Cdn\Model; use Magento\Framework\Exception\LocalizedException; @@ -36,10 +37,10 @@ */ class Api { - const FASTLY_HEADER_AUTH = 'Fastly-Key'; - const FASTLY_HEADER_TOKEN = 'X-Purge-Token'; + const FASTLY_HEADER_AUTH = 'Fastly-Key'; + const FASTLY_HEADER_TOKEN = 'X-Purge-Token'; const FASTLY_HEADER_SOFT_PURGE = 'Fastly-Soft-Purge'; - const PURGE_TIMEOUT = 10; + const PURGE_TIMEOUT = 10; const PURGE_TOKEN_LIFETIME = 30; const FASTLY_MAX_HEADER_KEY_SIZE = 256; @@ -103,7 +104,8 @@ public function __construct( Vcl $vcl, Session $authSession, State $state - ) { + ) + { $this->config = $config; $this->curlFactory = $curlFactory; $this->logger = $logger; @@ -192,7 +194,7 @@ public function cleanBySurrogateKey($keys) $parts = $num / self::FASTLY_MAX_HEADER_KEY_SIZE; $additional = ($parts > (int)$parts) ? 1 : 0; $parts = (int)$parts + (int)$additional; - $chunks = ceil($num/$parts); + $chunks = ceil($num / $parts); $collection = array_chunk($keys, $chunks); } else { $collection = [$keys]; @@ -283,20 +285,20 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload if ($method == 'PURGE') { // create purge token - $expiration = time() + self::PURGE_TOKEN_LIFETIME; + $expiration = time() + self::PURGE_TOKEN_LIFETIME; $zendUri = \Zend_Uri::factory($uri); $path = $zendUri->getPath(); $stringToSign = $path . $expiration; - $signature = hash_hmac('sha1', $stringToSign, $this->config->getServiceId()); - $token = $expiration . '_' . urlencode($signature); + $signature = hash_hmac('sha1', $stringToSign, $this->config->getServiceId()); + $token = $expiration . '_' . urlencode($signature); $headers = [ self::FASTLY_HEADER_TOKEN . ': ' . $token ]; } else { // set headers $headers = [ - self::FASTLY_HEADER_AUTH . ': ' . $this->config->getApiKey() + self::FASTLY_HEADER_AUTH . ': ' . $this->config->getApiKey() ]; } @@ -337,7 +339,7 @@ private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload } if ($this->config->areWebHooksEnabled() && $this->config->canPublishPurgeChanges()) { - $this->sendWebHook('*initiated ' . $type .'*'); + $this->sendWebHook('*initiated ' . $type . '*'); if ($this->config->canPublishPurgeDebugBacktrace() == false) { return $result; @@ -398,7 +400,7 @@ public function checkServiceDetails($test = false, $serviceId = null, $apiKey = */ public function cloneVersion($curVersion) { - $url = $this->_getApiServiceUri() . 'version/'.rawurlencode($curVersion).'/clone'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($curVersion) . '/clone'; $result = $this->_fetch($url, \Zend_Http_Client::PUT); if (!$result) { @@ -434,7 +436,7 @@ public function addComment($version, $comment) */ public function uploadVcl($version, $vcl) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/vcl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/vcl'; $result = $this->_fetch($url, 'POST', $vcl); return $result; @@ -450,7 +452,7 @@ public function uploadVcl($version, $vcl) */ public function setVclAsMain($version, $name) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/vcl/' .rawurlencode($name). '/main'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/vcl/' . rawurlencode($name) . '/main'; $result = $this->_fetch($url, 'PUT'); return $result; @@ -464,7 +466,7 @@ public function setVclAsMain($version, $name) */ public function validateServiceVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/validate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/validate'; $result = $this->_fetch($url, 'GET'); if ($result->status == 'error') { @@ -479,7 +481,7 @@ public function validateServiceVersion($version) */ public function containerValidateServiceVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/validate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/validate'; $result = $this->_fetch($url, 'GET'); return $result; @@ -494,7 +496,7 @@ public function containerValidateServiceVersion($version) */ public function activateVersion($version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/activate'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/activate'; $result = $this->_fetch($url, 'PUT'); return $result; @@ -532,18 +534,18 @@ public function uploadSnippet($version, array $snippet) $snippetName = $snippet['name']; $checkIfExists = $this->hasSnippet($version, $snippetName); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/snippet'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; if (!isset($snippet['dynamic']) || $snippet['dynamic'] != 1) { - $url .= '/'.rawurlencode($snippetName); + $url .= '/' . rawurlencode($snippetName); unset($snippet['name'], $snippet['type'], $snippet['dynamic']); } else { $snippet['name'] = $this->getSnippet($version, $snippetName)->id; - $url = $this->_getApiServiceUri(). 'snippet' . '/'.rawurlencode($snippet['name']); + $url = $this->_getApiServiceUri() . 'snippet' . '/' . rawurlencode($snippet['name']); } } @@ -564,7 +566,7 @@ public function uploadSnippet($version, array $snippet) */ public function getSnippet($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/snippet/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -579,7 +581,7 @@ public function getSnippet($version, $name) */ public function updateSnippet(array $snippet) { - $url = $this->_getApiServiceUri(). 'snippet' . '/'.rawurlencode($snippet['name']); + $url = $this->_getApiServiceUri() . 'snippet' . '/' . rawurlencode($snippet['name']); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $snippet); if (!$result) { @@ -592,8 +594,8 @@ public function updateSnippet(array $snippet) /** * Performs a lookup to determine if VCL snippet exists * - * @param string $version Fastly version - * @param string $name VCL snippet name + * @param string $version Fastly version + * @param string $name VCL snippet name * * @return bool * @throws LocalizedException @@ -619,7 +621,7 @@ public function hasSnippet($version, $name) */ public function removeSnippet($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/snippet/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -635,12 +637,12 @@ public function removeSnippet($version, $name) public function createCondition($version, array $condition) { $checkIfExists = $this->getCondition($version, $condition['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/condition'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($condition['name']); + $url .= '/' . rawurlencode($condition['name']); } $result = $this->_fetch($url, $verb, $condition); @@ -662,7 +664,7 @@ public function createCondition($version, array $condition) */ public function getCondition($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/condition/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -679,13 +681,13 @@ public function getCondition($version, $name) public function createHeader($version, array $condition) { $checkIfExists = $this->getHeader($version, $condition['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/header'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/header'; if ($checkIfExists === false) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($condition['name']); + $url .= '/' . rawurlencode($condition['name']); } $result = $this->_fetch($url, $verb, $condition); @@ -703,7 +705,7 @@ public function createHeader($version, array $condition) */ public function getHeader($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/header/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/header/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -720,12 +722,12 @@ public function getHeader($version, $name) public function createResponse($version, array $response) { $checkIfExists = $this->getResponse($version, $response['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/response_object'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/response_object'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($response['name']); + $url .= '/' . rawurlencode($response['name']); } $result = $this->_fetch($url, $verb, $response); @@ -743,7 +745,7 @@ public function createResponse($version, array $response) */ public function getResponse($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/response_object/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/response_object/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -758,12 +760,12 @@ public function getResponse($version, $name) public function createRequest($version, $request) { $checkIfExists = $this->getRequest($version, $request['name']); - $url = $this->_getApiServiceUri(). 'version/' .rawurlencode($version). '/request_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings'; if (!$checkIfExists) { $verb = \Zend_Http_Client::POST; } else { $verb = \Zend_Http_Client::PUT; - $url .= '/'.rawurlencode($request['name']); + $url .= '/' . rawurlencode($request['name']); } $result = $this->_fetch($url, $verb, $request); @@ -776,15 +778,15 @@ public function createRequest($version, $request) /** * Retrieves a specific Request settings object. * - * @param string $version Fastly version - * @param string $name Request name + * @param string $version Fastly version + * @param string $name Request name * * @return bool * @throws LocalizedException */ public function getRequest($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/request_settings/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -797,7 +799,7 @@ public function getRequest($version, $name) */ public function getAllConditions($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/condition'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/condition'; $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -810,7 +812,7 @@ public function getAllConditions($version) */ public function getAllDomains($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain'; $result = $this->_fetch($url, \Zend_Http_Client::GET, '', false, null, false); return $result; @@ -824,7 +826,7 @@ public function getAllDomains($version) */ public function deleteDomain($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -838,7 +840,7 @@ public function deleteDomain($version, $name) */ public function createDomain($version, $data) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/domain'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/domain'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $data); return $result; @@ -852,7 +854,7 @@ public function createDomain($version, $data) */ public function deleteRequest($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/request_settings/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/request_settings/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); if (!$result) { @@ -869,7 +871,7 @@ public function deleteRequest($version, $name) */ public function getBackends($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -886,7 +888,7 @@ public function getBackends($version) */ public function configureBackend($params, $version, $old_name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend/' . rawurlencode($old_name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend/' . rawurlencode($old_name); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $params); return $result; @@ -900,7 +902,7 @@ public function configureBackend($params, $version, $old_name) */ public function createBackend($params, $version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version). '/backend'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/backend'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -930,7 +932,7 @@ public function getAllLogEndpoints($version) $providers = $this->helper->getAvailableLogEndpointProviders(); $results = []; foreach ($providers as $type => $providerName) { - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); $endpoints = $this->_fetch($url, \Zend_Http_Client::GET); foreach ($endpoints as $endpoint) { $results[] = [ @@ -953,7 +955,7 @@ public function getLogEndpoints($version, $type) { $results = []; $providers = $this->helper->getAvailableLogEndpointProviders(); - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type); $endpoints = $this->_fetch($url, \Zend_Http_Client::GET); foreach ($endpoints as $endpoint) { $results[] = [ @@ -974,7 +976,7 @@ public function getLogEndpoints($version, $type) */ public function getLogEndpoint($version, $type, $name) { - $url = $this->_getApiServiceUri(). 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type) . '/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/logging/' . rawurlencode($type) . '/' . rawurlencode($name); return $this->_fetch($url, \Zend_Http_Client::GET); } @@ -1029,16 +1031,16 @@ public function sendWebHook($message) $storeName = $this->helper->getStoreName(); $storeUrl = $this->helper->getStoreUrl(); - $text = $messagePrefix.' user='.$currentUsername.' '.$message.' on <'.$storeUrl.'|Store> | '.$storeName; + $text = $messagePrefix . ' user=' . $currentUsername . ' ' . $message . ' on <' . $storeUrl . '|Store> | ' . $storeName; $headers = [ 'Content-type: application/json' ]; $body = json_encode([ - "text" => $text, + "text" => $text, "username" => "fastly-magento-bot", - "icon_emoji"=> ":airplane:" + "icon_emoji" => ":airplane:" ]); $client = $this->curlFactory->create(); @@ -1049,7 +1051,7 @@ public function sendWebHook($message) $responseCode = \Zend_Http_Response::extractCode($response); if ($responseCode != 200) { - $this->log->log(100, 'Failed to send message to the following Webhook: '.$url); + $this->log->log(100, 'Failed to send message to the following Webhook: ' . $url); } $client->close(); @@ -1065,7 +1067,7 @@ public function sendWebHook($message) */ public function createDictionary($version, $params) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -1081,7 +1083,7 @@ public function createDictionary($version, $params) */ public function deleteDictionary($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1096,7 +1098,7 @@ public function deleteDictionary($version, $name) */ public function dictionaryItemsList($dictionaryId) { - $url = $this->_getApiServiceUri(). 'dictionary/'.rawurlencode($dictionaryId).'/items'; + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/items'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1112,7 +1114,7 @@ public function dictionaryItemsList($dictionaryId) */ public function getSingleDictionary($version, $dictionaryName) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary/' . rawurlencode($dictionaryName); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary/' . rawurlencode($dictionaryName); $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1164,7 +1166,7 @@ public function checkAuthDictionaryPopulation($version) */ public function createDictionaryItems($dictionaryId, $params) { - $url = $this->_getApiServiceUri().'dictionary/'.rawurlencode($dictionaryId).'/items'; + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/items'; $result = $this->_fetch($url, \Zend_Http_Client::PATCH, $params); return $result; @@ -1179,7 +1181,7 @@ public function createDictionaryItems($dictionaryId, $params) */ public function getDictionaries($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/dictionary'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/dictionary'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1195,7 +1197,7 @@ public function getDictionaries($version) */ public function deleteDictionaryItem($dictionaryId, $itemKey) { - $url = $this->_getApiServiceUri(). 'dictionary/'. rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1212,7 +1214,7 @@ public function deleteDictionaryItem($dictionaryId, $itemKey) public function upsertDictionaryItem($dictionaryId, $itemKey, $itemValue) { $body = ['item_value' => $itemValue]; - $url = $this->_getApiServiceUri(). 'dictionary/'. rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); + $url = $this->_getApiServiceUri() . 'dictionary/' . rawurlencode($dictionaryId) . '/item/' . rawurlencode($itemKey); $result = $this->_fetch($url, \Zend_Http_Client::PUT, $body); if (!$result) { @@ -1229,7 +1231,7 @@ public function upsertDictionaryItem($dictionaryId, $itemKey, $itemValue) */ public function getSingleAcl($version, $acl) { - $url = $this->_getApiServiceUri(). 'version/'. $version . '/acl/' . $acl; + $url = $this->_getApiServiceUri() . 'version/' . $version . '/acl/' . $acl; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1245,7 +1247,7 @@ public function getSingleAcl($version, $acl) */ public function createAcl($version, $params) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $params); return $result; @@ -1260,7 +1262,7 @@ public function createAcl($version, $params) */ public function getAcls($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1276,7 +1278,7 @@ public function getAcls($version) */ public function deleteAcl($version, $name) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/acl/' . rawurlencode($name); + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/acl/' . rawurlencode($name); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1291,7 +1293,7 @@ public function deleteAcl($version, $name) */ public function aclItemsList($aclId) { - $url = $this->_getApiServiceUri() . 'acl/'. rawurlencode($aclId) . '/entries'; + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entries'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1320,7 +1322,7 @@ public function upsertAclItem($aclId, $itemValue, $negated, $comment = 'Added by $body['subnet'] = $subnet; } - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry'; + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry'; $result = $this->_fetch($url, \Zend_Http_Client::POST, $body); return $result; @@ -1336,7 +1338,7 @@ public function upsertAclItem($aclId, $itemValue, $negated, $comment = 'Added by */ public function deleteAclItem($aclId, $aclItemId) { - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); $result = $this->_fetch($url, \Zend_Http_Client::DELETE); return $result; @@ -1366,7 +1368,7 @@ public function updateAclItem($aclId, $aclItemId, $itemValue, $negated, $comment $body['subnet'] = $subnet; } - $url = $this->_getApiServiceUri(). 'acl/'. rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); + $url = $this->_getApiServiceUri() . 'acl/' . rawurlencode($aclId) . '/entry/' . rawurlencode($aclItemId); $result = $this->_fetch($url, \Zend_Http_Client::PATCH, json_encode($body)); return $result; @@ -1382,10 +1384,10 @@ public function updateAclItem($aclId, $aclItemId, $itemValue, $negated, $comment public function queryHistoricStats(array $parameters) { $uri = $this->_getHistoricalEndpoint() - . '?region='.rawurlencode($parameters['region']) - . '&from='.rawurlencode($parameters['from']) - . '&to='.rawurlencode($parameters['to']) - . '&by='.rawurlencode($parameters['sample_rate']); + . '?region=' . rawurlencode($parameters['region']) + . '&from=' . rawurlencode($parameters['from']) + . '&to=' . rawurlencode($parameters['to']) + . '&by=' . rawurlencode($parameters['sample_rate']); $result = $this->_fetch($uri); @@ -1423,7 +1425,7 @@ public function getParticularVersion($version) */ public function checkImageOptimizationStatus() { - $url = $this->_getApiServiceUri(). 'dynamic_io_settings'; + $url = $this->_getApiServiceUri() . 'dynamic_io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1438,7 +1440,7 @@ public function checkImageOptimizationStatus() */ public function getImageOptimizationDefaultConfigOptions($version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/io_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::GET); return $result; @@ -1454,7 +1456,7 @@ public function getImageOptimizationDefaultConfigOptions($version) */ public function configureImageOptimizationDefaultConfigOptions($params, $version) { - $url = $this->_getApiServiceUri(). 'version/'. rawurlencode($version) . '/io_settings'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/io_settings'; $result = $this->_fetch($url, \Zend_Http_Client::PATCH, $params); return $result; @@ -1505,12 +1507,12 @@ public function getLastErrorMessage() /** * Wrapper for API calls towards Fastly service * - * @param string $uri API Endpoint - * @param string $method HTTP Method for request - * @param mixed[]|string $body Content - * @param bool $test Use $testApiKey for request - * @param string $testApiKey API key to be tested - * @param bool $logError When set to false, prevents writing failed requests to log + * @param string $uri API Endpoint + * @param string $method HTTP Method for request + * @param mixed[]|string $body Content + * @param bool $test Use $testApiKey for request + * @param string $testApiKey API key to be tested + * @param bool $logError When set to false, prevents writing failed requests to log * * @return bool|mixed Returns false on failiure * @throws LocalizedException @@ -1522,7 +1524,8 @@ private function _fetch( $test = false, $testApiKey = null, $logError = true - ) { + ) + { $apiKey = ($test == true) ? $testApiKey : $this->config->getApiKey(); // Correctly format $body string @@ -1532,7 +1535,7 @@ private function _fetch( // Client headers $headers = [ - self::FASTLY_HEADER_AUTH . ': ' . $apiKey, + self::FASTLY_HEADER_AUTH . ': ' . $apiKey, 'Accept: application/json' ]; @@ -1605,7 +1608,7 @@ private function stackTrace($type) } } - $this->sendWebHook('*'. $type .' backtrace:*```' . implode("\n", $trace) . '```'); + $this->sendWebHook('*' . $type . ' backtrace:*```' . implode("\n", $trace) . '```'); } /** @@ -1633,7 +1636,7 @@ private function extractErrorDetails($responseBody, $responseMessage) */ public function getSnippets(int $version) { - $url = $this->_getApiServiceUri() . 'version/' .rawurlencode($version). '/snippet'; + $url = $this->_getApiServiceUri() . 'version/' . rawurlencode($version) . '/snippet'; $result = $this->_fetch($url, 'GET'); return $result; diff --git a/Model/Snippet/CheckIsAllowedForSync.php b/Model/Snippet/BuiltInSnippetList.php similarity index 65% rename from Model/Snippet/CheckIsAllowedForSync.php rename to Model/Snippet/BuiltInSnippetList.php index 07a43ce3..f649a747 100644 --- a/Model/Snippet/CheckIsAllowedForSync.php +++ b/Model/Snippet/BuiltInSnippetList.php @@ -6,9 +6,9 @@ use Fastly\Cdn\Model\Config; /** - * Class to check can snippet be deleted, predefined snippets can't be deleted + * Class to check is snippet builtin */ -class CheckIsAllowedForSync +class BuiltInSnippetList { /** @@ -17,7 +17,7 @@ class CheckIsAllowedForSync private $predefinedList; /** - * CheckIsAllowedForSync constructor. + * BuiltInSnippetList constructor. * * @param array $predefinedList */ @@ -27,22 +27,22 @@ public function __construct(array $predefinedList = []) } /** - * Check is snippet name allowed for sync / removing + * Check is builtin snippet * * @param string $snippetName * @return bool */ - public function checkIsAllowedForRemoving(string $snippetName): bool + public function checkIsBuiltInSnippet(string $snippetName): bool { if (\strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { - return false; + return true; } foreach ($this->predefinedList as $disableName) { if (\strpos($snippetName, $disableName) === 0) { - return false; + return true; } } - return true; + return false; } } diff --git a/etc/di.xml b/etc/di.xml index eeb5b9cd..2f66f95a 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -92,15 +92,15 @@ - + - magentomodule_force_tls - magentomodule_gzip_safety - magentomodule_image_optimization - magentomodule_rate_limiting - magentomodule_error_page - magentomodule_waf + magentomodule_force_tls + magentomodule_gzip_safety + magentomodule_image_optimization + magentomodule_rate_limiting + magentomodule_error_page + magentomodule_waf From 9c890a9e2f92c20a47b842eaa88c47bb43a0ae9d Mon Sep 17 00:00:00 2001 From: Domagoj Potkoc Date: Tue, 28 Jun 2022 13:02:25 +0200 Subject: [PATCH 5/5] WP --- Model/Api.php | 6 ++---- Model/Snippet/BuiltInSnippetList.php | 8 ++------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Model/Api.php b/Model/Api.php index ae57b555..99ae8089 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -104,8 +104,7 @@ public function __construct( Vcl $vcl, Session $authSession, State $state - ) - { + ) { $this->config = $config; $this->curlFactory = $curlFactory; $this->logger = $logger; @@ -1524,8 +1523,7 @@ private function _fetch( $test = false, $testApiKey = null, $logError = true - ) - { + ) { $apiKey = ($test == true) ? $testApiKey : $this->config->getApiKey(); // Correctly format $body string diff --git a/Model/Snippet/BuiltInSnippetList.php b/Model/Snippet/BuiltInSnippetList.php index f649a747..4880af17 100644 --- a/Model/Snippet/BuiltInSnippetList.php +++ b/Model/Snippet/BuiltInSnippetList.php @@ -34,12 +34,8 @@ public function __construct(array $predefinedList = []) */ public function checkIsBuiltInSnippet(string $snippetName): bool { - if (\strpos($snippetName, Config::FASTLY_MAGENTO_MODULE . '_') !== 0) { - return true; - } - - foreach ($this->predefinedList as $disableName) { - if (\strpos($snippetName, $disableName) === 0) { + foreach ($this->predefinedList as $builtinSnippet) { + if (\strpos($snippetName, $builtinSnippet) === 0) { return true; } }