SecretsListCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Helper\Dumper;
  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. final class SecretsListCommand extends Command
  27. {
  28. protected static $defaultName = 'secrets:list';
  29. private $vault;
  30. private $localVault;
  31. public function __construct(AbstractVault $vault, AbstractVault $localVault = null)
  32. {
  33. $this->vault = $vault;
  34. $this->localVault = $localVault;
  35. parent::__construct();
  36. }
  37. protected function configure()
  38. {
  39. $this
  40. ->setDescription('List all secrets')
  41. ->addOption('reveal', 'r', InputOption::VALUE_NONE, 'Display decrypted values alongside names')
  42. ->setHelp(<<<'EOF'
  43. The <info>%command.name%</info> command list all stored secrets.
  44. <info>%command.full_name%</info>
  45. When the option <info>--reveal</info> is provided, the decrypted secrets are also displayed.
  46. <info>%command.full_name% --reveal</info>
  47. EOF
  48. )
  49. ;
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int
  52. {
  53. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  54. $io->comment('Use <info>"%env(<name>)%"</info> to reference a secret in a config file.');
  55. if (!$reveal = $input->getOption('reveal')) {
  56. $io->comment(sprintf('To reveal the secrets run <info>php %s %s --reveal</info>', $_SERVER['PHP_SELF'], $this->getName()));
  57. }
  58. $secrets = $this->vault->list($reveal);
  59. $localSecrets = null !== $this->localVault ? $this->localVault->list($reveal) : null;
  60. $rows = [];
  61. $dump = new Dumper($output);
  62. $dump = static function (?string $v) use ($dump) {
  63. return null === $v ? '******' : $dump($v);
  64. };
  65. foreach ($secrets as $name => $value) {
  66. $rows[$name] = [$name, $dump($value)];
  67. }
  68. if (null !== $message = $this->vault->getLastMessage()) {
  69. $io->comment($message);
  70. }
  71. foreach ($localSecrets ?? [] as $name => $value) {
  72. if (isset($rows[$name])) {
  73. $rows[$name][] = $dump($value);
  74. }
  75. }
  76. if (null !== $this->localVault && null !== $message = $this->localVault->getLastMessage()) {
  77. $io->comment($message);
  78. }
  79. (new SymfonyStyle($input, $output))
  80. ->table(['Secret', 'Value'] + (null !== $localSecrets ? [2 => 'Local Value'] : []), $rows);
  81. $io->comment("Local values override secret values.\nUse <info>secrets:set --local</info> to define them.");
  82. return 0;
  83. }
  84. }