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

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\Command\Command;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. /**
  19.  * @author Tobias Schultze <http://tobion.de>
  20.  * @author Jérémy Derussé <jeremy@derusse.com>
  21.  * @author Nicolas Grekas <p@tchwork.com>
  22.  *
  23.  * @internal
  24.  */
  25. final class SecretsGenerateKeysCommand extends Command
  26. {
  27.     protected static $defaultName 'secrets:generate-keys';
  28.     protected static $defaultDescription 'Generate new encryption keys';
  29.     private $vault;
  30.     private $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.             ->setDescription(self::$defaultDescription)
  41.             ->addOption('local''l'InputOption::VALUE_NONE'Update the local vault.')
  42.             ->addOption('rotate''r'InputOption::VALUE_NONE'Re-encrypt existing secrets with the newly generated keys.')
  43.             ->setHelp(<<<'EOF'
  44. The <info>%command.name%</info> command generates a new encryption key.
  45.     <info>%command.full_name%</info>
  46. If encryption keys already exist, the command must be called with
  47. the <info>--rotate</info> option in order to override those keys and re-encrypt
  48. existing secrets.
  49.     <info>%command.full_name% --rotate</info>
  50. EOF
  51.             )
  52.         ;
  53.     }
  54.     protected function execute(InputInterface $inputOutputInterface $output): int
  55.     {
  56.         $io = new SymfonyStyle($input$output instanceof ConsoleOutputInterface $output->getErrorOutput() : $output);
  57.         $vault $input->getOption('local') ? $this->localVault $this->vault;
  58.         if (null === $vault) {
  59.             $io->success('The local vault is disabled.');
  60.             return 1;
  61.         }
  62.         if (!$input->getOption('rotate')) {
  63.             if ($vault->generateKeys()) {
  64.                 $io->success($vault->getLastMessage());
  65.                 if ($this->vault === $vault) {
  66.                     $io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️');
  67.                 }
  68.                 return 0;
  69.             }
  70.             $io->warning($vault->getLastMessage());
  71.             return 1;
  72.         }
  73.         $secrets = [];
  74.         foreach ($vault->list(true) as $name => $value) {
  75.             if (null === $value) {
  76.                 $io->error($vault->getLastMessage());
  77.                 return 1;
  78.             }
  79.             $secrets[$name] = $value;
  80.         }
  81.         if (!$vault->generateKeys(true)) {
  82.             $io->warning($vault->getLastMessage());
  83.             return 1;
  84.         }
  85.         $io->success($vault->getLastMessage());
  86.         if ($secrets) {
  87.             foreach ($secrets as $name => $value) {
  88.                 $vault->seal($name$value);
  89.             }
  90.             $io->comment('Existing secrets have been rotated to the new keys.');
  91.         }
  92.         if ($this->vault === $vault) {
  93.             $io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️');
  94.         }
  95.         return 0;
  96.     }
  97. }