__init__.py 1.7 KB

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