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

Add support for secure Redis (TLS support) #5526

Merged
merged 55 commits into from
Nov 21, 2024

Conversation

qwerrrqw
Copy link
Contributor

@qwerrrqw qwerrrqw commented Nov 12, 2024

Add Redis TLS support for Heroku

Fixes #5500

base.py(286~287)

  • CELERY_BROKER_USE_SSL
  • CELERY_REDIS_BACKEND_USE_SSL

production.py(51~54)

  • REDIS_SSL
  • Set to default=False, no impact on existing environments

You only need to set REDIS_SSL=True in Heroku environment
Add only new options without modifying existing settings

base.py(286~287)
- CELERY_BROKER_USE_SSL
- CELERY_REDIS_BACKEND_USE_SSL

production.py(51~54)
- REDIS_SSL
- Set to default=False, no impact on existing environments

You only need to set REDIS_SSL=True in Heroku environment
Add only new options without modifying existing settings
@qwerrrqw
Copy link
Contributor Author

How to use Redis TLS in Heroku

  1. Create Redis addon:
    [bash] heroku addons:create heroku-redis:mini

  2. Set environment variables:
    [bash] heroku config:set REDIS_SSL=True
    [bash] heroku config:set CELERY_BROKER_USE_SSL=True
    [bash] heroku config:set CELERY_REDIS_BACKEND_USE_SSL=True

That's it! Your Redis connection will now use TLS encryption.

Note: These settings are optional and default to False, so they won't affect existing deployments unless explicitly enabled.

@browniebroke
Copy link
Member

We probably should update the documentation accordingly: https://cookiecutter-django.readthedocs.io/en/latest/3-deployment/deployment-on-heroku.html

The source is here: https://github.com/cookiecutter/cookiecutter-django/blob/master/docs/3-deployment/deployment-on-heroku.rst

@qwerrrqw
Copy link
Contributor Author

qwerrrqw commented Nov 14, 2024

I've updated the documentation as requested:

deployment-on-heroku.rst

  1. Added Redis TLS configuration commands in the Script section: line 18-23

  2. Added explanation in the Notes section under "Redis Configuration": line 69-73

Please let me know if any further changes are needed.

@@ -14,6 +14,14 @@ Run these commands to deploy the project to Heroku:

# Note: this is not a free plan
heroku addons:create heroku-postgresql:essential-0

heroku addons:create heroku-redis:mini
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already present at line 30

Comment on lines 20 to 23
# Enable Redis TLS support (required for new Heroku Redis instances)
heroku config:set REDIS_SSL=True
heroku config:set CELERY_BROKER_USE_SSL=True
heroku config:set CELERY_REDIS_BACKEND_USE_SSL=True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put it after the existing line, please

Comment on lines 69 to 72
Redis Configuration
+++++++++++++++++++

Heroku Redis now requires TLS connections by default. The script above includes the necessary TLS configuration. These settings are optional and default to False, so they won't affect existing deployments unless explicitly enabled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this. While it seems like an important information right now, in month+ time, it will be irrelevant, so I'd rather not have this part in our docs

@qwerrrqw
Copy link
Contributor Author

I've made the requested changes:

  1. Removed the duplicate Redis installation command
  2. Moved the TLS configuration commands right after the Redis installation. line: 25-28
  3. Removed the Redis Configuration section from the Notes

Thank you for your kind explanation

Copy link
Member

@browniebroke browniebroke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I missed that earlier, but feels like we have 3 settings to control the same thing, and they should all have the same value. Maybe let's put all under the same?

Comment on lines 286 to 287
CELERY_BROKER_USE_SSL = env.bool("CELERY_BROKER_USE_SSL", default=False)
CELERY_REDIS_BACKEND_USE_SSL = env.bool("CELERY_REDIS_BACKEND_USE_SSL", default=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can simplify things?

Suggested change
CELERY_BROKER_USE_SSL = env.bool("CELERY_BROKER_USE_SSL", default=False)
CELERY_REDIS_BACKEND_USE_SSL = env.bool("CELERY_REDIS_BACKEND_USE_SSL", default=False)
CELERY_BROKER_USE_SSL = env.bool("REDIS_SSL", default=False)
CELERY_REDIS_BACKEND_USE_SSL = env.bool("REDIS_SSL", default=False)

docs/3-deployment/deployment-on-heroku.rst Outdated Show resolved Hide resolved
@qwerrrqw
Copy link
Contributor Author

I've simplified the Redis TLS configuration as advised. It's much better!

@dqunbp
Copy link

dqunbp commented Nov 14, 2024

These changes will not work, please check #5500

@browniebroke
Copy link
Member

These changes will not work, please check #5500

Could you be a bit more specific as to why? Do you have any suggestions on how to improve the patch to make it work?

@qwerrrqw qwerrrqw force-pushed the fix/redis-TLS-support branch from 6b57535 to d93e629 Compare November 15, 2024 13:03
@qwerrrqw qwerrrqw force-pushed the fix/redis-TLS-support branch from d93e629 to 1f785bd Compare November 15, 2024 13:10
- Move Redis SSL configuration to base settings
- Remove redundant Redis SSL settings from production
- Remove REDIS_SSL env var from Heroku deployment docs
- Use rediss:// URL scheme detection for SSL
The ssl module is only used in Celery SSL settings, so it is not actually used when the use_celery option is 'n'. I modified it to import conditionally as follows.
Change import ssl settings to conditional
@browniebroke browniebroke changed the title Add Redis TLS support for Heroku Add support for secure Redis (TLS support) Nov 21, 2024
@browniebroke browniebroke merged commit 7fc33c2 into cookiecutter:master Nov 21, 2024
13 checks passed
@browniebroke
Copy link
Member

Thanks for the contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Heroku - Default Redis connection uses TSLS
3 participants