SecretsEncryptFromLocalCommand.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Output\ConsoleOutputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. /**
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. *
  20. * @internal
  21. */
  22. final class SecretsEncryptFromLocalCommand extends Command
  23. {
  24. protected static $defaultName = 'secrets:encrypt-from-local';
  25. private $vault;
  26. private $localVault;
  27. public function __construct(AbstractVault $vault, AbstractVault $localVault = null)
  28. {
  29. $this->vault = $vault;
  30. $this->localVault = $localVault;
  31. parent::__construct();
  32. }
  33. protected function configure()
  34. {
  35. $this
  36. ->setDescription('Encrypt all local secrets to the vault')
  37. ->setHelp(<<<'EOF'
  38. The <info>%command.name%</info> command encrypts all locally overridden secrets to the vault.
  39. <info>%command.full_name%</info>
  40. EOF
  41. )
  42. ;
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output): int
  45. {
  46. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  47. if (null === $this->localVault) {
  48. $io->error('The local vault is disabled.');
  49. return 1;
  50. }
  51. foreach ($this->vault->list(true) as $name => $value) {
  52. $localValue = $this->localVault->reveal($name);
  53. if (null !== $localValue && $value !== $localValue) {
  54. $this->vault->seal($name, $localValue);
  55. } elseif (null !== $message = $this->localVault->getLastMessage()) {
  56. $io->error($message);
  57. return 1;
  58. }
  59. }
  60. return 0;
  61. }
  62. }