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

Fix blocking issues on php 8.0 and up. #582

Merged
merged 5 commits into from
Dec 15, 2022
Merged
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
71 changes: 71 additions & 0 deletions Controller/Adminhtml/FastlyCdn/Blocking/AbstractBlocking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking;

use Fastly\Cdn\Model\Config;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;

abstract class AbstractBlocking extends Action
{
protected ConfigWriter $configWriter;

public function __construct(
Context $context,
ConfigWriter $configWriter
) {
$this->configWriter = $configWriter;

parent::__construct($context);
}

/**
* @param string[] $countryCodes
* @param string[] $acls
* @param int $blockingType
* @return string
*/
protected function prepareBlockedItems(array $countryCodes, array $acls, int $blockingType): string
{
$list = [];
foreach ($countryCodes as $countryCode) {
$list[] = sprintf('client.geo.country_code == "%s"', $countryCode);
}

foreach ($acls as $acl) {
$list[] = sprintf('req.http.Fastly-Client-Ip ~ %s', $acl);
}

$result = implode(' || ', $list);
if ($blockingType === 1 && !empty($result)) {
$result = sprintf('!(%s)', $result);
}

return $result;
}

protected function storeConfigArray(string $path, array $data): void
{
$this->configWriter->save(
$path,
implode(',', $data),
'default',
'0'
);
}

protected function getParamArray(string $param): array
{
$request = $this->getRequest();

$data = $request->getParam($param);
if (empty($data)) {
return [];
}

return array_map(static function ($row) {
return $row['value'];
}, $data);
}
}
100 changes: 11 additions & 89 deletions Controller/Adminhtml/FastlyCdn/Blocking/Blocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking
*/
class Blocking extends Action
class Blocking extends AbstractBlocking
{
/**
* @var Http
Expand All @@ -57,10 +57,6 @@ class Blocking extends Action
* @var Vcl
*/
private $vcl;
/**
* @var ConfigWriter
*/
private $configWriter;

/**
* Blocking constructor.
Expand All @@ -87,8 +83,7 @@ public function __construct(
$this->config = $config;
$this->api = $api;
$this->vcl = $vcl;
$this->configWriter = $configWriter;
parent::__construct($context);
parent::__construct($context, $configWriter);
}

/**
Expand All @@ -114,11 +109,13 @@ public function execute()
Config::VCL_BLOCKING_SNIPPET
);

$country_codes = $this->prepareCountryCodes($this->getRequest()->getParam('countries'));
$acls = $this->prepareAcls($this->getRequest()->getParam('acls'));
$countryCodes = $this->getParamArray('countries');
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_COUNTRY, $countryCodes);

$blockedItems = $country_codes . $acls;
$strippedBlockedItems = substr($blockedItems, 0, strrpos($blockedItems, '||', -1));
$acls = $this->getParamArray('acls');
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_ACL, $acls);

$blockedItems = $this->prepareBlockedItems($countryCodes, $acls, (int) $blockingType);

$this->configWriter->save(
Config::XML_FASTLY_BLOCKING_TYPE,
Expand All @@ -136,12 +133,9 @@ public function execute()
continue;
}

if ($strippedBlockedItems === '') {
$value = '';
} else {
$strippedBlockedItems = $this->config->processBlockedItems($strippedBlockedItems, $blockingType);
$value = str_replace('####BLOCKED_ITEMS####', $strippedBlockedItems, $value);
}
$value = $blockedItems !== '' ?
str_replace('####BLOCKED_ITEMS####', $blockedItems, $value) :
'';

$snippetData = [
'name' => $name,
Expand Down Expand Up @@ -188,78 +182,6 @@ public function execute()
}
}

/**
* Prepares ACL VCL snippets
*
* @param $blockedAcls
* @return string
*/
private function prepareAcls($blockedAcls)
{
$result = '';
$aclsArray = [];
$acls = '';

if ($blockedAcls != null) {
foreach ($blockedAcls as $key => $value) {
$aclsArray[] = $value['value'];
}
$acls = implode(',', $aclsArray);
}

$this->configWriter->save(
Config::XML_FASTLY_BLOCK_BY_ACL,
$acls,
'default',
'0'
);

if ($acls != '') {
$blockedAclsPieces = explode(",", $acls);
foreach ($blockedAclsPieces as $acl) {
$result .= ' req.http.Fastly-Client-Ip ~ ' . $acl . ' ||';
}
}

return $result;
}

/**
* Prepares blocked countries VCL snippet
*
* @param $blockedCountries
* @return string
*/
private function prepareCountryCodes($blockedCountries)
{
$result = '';
$countriesArray = [];
$countries = '';

if ($blockedCountries != null) {
foreach ($blockedCountries as $key => $value) {
$countriesArray[] = $value['value'];
}
$countries = implode(',', $countriesArray);
}

$this->configWriter->save(
Config::XML_FASTLY_BLOCK_BY_COUNTRY,
$countries,
'default',
'0'
);

if ($countries != '') {
$blockedCountriesPieces = explode(",", $countries);
foreach ($blockedCountriesPieces as $code) {
$result .= ' client.geo.country_code == "' . $code . '" ||';
}
}

return $result;
}

private function sendWebhook($checkIfSettingExists, $clone)
{
if ($this->config->areWebHooksEnabled() && $this->config->canPublishConfigChanges()) {
Expand Down
119 changes: 22 additions & 97 deletions Controller/Adminhtml/FastlyCdn/Blocking/UpdateBlocking.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,23 @@
*/
namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking;

use Magento\Backend\App\Action;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Model\Config;
use Magento\Backend\App\Action\Context;
use Magento\Config\App\Config\Type\System as SystemConfig;
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Controller\Result\JsonFactory;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Helper\Vcl;
use Magento\Framework\App\Config\Storage\WriterInterface as ConfigWriter;
use Magento\Framework\App\Cache\TypeListInterface as CacheTypeList;
use Magento\Config\App\Config\Type\System as SystemConfig;
use Magento\Framework\Exception\LocalizedException;

/**
* Class UpdateBlocking
*
* @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Blocking
*/
class UpdateBlocking extends Action
class UpdateBlocking extends AbstractBlocking
{
/**
* @var Http
Expand All @@ -59,10 +58,6 @@ class UpdateBlocking extends Action
* @var Vcl
*/
private $vcl;
/**
* @var ConfigWriter
*/
private $configWriter;
/**
* @var CacheTypeList
*/
Expand Down Expand Up @@ -104,7 +99,7 @@ public function __construct(
$this->configWriter = $configWriter;
$this->cacheTypeList = $cacheTypeList;
$this->systemConfig = $systemConfig;
parent::__construct($context);
parent::__construct($context, $configWriter);
}

/**
Expand All @@ -118,34 +113,36 @@ public function execute()
try {
$service = $this->api->checkServiceDetails();
$currActiveVersion = $this->vcl->determineVersions($service->versions);
if (!isset($currActiveVersion['active_version'])) {
throw new LocalizedException(__('No version is currently active.'));
}

$snippet = $this->config->getVclSnippets(
Config::VCL_BLOCKING_PATH,
Config::VCL_BLOCKING_SNIPPET
);

$country_codes = $this->prepareCountryCodes($this->request->getParam('countries'));
$acls = $this->prepareAcls($this->request->getParam('acls'));

$blockedItems = $country_codes . $acls;
$strippedBlockedItems = substr($blockedItems, 0, strrpos($blockedItems, '||', -1));
$blockingType = $this->request->getParam('blocking_type');

$this->configWriter->save(
Config::XML_FASTLY_BLOCKING_TYPE,
$blockingType,
'default',
'0'
);

$countryCodes = $this->getParamArray('countries');
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_COUNTRY, $countryCodes);

$acls = $this->getParamArray('acls');
$this->storeConfigArray(Config::XML_FASTLY_BLOCK_BY_ACL, $acls);

$blockedItems = $this->prepareBlockedItems($countryCodes, $acls, (int) $blockingType);

// Add blocking snippet
foreach ($snippet as $key => $value) {
if ($strippedBlockedItems === '') {
$value = '';
} else {
$strippedBlockedItems = $this->config->processBlockedItems($strippedBlockedItems, $blockingType);
$value = str_replace('####BLOCKED_ITEMS####', $strippedBlockedItems, $value);
}
$value = $blockedItems !== '' ?
str_replace('####BLOCKED_ITEMS####', $blockedItems, $value) :
'';

$snippetName = Config::FASTLY_MAGENTO_MODULE . '_blocking_' . $key;
$snippetId = $this->api->getSnippet($currActiveVersion['active_version'], $snippetName);
Expand Down Expand Up @@ -176,76 +173,4 @@ public function execute()
]);
}
}

/**
* Prepares ACL VCL snippets
*
* @param $blockedAcls
* @return string
*/
private function prepareAcls($blockedAcls)
{
$result = '';
$aclsArray = [];
$acls = '';

if ($blockedAcls != null) {
foreach ($blockedAcls as $key => $value) {
$aclsArray[] = $value['value'];
}
$acls = implode(',', $aclsArray);
}

$this->configWriter->save(
Config::XML_FASTLY_BLOCK_BY_ACL,
$acls,
'default',
'0'
);

if ($acls != '') {
$blockedAclsPieces = explode(",", $acls);
foreach ($blockedAclsPieces as $acl) {
$result .= ' req.http.Fastly-Client-Ip ~ ' . $acl . ' ||';
}
}

return $result;
}

/**
* Prepares blocked countries VCL snippet
*
* @param $blockedCountries
* @return string
*/
private function prepareCountryCodes($blockedCountries)
{
$result = '';
$countriesArray = [];
$countries = '';

if ($blockedCountries != null) {
foreach ($blockedCountries as $key => $value) {
$countriesArray[] = $value['value'];
}
$countries = implode(',', $countriesArray);
}

$this->configWriter->save(
Config::XML_FASTLY_BLOCK_BY_COUNTRY,
$countries,
'default',
'0'
);

if ($countries != '') {
$blockedCountriesPieces = explode(",", $countries);
foreach ($blockedCountriesPieces as $code) {
$result .= ' client.geo.country_code == "' . $code . '" ||';
}
}

return $result;
}
}
Loading