How to specify permissions of FileBasedCache files

I am using Django 3, Nginx, uWSGI

My normal Django Web/HTTP server creates and reads various cache files using FileBasedCache under /var/tmp/django_cache/. So far, this all works great...

Nginx and uwsgi run as the www-data user. So the various cache files and sub directories in the django_cache directory are owned by the the www-data user and group. However I also have another user: website, which is what I use to run various manage.py commands, and do things from cron, etc. The website user is in the www-data group, so normally, this has worked totally fine.

My problem is now that I am writing a management script (which is run by the website user, not the www-data user) that needs to access some cached files which were created by the www-data user, and because Django creates those files as

-rw------- 1 www-data www-data

my website user isn't able to read them and it creates an Exception.

I have tried:

  • adding session optional pam_umask.so umask=0137 to the common-session and common-session-interactive pam config files.
  • adding umask = 0137 to the site's uWSGI app configuration file

I believe that both of these changes/settings "work" in that if I do a test to tell django to write an arbitary file to /var/tmp/ it does so with the correct permissions, however Django's FileBasedCache continues to only write cache files that are read-write by the www-data user only.

I have looked at cache/backends/filebased.py's set() function - which appears to be the location that the files are actually written to disk - and I see no way to specify/override the default permissions that the directories and files are created as 660 (read and write by the www-data user and group).

I have two questions:

  1. Why is my test file write producing files with different permissions compared to Django's FileBasedCache set()?

In other words: for my "test", I did a very simple

with open("/var/tmp/django_cache/test", 'w') as f:
    f.write("test!")

in a quick little web view, and that creates the file with the correct permissions (as I would have expected it to...)

However Django's FileBasedCache set() seems to do essentially the same thing:

with open(fd, 'wb') as f:
    self._write_content(f, timeout, value)

in other words, it's not specifying permissions, there's no umask setting, etc...

So why is my test open/write producing a different result to what Django is doing? It's also running from with in the nginx/uwsgi/django web system (not a management command), so why is it different?

  1. And secondly; how to fix this problem! Do I need to extend the FileBasedCache class, use that instead, and override the set() function? That seems so over the top for what should be a simple problem to fix. Why isn't Django's cache set() creating the temp files with the right permissions?

I appreciate any insight!

Oh, two things I have considered but really don't want to do, because of how much extra work/change/testing it will involve:

  • run uwsgi as the website user
  • switch to some other form of caching (redis etc)
Back to Top