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

Path issues with read_env since 11.0+ #497

Open
hannylicious opened this issue Sep 6, 2023 · 11 comments
Open

Path issues with read_env since 11.0+ #497

hannylicious opened this issue Sep 6, 2023 · 11 comments
Assignees
Labels
need reproducible example Example is required to reproduce the issue

Comments

@hannylicious
Copy link

I have the following in a wsgi.py:

PROJECT_ROOT = Path(__file__).resolve().parent.parent

import environ

env = environ.Env(
    DJANGO_SETTINGS_MODULE=(str, "myapp.settings.base"),
)
# Read the .env and override any existing environment variables
env.read_env(PROJECT_ROOT / ".env", overwrite=True)

I have this in the settings.py:

ROOT_DIR = Path(__file__).parent.parent.parent
env = environ.Env(
    # set castings, default values, Django settings
    DJANGO_DEBUG=(bool, False),
    DJANGO_ALLOWED_HOSTS=(list, []),
    DJANGO_SESSION_EXPIRE_AT_BROWSER_CLOSE=(bool, True),
    DB_ENGINE=(str, "mssql"),
)
env.read_env(ROOT_DIR / ".env")

In versions prior to 0.11.0 this all worked without issue. In 0.11.0 and above (including the current 11.2) that code is no longer working.

I am getting errors that environment variables are not loaded (such as the SECRET_KEY) which is set in the settings.

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

I have tried updating the wsgi code to this (per the docs):

# wsgi
import environ

ENVIRON_ROOT = environ.Path(__file__) - 2

env = environ.Env(
    DJANGO_SETTINGS_MODULE=(str, "myapp.settings.base"),
)
# Read the .env and override any existing environment variables
env.read_env(ENVIRON_ROOT(".env"), overwrite=True)

I updated the settings to the following:

ENVIRON_DIR = environ.Path(__file__) - 3
# Environ
env = environ.Env(
    # set castings, default values, Django settings
    DJANGO_DEBUG=(bool, False),
    DJANGO_ALLOWED_HOSTS=(list, []),
    DJANGO_SESSION_EXPIRE_AT_BROWSER_CLOSE=(bool, True),
    DB_ENGINE=(str, "mssql"),
)
# OS environment variables take precedence over variables from .env
env.read_env(ENVIRON_DIR(".env"))

I'm getting the same error: that 'SECRET_KEY' is empty.

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

If I roll back to version 0.10.0 everything goes back to working as expected.

I have tried modifying my code to use each example given in the docs - each resulted in the same error being thrown.

I load my 'SECRET_KEY' in settings immediately following loading the env file:

# settings/base.py
env.read_env(os.path.join(ENVIRON_DIR, '.env'), overwrite=True)

DEBUG = env.bool("DJANGO_DEBUG")
SECRET_KEY = env.str("DJANGO_SECRET_KEY")

DJANGO_SECRET_KEY is set in my .env file.

Again, this all worked perfectly prior to 0.11.0 - but I don't see any references to changes to read_env in the Changelog.

Any ideas?

@hannylicious
Copy link
Author

Additional note: if I change DJANGO_SECRET_KEY to SECRET_KEY in my .env file the application will work... but seeing as I have this line:

SECRET_KEY = env.str("DJANGO_SECRET_KEY")

in my settings/base.py and it had always worked prior, I'm a little confused as to why it now has to be explicitly named in the .env file and isn't being read in the settings as it had been prior.

@hannylicious
Copy link
Author

The issue has to do with the SECRET_KEY value itself and how it's being read by django-environ.

