SensitiveConfigSetCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Console\Command\App;
  7. use Magento\Config\App\Config\Type\System;
  8. use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
  9. use Magento\Deploy\Console\Command\App\SensitiveConfigSet\SensitiveConfigSetFacade;
  10. use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
  11. use Magento\Deploy\Model\DeploymentConfig\Hash;
  12. use Magento\Framework\App\Config\ScopeConfigInterface;
  13. use Magento\Framework\Console\Cli;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * Command for set sensitive variable through deploy process
  21. */
  22. class SensitiveConfigSetCommand extends Command
  23. {
  24. /**
  25. * Name of "interactive" input option
  26. */
  27. const INPUT_OPTION_INTERACTIVE = 'interactive';
  28. /**
  29. * Name of "configuration scope" input option
  30. */
  31. const INPUT_OPTION_SCOPE = 'scope';
  32. /**
  33. * Name of "configuration scope code" input option
  34. */
  35. const INPUT_OPTION_SCOPE_CODE = 'scope-code';
  36. /**
  37. * Name of "configuration path" input argument
  38. */
  39. const INPUT_ARGUMENT_PATH = 'path';
  40. /**
  41. * Name of "configuration value" input argument
  42. */
  43. const INPUT_ARGUMENT_VALUE = 'value';
  44. /**
  45. * The config change detector.
  46. *
  47. * @var ChangeDetector
  48. */
  49. private $changeDetector;
  50. /**
  51. * The hash manager.
  52. *
  53. * @var Hash
  54. */
  55. private $hash;
  56. /**
  57. * The facade for command.
  58. *
  59. * @var SensitiveConfigSetFacade
  60. */
  61. private $facade;
  62. /**
  63. * Emulator adminhtml area for CLI command.
  64. *
  65. * @var EmulatedAdminhtmlAreaProcessor
  66. */
  67. private $emulatedAreaProcessor;
  68. /**
  69. * @param SensitiveConfigSetFacade $facade The processor facade
  70. * @param ChangeDetector $changeDetector The config change detector
  71. * @param Hash $hash The hash manager
  72. * @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor Emulator adminhtml area for CLI command
  73. */
  74. public function __construct(
  75. SensitiveConfigSetFacade $facade,
  76. ChangeDetector $changeDetector,
  77. Hash $hash,
  78. EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
  79. ) {
  80. $this->facade = $facade;
  81. $this->changeDetector = $changeDetector;
  82. $this->hash = $hash;
  83. $this->emulatedAreaProcessor = $emulatedAreaProcessor;
  84. parent::__construct();
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. protected function configure()
  90. {
  91. $this->addArgument(
  92. self::INPUT_ARGUMENT_PATH,
  93. InputArgument::OPTIONAL,
  94. 'Configuration path for example group/section/field_name'
  95. );
  96. $this->addArgument(
  97. self::INPUT_ARGUMENT_VALUE,
  98. InputArgument::OPTIONAL,
  99. 'Configuration value'
  100. );
  101. $this->addOption(
  102. self::INPUT_OPTION_INTERACTIVE,
  103. 'i',
  104. InputOption::VALUE_NONE,
  105. 'Enable interactive mode to set all sensitive variables'
  106. );
  107. $this->addOption(
  108. self::INPUT_OPTION_SCOPE,
  109. null,
  110. InputOption::VALUE_OPTIONAL,
  111. 'Scope for configuration, if not set use \'default\'',
  112. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  113. );
  114. $this->addOption(
  115. self::INPUT_OPTION_SCOPE_CODE,
  116. null,
  117. InputOption::VALUE_OPTIONAL,
  118. 'Scope code for configuration, empty string by default',
  119. ''
  120. );
  121. $this->setName('config:sensitive:set')
  122. ->setDescription('Set sensitive configuration values');
  123. parent::configure();
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. protected function execute(InputInterface $input, OutputInterface $output)
  129. {
  130. if ($this->changeDetector->hasChanges(System::CONFIG_TYPE)) {
  131. $output->writeln(
  132. '<error>'
  133. . 'This command is unavailable right now. '
  134. . 'To continue working with it please run app:config:import or setup:upgrade command before.'
  135. . '</error>'
  136. );
  137. return Cli::RETURN_FAILURE;
  138. }
  139. try {
  140. $this->emulatedAreaProcessor->process(function () use ($input, $output) {
  141. $this->facade->process($input, $output);
  142. });
  143. $this->hash->regenerate(System::CONFIG_TYPE);
  144. return Cli::RETURN_SUCCESS;
  145. } catch (\Exception $e) {
  146. $output->writeln(
  147. sprintf('<error>%s</error>', $e->getMessage())
  148. );
  149. return Cli::RETURN_FAILURE;
  150. }
  151. }
  152. }