make use of environments in env:encrypt and env:decrypt consistent.
Hey,
when I am in APP_ENV=test, env:decrypt will decrypt .env.test.encrypted to .env and not to env.test. I had to specify --env=test for it to decrypt .env.test.encrypted to .env.test, even though I am already in the test environment. Also there is no way to decrypt .env.encrypted as long as I am inside of an APP_ENV=test because --env is not checked for an empty value.
Along the same lines I can not encrypt .env as long as I am in APP_ENV=test.
This PR makes:
- use
APP_ENVconsistent, so we don't cross-decrypt.env.${APP_ENV}.encyptedto.envbut to .env.${APP_ENV}instead. - allow
--env=to be set empty and en-/decrypt.envto/from.env.encryptedeven when running in anAPP_ENV=
Need @joedixon to take a look at this.
Sure thing. When he gave me a quick "looks good" or "go home" I will take care of the tests also.
Hey @foertel, I have just tested this and I'm not seeing the same behaviour.
After setting APP_ENV=test:
env:encrypt encrypts my .env file and not my .env.test file. Similarly, running env:decrypt decrypts the `.env.encrypted.
The only way to change which file gets encrypted / decrypted is by passing the --env option which is the intended behaviour of this command.
We are leveraging $this->laravel->environmentFilePath() in case the app has been configured to load the environment from a different location, but this behaviour is the same in both commands.
env:encrypt encrypts my
.envfile and not my.env.testfile.
So what you are saying is, that $this->laravel->environmentFilePath() is not taking into account your set APP_ENV? That doesn't sound right.
Did you have both an .env and and .env.testing file? Otherwise I think environmentFilePath() migt just have fallen back.
@joedixon This has been discussed in the Laravel community discord internal channel (if you want context). You have to set the environment variable in the command for it to work. For example, I've added .env.testing to my project and ran the following:
APP_ENV=testing php artisan env:encrypt
which gave the following output:
INFO Environment successfully encrypted.
Key .......................................................................................... base64:IQI31Zr8ep9NLtLGQzEH3Zh/741ufdgjlfMvgIj+xpU=
Cipher ............................................................................................................................... AES-256-CBC
Encrypted file ............................................................................................................ .env.testing.encrypted
When doing the reverse. It finds the env.testing environment file due to logic in LoadEnvironmentVariables@checkForSpecificEnvironmentFile. However EnvironmentDecryptCommand@outputFilePath() doesn't check environment variables when setting as it's looking for command flags so this gets missed and it defaults to decrypting env.testing.encrypted -> .env instead of env.testing.
I'd just rely on flags and this seems like a strange edge cast caused by not following documentation properly.. However, I hope that helps explain OP's issue so you can discuss it further.
this seems like a strange edge cast caused by not following documentation properly
As we have already discussed, it's litterally right above the headline, marked by a big violet lightbulb, saying:
The current application environment detection can be overridden by defining a server-level APP_ENV environment variable.
While I understand your point, that callout relates to the "Determining The Current Environment" section as opposed to the "Encrypting Environment Files" section.
That said, I've dug into the issue in more detail this morning as the behaviour is a little inconsistent. I've come up with the following PR which I'd love your feedback on: https://github.com/laravel/framework/pull/49439
In the scneario where you run the following:
APP_ENV=production php artisan env:encrypt
Your implementation would always look for .env.production and never fall back to .env which environmentFilePath() affords for us.
My solution is to still rely on environmentFilePath(), but use this file when generating the output file name so running the following:
APP_ENV=production php artisan env:decrypt --key abc-123
Results in the file being written to .env.production rather than .env.
The only time this would not be the case is if there isn't a .env.production present when decrypting where Laravel will fallback to .env which feels like the correct behviour to me.
Would love to hear your thoughts.
@foertel feel free to resend this if you still want to give it a go.