-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add secrets:*
commands and %env(secret:...)%
processor to deal with secrets seamlessly
#33997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/Symfony/Bundle/FrameworkBundle/Command/SecretsDecryptToLocalCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class SecretsDecryptToLocalCommand extends Command | ||
{ | ||
protected static $defaultName = 'secrets:decrypt-to-local'; | ||
|
||
private $vault; | ||
private $localVault; | ||
|
||
public function __construct(AbstractVault $vault, AbstractVault $localVault = null) | ||
{ | ||
$this->vault = $vault; | ||
$this->localVault = $localVault; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Decrypts all secrets and stores them in the local vault.') | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces overriding of secrets that already exist in the local vault') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command list decrypts all secrets and stores them in the local vault.. | ||
|
||
<info>%command.full_name%</info> | ||
|
||
When the option <info>--force</info> is provided, secrets that already exist in the local vault are overriden. | ||
|
||
<info>%command.full_name% --force</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
|
||
if (null === $this->localVault) { | ||
$io->error('The local vault is disabled.'); | ||
|
||
return 1; | ||
} | ||
|
||
$secrets = $this->vault->list(true); | ||
|
||
if (!$input->getOption('force')) { | ||
foreach ($this->localVault->list() as $k => $v) { | ||
unset($secrets[$k]); | ||
} | ||
} | ||
|
||
foreach ($secrets as $k => $v) { | ||
if (null === $v) { | ||
$io->error($this->vault->getLastMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$this->localVault->seal($k, $v); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/Symfony/Bundle/FrameworkBundle/Command/SecretsEncryptFromLocalCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class SecretsEncryptFromLocalCommand extends Command | ||
{ | ||
protected static $defaultName = 'secrets:encrypt-from-local'; | ||
|
||
private $vault; | ||
private $localVault; | ||
|
||
public function __construct(AbstractVault $vault, AbstractVault $localVault = null) | ||
{ | ||
$this->vault = $vault; | ||
$this->localVault = $localVault; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Encrypts all local secrets to the vault.') | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces overriding of secrets that already exist in the vault') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command list encrypts all local secrets and stores them in the vault.. | ||
|
||
<info>%command.full_name%</info> | ||
|
||
When the option <info>--force</info> is provided, secrets that already exist in the vault are overriden. | ||
|
||
<info>%command.full_name% --force</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
|
||
if (null === $this->localVault) { | ||
$io->error('The local vault is disabled.'); | ||
|
||
return 1; | ||
} | ||
|
||
$secrets = $this->localVault->list(true); | ||
|
||
if (!$input->getOption('force')) { | ||
foreach ($this->vault->list() as $k => $v) { | ||
unset($secrets[$k]); | ||
} | ||
} | ||
|
||
foreach ($secrets as $k => $v) { | ||
if (null === $v) { | ||
$io->error($this->localVault->getLastMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$this->vault->seal($k, $v); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
src/Symfony/Bundle/FrameworkBundle/Command/SecretsGenerateKeysCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Secrets\AbstractVault; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Tobias Schultze <http://tobion.de> | ||
* @author Jérémy Derussé <[email protected]> | ||
* @author Nicolas Grekas <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class SecretsGenerateKeysCommand extends Command | ||
{ | ||
protected static $defaultName = 'secrets:generate-keys'; | ||
|
||
private $vault; | ||
private $localVault; | ||
|
||
public function __construct(AbstractVault $vault, AbstractVault $localVault = null) | ||
{ | ||
$this->vault = $vault; | ||
$this->localVault = $localVault; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Generates new encryption keys.') | ||
->addOption('local', 'l', InputOption::VALUE_NONE, 'Updates the local vault.') | ||
->addOption('rotate', 'r', InputOption::VALUE_NONE, 'Re-encrypts existing secrets with the newly generated keys.') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command generates a new encryption key. | ||
|
||
<info>%command.full_name%</info> | ||
|
||
If encryption keys already exist, the command must be called with | ||
the <info>--rotate</info> option in order to override those keys and re-encrypt | ||
existing secrets. | ||
|
||
<info>%command.full_name% --rotate</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
$vault = $input->getOption('local') ? $this->localVault : $this->vault; | ||
|
||
if (null === $vault) { | ||
$io->success('The local vault is disabled.'); | ||
|
||
return 1; | ||
} | ||
|
||
if (!$input->getOption('rotate')) { | ||
if ($vault->generateKeys()) { | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$io->success($vault->getLastMessage()); | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ($this->vault === $vault) { | ||
$io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️'); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
$io->warning($vault->getLastMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$secrets = []; | ||
foreach ($vault->list(true) as $name => $value) { | ||
if (null === $value) { | ||
$io->error($vault->getLastMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$secrets[$name] = $value; | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (!$vault->generateKeys(true)) { | ||
$io->warning($vault->getLastMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$io->success($vault->getLastMessage()); | ||
|
||
if ($secrets) { | ||
foreach ($secrets as $name => $value) { | ||
$vault->seal($name, $value); | ||
} | ||
|
||
$io->comment('Existing secrets have been rotated to the new keys.'); | ||
} | ||
|
||
if ($this->vault === $vault) { | ||
$io->caution('DO NOT COMMIT THE DECRYPTION KEY FOR THE PROD ENVIRONMENT⚠️'); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.