Skip to content

Commit

Permalink
Fix for issue magento#39530 to avoid regenerating admin grid flat table
Browse files Browse the repository at this point in the history
Updating indexer state hash upon creation of new encryption key from admin
  • Loading branch information
senthilengg authored Jan 14, 2025
1 parent 6cfb9b6 commit e881f57
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
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;
use Magento\Framework\Filesystem\Directory\WriteInterface;
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
Expand Down Expand Up @@ -71,13 +75,37 @@ 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
* @param Structure $structure
* @param EncryptorInterface $encryptor
* @param Writer $writer
* @param Random $random
* @param ConfigInterface $indexerConfig
* @param EncoderInterface $encoder
* @param CollectionFactory $indexerStateCollection
* @param string $connectionName
*/
public function __construct(
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -139,6 +173,7 @@ public function changeEncryptionKey($key = null)
try {
$this->_reEncryptSystemConfigurationValues();
$this->_reEncryptCreditCardNumbers();
$this->_updateIndexersHash();
$this->writer->saveConfig($configData);
$this->commit();
return $key;
Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit e881f57

Please sign in to comment.