security.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. .. _guide-security:
  2. ==========
  3. Security
  4. ==========
  5. .. contents::
  6. :local:
  7. Introduction
  8. ============
  9. While Celery is written with security in mind, it should be treated as an
  10. unsafe component.
  11. Depending on your `Security Policy`_, there are
  12. various steps you can take to make your Celery installation more secure.
  13. .. _`Security Policy`: http://en.wikipedia.org/wiki/Security_policy
  14. Areas of Concern
  15. ================
  16. Broker
  17. ------
  18. It is imperative that the broker is guarded from unwanted access, especially
  19. if it is publically accesible.
  20. By default, workers trust that the data they get from the broker has not
  21. been tampered with. See `Message Signing`_ for information on how to make
  22. the broker connection more trusthworthy.
  23. The first line of defence should be to put a firewall in front of the broker,
  24. allowing only white-listed machines to access it.
  25. Keep in mind that both firewall misconfiguration, and temproraily disabling
  26. the firewall, is common in the real world. Solid security policy includes
  27. monitoring of firewall equipment to detect if they have been disabled, be it
  28. accidentally or on purpose.
  29. In other words, one should not blindly trust the firewall either.
  30. If your broker supports fine-grained access control, like RabbitMQ,
  31. this is something you should look at enabling. See for example
  32. http://www.rabbitmq.com/access-control.html.
  33. Client
  34. ------
  35. In Celery, "client" refers to anything that sends messages to the
  36. broker, e.g. web-servers that apply tasks.
  37. Having the broker properly secured doesn't matter if arbitrary messages
  38. can be sent through a client.
  39. *[Need more text here]*
  40. Worker
  41. ------
  42. The default permissions of tasks running inside a worker are the same ones as
  43. the privileges of the worker itself. This applies to resources such as
  44. memory, file-systems and devices.
  45. An exception to this rule is when using the multiprocessing based task pool,
  46. which is currently the default. In this case, the task will have access to
  47. any memory copied as a result of the :func:`fork` call (does not apply
  48. under MS Windows), and access to memory contents written
  49. by parent tasks in the same worker child process.
  50. Limiting access to memory contents can be done by launching every task
  51. in a subprocess (:func:`fork` + :func:`execve`).
  52. Limiting file-system and device access can be accomplished by using
  53. `chroot`_, `jail`_, `sandboxing`_, virtual machines or other
  54. mechanisms as enabled by the platform or additional software.
  55. Note also that any task executed in the worker will have the
  56. same network access as the machine on which it's running. If the worker
  57. is located on an internal network it's recommended to add firewall rules for
  58. outbound traffic.
  59. .. _`chroot`: http://en.wikipedia.org/wiki/Chroot
  60. .. _`jail`: http://en.wikipedia.org/wiki/FreeBSD_jail
  61. .. _`sandboxing`:
  62. http://en.wikipedia.org/wiki/Sandbox_(computer_security)
  63. Serializers
  64. ===========
  65. Celery uses `pickle` as default serializer. `pickle`_ is an insecure
  66. serialization method and should be avoided in cases when clients are
  67. untrusted or unauthenticated.
  68. Celery has a special `auth` serializer which is intended for authenticating
  69. the communication between Celery clients and workers. The `auth` serializer
  70. uses public-key cryptography to check the authenticity of senders. See
  71. `Message Signing`_ for information on how to enable the `auth` serializer.
  72. .. _`pickle`: http://docs.python.org/library/pickle.html
  73. .. _message-signing:
  74. Message Signing
  75. ===============
  76. Celery uses public-key cryptography to sign messages. Messages exchanged
  77. between clients and workers are signed with private key and
  78. verified with public certificate.
  79. Celery uses `X.509`_ certificates and `pyOpenSSL`_ library for message signing.
  80. Normally, the certificates should be signed by a Certificate Authority,
  81. but they can be self-signed or signed by an untrusted third party.
  82. The message signing is implemented in the `auth` serializer.
  83. The `auth` serializer can be enabled with :setting:`CELERY_TASK_SERIALIZER`
  84. configuration option. The `auth` serializer requires
  85. :setting:`CELERY_SECURITY_KEY`, :setting:`CELERY_SECURITY_CERTIFICATE` and
  86. :setting:`CELERY_SECURITY_CERT_STORE` configuration options to be provided.
  87. They are used for locating private-keys and certificates.
  88. After providing :setting:`CELERY_SECURITY_*` options it is necessary to call
  89. :meth:`celery.security.setup_security` method. :meth:`celery.security.setup_security`
  90. disables all insecure serializers.
  91. .. code-block:: python
  92. # sample Celery auth configuration
  93. CELERY_SECURITY_KEY = "/etc/ssl/private/worker.key"
  94. CELERY_SECURITY_CERTIFICATE = "/etc/ssl/certs/worker.pem"
  95. CELERY_SECURITY_CERT_STORE = "/etc/ssl/certs/*.pem"
  96. from celery.security import setup_security
  97. setup_security()
  98. .. note::
  99. The `auth` serializer doesn't encrypt the content of a message
  100. .. setting:: CELERY_TASK_ERROR_WHITELIST
  101. .. _`pyOpenSSL`: http://pypi.python.org/pypi/pyOpenSSL
  102. .. _`X.509`: http://en.wikipedia.org/wiki/X.509
  103. Intrusion Detection
  104. ===================
  105. The most important part when defending your systems against
  106. intruders is being able to detect if the system has been compromised.
  107. Logs
  108. ----
  109. Logs are usually the first place to look for evidence
  110. of security breaches, but they are useless if they can be tampered with.
  111. A good solution is to set up centralized logging with a dedicated logging
  112. server. Acess to it should be restricted.
  113. In addition to having all of the logs in a single place, if configured
  114. correctly, it can make it harder for intruders to tamper with your logs.
  115. This should be fairly easy to setup using syslog (see also `syslog-ng`_ and
  116. `rsyslog`_.). Celery uses the :mod:`logging` library, and already has
  117. support for using syslog.
  118. A tip for the paranoid is to send logs using UDP and cut the
  119. transmit part of the logging servers network cable :-)
  120. .. _`syslog-ng`: http://en.wikipedia.org/wiki/Syslog-ng
  121. .. _`rsyslog`: http://www.rsyslog.com/
  122. Tripwire
  123. --------
  124. `Tripwire`_ is a (now commercial) data integrity tool, with several
  125. open source implementations, used to keep
  126. cryptographic hashes of files in the file-system, so that administrators
  127. can be alerted when they change. This way when the damage is done and your
  128. system has been compromised you can tell exactly what files intruders
  129. have changed (password files, logs, backdoors, rootkits and so on).
  130. Often this is the only way you will be able to detect an intrusion.
  131. Some open source implementations include:
  132. * `OSSEC`_
  133. * `Samhain`_
  134. * `Open Source Tripwire`_
  135. * `AIDE`_
  136. Also, the `ZFS`_ file-system comes with built-in integrity checks
  137. that can be used.
  138. .. _`Tripwire`: http://tripwire.com/
  139. .. _`OSSEC`: http://www.ossec.net/
  140. .. _`Samhain`: http://la-samhna.de/samhain/index.html
  141. .. _`AIDE`: http://aide.sourceforge.net/
  142. .. _`Open Source Tripwire`: http://sourceforge.net/projects/tripwire/
  143. .. _`ZFS`: http://en.wikipedia.org/wiki/ZFS