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

Set application key in .env file (if exists) or fallback config file. #560

Closed
wants to merge 12 commits into from
Closed

Set application key in .env file (if exists) or fallback config file. #560

wants to merge 12 commits into from

Conversation

ericp-mrel
Copy link

@bennothommo
Copy link
Contributor

@ericp-mrel Tested and it works fine, but there's one more scenario we need to cover here. A not-so-common but still supported way of configuration in October is to use a .env file just to define the current environment (dev, staging, prod, etc.) and still use the environment configuration directories under config/dev, config/staging, etc.

Your code however will only try the environment file if it exists, but won't write anything as there's no APP_KEY item defined. We'll need to do a check in .env first to see if there's an APP_KEY item that is defined - if not, it needs to fall back to the config file scenario, which I believe covers the multiple environment setup.

@ericp-mrel
Copy link
Author

@bennothommo I've added functionality so it will check if the environment specific app config file exists and write the key there instead of only falling back to the main app config file.

Do you think it's worth me adding checks to actually validate the app key line exists in that file as well (e.g. /config/dev/app.php)?
'key' => 'CHANGE_ME!!!!!!!!!!!!!!!!!!!!!!!',

@bennothommo
Copy link
Contributor

@ericp-mrel That would be good. Ideally, the flow would be as follows:

  • If .env exists
    • If APP_KEY is specified, write the key to .env
    • If APP_KEY is not specified, but APP_ENV is specified, go to If environment config exists and try to apply key to specified environment.
    • Else, go to Else.
  • If environment config exists
    • If config/<environment>/app.php exists for environment, and key attribute is available, write key to this config value.
    • Else, go to Else.
  • Else
    • If config/app.php exists, and key attribute is available, write key to this config value.
    • Else, do nothing.

@ericp-mrel
Copy link
Author

@bennothommo I've added the necessary logic to properly check the environment specific app config file after the .env file check and then the fallback to the main app config file.

I've also switched to using a regex for the key replacement in order to fix the issue where the app key wasn't being updated if it was originally empty since it was trying to perform a string replacement for an empty string on the entire file, which obviously doesn't result in any change.

Let me know if there's any code you think I could clean up.

Thanks

ericp-mrel and others added 2 commits February 11, 2021 07:31
Co-authored-by: Luke Towers <github@luketowers.ca>
Co-authored-by: Luke Towers <github@luketowers.ca>
@LukeTowers
Copy link
Contributor

Would it be possible to get some unit tests for each of the cases @bennothommo described?

@ericp-mrel
Copy link
Author

Would it be possible to get some unit tests for each of the cases @bennothommo described?

Sorry, I'm new to testing. How exactly would I write a test for this command without affecting the rest of the test since it requires creating/updating existing files (which could affect tests which ran after it).

I tried looking at Laravel itself as a starting point to see how they do it, and they don't have a any tests for the key generate command either.

@bennothommo
Copy link
Contributor

@ericp-mrel You could check the tests I wrote for the october:env command - they may be able to assist you:
https://github.com/octobercms/october/blob/develop/tests/unit/system/console/OctoberEnvTest.php

@bennothommo
Copy link
Contributor

I'd in fact suggest putting the tests in the main October repo as a separate PR - the key:generate command functionality makes changes to core code, not framework files.

@ericp-mrel
Copy link
Author

Alright, I'll send a PR to the main repo, but is there anything special I need to do in order for the tests to actually work with the CI tests since these tests require another PR on the framework repo which isn't merge?

Fixes an exception from being thrown when running tests about the env option not existing
@bennothommo
Copy link
Contributor

bennothommo commented Feb 22, 2021

@ericp-mrel don't stress too much about the CI on the separate repo - the tests will fail, but I'll manually test it with everything merged together when it's ready.

@daftspunk
Copy link
Member

daftspunk commented Mar 8, 2021

See comment in octobercms/october#5496

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants