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 11 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.

Additional thoughts/questions:

  • use ConfirmableTrait trait?
  • skip all confirmations, when not in production?
  • skip entirely, when not in production? The command can always be used by itself...

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


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

tanthammar and others added 5 commits February 1, 2025 21:35
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: Mior Muhammad Zaki <[email protected]>
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