From e881f5774c4124b7d4af30a95ce679996a8fac92 Mon Sep 17 00:00:00 2001 From: Senthil Kumar Muppidathi Date: Tue, 14 Jan 2025 15:12:25 +0700 Subject: [PATCH] Fix for issue #39530 to avoid regenerating admin grid flat table Updating indexer state hash upon creation of new encryption key from admin --- .../Model/ResourceModel/Key/Change.php | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php b/app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php index 0d916389af99..7037ae967b88 100644 --- a/app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php +++ b/app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php @@ -14,6 +14,7 @@ use Magento\Framework\Config\Data\ConfigData; use Magento\Framework\Config\File\ConfigFilePool; use Magento\Framework\Encryption\EncryptorInterface; +use Magento\Framework\Encryption\Encryptor; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem; @@ -21,6 +22,9 @@ use Magento\Framework\Math\Random; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; use Magento\Framework\Model\ResourceModel\Db\Context; +use Magento\Framework\Indexer\ConfigInterface; +use Magento\Framework\Json\EncoderInterface; +use Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory; /** * Encryption key changer resource model @@ -71,6 +75,27 @@ class Change extends AbstractDb */ protected $random; + /** + * Indexer Config + * + * @var IndexerConfig + */ + protected $indexerConfig; + + /** + * Json Encoder + * + * @var Encoder + */ + protected $encoder; + + /** + * Indexer State Collection Factory + * + * @var IndexerStateCollection + */ + protected $indexerStateCollection; + /** * @param Context $context * @param Filesystem $filesystem @@ -78,6 +103,9 @@ class Change extends AbstractDb * @param EncryptorInterface $encryptor * @param Writer $writer * @param Random $random + * @param ConfigInterface $indexerConfig + * @param EncoderInterface $encoder + * @param CollectionFactory $indexerStateCollection * @param string $connectionName */ public function __construct( @@ -87,6 +115,9 @@ public function __construct( EncryptorInterface $encryptor, Writer $writer, Random $random, + ConfigInterface $indexerConfig, + EncoderInterface $encoder, + CollectionFactory $indexerStateCollection, $connectionName = null ) { $this->encryptor = clone $encryptor; @@ -95,6 +126,9 @@ public function __construct( $this->structure = $structure; $this->writer = $writer; $this->random = $random; + $this->indexerConfig = $indexerConfig; + $this->encoder = $encoder; + $this->indexerStateCollection = $indexerStateCollection; } /** @@ -139,6 +173,7 @@ public function changeEncryptionKey($key = null) try { $this->_reEncryptSystemConfigurationValues(); $this->_reEncryptCreditCardNumbers(); + $this->_updateIndexersHash(); $this->writer->saveConfig($configData); $this->commit(); return $key; @@ -207,4 +242,31 @@ protected function _reEncryptCreditCardNumbers() ); } } + + /** + * Retrieve indexer state and update the hash with new encryption key + * + * @return void + */ + protected function _updateIndexersHash(){ + + $stateIndexers = []; + $stateCollection = $this->indexerStateCollection->create(); + foreach ($stateCollection->getItems() as $state) { + /** @var \Magento\Indexer\Model\Indexer\State $state */ + $stateIndexers[$state->getIndexerId()] = $state; + } + + foreach ($this->indexerConfig->getIndexers() as $indexerId => $indexerConfig) { + $newHashConfig = $this->encryptor->hash( + $this->encoder->encode($indexerConfig), + Encryptor::HASH_VERSION_MD5 + ); + + if (isset($stateIndexers[$indexerId])) { + $stateIndexers[$indexerId]->setHashConfig($newHashConfig); + $stateIndexers[$indexerId]->save(); + } + } + } }