|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\EncryptionKey\Console\Command; |
| 10 | + |
| 11 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 12 | +use Magento\Framework\Config\ConfigOptionsListConstants; |
| 13 | +use Magento\Framework\Config\Data\ConfigData; |
| 14 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 15 | +use Magento\Framework\Exception\FileSystemException; |
| 16 | +use Magento\Framework\Math\Random; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Magento\Framework\App\CacheInterface; |
| 22 | +use Magento\Framework\Encryption\EncryptorInterface; |
| 23 | + |
| 24 | +class UpdateEncryptionKeyCommand extends Command |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var EncryptorInterface |
| 28 | + */ |
| 29 | + private EncryptorInterface $encryptor; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var CacheInterface |
| 33 | + */ |
| 34 | + private CacheInterface $cache; |
| 35 | + |
| 36 | + /** |
| 37 | + * Configuration writer |
| 38 | + * |
| 39 | + * @var Writer |
| 40 | + */ |
| 41 | + private Writer $writer; |
| 42 | + |
| 43 | + /** |
| 44 | + * Random string generator |
| 45 | + * |
| 46 | + * @var Random |
| 47 | + */ |
| 48 | + private Random $random; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param EncryptorInterface $encryptor |
| 52 | + * @param CacheInterface $cache |
| 53 | + * @param Writer $writer |
| 54 | + * @param Random $random |
| 55 | + */ |
| 56 | + public function __construct(EncryptorInterface $encryptor, CacheInterface $cache, Writer $writer, Random $random) |
| 57 | + { |
| 58 | + $this->encryptor = $encryptor; |
| 59 | + $this->cache = $cache; |
| 60 | + $this->writer = $writer; |
| 61 | + $this->random = $random; |
| 62 | + |
| 63 | + parent::__construct(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @inheritDoc |
| 68 | + */ |
| 69 | + protected function configure() |
| 70 | + { |
| 71 | + $this->setName('encryption:key:change'); |
| 72 | + $this->setDescription('Change the encryption key inside the env.php file.'); |
| 73 | + $this->addOption( |
| 74 | + 'key', |
| 75 | + 'k', |
| 76 | + InputOption::VALUE_OPTIONAL, |
| 77 | + 'Key has to be a 32 characters long string. If not provided, a random key will be generated.' |
| 78 | + ); |
| 79 | + |
| 80 | + parent::configure(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @inheritDoc |
| 85 | + */ |
| 86 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 87 | + { |
| 88 | + try { |
| 89 | + $key = $input->getOption('key'); |
| 90 | + |
| 91 | + if (!empty($key)) { |
| 92 | + $this->encryptor->validateKey($key); |
| 93 | + } |
| 94 | + |
| 95 | + $this->updateEncryptionKey($key); |
| 96 | + $this->cache->clean(); |
| 97 | + |
| 98 | + $output->writeln('<info>Encryption key has been updated successfully.</info>'); |
| 99 | + |
| 100 | + return Command::SUCCESS; |
| 101 | + } catch (\Exception $e) { |
| 102 | + $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 103 | + return Command::FAILURE; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Update encryption key |
| 109 | + * |
| 110 | + * @param string|null $key |
| 111 | + * @return void |
| 112 | + * @throws FileSystemException |
| 113 | + */ |
| 114 | + private function updateEncryptionKey(string $key = null): void |
| 115 | + { |
| 116 | + // prepare new key, encryptor and new configuration segment |
| 117 | + if (!$this->writer->checkIfWritable()) { |
| 118 | + throw new FileSystemException(__('Deployment configuration file is not writable.')); |
| 119 | + } |
| 120 | + |
| 121 | + if (null === $key) { |
| 122 | + $key = ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX . |
| 123 | + $this->random->getRandomBytes(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE); |
| 124 | + } |
| 125 | + |
| 126 | + $this->encryptor->setNewKey($key); |
| 127 | + |
| 128 | + $encryptSegment = new ConfigData(ConfigFilePool::APP_ENV); |
| 129 | + $encryptSegment->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $this->encryptor->exportKeys()); |
| 130 | + |
| 131 | + $configData = [$encryptSegment->getFileKey() => $encryptSegment->getData()]; |
| 132 | + |
| 133 | + $this->writer->saveConfig($configData); |
| 134 | + } |
| 135 | +} |
0 commit comments