Skip to content

Commit ec3b557

Browse files
committed
B2B-3035: Add Salting Mechanism to GraphQL Resolver Cache Key Generation
1 parent 6b94e66 commit ec3b557

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/CacheKey/Calculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function calculateCacheKey(?array $parentData = null): ?string
8787
$this->initializeFactorProviderInstances();
8888
$factors = $this->getFactors($parentData);
8989
$salt = (string)$this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
90-
$keysString = strtoupper(implode('|', array_values($factors))). "|$salt";
90+
$keysString = strtoupper(implode('|', array_values($factors))) . "|$salt";
9191
return hash('sha256', $keysString);
9292
} catch (\Throwable $e) {
9393
throw new CalculationException($e->getMessage(), $e->getCode(), $e);

dev/tests/integration/testsuite/Magento/GraphQlResolverCache/Model/Resolver/Result/Cache/KeyCalculator/ProviderTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use Magento\CustomerGraphQl\Model\Resolver\Customer;
1111
use Magento\CustomerGraphQl\Model\Resolver\CustomerAddresses;
12+
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\Config\ConfigOptionsListConstants;
1214
use Magento\Framework\GraphQl\Query\ResolverInterface;
1315
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator;
1416
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\Provider;
@@ -106,7 +108,12 @@ public function testProviderNonGenericKey()
106108

107109
$this->objectManager->addSharedInstance($storeFactorMock, StoreProvider::class);
108110
$this->objectManager->addSharedInstance($currencyFactorMock, CurrencyProvider::class);
109-
$expectedKey = hash('sha256', strtoupper(implode('|', ['currency' => 'USD', 'store' => 'default'])));
111+
$salt = $this->objectManager->get(DeploymentConfig::class)
112+
->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
113+
$expectedKey = hash(
114+
'sha256',
115+
strtoupper(implode('|', ['currency' => 'USD', 'store' => 'default'])) . "|$salt"
116+
);
110117
$calc = $this->provider->getKeyCalculatorForResolver($resolver);
111118
$key = $calc->calculateCacheKey();
112119
$this->assertNotEmpty($key);

dev/tests/integration/testsuite/Magento/GraphQlResolverCache/Model/Resolver/Result/Cache/KeyCalculatorTest.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\GraphQlResolverCache\Model\Resolver\Result\Cache;
99

10+
use Magento\Framework\App\DeploymentConfig;
11+
use Magento\Framework\Config\ConfigOptionsListConstants;
1012
use Magento\GraphQl\Model\Query\ContextFactoryInterface;
1113
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\CalculationException;
1214
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator;
@@ -15,7 +17,6 @@
1517
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\ParentValue\ProcessedValueFactorInterface;
1618
use Magento\GraphQlResolverCache\Model\Resolver\Result\ValueProcessorInterface;
1719
use Magento\TestFramework\Helper\Bootstrap;
18-
use Psr\Log\LoggerInterface;
1920

2021
/**
2122
* Test for graphql resolver-level cache key calculator.
@@ -183,6 +184,8 @@ private function resetMocksForObjectManager(array $factorDataArray)
183184
*/
184185
public function keyFactorDataProvider()
185186
{
187+
$salt = Bootstrap::getObjectManager()->get(DeploymentConfig::class)
188+
->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
186189
return [
187190
'no factors' => [
188191
'factorProviders' => [],
@@ -198,7 +201,7 @@ public function keyFactorDataProvider()
198201
],
199202
],
200203
'parentResolverData' => null,
201-
'expectedCacheKey' => hash('sha256', strtoupper('testValue')),
204+
'expectedCacheKey' => hash('sha256', strtoupper('testValue') . "|$salt"),
202205
],
203206
'unsorted multiple factors' => [
204207
'factorProviders' => [
@@ -219,7 +222,10 @@ public function keyFactorDataProvider()
219222
],
220223
],
221224
'parentResolverData' => null,
222-
'expectedCacheKey' => hash('sha256', strtoupper('a_testValue|b_testValue|c_testValue')),
225+
'expectedCacheKey' => hash(
226+
'sha256',
227+
strtoupper('a_testValue|b_testValue|c_testValue') . "|$salt"
228+
),
223229
],
224230
'unsorted multiple factors with parent data' => [
225231
'factorProviders' => [
@@ -242,7 +248,10 @@ public function keyFactorDataProvider()
242248
'parentResolverData' => [
243249
'object_id' => 123
244250
],
245-
'expectedCacheKey' => hash('sha256', strtoupper('a_testValue|object_123|c_testValue')),
251+
'expectedCacheKey' => hash(
252+
'sha256',
253+
strtoupper('a_testValue|object_123|c_testValue') . "|$salt"
254+
),
246255
],
247256
'unsorted multifactor with no parent data and parent factored interface' => [
248257
'factorProviders' => [
@@ -263,7 +272,10 @@ public function keyFactorDataProvider()
263272
],
264273
],
265274
'parentResolverData' => null,
266-
'expectedCacheKey' => hash('sha256', strtoupper('a_testValue|some value|c_testValue')),
275+
'expectedCacheKey' => hash(
276+
'sha256',
277+
strtoupper('a_testValue|some value|c_testValue') . "|$salt"
278+
),
267279
],
268280
];
269281
}
@@ -302,7 +314,10 @@ public function testValueProcessingIsCalledForParentValueFromCache()
302314
]);
303315

304316
$key = $keyCalculator->calculateCacheKey($value);
305-
$this->assertEquals('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', $key);
317+
$salt = Bootstrap::getObjectManager()->get(DeploymentConfig::class)
318+
->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
319+
$expectedResult = hash('sha256', "|$salt");
320+
$this->assertEquals($expectedResult, $key);
306321

307322
$this->objectManager->removeSharedInstance('TestValueFactorMock');
308323
$this->objectManager->removeSharedInstance('TestContextFactorMock');
@@ -366,7 +381,10 @@ public function testValueProcessingIsNotCalledForParentValueFromResolver()
366381
]);
367382

368383
$key = $keyCalculator->calculateCacheKey($value);
369-
$this->assertEquals('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', $key);
384+
$salt = Bootstrap::getObjectManager()->get(DeploymentConfig::class)
385+
->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
386+
$expectedResult = hash('sha256', "|$salt");
387+
$this->assertEquals($expectedResult, $key);
370388

371389
$this->objectManager->removeSharedInstance('TestValueFactorMock');
372390
$this->objectManager->removeSharedInstance('TestContextFactorMock');
@@ -404,7 +422,10 @@ public function testValueProcessingIsSkippedForContextOnlyFactors()
404422
]);
405423

406424
$key = $keyCalculator->calculateCacheKey($value);
407-
$this->assertEquals('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', $key);
425+
$salt = Bootstrap::getObjectManager()->get(DeploymentConfig::class)
426+
->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
427+
$expectedResult = hash('sha256', "|$salt");
428+
$this->assertEquals($expectedResult, $key);
408429

409430
$this->objectManager->removeSharedInstance('TestContextFactorMock');
410431
}

0 commit comments

Comments
 (0)