Skip to content

Commit c53c1b0

Browse files
Merge branch 'AC-12767' into AC-12767-AC-12680
2 parents d220f78 + 1841192 commit c53c1b0

File tree

19 files changed

+1523
-8
lines changed

19 files changed

+1523
-8
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Config\Model\Data\ReEncryptorList\CoreConfigDataReEncryptor;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\Encryption\EncryptorInterface;
12+
use Magento\EncryptionKey\Model\Data\ReEncryptorList\ReEncryptor\HandlerInterface;
13+
use Magento\EncryptionKey\Model\Data\ReEncryptorList\ReEncryptor\Handler\ErrorFactory;
14+
use Magento\Framework\DB\Query\Generator;
15+
16+
/**
17+
* Handler for core configuration re-encryption.
18+
*/
19+
class Handler implements HandlerInterface
20+
{
21+
/**
22+
* @var string
23+
*/
24+
private const PATTERN = "^[[:digit:]]+:[[:digit:]]+:.*$";
25+
26+
/**
27+
* @var string
28+
*/
29+
private const TABLE_NAME = "core_config_data";
30+
31+
/**
32+
* @var int
33+
*/
34+
private const BATCH_SIZE = 1000;
35+
36+
/**
37+
* @var string
38+
*/
39+
private const IDENTIFIER = "config_id";
40+
41+
/**
42+
* @var EncryptorInterface
43+
*/
44+
private EncryptorInterface $encryptor;
45+
46+
/**
47+
* @var ResourceConnection
48+
*/
49+
private ResourceConnection $resourceConnection;
50+
51+
/**
52+
* @var ErrorFactory
53+
*/
54+
private ErrorFactory $errorFactory;
55+
56+
/**
57+
* @var Generator
58+
*/
59+
private Generator $queryGenerator;
60+
61+
/**
62+
* @param EncryptorInterface $encryptor
63+
* @param ResourceConnection $resourceConnection
64+
* @param ErrorFactory $errorFactory
65+
* @param Generator $queryGenerator
66+
*/
67+
public function __construct(
68+
EncryptorInterface $encryptor,
69+
ResourceConnection $resourceConnection,
70+
ErrorFactory $errorFactory,
71+
Generator $queryGenerator
72+
) {
73+
$this->encryptor = $encryptor;
74+
$this->resourceConnection = $resourceConnection;
75+
$this->errorFactory = $errorFactory;
76+
$this->queryGenerator = $queryGenerator;
77+
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public function reEncrypt(): array
83+
{
84+
$errors = [];
85+
$tableName = $this->resourceConnection->getTableName(
86+
self::TABLE_NAME
87+
);
88+
$connection = $this->resourceConnection->getConnection();
89+
90+
$select = $connection->select()
91+
->from($tableName, ['config_id', 'value'])
92+
->where('value != ?', '')
93+
->where('value IS NOT NULL')
94+
->where('value REGEXP ?', self::PATTERN);
95+
96+
$iterator = $this->queryGenerator->generate(
97+
self::IDENTIFIER,
98+
$select,
99+
self::BATCH_SIZE
100+
);
101+
102+
foreach ($iterator as $batch) {
103+
foreach ($connection->fetchAll($batch) as $row) {
104+
try {
105+
$connection->update(
106+
$tableName,
107+
['value' => $this->encryptor->encrypt($this->encryptor->decrypt($row['value']))],
108+
['config_id = ?' => $row['config_id']]
109+
);
110+
} catch (\Throwable $e) {
111+
$errors[] = $this->errorFactory->create(
112+
self::IDENTIFIER,
113+
$row['config_id'],
114+
$e->getMessage()
115+
);
116+
117+
continue;
118+
}
119+
}
120+
}
121+
122+
return $errors;
123+
}
124+
}

app/code/Magento/Config/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"magento/module-directory": "*",
1414
"magento/module-email": "*",
1515
"magento/module-media-storage": "*",
16-
"magento/module-store": "*"
16+
"magento/module-store": "*",
17+
"magento/module-encryption-key": "*"
1718
},
1819
"type": "magento2-module",
1920
"license": [

app/code/Magento/Config/etc/di.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
@@ -409,4 +409,17 @@
409409
</argument>
410410
</arguments>
411411
</type>
412+
<virtualType name="Magento\Config\Model\Data\ReEncryptorList\CoreConfigDataReEncryptor" type="Magento\EncryptionKey\Model\Data\ReEncryptorList\ReEncryptor">
413+
<arguments>
414+
<argument name="description" xsi:type="string">Re-encrypts 'value' column in the 'core_config_data' DB table.</argument>
415+
<argument name="handler" xsi:type="object">Magento\Config\Model\Data\ReEncryptorList\CoreConfigDataReEncryptor\Handler</argument>
416+
</arguments>
417+
</virtualType>
418+
<type name="Magento\EncryptionKey\Model\Data\ReEncryptorList">
419+
<arguments>
420+
<argument name="reEncryptors" xsi:type="array">
421+
<item name="core_config_data" xsi:type="object">Magento\Config\Model\Data\ReEncryptorList\CoreConfigDataReEncryptor</item>
422+
</argument>
423+
</arguments>
424+
</type>
412425
</config>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\EncryptionKey\Console\Command;
9+
10+
use Magento\Framework\Console\Cli;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Magento\EncryptionKey\Model\Data\ReEncryptorList;
15+
16+
/**
17+
* Command for displaying a list of available data re-encryptors.
18+
*/
19+
class ListReEncryptorsCommand extends Command
20+
{
21+
/**
22+
* @var ReEncryptorList
23+
*/
24+
private ReEncryptorList $reEncryptorList;
25+
26+
/**
27+
* @param ReEncryptorList $reEncryptorList
28+
*/
29+
public function __construct(
30+
ReEncryptorList $reEncryptorList
31+
) {
32+
$this->reEncryptorList = $reEncryptorList;
33+
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
protected function configure()
41+
{
42+
$this->setName('encryption:data:list-re-encryptors');
43+
44+
$this->setDescription(
45+
'Shows a list of available data re-encryptors.'
46+
);
47+
48+
parent::configure();
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
protected function execute(InputInterface $input, OutputInterface $output)
55+
{
56+
foreach ($this->reEncryptorList->getReEncryptors() as $name => $reEncryptor) {
57+
$output->writeln(
58+
sprintf(
59+
'<fg=green>%-40s</> %s',
60+
$name,
61+
$reEncryptor->getDescription()
62+
)
63+
);
64+
}
65+
66+
return Cli::RETURN_SUCCESS;
67+
}
68+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\EncryptionKey\Console\Command;
9+
10+
use DateInterval;
11+
use Magento\Framework\Console\Cli;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
use Magento\EncryptionKey\Model\Data\ReEncryptorList;
17+
18+
/**
19+
* Command for re-encryption of encrypted data using current encryption key.
20+
*/
21+
class ReEncryptDataCommand extends Command
22+
{
23+
/**
24+
* @var string
25+
*/
26+
private const INPUT_KEY_ENCRYPTORS = 'encryptors';
27+
28+
/**
29+
* @var ReEncryptorList
30+
*/
31+
private ReEncryptorList $reEncryptorList;
32+
33+
/**
34+
* @param ReEncryptorList $reEncryptorList
35+
*/
36+
public function __construct(
37+
ReEncryptorList $reEncryptorList
38+
) {
39+
$this->reEncryptorList = $reEncryptorList;
40+
41+
parent::__construct();
42+
}
43+
44+
/**
45+
* @inheritDoc
46+
*/
47+
protected function configure()
48+
{
49+
$this->setName('encryption:data:re-encrypt');
50+
51+
$this->setDescription(
52+
'Re-encrypts encrypted data using current encryption key.'
53+
);
54+
55+
$this->addArgument(
56+
self::INPUT_KEY_ENCRYPTORS,
57+
InputArgument::IS_ARRAY,
58+
'Space-separated list of re-encryptors to use.'
59+
);
60+
61+
parent::configure();
62+
}
63+
64+
/**
65+
* @inheritDoc
66+
*/
67+
protected function execute(InputInterface $input, OutputInterface $output)
68+
{
69+
$requestedReEncryptorsNames = $input->getArgument(
70+
self::INPUT_KEY_ENCRYPTORS
71+
);
72+
73+
$availableReEncryptors = $this->reEncryptorList->getReEncryptors();
74+
75+
if (empty($requestedReEncryptorsNames)) {
76+
$requestedReEncryptorsNames = array_keys($availableReEncryptors);
77+
}
78+
79+
foreach ($requestedReEncryptorsNames as $name) {
80+
if (!isset($availableReEncryptors[$name])) {
81+
$output->writeLn(
82+
sprintf("<fg=red>Re-encryptor '%s' could not be found!</>", $name)
83+
);
84+
85+
continue;
86+
}
87+
88+
$reEncryptor = $availableReEncryptors[$name];
89+
90+
$output->writeLn(
91+
sprintf("Executing '%s' re-encryptor...", $name)
92+
);
93+
94+
try {
95+
$startTime = new \DateTimeImmutable();
96+
97+
$errors = $reEncryptor->reEncrypt();
98+
99+
$endTime = new \DateTimeImmutable();
100+
101+
$elapsedTime = $this->formatInterval(
102+
$startTime->diff($endTime)
103+
);
104+
} catch (\Throwable $e) {
105+
$output->writeLn("<fg=red>Failed due to the following error:</>");
106+
107+
$output->writeLn(
108+
sprintf("<fg=white;bg=red>%s</>", $e->getMessage())
109+
);
110+
111+
continue;
112+
}
113+
114+
if (empty($errors)) {
115+
$output->writeLn(
116+
sprintf(
117+
"<fg=green>Done successfully in %s.</>",
118+
$elapsedTime
119+
)
120+
);
121+
} else {
122+
$output->writeLn(
123+
sprintf(
124+
"<fg=yellow>Done in %s but with the following errors:</>",
125+
$elapsedTime
126+
)
127+
);
128+
129+
foreach ($errors as $error) {
130+
$output->writeLn(
131+
sprintf(
132+
"<fg=black;bg=yellow>[%s %s]: %s</>",
133+
$error->getRowIdField(),
134+
$error->getRowIdValue(),
135+
$error->getMessage()
136+
)
137+
);
138+
}
139+
}
140+
}
141+
142+
return Cli::RETURN_SUCCESS;
143+
}
144+
145+
/**
146+
* Formats a date interval.
147+
*
148+
* @param DateInterval $interval
149+
*
150+
* @return string
151+
*/
152+
private function formatInterval(DateInterval $interval): string
153+
{
154+
$days = (int) $interval->format('%d');
155+
156+
$hours = $days * 24 + (int) $interval->format('%H');
157+
$minutes = $interval->format('%I');
158+
$seconds = $interval->format('%S');
159+
160+
return sprintf("%s:%s:%s", $hours, $minutes, $seconds);
161+
}
162+
}

0 commit comments

Comments
 (0)