|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Symfony\Component\Secret\Command; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 | +use Symfony\Component\Secret\AbstractVault; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Tobias Schultze <http://tobion.de> |
| 26 | + * @author Jérémy Derussé <jeremy@derusse.com> |
| 27 | + * @author Nicolas Grekas <p@tchwork.com> |
| 28 | + */ |
| 29 | +final class SecretsGenerateKeysCommand extends Command |
| 30 | +{ |
| 31 | + protected static $defaultName = 'generate-keys'; |
| 32 | + |
| 33 | + private $vault; |
| 34 | + private $localVault; |
| 35 | + |
| 36 | + public function __construct(AbstractVault $vault, AbstractVault $localVault = null) |
| 37 | + { |
| 38 | + $this->vault = $vault; |
| 39 | + $this->localVault = $localVault; |
| 40 | + |
| 41 | + parent::__construct(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this |
| 47 | + ->setDescription('Generate new encryption keys') |
| 48 | + ->addOption('local', 'l', InputOption::VALUE_NONE, 'Update the local vault.') |
| 49 | + ->addOption('rotate', 'r', InputOption::VALUE_NONE, 'Re-encrypt existing secrets with the newly generated keys.') |
| 50 | + ->addOption('dir', 'd', InputOption::VALUE_REQUIRED, 'Target directory') |
| 51 | + ->setHelp(<<<'EOF' |
| 52 | +The <info>%command.name%</info> command generates a new encryption key. |
| 53 | +
|
| 54 | + <info>%command.full_name%</info> |
| 55 | +
|
| 56 | +If encryption keys already exist, the command must be called with |
| 57 | +the <info>--rotate</info> option in order to override those keys and re-encrypt |
| 58 | +existing secrets. |
| 59 | +
|
| 60 | + <info>%command.full_name% --rotate</info> |
| 61 | +EOF |
| 62 | + ) |
| 63 | + ; |
| 64 | + } |
| 65 | + |
| 66 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 67 | + { |
| 68 | + $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); |
| 69 | + $vault = $input->getOption('local') ? $this->localVault : $this->vault; |
| 70 | + |
| 71 | + if (null === $vault) { |
| 72 | + $io->success('The local vault is disabled.'); |
| 73 | + |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + if (!$input->getOption('rotate')) { |
| 78 | + if ($vault->generateKeys()) { |
| 79 | + $io->success($vault->getLastMessage()); |
| 80 | + |
| 81 | + if ($this->vault === $vault) { |
| 82 | + $io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️'); |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | + $io->warning($vault->getLastMessage()); |
| 89 | + |
| 90 | + return 1; |
| 91 | + } |
| 92 | + |
| 93 | + $secrets = []; |
| 94 | + foreach ($vault->list(true) as $name => $value) { |
| 95 | + if (null === $value) { |
| 96 | + $io->error($vault->getLastMessage()); |
| 97 | + |
| 98 | + return 1; |
| 99 | + } |
| 100 | + |
| 101 | + $secrets[$name] = $value; |
| 102 | + } |
| 103 | + |
| 104 | + if (!$vault->generateKeys(true)) { |
| 105 | + $io->warning($vault->getLastMessage()); |
| 106 | + |
| 107 | + return 1; |
| 108 | + } |
| 109 | + |
| 110 | + $io->success($vault->getLastMessage()); |
| 111 | + |
| 112 | + if ($secrets) { |
| 113 | + foreach ($secrets as $name => $value) { |
| 114 | + $vault->seal($name, $value); |
| 115 | + } |
| 116 | + |
| 117 | + $io->comment('Existing secrets have been rotated to the new keys.'); |
| 118 | + } |
| 119 | + |
| 120 | + if ($this->vault === $vault) { |
| 121 | + $io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️'); |
| 122 | + } |
| 123 | + |
| 124 | + return 0; |
| 125 | + } |
| 126 | +} |
0 commit comments