Skip to content

Commit 2601ccb

Browse files
committed
MAGETWO-80514: Refactor ConfigGenerator to replace/set crypt key instead of append magento#11155
- Merge Pull Request magento#11155 from renttek/magento2:issue/11089 - Merged commits: 1. 13daa74 2. f50ad05 3. 4f3e981 4. 2718be3 5. b517623 6. 345a7f3 7. ae06955 8. 43e9751 9. 4b748cf 10. 010097d 11. 2316cd8 12. c7ca261 13. eb33d60 14. 61d9767 15. aec956f 16. e440946 17. 885ecf8 18. 91ee875 19. 3faf8b5 20. 1fd5b0e 21. a2ae382 22. c868fa2 23. d78ae56 24. b2146db 25. bab5e31 26. c638fd7 27. 62eff1d 28. 58aab5e 29. e8fe7ed
2 parents 850b9e5 + e8fe7ed commit 2601ccb

File tree

7 files changed

+365
-78
lines changed

7 files changed

+365
-78
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Config\Data;
8+
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\ObjectManagerInterface;
11+
12+
/**
13+
* Factory for ConfigData
14+
*/
15+
class ConfigDataFactory
16+
{
17+
/**
18+
* @var ObjectManager
19+
*/
20+
private $objectManager;
21+
22+
/**
23+
* Factory constructor
24+
*
25+
* @param ObjectManagerInterface $objectManager
26+
*/
27+
public function __construct(ObjectManagerInterface $objectManager)
28+
{
29+
$this->objectManager = $objectManager;
30+
}
31+
32+
/**
33+
* Returns a new instance of ConfigData on every call.
34+
*
35+
* @param string $fileKey
36+
* @return ConfigData
37+
*/
38+
public function create($fileKey)
39+
{
40+
return $this->objectManager->create(ConfigData::class, ['fileKey' => $fileKey]);
41+
}
42+
}

setup/src/Magento/Setup/Model/ConfigGenerator.php

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
namespace Magento\Setup\Model;
88

9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Config\Data\ConfigData;
11+
use Magento\Framework\Config\Data\ConfigDataFactory;
1012
use Magento\Framework\Config\File\ConfigFilePool;
11-
use Magento\Framework\Math\Random;
1213
use Magento\Framework\App\DeploymentConfig;
1314
use Magento\Framework\Config\ConfigOptionsListConstants;
1415
use Magento\Framework\App\State;
15-
use Magento\Framework\App\ObjectManagerFactory;
16+
use Magento\Framework\Math\Random;
1617

1718
/**
1819
* Creates deployment config data based on user input array
@@ -26,39 +27,58 @@ class ConfigGenerator
2627
* @var array
2728
*/
2829
private static $paramMap = [
29-
ConfigOptionsListConstants::INPUT_KEY_DB_HOST => ConfigOptionsListConstants::KEY_HOST,
30-
ConfigOptionsListConstants::INPUT_KEY_DB_NAME => ConfigOptionsListConstants::KEY_NAME,
31-
ConfigOptionsListConstants::INPUT_KEY_DB_USER => ConfigOptionsListConstants::KEY_USER,
32-
ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD => ConfigOptionsListConstants::KEY_PASSWORD,
33-
ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX => ConfigOptionsListConstants::KEY_PREFIX,
34-
ConfigOptionsListConstants::INPUT_KEY_DB_MODEL => ConfigOptionsListConstants::KEY_MODEL,
35-
ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE => ConfigOptionsListConstants::KEY_ENGINE,
30+
ConfigOptionsListConstants::INPUT_KEY_DB_HOST => ConfigOptionsListConstants::KEY_HOST,
31+
ConfigOptionsListConstants::INPUT_KEY_DB_NAME => ConfigOptionsListConstants::KEY_NAME,
32+
ConfigOptionsListConstants::INPUT_KEY_DB_USER => ConfigOptionsListConstants::KEY_USER,
33+
ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD => ConfigOptionsListConstants::KEY_PASSWORD,
34+
ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX => ConfigOptionsListConstants::KEY_PREFIX,
35+
ConfigOptionsListConstants::INPUT_KEY_DB_MODEL => ConfigOptionsListConstants::KEY_MODEL,
36+
ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE => ConfigOptionsListConstants::KEY_ENGINE,
3637
ConfigOptionsListConstants::INPUT_KEY_DB_INIT_STATEMENTS => ConfigOptionsListConstants::KEY_INIT_STATEMENTS,
37-
ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => ConfigOptionsListConstants::KEY_ENCRYPTION_KEY,
38-
ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE => ConfigOptionsListConstants::KEY_SAVE,
39-
ConfigOptionsListConstants::INPUT_KEY_RESOURCE => ConfigOptionsListConstants::KEY_RESOURCE,
38+
ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => ConfigOptionsListConstants::KEY_ENCRYPTION_KEY,
39+
ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE => ConfigOptionsListConstants::KEY_SAVE,
40+
ConfigOptionsListConstants::INPUT_KEY_RESOURCE => ConfigOptionsListConstants::KEY_RESOURCE,
4041
];
4142

