Skip to content

Commit 6e51bb7

Browse files
authored
MCLOUD-6120: Crypt key absence message appear on installation process (magento#735)
1 parent 38a7e9b commit 6e51bb7

File tree

4 files changed

+42
-55
lines changed

4 files changed

+42
-55
lines changed

src/Config/Environment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Environment
2222

2323
public const VAL_ENABLED = 'enabled';
2424
public const VAL_DISABLED = 'disabled';
25+
public const VARIABLE_CRYPT_KEY = 'CRYPT_KEY';
2526

2627
/**
2728
* The environment variable for controlling the directory nesting level for error reporting
@@ -134,7 +135,7 @@ public function getVariable($name, $default = null)
134135
*/
135136
public function getCryptKey(): string
136137
{
137-
return $this->getVariable('CRYPT_KEY', '');
138+
return $this->getVariable(self::VARIABLE_CRYPT_KEY, '');
138139
}
139140

140141
/**

src/Config/Validator/Deploy/AdminData.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,31 @@ public function __construct(
6565
*/
6666
public function validate(): ResultInterface
6767
{
68-
$data = $this->getAdminData();
69-
70-
if ($this->databaseConfiguration->validate() instanceof Success) {
71-
if ($this->state->isInstalled() && $data) {
72-
return $this->resultFactory->error(
73-
'The following admin data is required to create an admin user during initial installation'
74-
. ' only and is ignored during upgrade process: ' . implode(', ', $data)
75-
);
68+
try {
69+
$data = $this->getAdminData();
70+
71+
if ($this->databaseConfiguration->validate() instanceof Success) {
72+
if ($this->state->isInstalled() && $data) {
73+
return $this->resultFactory->error(
74+
'The following admin data is required to create an admin user during initial installation'
75+
. ' only and is ignored during upgrade process: ' . implode(', ', $data)
76+
);
77+
}
78+
79+
if (!$this->adminData->getEmail() && $data) {
80+
return $this->resultFactory->error(
81+
'The following admin data was ignored and an admin was not created '
82+
. 'because admin email is not set: ' . implode(', ', $data),
83+
'Create an admin user via ssh manually: bin/magento admin:user:create'
84+
);
85+
}
7686
}
7787

78-
if (!$this->adminData->getEmail() && $data) {
79-
return $this->resultFactory->error(
80-
'The following admin data was ignored and an admin was not created because admin email is not set: '
81-
. implode(', ', $data),
82-
'Create an admin user via ssh manually: bin/magento admin:user:create'
83-
);
84-
}
88+
return $this->resultFactory->success();
89+
} catch (\Exception $e) {
90+
// Exception on this step is not critical and can be only logged without interraption of the process
91+
return $this->resultFactory->error($e->getMessage());
8592
}
86-
87-
return $this->resultFactory->success();
8893
}
8994

9095
/**

src/Step/Deploy/SetCryptKey.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,19 @@ public function __construct(
6666
*/
6767
public function execute(): void
6868
{
69+
$this->logger->info('Checking existence of encryption key');
70+
6971
if (!empty($this->configReader->read()['crypt']['key'])) {
7072
return;
7173
}
7274

7375
$key = $this->environment->getCryptKey();
7476

7577
if (empty($key)) {
76-
$this->logger->notice(
77-
'Crypt key missing. Add the crypt key to the "app/etc/env.php" file, or set the "CRYPT_KEY" '
78-
. 'environment variable in the Cloud Project web UI.'
79-
);
80-
8178
return;
8279
}
8380

84-
$this->logger->info('Setting encryption key');
81+
$this->logger->info(sprintf('Setting encryption key from %s', Environment::VARIABLE_CRYPT_KEY));
8582

8683
$config['crypt']['key'] = $key;
8784

src/Test/Unit/Step/Deploy/SetCryptKeyTest.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ public function testConfigUpdate(): void
7777
$this->environmentMock->expects($this->once())
7878
->method('getCryptKey')
7979
->willReturn('TWFnZW50byBSb3g=');
80-
$this->loggerMock->expects($this->never())
81-
->method('error')
82-
->with(
83-
'Crypt key missing. Add the crypt key to the "app/etc/env.php" file, or set the "CRYPT_KEY" '
84-
. 'environment variable in the Cloud Project web UI.'
85-
);
86-
$this->loggerMock->expects($this->once())
80+
$this->loggerMock->expects($this->exactly(2))
8781
->method('info')
88-
->with('Setting encryption key');
82+
->withConsecutive(
83+
['Checking existence of encryption key'],
84+
[sprintf('Setting encryption key from %s', Environment::VARIABLE_CRYPT_KEY)]
85+
);
8986
$this->configWriterMock->expects($this->once())
9087
->method('update')
9188
->with(['crypt' => ['key' => 'TWFnZW50byBSb3g=']]);
@@ -107,15 +104,12 @@ public function testConfigUpdateWithError(): void
107104
$this->environmentMock->expects($this->once())
108105
->method('getCryptKey')
109106
->willReturn('TWFnZW50byBSb3g=');
110-
$this->loggerMock->expects($this->never())
111-
->method('error')
112-
->with(
113-
'Crypt key missing. Add the crypt key to the "app/etc/env.php" file, or set the "CRYPT_KEY" '
114-
. 'environment variable in the Cloud Project web UI.'
115-
);
116-
$this->loggerMock->expects($this->once())
107+
$this->loggerMock->expects($this->exactly(2))
117108
->method('info')
118-
->with('Setting encryption key');
109+
->withConsecutive(
110+
['Checking existence of encryption key'],
111+
[sprintf('Setting encryption key from %s', Environment::VARIABLE_CRYPT_KEY)]
112+
);
119113
$this->configWriterMock->expects($this->once())
120114
->method('update')
121115
->with(['crypt' => ['key' => 'TWFnZW50byBSb3g=']])
@@ -136,13 +130,8 @@ public function testEnvironmentVariableNotSet(): void
136130
->method('getCryptKey')
137131
->willReturn('');
138132
$this->loggerMock->expects($this->once())
139-
->method('notice')
140-
->with(
141-
'Crypt key missing. Add the crypt key to the "app/etc/env.php" file, or set the "CRYPT_KEY" '
142-
. 'environment variable in the Cloud Project web UI.'
143-
);
144-
$this->loggerMock->expects($this->never())
145-
->method('info');
133+
->method('info')
134+
->with('Checking existence of encryption key');
146135
$this->configWriterMock->expects($this->never())
147136
->method('update');
148137

@@ -159,14 +148,9 @@ public function testKeyAlreadySet(): void
159148
->willReturn(['crypt' => ['key' => 'QmVuIHd1eiBoZXJl']]);
160149
$this->environmentMock->expects($this->never())
161150
->method('getCryptKey');
162-
$this->loggerMock->expects($this->never())
163-
->method('notice')
164-
->with(
165-
'Crypt key missing. Add the crypt key to the "app/etc/env.php" file, or set the "CRYPT_KEY" '
166-
. 'environment variable in the Cloud Project web UI.'
167-
);
168-
$this->loggerMock->expects($this->never())
169-
->method('info');
151+
$this->loggerMock->expects($this->once())
152+
->method('info')
153+
->with('Checking existence of encryption key');
170154
$this->configWriterMock->expects($this->never())
171155
->method('update');
172156

0 commit comments

Comments
 (0)