Pārlūkot izejas kodu

Adds Celery.setup_security

Ask Solem 11 gadi atpakaļ
vecāks
revīzija
2cc33535c5
3 mainītis faili ar 37 papildinājumiem un 26 dzēšanām
  1. 6 0
      celery/app/base.py
  2. 6 25
      celery/security/__init__.py
  3. 25 1
      docs/reference/celery.rst

+ 6 - 0
celery/app/base.py

@@ -261,6 +261,12 @@ class Celery(object):
     def config_from_cmdline(self, argv, namespace='celery'):
     def config_from_cmdline(self, argv, namespace='celery'):
         self.conf.update(self.loader.cmdline_config_parser(argv, namespace))
         self.conf.update(self.loader.cmdline_config_parser(argv, namespace))
 
 
+    def setup_security(self, allowed_serializers=None, key=None, cert=None,
+            store=None, digest='sha1', serializer='json'):
+        from celery.security import setup_security
+        return setup_security(allowed_serializers, key, cert,
+                              store, digest, serializer, app=self)
+
     def autodiscover_tasks(self, packages, related_name='tasks'):
     def autodiscover_tasks(self, packages, related_name='tasks'):
         if self.conf.CELERY_FORCE_BILLIARD_LOGGING:
         if self.conf.CELERY_FORCE_BILLIARD_LOGGING:
             # we'll use billiard's processName instead of
             # we'll use billiard's processName instead of

+ 6 - 25
celery/security/__init__.py

@@ -10,7 +10,6 @@ from __future__ import absolute_import
 
 
 from kombu.serialization import registry
 from kombu.serialization import registry
 
 
-from celery import current_app
 from celery.exceptions import ImproperlyConfigured
 from celery.exceptions import ImproperlyConfigured
 
 
 from .serialization import register_auth
 from .serialization import register_auth
@@ -39,33 +38,15 @@ def disable_untrusted_serializers(whitelist=None):
 
 
 
 
 def setup_security(allowed_serializers=None, key=None, cert=None, store=None,
 def setup_security(allowed_serializers=None, key=None, cert=None, store=None,
-                   digest='sha1', serializer='json'):
-    """Setup the message-signing serializer.
-
-    Disables untrusted serializers and if configured to use the ``auth``
-    serializer will register the auth serializer with the provided settings
-    into the Kombu serializer registry.
-
-    :keyword allowed_serializers:  List of serializer names, or content_types
-        that should be exempt from being disabled.
-    :keyword key: Name of private key file to use.
-        Defaults to the :setting:`CELERY_SECURITY_KEY` setting.
-    :keyword cert: Name of certificate file to use.
-        Defaults to the :setting:`CELERY_SECURITY_CERTIFICATE` setting.
-    :keyword store: Directory containing certificates.
-        Defaults to the :setting:`CELERY_SECURITY_CERT_STORE` setting.
-    :keyword digest: Digest algorithm used when signing messages.
-        Default is ``sha1``.
-    :keyword serializer: Serializer used to encode messages after
-        they have been signed.  See :setting:`CELERY_TASK_SERIALIZER` for
-        the serializers supported.
-        Default is ``json``.
-
-    """
+                   digest='sha1', serializer='json', app=None):
+    """See :meth:`@Celery.setup_security`."""
+    if app is None:
+        from celery import current_app
+        app = current_app._get_current_object()
 
 
     disable_untrusted_serializers(allowed_serializers)
     disable_untrusted_serializers(allowed_serializers)
 
 
-    conf = current_app.conf
+    conf = app.conf
     if conf.CELERY_TASK_SERIALIZER != 'auth':
     if conf.CELERY_TASK_SERIALIZER != 'auth':
         return
         return
 
 

+ 25 - 1
docs/reference/celery.rst

@@ -188,6 +188,30 @@ and creating Celery applications.
         it's important that the same configuration happens at import time
         it's important that the same configuration happens at import time
         when pickle restores the object on the other side.
         when pickle restores the object on the other side.
 
 
+    .. method:: Celery.setup_security(...)
+
+        Setup the message-signing serializer.
+        This will affect all application instances (a global operation).
+
+        Disables untrusted serializers and if configured to use the ``auth``
+        serializer will register the auth serializer with the provided settings
+        into the Kombu serializer registry.
+
+        :keyword allowed_serializers:  List of serializer names, or content_types
+            that should be exempt from being disabled.
+        :keyword key: Name of private key file to use.
+            Defaults to the :setting:`CELERY_SECURITY_KEY` setting.
+        :keyword cert: Name of certificate file to use.
+            Defaults to the :setting:`CELERY_SECURITY_CERTIFICATE` setting.
+        :keyword store: Directory containing certificates.
+            Defaults to the :setting:`CELERY_SECURITY_CERT_STORE` setting.
+        :keyword digest: Digest algorithm used when signing messages.
+            Default is ``sha1``.
+        :keyword serializer: Serializer used to encode messages after
+            they have been signed.  See :setting:`CELERY_TASK_SERIALIZER` for
+            the serializers supported.
+            Default is ``json``.
+
     .. method:: Celery.start(argv=None)
     .. method:: Celery.start(argv=None)
 
 
         Run :program:`celery` using `argv`.
         Run :program:`celery` using `argv`.
@@ -248,7 +272,7 @@ and creating Celery applications.
 
 
         Run :program:`celery worker` using `argv`.
         Run :program:`celery worker` using `argv`.
 
 
-        Uses :data:`sys.argv` if `argv` is not specified."""
+        Uses :data:`sys.argv` if `argv` is not specified.
 
 
     .. attribute:: Celery.Worker
     .. attribute:: Celery.Worker