Change.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\EncryptionKey\Model\ResourceModel\Key;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. use Magento\Framework\Config\Data\ConfigData;
  10. use Magento\Framework\Config\File\ConfigFilePool;
  11. /**
  12. * Encryption key changer resource model
  13. * The operation must be done in one transaction
  14. *
  15. * @api
  16. *
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. * @since 100.0.2
  19. */
  20. class Change extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  21. {
  22. /**
  23. * Encryptor interface
  24. *
  25. * @var \Magento\Framework\Encryption\EncryptorInterface
  26. */
  27. protected $encryptor;
  28. /**
  29. * Filesystem directory write interface
  30. *
  31. * @var \Magento\Framework\Filesystem\Directory\WriteInterface
  32. */
  33. protected $directory;
  34. /**
  35. * System configuration structure
  36. *
  37. * @var \Magento\Config\Model\Config\Structure
  38. */
  39. protected $structure;
  40. /**
  41. * Configuration writer
  42. *
  43. * @var \Magento\Framework\App\DeploymentConfig\Writer
  44. */
  45. protected $writer;
  46. /**
  47. * Random
  48. *
  49. * @var \Magento\Framework\Math\Random
  50. * @since 100.0.4
  51. */
  52. protected $random;
  53. /**
  54. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  55. * @param \Magento\Framework\Filesystem $filesystem
  56. * @param \Magento\Config\Model\Config\Structure $structure
  57. * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
  58. * @param \Magento\Framework\App\DeploymentConfig\Writer $writer
  59. * @param \Magento\Framework\Math\Random $random
  60. * @param string $connectionName
  61. */
  62. public function __construct(
  63. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  64. \Magento\Framework\Filesystem $filesystem,
  65. \Magento\Config\Model\Config\Structure $structure,
  66. \Magento\Framework\Encryption\EncryptorInterface $encryptor,
  67. \Magento\Framework\App\DeploymentConfig\Writer $writer,
  68. \Magento\Framework\Math\Random $random,
  69. $connectionName = null
  70. ) {
  71. $this->encryptor = clone $encryptor;
  72. parent::__construct($context, $connectionName);
  73. $this->directory = $filesystem->getDirectoryWrite(DirectoryList::CONFIG);
  74. $this->structure = $structure;
  75. $this->writer = $writer;
  76. $this->random = $random;
  77. }
  78. /**
  79. * Initialize
  80. *
  81. * @return void
  82. */
  83. protected function _construct()
  84. {
  85. $this->_init('core_config_data', 'config_id');
  86. }
  87. /**
  88. * Change encryption key
  89. *
  90. * @param string|null $key
  91. * @return null|string
  92. * @throws \Exception
  93. */
  94. public function changeEncryptionKey($key = null)
  95. {
  96. // prepare new key, encryptor and new configuration segment
  97. if (!$this->writer->checkIfWritable()) {
  98. throw new \Exception(__('Deployment configuration file is not writable.'));
  99. }
  100. if (null === $key) {
  101. $key = md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE));
  102. }
  103. $this->encryptor->setNewKey($key);
  104. $encryptSegment = new ConfigData(ConfigFilePool::APP_ENV);
  105. $encryptSegment->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $this->encryptor->exportKeys());
  106. $configData = [$encryptSegment->getFileKey() => $encryptSegment->getData()];
  107. // update database and config.php
  108. $this->beginTransaction();
  109. try {
  110. $this->_reEncryptSystemConfigurationValues();
  111. $this->_reEncryptCreditCardNumbers();
  112. $this->writer->saveConfig($configData);
  113. $this->commit();
  114. return $key;
  115. } catch (\Exception $e) {
  116. $this->rollBack();
  117. throw $e;
  118. }
  119. }
  120. /**
  121. * Gather all encrypted system config values and re-encrypt them
  122. *
  123. * @return void
  124. */
  125. protected function _reEncryptSystemConfigurationValues()
  126. {
  127. // look for encrypted node entries in all system.xml files
  128. /** @var \Magento\Config\Model\Config\Structure $configStructure */
  129. $configStructure = $this->structure;
  130. $paths = $configStructure->getFieldPathsByAttribute(
  131. 'backend_model',
  132. \Magento\Config\Model\Config\Backend\Encrypted::class
  133. );
  134. // walk through found data and re-encrypt it
  135. if ($paths) {
  136. $table = $this->getTable('core_config_data');
  137. $values = $this->getConnection()->fetchPairs(
  138. $this->getConnection()
  139. ->select()
  140. ->from($table, ['config_id', 'value'])
  141. ->where('path IN (?)', $paths)
  142. ->where('value NOT LIKE ?', '')
  143. );
  144. foreach ($values as $configId => $value) {
  145. $this->getConnection()->update(
  146. $table,
  147. ['value' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
  148. ['config_id = ?' => (int)$configId]
  149. );
  150. }
  151. }
  152. }
  153. /**
  154. * Gather saved credit card numbers from sales order payments and re-encrypt them
  155. *
  156. * @return void
  157. */
  158. protected function _reEncryptCreditCardNumbers()
  159. {
  160. $table = $this->getTable('sales_order_payment');
  161. $select = $this->getConnection()->select()->from($table, ['entity_id', 'cc_number_enc']);
  162. $attributeValues = $this->getConnection()->fetchPairs($select);
  163. // save new values
  164. foreach ($attributeValues as $valueId => $value) {
  165. $this->getConnection()->update(
  166. $table,
  167. ['cc_number_enc' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
  168. ['entity_id = ?' => (int)$valueId]
  169. );
  170. }
  171. }
  172. }