Skip to content

Commit b5b7d06

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent 417044d commit b5b7d06

File tree

13 files changed

+748
-187
lines changed

13 files changed

+748
-187
lines changed

app/code/Magento/Quote/Model/Backpressure/OrderLimitConfigManager.php

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Magento\Framework\App\Backpressure\ContextInterface;
1212
use Magento\Framework\App\Backpressure\SlidingWindow\LimitConfig;
1313
use Magento\Framework\App\Backpressure\SlidingWindow\LimitConfigManagerInterface;
14+
use Magento\Framework\App\Backpressure\SlidingWindow\RequestLoggerInterface;
1415
use Magento\Framework\App\Config\ScopeConfigInterface;
15-
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\App\DeploymentConfig;
17+
use Magento\Framework\Exception\FileSystemException;
1618
use Magento\Framework\Exception\RuntimeException;
1719
use Magento\Store\Model\ScopeInterface;
1820

@@ -28,12 +30,21 @@ class OrderLimitConfigManager implements LimitConfigManagerInterface
2830
*/
2931
private ScopeConfigInterface $config;
3032

33+
/**
34+
* @var DeploymentConfig
35+
*/
36+
private DeploymentConfig $deploymentConfig;
37+
3138
/**
3239
* @param ScopeConfigInterface $config
40+
* @param DeploymentConfig $deploymentConfig
3341
*/
34-
public function __construct(ScopeConfigInterface $config)
35-
{
42+
public function __construct(
43+
ScopeConfigInterface $config,
44+
DeploymentConfig $deploymentConfig
45+
) {
3646
$this->config = $config;
47+
$this->deploymentConfig = $deploymentConfig;
3748
}
3849

3950
/**
@@ -62,73 +73,50 @@ public function readLimit(ContextInterface $context): LimitConfig
6273
* Checks if enforcement enabled for the current store
6374
*
6475
* @return bool
76+
* @throws RuntimeException
77+
* @throws FileSystemException
6578
*/
6679
public function isEnforcementEnabled(): bool
6780
{
81+
$loggerType = $this->deploymentConfig->get(RequestLoggerInterface::CONFIG_PATH_BACKPRESSURE_LOGGER);
6882
$enabled = $this->config->isSetFlag('sales/backpressure/enabled', ScopeInterface::SCOPE_STORE);
69-
if (!$enabled) {
70-
return false;
71-
}
72-
73-
try {
74-
$this->fetchPeriod();
75-
$this->fetchAuthenticatedLimit();
76-
$this->fetchGuestLimit();
77-
} catch (RuntimeException $ex) {
78-
return false;
83+
if ($loggerType && $enabled) {
84+
return true;
7985
}
8086

81-
return true;
87+
return false;
8288
}
8389

8490
/**
8591
* Limit for authenticated customers
8692
*
8793
* @return int
88-
* @throws RuntimeException
8994
*/
9095
private function fetchAuthenticatedLimit(): int
9196
{
92-
$value = (int)$this->config->getValue('sales/backpressure/limit', ScopeInterface::SCOPE_STORE);
93-
if ($value <= 0) {
94-
throw new RuntimeException(__("Invalid order backpressure limit config"));
95-
}
96-
97-
return $value;
97+
return (int)$this->config->getValue('sales/backpressure/limit', ScopeInterface::SCOPE_STORE);
9898
}
9999

100100
/**
101101
* Limit for guests
102102
*
103103
* @return int
104-
* @throws RuntimeException
105104
*/
106105
private function fetchGuestLimit(): int
107106
{
108-
$value = (int)$this->config->getValue(
107+
return (int)$this->config->getValue(
109108
'sales/backpressure/guest_limit',
110109
ScopeInterface::SCOPE_STORE
111110
);
112-
if ($value <= 0) {
113-
throw new RuntimeException(__("Invalid order backpressure guest limit config"));
114-
}
115-
116-
return $value;
117111
}
118112

119113
/**
120114
* Counter reset period
121115
*
122116
* @return int
123-
* @throws RuntimeException
124117
*/
125118
private function fetchPeriod(): int
126119
{
127-
$value = (int)$this->config->getValue('sales/backpressure/period', ScopeInterface::SCOPE_STORE);
128-
if ($value <= 0) {
129-
throw new RuntimeException(__("Invalid order backpressure counter reset period config"));
130-
}
131-
132-
return $value;
120+
return (int)$this->config->getValue('sales/backpressure/period', ScopeInterface::SCOPE_STORE);
133121
}
134122
}

app/etc/di.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,8 +1980,8 @@
19801980
</type>
19811981
<preference for="Magento\Framework\App\BackpressureEnforcerInterface"
19821982
type="Magento\Framework\App\Backpressure\SlidingWindow\SlidingWindowEnforcer"/>
1983-
<preference for="Magento\Framework\App\Backpressure\SlidingWindow\RequestLoggerInterface"
1984-
type="Magento\Framework\App\Backpressure\SlidingWindow\CacheRequestLogger"/>
1983+
<preference for="Magento\Framework\App\Backpressure\SlidingWindow\RequestLoggerFactoryInterface"
1984+
type="Magento\Framework\App\Backpressure\SlidingWindow\RequestLoggerFactory"/>
19851985
<preference for="Magento\Framework\App\Backpressure\SlidingWindow\LimitConfigManagerInterface"
19861986
type="Magento\Framework\App\Backpressure\SlidingWindow\CompositeLimitConfigManager"/>
19871987
<preference for="Magento\Framework\App\Request\Backpressure\RequestTypeExtractorInterface"
@@ -1993,9 +1993,11 @@
19931993
<argument name="extractors" xsi:type="array" />
19941994
</arguments>
19951995
</type>
1996-
<type name="Magento\Framework\App\Backpressure\SlidingWindow\CacheRequestLogger">
1996+
<type name="Magento\Framework\App\Backpressure\SlidingWindow\RequestLoggerFactory">
19971997
<arguments>
1998-
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
1998+
<argument name="types" xsi:type="array">
1999+
<item name="redis" xsi:type="string">\Magento\Framework\App\Backpressure\SlidingWindow\RedisRequestLogger</item>
2000+
</argument>
19992001
</arguments>
20002002
</type>
20012003
<preference for="Magento\Framework\Filter\Input\PurifierInterface" type="Magento\Framework\Filter\Input\Purifier"/>

dev/tests/integration/testsuite/Magento/Framework/App/Backpressure/SlidingWindow/CacheRequestLoggerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class CacheRequestLoggerTest extends TestCase
2020
{
2121
/**
22-
* @var CacheRequestLogger
22+
* @var RedisRequestLogger
2323
*/
2424
private $model;
2525

@@ -35,7 +35,7 @@ protected function setUp(): void
3535
{
3636
parent::setUp();
3737

38-
$this->model = Bootstrap::getObjectManager()->get(CacheRequestLogger::class);
38+
$this->model = Bootstrap::getObjectManager()->get(RedisRequestLogger::class);
3939
}
4040

4141
/**

lib/internal/Magento/Framework/App/Backpressure/SlidingWindow/CacheRequestLogger.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)