Hashtags (#) are a naturally occurring element in Django secret keys: however for some reason when django-environ comes across them it is not reading anything beyond the hashtag.

I'm not sure the best way to fix this - but I'll keep digging. For anyone else at this time - just make sure there are no hashtags in your SECRET_KEY and you should be fine.

@hannylicious
Copy link
Author

So, after digging around in the code I realized that I could wrap my secret key in quotes and it would render appropriately.

However, it didn't used to be this way and in the documentation it doesn't show wrapping the secret key in quotes.

SECRET_KEY=your-secret-key (from the docs)

But a temporary workaround is:

SECRET_KEY="your-secret-key"

@sergeyklay sergeyklay added the bug Something isn't working label Sep 8, 2023
@sergeyklay
Copy link
Collaborator

Hello @hannylicious,

Thank you for bringing this issue to our attention. We are currently investigating several issues related to environment variable parsing, especially concerning the SECRET_KEY.

Could you please try using the latest version and see if the issue persists? If it does, consider rolling back to version 0.10.0 as a temporary workaround.

Your input is invaluable as we try to get to the bottom of this. It seems a few things have slipped through the cracks in our codebase that require immediate attention.

Best regards,

@sergeyklay sergeyklay self-assigned this Sep 8, 2023
@sergeyklay sergeyklay added need reproducible example Example is required to reproduce the issue and removed bug Something isn't working labels Sep 8, 2023
@hannylicious
Copy link
Author

I can confirm this occurs with 11.0, 11.1 and 11.2.

I'll see if I can write some tests that show the cases where it's an issue (essentially it's any SECRET_KEY that has a hashtag in it that is not surrounded in quotes)

@hong539
Copy link

hong539 commented Sep 19, 2023

Hello, every body, I think I got the similar issue when I want to using django-environ for My little django app.
And here is My github repo link:
local_library_website

And My django environment is setup with pyenv+poetry on my Arch Linux VM.
pyenv to lock My python interpreter and verion == 3.8.16
poetry to manage My project denpendency and also the version info is Poetry (version 1.5.1)
dependency details will be included in these files, and there are the links:

pyproject.toml
poetry.lock

And I've tried django-environ versions 0.11.2/0.10.0 with this little command
Also here is my codes for settings.py
And .env file is here .env.test

python3 settings.py

Both verions of django-environ give me the messages:

Traceback (most recent call last):
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 387, in get_value
    value = self.ENVIRON[var_name]
  File "/home/hong/.pyenv/versions/3.8.16/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "settings.py", line 33, in <module>
    SECRET_KEY = env('SECRET_KEY')
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 198, in __call__
    return self.get_value(
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 391, in get_value
    raise ImproperlyConfigured(error_msg) from exc
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

And how should I do next step? Any guides/tips? Thanks a lot!

@hong539
Copy link

hong539 commented Sep 19, 2023

Hello, every body, I think I got the similar issue when I want to using django-environ for My little django app. And here is My github repo link: local_library_website

And My django environment is setup with pyenv+poetry on my Arch Linux VM. pyenv to lock My python interpreter and verion == 3.8.16 poetry to manage My project denpendency and also the version info is Poetry (version 1.5.1) dependency details will be included in these files, and there are the links:

pyproject.toml poetry.lock

And I've tried django-environ versions 0.11.2/0.10.0 with this little command Also here is my codes for settings.py And .env file is here .env.test

python3 settings.py

Both verions of django-environ give me the messages:

Traceback (most recent call last):
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 387, in get_value
    value = self.ENVIRON[var_name]
  File "/home/hong/.pyenv/versions/3.8.16/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'SECRET_KEY'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "settings.py", line 33, in <module>
    SECRET_KEY = env('SECRET_KEY')
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 198, in __call__
    return self.get_value(
  File "/home/hong/.cache/pypoetry/virtualenvs/local-library-website-nGKGTRaN-py3.8/lib/python3.8/site-packages/environ/environ.py", line 391, in get_value
    raise ImproperlyConfigured(error_msg) from exc
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable

And how should I do next step? Any guides/tips? Thanks a lot!

Oh...Sorry I think I have to settinup the right PATH for BASE_DIR... to actually find out where is ".env.test" files. XD

@mindcruzer
Copy link

mindcruzer commented Oct 18, 2023

I'm surprised that it's been over a month and this hasn't been fixed. My CI tests caught this error and it took me about 30 mins to figure out that it was because there was a # character at the start of my production SECRET_KEY. My app secrets are read from a file, and putting the SECRET_KEY in quotes did not solve the problem. Downgrading to 0.10.0 was the only way to get around this as changing the SECRET_KEY value will kill all existing sessions.

@amstilp
Copy link

amstilp commented Nov 17, 2023

I'm running into this issue as well with a secret key that has a # character in it. It is stored a file; putting quotes around it did not help.

@amstilp
Copy link

amstilp commented Nov 17, 2023

Hi @sergeyklay I put together a repo containing a reproducible example of the bug as I'm seeing it! Hope this helps.

https://github.com/amstilp/django-environ-issue-497

@dk-WZFinTech
Copy link

Any news for this problem - it blocks most of out project for update this package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need reproducible example Example is required to reproduce the issue
Projects
None yet
Development

No branches or pull requests

6 participants