SecretsDecryptToLocalCommand.php 3.1 KB

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