Skip to content

Commit 859986b

Browse files
author
Joan He
committed
Merge remote-tracking branch 'trigger/MAGETWO-96566' into BugFixPR
2 parents 51013a1 + 47fc9df commit 859986b

File tree

3 files changed

+65
-29
lines changed

3 files changed

+65
-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: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88
namespace Magento\EncryptionKey\Setup\Patch\Data;
99

1010
use Magento\Framework\Setup\Patch\DataPatchInterface;
11+
use Magento\Framework\App\ObjectManager;
1112

1213
/**
1314
* Migrate encrypted configuration values to the latest cipher
1415
*/
1516
class SodiumChachaPatch implements DataPatchInterface
1617
{
18+
/**
19+
* @var \Magento\Framework\Config\ScopeInterface
20+
*/
21+
private $scope;
22+
1723
/**
1824
* @var \Magento\Framework\Setup\ModuleDataSetupInterface
1925
*/
@@ -35,25 +41,29 @@ class SodiumChachaPatch implements DataPatchInterface
3541
private $state;
3642

3743
/**
44+
* SodiumChachaPatch constructor.
3845
* @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
3946
* @param \Magento\Config\Model\Config\Structure\Proxy $structure
4047
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
4148
* @param \Magento\Framework\App\State $state
49+
* @param \Magento\Framework\Config\ScopeInterface|null $scope
4250
*/
4351
public function __construct(
4452
\Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
4553
\Magento\Config\Model\Config\Structure\Proxy $structure,
4654
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
47-
\Magento\Framework\App\State $state
55+
\Magento\Framework\App\State $state,
56+
\Magento\Framework\Config\ScopeInterface $scope = null
4857
) {
4958
$this->moduleDataSetup = $moduleDataSetup;
5059
$this->structure = $structure;
5160
$this->encryptor = $encryptor;
5261
$this->state = $state;
62+
$this->scope = $scope ?? ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class);
5363
}
5464

5565
/**
56-
* {@inheritdoc}
66+
* @inheritdoc
5767
*/
5868
public function apply()
5969
{
@@ -65,49 +75,75 @@ public function apply()
6575
}
6676

6777
/**
68-
* {@inheritdoc}
78+
* @inheritdoc
6979
*/
7080
public static function getDependencies()
7181
{
7282
return [];
7383
}
7484

7585
/**
76-
* {@inheritdoc}
86+
* @inheritdoc
7787
*/
7888
public function getAliases()
7989
{
8090
return [];
8191
}
8292

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

0 commit comments

Comments
 (0)