43+
/**
44+
* @var DeploymentConfig
45+
*/
46+
protected $deploymentConfig;
47+
4248
/**
4349
* @var Random
50+
* @deprecated since 100.2.0
4451
*/
4552
protected $random;
4653

4754
/**
48-
* @var DeploymentConfig
55+
* @var ConfigDataFactory
4956
*/
50-
protected $deploymentConfig;
57+
private $configDataFactory;
58+
59+
/**
60+
* @var CryptKeyGeneratorInterface
61+
*/
62+
private $cryptKeyGenerator;
5163

5264
/**
5365
* Constructor
5466
*
55-
* @param Random $random
67+
* @param Random $random Deprecated since 100.2.0
5668
* @param DeploymentConfig $deploymentConfig
69+
* @param ConfigDataFactory|null $configDataFactory
70+
* @param CryptKeyGeneratorInterface|null $cryptKeyGenerator
5771
*/
58-
public function __construct(Random $random, DeploymentConfig $deploymentConfig)
59-
{
72+
public function __construct(
73+
Random $random,
74+
DeploymentConfig $deploymentConfig,
75+
ConfigDataFactory $configDataFactory = null,
76+
CryptKeyGeneratorInterface $cryptKeyGenerator = null
77+
) {
6078
$this->random = $random;
6179
$this->deploymentConfig = $deploymentConfig;
80+
$this->configDataFactory = $configDataFactory ?? ObjectManager::getInstance()->get(ConfigDataFactory::class);
81+
$this->cryptKeyGenerator = $cryptKeyGenerator ?? ObjectManager::getInstance()->get(CryptKeyGenerator::class);
6282
}
6383

6484
/**
@@ -70,23 +90,17 @@ public function createCryptConfig(array $data)
7090
{
7191
$currentKey = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
7292

73-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
74-
if (isset($data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY])) {
75-
if ($currentKey !== null) {
76-
$key = $currentKey . "\n" . $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY];
77-
} else {
78-
$key = $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY];
79-
}
93+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
8094

81-
$configData->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $key);
82-
} else {
83-
if ($currentKey === null) {
84-
$configData->set(
85-
ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY,
86-
md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE))
87-
);
88-
}
89-
}
95+
// Use given key if set, else use current
96+
$key = $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY] ?? $currentKey;
97+
98+
// If there is no key given or currently set, generate a new one
99+
$key = $key ?? $this->cryptKeyGenerator->generate();
100+
101+
// Chaining of ".. ?? .." is not used to keep it simpler to understand
102+
103+
$configData->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $key);
90104

91105
return $configData;
92106
}
@@ -99,7 +113,7 @@ public function createCryptConfig(array $data)
99113
*/
100114
public function createSessionConfig(array $data)
101115
{
102-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
116+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
103117

104118
if (isset($data[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE])) {
105119
$configData->set(
@@ -132,7 +146,7 @@ public function createDefinitionsConfig(array $data)
132146
*/
133147
public function createDbConfig(array $data)
134148
{
135-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
149+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
136150

137151
$optional = [
138152
ConfigOptionsListConstants::INPUT_KEY_DB_HOST,
@@ -151,25 +165,18 @@ public function createDbConfig(array $data)
151165
);
152166
}
153167

168+
$dbConnectionPrefix = ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/';
169+
154170
foreach ($optional as $key) {
155171
if (isset($data[$key])) {
156-
$configData->set(
157-
ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/' . self::$paramMap[$key],
158-
$data[$key]
159-
);
172+
$configData->set($dbConnectionPrefix . self::$paramMap[$key], $data[$key]);
160173
}
161174
}
162175

163-
$currentStatus = $this->deploymentConfig->get(
164-
ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/' . ConfigOptionsListConstants::KEY_ACTIVE
165-
);
176+
$currentStatus = $this->deploymentConfig->get($dbConnectionPrefix . ConfigOptionsListConstants::KEY_ACTIVE);
166177

167178
if ($currentStatus === null) {
168-
$configData->set(
169-
ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT
170-
. '/' . ConfigOptionsListConstants::KEY_ACTIVE,
171-
'1'
172-
);
179+
$configData->set($dbConnectionPrefix . ConfigOptionsListConstants::KEY_ACTIVE, '1');
173180
}
174181

175182
return $configData;
@@ -182,7 +189,7 @@ public function createDbConfig(array $data)
182189
*/
183190
public function createResourceConfig()
184191
{
185-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
192+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
186193

187194
if ($this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP) === null) {
188195
$configData->set(ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP, 'default');
@@ -198,10 +205,12 @@ public function createResourceConfig()
198205
*/
199206
public function createXFrameConfig()
200207
{
201-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
208+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
209+
202210
if ($this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT) === null) {
203211
$configData->set(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT, 'SAMEORIGIN');
204212
}
213+
205214
return $configData;
206215
}
207216

@@ -212,10 +221,12 @@ public function createXFrameConfig()
212221
*/
213222
public function createModeConfig()
214223
{
215-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
224+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
225+
216226
if ($this->deploymentConfig->get(State::PARAM_MODE) === null) {
217227
$configData->set(State::PARAM_MODE, State::MODE_DEFAULT);
218228
}
229+
219230
return $configData;
220231
}
221232

@@ -227,21 +238,29 @@ public function createModeConfig()
227238
*/
228239
public function createCacheHostsConfig(array $data)
229240
{
230-
$configData = new ConfigData(ConfigFilePool::APP_ENV);
241+
$configData = $this->configDataFactory->create(ConfigFilePool::APP_ENV);
242+
231243
if (isset($data[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS])) {
232-
$hostData = explode(',', $data[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS]);
233-
$hosts = [];
234-
foreach ($hostData as $data) {
235-
$dataArray = explode(':', trim($data));
236-
$host = [];
237-
$host['host'] = $dataArray[0];
238-
if (isset($dataArray[1])) {
239-
$host['port'] = $dataArray[1];
240-
}
241-
$hosts[] = $host;
242-
}
244+
$hosts = explode(',', $data[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS]);
245+
246+
$hosts = array_map(
247+
function ($hostData) {
248+
$hostDataParts = explode(':', trim($hostData));
249+
250+
$tmp = ['host' => $hostDataParts[0]];
251+
252+
if (isset($hostDataParts[1])) {
253+
$tmp['port'] = $hostDataParts[1];
254+
}
255+
256+
return $tmp;
257+
},
258+
$hosts
259+
);
260+
243261
$configData->set(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS, $hosts);
244262
}
263+
245264
$configData->setOverrideWhenSave(true);
246265
return $configData;
247266
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup\Model;
8+
9+
use Magento\Framework\Config\ConfigOptionsListConstants;
10+
use Magento\Framework\Math\Random;
11+
12+
/**
13+
* Generates a crypt
14+
*/
15+
class CryptKeyGenerator implements CryptKeyGeneratorInterface
16+
{
17+
/**
18+
* @var Random
19+
*/
20+
private $random;
21+
22+
/**
23+
* CryptKeyGenerator constructor.
24+
*
25+
* @param Random $random
26+
*/
27+
public function __construct(Random $random)
28+
{
29+
$this->random = $random;
30+
}
31+
32+
/**
33+
* Generates & returns a string to be used as crypt key.
34+
*
35+
* The key length is not a parameter, but an implementation detail.
36+
*
37+
* @return string
38+
*
39+
* @throws \Magento\Framework\Exception\LocalizedException
40+
*/
41+
public function generate()
42+
{
43+
return md5($this->getRandomString());
44+
}
45+
46+
/**
47+
* Returns a random string.
48+
*
49+
* @return string
50+
*/
51+
private function getRandomString()
52+
{
53+
return $this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE);
54+
}
55+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Setup\Model;
8+
9+
/**
10+
* Interface for crypt key generators.
11+
*/
12+
interface CryptKeyGeneratorInterface
13+
{
14+
/**
15+
* Generates & returns a string to be used as crypt key.
16+
*
17+
* The key length is not a parameter, but an implementation detail.
18+
*
19+
* @return string
20+
*/
21+
public function generate();
22+
}

0 commit comments

Comments
 (0)