Skip to content

Commit a44e849

Browse files
committed
MAGETWO-96566: Data is not re encrypted in database after upgrade from 2.2 to 2.3 and switching PHP version
1 parent ab30035 commit a44e849

File tree

3 files changed

+64
-29
lines changed

3 files changed

+64
-29
lines changed

app/code/Magento/Config/Model/Config/Structure.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ protected function _getGroupFieldPathsByAttribute(array $fields, $parentPath, $a
337337
/**
338338
* Collects config paths and their structure paths from configuration files.
339339
* Returns the map of config paths and their structure paths.
340-
*
341340
* All paths are declared in module's system.xml.
342341
*
343342
* ```xml
@@ -394,7 +393,7 @@ private function getFieldsRecursively(array $elements = [])
394393

395394
foreach ($elements as $element) {
396395
if (isset($element['children'])) {
397-
$result = array_replace_recursive(
396+
$result = array_merge_recursive(
398397
$result,
399398
$this->getFieldsRecursively($element['children'])
400399
);

app/code/Magento/Config/Test/Unit/Model/Config/StructureTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ public function testGetFieldPaths()
418418
'field_2'
419419
],
420420
'field_3' => [
421+
'field_3',
421422
'field_3'
422423
],
423424
'field_3_1' => [

app/code/Magento/EncryptionKey/Setup/Patch/Data/SodiumChachaPatch.php

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
*/
1515
class SodiumChachaPatch implements DataPatchInterface
1616
{
17+
/**
18+
* @var \Magento\Framework\Config\ScopeInterface
19+
*/
20+
private $scope;
21+
1722
/**
1823
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
1924
*/
@@ -35,25 +40,29 @@ class SodiumChachaPatch implements DataPatchInterface
3540
private $state;
3641

3742
/**
43+
* SodiumChachaPatch constructor.
3844
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
3945
* @param \Magento\Config\Model\Config\Structure\Proxy $structure
4046
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
4147
* @param \Magento\Framework\App\State $state
48+
* @param \Magento\Framework\Config\ScopeInterface $scope
4249
*/
4350
public function __construct(
4451
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
4552
\Magento\Config\Model\Config\Structure\Proxy $structure,
4653
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
47-
\Magento\Framework\App\State $state
54+
\Magento\Framework\App\State $state,
55+
\Magento\Framework\Config\ScopeInterface $scope
4856
) {
4957
$this->moduleDataSetup = $moduleDataSetup;
5058
$this->structure = $structure;
5159
$this->encryptor = $encryptor;
5260
$this->state = $state;
61+
$this->scope = $scope;
5362
}
5463

5564
/**
56-
* {@inheritdoc}
65+
* @inheritdoc
5766
*/
5867
public function apply()
5968
{
@@ -65,49 +74,75 @@ public function apply()
6574
}
6675

6776
/**
68-
* {@inheritdoc}
77+
* @inheritdoc
6978
*/
7079
public static function getDependencies()
7180
{
7281
return [];
7382
}
7483

7584
/**
76-
* {@inheritdoc}
85+
* @inheritdoc
7786
*/
7887
public function getAliases()
7988
{
8089
return [];
8190
}
8291

92+
/**
93+
* Re encrypt sensitive data in the system configuration
94+
*/
8395
private function reEncryptSystemConfigurationValues()
8496
{
85-
$structure = $this->structure;
86-
$paths = $this->state->emulateAreaCode(
87-
\Magento\Framework\App\Area::AREA_ADMINHTML,
88-
function () use ($structure) {
89-
return $structure->getFieldPathsByAttribute(
90-
'backend_model',
91-
\Magento\Config\Model\Config\Backend\Encrypted::class
92-
);
93-
}
97+
$table = $this->moduleDataSetup->getTable('core_config_data');
98+
$hasEncryptedData = $this->moduleDataSetup->getConnection()->fetchOne(
99+
$this->moduleDataSetup->getConnection()
100+
->select()
101+
->from($table, [new \Zend_Db_Expr('count(value)')])
102+
->where('value LIKE ?', '0:2%')
94103
);
95-
// walk through found data and re-encrypt it
96-
if ($paths) {
97-
$table = $this->moduleDataSetup->getTable('core_config_data');
98-
$values = $this->moduleDataSetup->getConnection()->fetchPairs(
99-
$this->moduleDataSetup->getConnection()
100-
->select()
101-
->from($table, ['config_id', 'value'])
102-
->where('path IN (?)', $paths)
103-
->where('value NOT LIKE ?', '')
104+
if ($hasEncryptedData !== '0') {
105+
$currentScope = $this->scope->getCurrentScope();
106+
$structure = $this->structure;
107+
$paths = $this->state->emulateAreaCode(
108+
\Magento\Framework\App\Area::AREA_ADMINHTML,
109+
function () use ($structure) {
110+
$this->scope->setCurrentScope(\Magento\Framework\App\Area::AREA_ADMINHTML);
111+
/** Returns list of structure paths to be re encrypted */
112+
$paths = $structure->getFieldPathsByAttribute(
113+
'backend_model',
114+
\Magento\Config\Model\Config\Backend\Encrypted::class
115+
);
116+
/** Returns list of mapping between configPath => [structurePaths] */
117+
$mappedPaths = $structure->getFieldPaths();
118+
foreach ($mappedPaths as $mappedPath => $data) {
119+
foreach ($data as $structurePath) {
120+
if ($structurePath !== $mappedPath && $key = array_search($structurePath, $paths)) {
121+
$paths[$key] = $mappedPath;
122+
}
123+
}
124+
}
125+
126+
return array_unique($paths);
127+
}
104128
);
105-
foreach ($values as $configId => $value) {
106-
$this->moduleDataSetup->getConnection()->update(
107-
$table,
108-
['value' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
109-
['config_id = ?' => (int)$configId]
129+
$this->scope->setCurrentScope($currentScope);
130+
// walk through found data and re-encrypt it
131+
if ($paths) {
132+
$values = $this->moduleDataSetup->getConnection()->fetchPairs(
133+
$this->moduleDataSetup->getConnection()
134+
->select()
135+
->from($table, ['config_id', 'value'])
136+
->where('path IN (?)', $paths)
137+
->where('value NOT LIKE ?', '')
110138
);
139+
foreach ($values as $configId => $value) {
140+
$this->moduleDataSetup->getConnection()->update(
141+
$table,
142+
['value' => $this->encryptor->encrypt($this->encryptor->decrypt($value))],
143+
['config_id = ?' => (int)$configId]
144+
);
145+
}
111146
}
112147
}
113148
}

0 commit comments

Comments
 (0)