Skip to content
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

Feature: php artisan key:rotate command #54437

Open
wants to merge 8 commits into
base: 11.x
Choose a base branch
from

Conversation

tanthammar
Copy link
Contributor

@tanthammar tanthammar commented Jan 31, 2025

New Feature:

This PR adds a new php artisan key:rotate command and adds a safety level to the KeyGenerateCommand.

When generating a new application key, users are now prompted to optionally save the existing key before overwriting it.
This completes the Laravel 11 rotating APP_KEY feature.

Example:

When running php artisan key:generate, if an existing key is found, the user is prompted:

There is already an app key. Do you want to store the old key before generating a new one? (yes/no) [yes]:

If the user confirms, the key:rotate command is executed to save the old key before generating a new one.

Key Changes:

  • Added a new shouldSaveExistingKey() method to KeyGenerateCommand to prompt users to save the existing key.
  • Introduced a new RotateKeyCommand to handle the key rotation process.
  • Added missing return types to improve code clarity and consistency.

Testing - help, please:

I need help with this part. I tested it locally but I don't know how to write a test that alters the .env file

Non-breaking change:

This change is fully backward-compatible and does not have any effect, unless the user opts to save the old key.

Let me know if you’d like to refine this further!


Yes, phpstorm AI plugin, helped me write this PR description ;)

@tanthammar
Copy link
Contributor Author

tanthammar commented Feb 1, 2025

Additional thoughts:

  • Do key generate --force, if user said yes to store the key, to avoid ending up with an empty app key?
  • use should confirm trait?
  • skip all confirmations, when not in production?
  • skip entirely, when not in production? The command can always be used by itself...

tanthammar and others added 5 commits February 1, 2025 21:35
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
'There is already an app key. Do you want to store the old key before generating a new one?',
true
)) {
$this->call('key:rotate');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to have the exact action of the rotate inside method which is named "should*" and returns boolean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants