vendor/symfony/framework-bundle/Command/SecretsDecryptToLocalCommand.php line 35

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 Nicolas Grekas <p@tchwork.com>
  20.  *
  21.  * @internal
  22.  */
  23. final class SecretsDecryptToLocalCommand extends Command
  24. {
  25.     protected static $defaultName 'secrets:decrypt-to-local';
  26.     protected static $defaultDescription 'Decrypt all secrets and stores them in the local vault';
  27.     private $vault;
  28.     private $localVault;
  29.     public function __construct(AbstractVault $vaultAbstractVault $localVault null)
  30.     {
  31.         $this->vault $vault;
  32.         $this->localVault $localVault;
  33.         parent::__construct();
  34.     }
  35.     protected function configure()
  36.     {
  37.         $this
  38.             ->setDescription(self::$defaultDescription)
  39.             ->addOption('force''f'InputOption::VALUE_NONE'Force overriding of secrets that already exist in the local vault')
  40.             ->setHelp(<<<'EOF'
  41. The <info>%command.name%</info> command decrypts all secrets and copies them in the local vault.
  42.     <info>%command.full_name%</info>
  43. When the option <info>--force</info> is provided, secrets that already exist in the local vault are overriden.
  44.     <info>%command.full_name% --force</info>
  45. EOF
  46.             )
  47.         ;
  48.     }
  49.     protected function execute(InputInterface $inputOutputInterface $output): int
  50.     {
  51.         $io = new SymfonyStyle($input$output instanceof ConsoleOutputInterface $output->getErrorOutput() : $output);
  52.         if (null === $this->localVault) {
  53.             $io->error('The local vault is disabled.');
  54.             return 1;
  55.         }
  56.         $secrets $this->vault->list(true);
  57.         $io->comment(sprintf('%d secret%s found in the vault.'\count($secrets), !== \count($secrets) ? 's' ''));
  58.         $skipped 0;
  59.         if (!$input->getOption('force')) {
  60.             foreach ($this->localVault->list() as $k => $v) {
  61.                 if (isset($secrets[$k])) {
  62.                     ++$skipped;
  63.                     unset($secrets[$k]);
  64.                 }
  65.             }
  66.         }
  67.         if ($skipped 0) {
  68.             $io->warning([
  69.                 sprintf('%d secret%s already overridden in the local vault and will be skipped.'$skipped!== $skipped 's are' ' is'),
  70.                 'Use the --force flag to override these.',
  71.             ]);
  72.         }
  73.         foreach ($secrets as $k => $v) {
  74.             if (null === $v) {
  75.                 $io->error($this->vault->getLastMessage() ?? sprintf('Secret "%s" has been skipped as there was an error reading it.'$k));
  76.                 continue;
  77.             }
  78.             $this->localVault->seal($k$v);
  79.             $io->note($this->localVault->getLastMessage());
  80.         }
  81.         return 0;
  82.     }
  83. }