vendor/symfony/framework-bundle/Command/SecretsGenerateKeysCommand.php line 36

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