__init__.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.security
  4. ~~~~~~~~~~~~~~~
  5. Module implementing the signing message serializer.
  6. """
  7. from __future__ import absolute_import
  8. from __future__ import with_statement
  9. from kombu.serialization import registry
  10. from celery import current_app
  11. from celery.exceptions import ImproperlyConfigured
  12. from .serialization import register_auth
  13. SSL_NOT_INSTALLED = """\
  14. You need to install the pyOpenSSL library to use the auth serializer.
  15. Please install by:
  16. $ pip install pyOpenSSL
  17. """
  18. SETTING_MISSING = """\
  19. Sorry, but you have to configure the
  20. * CELERY_SECURITY_KEY
  21. * CELERY_SECURITY_CERTIFICATE, and the
  22. * CELERY_SECURITY_CERT_STORE
  23. configuration settings to use the auth serializer.
  24. Please see the configuration reference for more information.
  25. """
  26. def disable_untrusted_serializers(whitelist=None):
  27. for name in set(registry._decoders) - set(whitelist or []):
  28. registry.disable(name)
  29. def setup_security(allowed_serializers=None, key=None, cert=None, store=None,
  30. digest='sha1', serializer='json'):
  31. """Setup the message-signing serializer.
  32. Disables untrusted serializers and if configured to use the ``auth``
  33. serializer will register the auth serializer with the provided settings
  34. into the Kombu serializer registry.
  35. :keyword allowed_serializers: List of serializer names, or content_types
  36. that should be exempt from being disabled.
  37. :keyword key: Name of private key file to use.
  38. Defaults to the :setting:`CELERY_SECURITY_KEY` setting.
  39. :keyword cert: Name of certificate file to use.
  40. Defaults to the :setting:`CELERY_SECURITY_CERTIFICATE` setting.
  41. :keyword store: Directory containing certificates.
  42. Defaults to the :setting:`CELERY_SECURITY_CERT_STORE` setting.
  43. :keyword digest: Digest algorithm used when signing messages.
  44. Default is ``sha1``.
  45. :keyword serializer: Serializer used to encode messages after
  46. they have been signed. See :setting:`CELERY_TASK_SERIALIZER` for
  47. the serializers supported.
  48. Default is ``json``.
  49. """
  50. disable_untrusted_serializers(allowed_serializers)
  51. conf = current_app.conf
  52. if conf.CELERY_TASK_SERIALIZER != 'auth':
  53. return
  54. try:
  55. from OpenSSL import crypto # noqa
  56. except ImportError:
  57. raise ImproperlyConfigured(SSL_NOT_INSTALLED)
  58. key = key or conf.CELERY_SECURITY_KEY
  59. cert = cert or conf.CELERY_SECURITY_CERTIFICATE
  60. store = store or conf.CELERY_SECURITY_CERT_STORE
  61. if not (key and cert and store):
  62. raise ImproperlyConfigured(SETTING_MISSING)
  63. with open(key) as kf:
  64. with open(cert) as cf:
  65. register_auth(kf.read(), cf.read(), store)