__init__.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. """Message Signing Serializer."""
  3. from __future__ import absolute_import, unicode_literals
  4. from kombu.serialization import (
  5. registry, disable_insecure_serializers as _disable_insecure_serializers,
  6. )
  7. from celery.exceptions import ImproperlyConfigured
  8. from .serialization import register_auth
  9. SSL_NOT_INSTALLED = """\
  10. You need to install the pyOpenSSL library to use the auth serializer.
  11. Please install by:
  12. $ pip install pyOpenSSL
  13. """
  14. SETTING_MISSING = """\
  15. Sorry, but you have to configure the
  16. * security_key
  17. * security_certificate, and the
  18. * security_cert_storE
  19. configuration settings to use the auth serializer.
  20. Please see the configuration reference for more information.
  21. """
  22. __all__ = ['setup_security']
  23. def setup_security(allowed_serializers=None, key=None, cert=None, store=None,
  24. digest='sha1', serializer='json', app=None):
  25. """See :meth:`@Celery.setup_security`."""
  26. if app is None:
  27. from celery import current_app
  28. app = current_app._get_current_object()
  29. _disable_insecure_serializers(allowed_serializers)
  30. conf = app.conf
  31. if conf.task_serializer != 'auth':
  32. return
  33. try:
  34. from OpenSSL import crypto # noqa
  35. except ImportError:
  36. raise ImproperlyConfigured(SSL_NOT_INSTALLED)
  37. key = key or conf.security_key
  38. cert = cert or conf.security_certificate
  39. store = store or conf.security_cert_store
  40. if not (key and cert and store):
  41. raise ImproperlyConfigured(SETTING_MISSING)
  42. with open(key) as kf:
  43. with open(cert) as cf:
  44. register_auth(kf.read(), cf.read(), store, digest, serializer)
  45. registry._set_default_serializer('auth')
  46. def disable_untrusted_serializers(whitelist=None):
  47. _disable_insecure_serializers(allowed=whitelist)