-
Notifications
You must be signed in to change notification settings - Fork 84
/
SetCryptKey.php
92 lines (78 loc) · 2.3 KB
/
SetCryptKey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\MagentoCloud\Step\Deploy;
use Magento\MagentoCloud\App\Error;
use Magento\MagentoCloud\Config\Magento\Env\ReaderInterface as ConfigReader;
use Magento\MagentoCloud\Config\Magento\Env\WriterInterface as ConfigWriter;
use Magento\MagentoCloud\Config\Environment;
use Magento\MagentoCloud\Filesystem\FileSystemException;
use Magento\MagentoCloud\Step\StepException;
use Magento\MagentoCloud\Step\StepInterface;
use Psr\Log\LoggerInterface;
/**
* @inheritdoc
*/
class SetCryptKey implements StepInterface
{
/**
* @var Environment
*/
private $environment;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ConfigWriter
*/
private $configWriter;
/**
* @var ConfigReader
*/
private $configReader;
/**
* @param Environment $environment
* @param LoggerInterface $logger
* @param ConfigReader $configReader
* @param ConfigWriter $configWriter
*/
public function __construct(
Environment $environment,
LoggerInterface $logger,
ConfigReader $configReader,
ConfigWriter $configWriter
) {
$this->environment = $environment;
$this->logger = $logger;
$this->configReader = $configReader;
$this->configWriter = $configWriter;
}
/**
* Update crypt/key in app/etc/env.php with CRYPT_KEY value from environment.
* Will not change anything if the value is already set.
*
* {@inheritdoc}
*/
public function execute(): void
{
$this->logger->info('Checking existence of encryption key');
if (!empty($this->configReader->read()['crypt']['key'])) {
return;
}
$key = $this->environment->getCryptKey();
if (empty($key)) {
return;
}
$this->logger->info(sprintf('Setting encryption key from %s', Environment::VARIABLE_CRYPT_KEY));
$config['crypt']['key'] = $key;
try {
$this->configWriter->update($config);
} catch (FileSystemException $exception) {
throw new StepException($exception->getMessage(), Error::DEPLOY_ENV_PHP_IS_NOT_WRITABLE, $exception);
}
}
}