| 1 |
.. _guide-security: |
| 2 |
|
| 3 |
========== |
| 4 |
Security |
| 5 |
========== |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
Introduction |
| 11 |
============ |
| 12 |
|
| 13 |
While Celery is written with security in mind, it should be treated as an |
| 14 |
unsafe component. |
| 15 |
|
| 16 |
Depending on your `Security Policy`_, there are |
| 17 |
various steps you can take to make your Celery installation more secure. |
| 18 |
|
| 19 |
|
| 20 |
.. _`Security Policy`: https://en.wikipedia.org/wiki/Security_policy |
| 21 |
|
| 22 |
|
| 23 |
Areas of Concern |
| 24 |
================ |
| 25 |
|
| 26 |
Broker |
| 27 |
------ |
| 28 |
|
| 29 |
It's imperative that the broker is guarded from unwanted access, especially |
| 30 |
if accessible to the public. |
| 31 |
By default, workers trust that the data they get from the broker hasn't |
| 32 |
been tampered with. See `Message Signing`_ for information on how to make |
| 33 |
the broker connection more trustworthy. |
| 34 |
|
| 35 |
The first line of defense should be to put a firewall in front of the broker, |
| 36 |
allowing only white-listed machines to access it. |
| 37 |
|
| 38 |
Keep in mind that both firewall misconfiguration, and temporarily disabling |
| 39 |
the firewall, is common in the real world. Solid security policy includes |
| 40 |
monitoring of firewall equipment to detect if they've been disabled, be it |
| 41 |
accidentally or on purpose. |
| 42 |
|
| 43 |
In other words, one shouldn't blindly trust the firewall either. |
| 44 |
|
| 45 |
If your broker supports fine-grained access control, like RabbitMQ, |
| 46 |
this is something you should look at enabling. See for example |
| 47 |
http://www.rabbitmq.com/access-control.html. |
| 48 |
|
| 49 |
If supported by your broker backend, you can enable end-to-end SSL encryption |
| 50 |
and authentication using :setting:`broker_use_ssl`. |
| 51 |
|
| 52 |
Client |
| 53 |
------ |
| 54 |
|
| 55 |
In Celery, "client" refers to anything that sends messages to the |
| 56 |
broker, for example web-servers that apply tasks. |
| 57 |
|
| 58 |
Having the broker properly secured doesn't matter if arbitrary messages |
| 59 |
can be sent through a client. |
| 60 |
|
| 61 |
*[Need more text here]* |
| 62 |
|
| 63 |
Worker |
| 64 |
------ |
| 65 |
|
| 66 |
The default permissions of tasks running inside a worker are the same ones as |
| 67 |
the privileges of the worker itself. This applies to resources, such as; |
| 68 |
memory, file-systems, and devices. |
| 69 |
|
| 70 |
An exception to this rule is when using the multiprocessing based task pool, |
| 71 |
which is currently the default. In this case, the task will have access to |
| 72 |
any memory copied as a result of the :func:`fork` call, |
| 73 |
and access to memory contents written by parent tasks in the same worker |
| 74 |
child process. |
| 75 |
|
| 76 |
Limiting access to memory contents can be done by launching every task |
| 77 |
in a subprocess (:func:`fork` + :func:`execve`). |
| 78 |
|
| 79 |
Limiting file-system and device access can be accomplished by using |
| 80 |
`chroot`_, `jail`_, `sandboxing`_, virtual machines, or other |
| 81 |
mechanisms as enabled by the platform or additional software. |
| 82 |
|
| 83 |
Note also that any task executed in the worker will have the |
| 84 |
same network access as the machine on which it's running. If the worker |
| 85 |
is located on an internal network it's recommended to add firewall rules for |
| 86 |
outbound traffic. |
| 87 |
|
| 88 |
.. _`chroot`: https://en.wikipedia.org/wiki/Chroot |
| 89 |
.. _`jail`: https://en.wikipedia.org/wiki/FreeBSD_jail |
| 90 |
.. _`sandboxing`: |
| 91 |
https://en.wikipedia.org/wiki/Sandbox_(computer_security) |
| 92 |
|
| 93 |
.. _security-serializers: |
| 94 |
|
| 95 |
Serializers |
| 96 |
=========== |
| 97 |
|
| 98 |
The default serializer is JSON since version 4.0, but since it has |
| 99 |
only support for a restricted set of types you may want to consider |
| 100 |
using pickle for serialization instead. |
| 101 |
|
| 102 |
The `pickle` serializer is convenient as it can serialize |
| 103 |
almost any Python object, even functions with some work, |
| 104 |
but for the same reasons `pickle` is inherently insecure [*]_, |
| 105 |
and should be avoided whenever clients are untrusted or |
| 106 |
unauthenticated. |
| 107 |
|
| 108 |
You can disable untrusted content by specifying |
| 109 |
a white-list of accepted content-types in the :setting:`accept_content` |
| 110 |
setting: |
| 111 |
|
| 112 |
.. versionadded:: 3.0.18 |
| 113 |
|
| 114 |
.. note:: |
| 115 |
|
| 116 |
This setting was first supported in version 3.0.18. If you're |
| 117 |
running an earlier version it will simply be ignored, so make |
| 118 |
sure you're running a version that supports it. |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
accept_content = ['json'] |
| 123 |
|
| 124 |
|
| 125 |
This accepts a list of serializer names and content-types, so you could |
| 126 |
also specify the content type for json: |
| 127 |
|
| 128 |
.. code-block:: python |
| 129 |
|
| 130 |
accept_content = ['application/json'] |
| 131 |
|
| 132 |
Celery also comes with a special `auth` serializer that validates |
| 133 |
communication between Celery clients and workers, making sure |
| 134 |
that messages originates from trusted sources. |
| 135 |
Using `Public-key cryptography` the `auth` serializer can verify the |
| 136 |
authenticity of senders, to enable this read :ref:`message-signing` |
| 137 |
for more information. |
| 138 |
|
| 139 |
.. _`Public-key cryptography`: |
| 140 |
https://en.wikipedia.org/wiki/Public-key_cryptography |
| 141 |
|
| 142 |
.. _message-signing: |
| 143 |
|
| 144 |
Message Signing |
| 145 |
=============== |
| 146 |
|
| 147 |
Celery can use the :pypi:`cryptography` library to sign message using |
| 148 |
`Public-key cryptography`, where |
| 149 |
messages sent by clients are signed using a private key |
| 150 |
and then later verified by the worker using a public certificate. |
| 151 |
|
| 152 |
Optimally certificates should be signed by an official |
| 153 |
`Certificate Authority`_, but they can also be self-signed. |
| 154 |
|
| 155 |
To enable this you should configure the :setting:`task_serializer` |
| 156 |
setting to use the `auth` serializer. Enforcing the workers to only accept |
| 157 |
signed messages, you should set `accept_content` to `['auth']`. |
| 158 |
For additional signing of the event protocol, set `event_serializer` to `auth`. |
| 159 |
Also required is configuring the |
| 160 |
paths used to locate private keys and certificates on the file-system: |
| 161 |
the :setting:`security_key`, |
| 162 |
:setting:`security_certificate`, and :setting:`security_cert_store` |
| 163 |
settings respectively. |
| 164 |
You can tweak the signing algorithm with :setting:`security_digest`. |
| 165 |
If using an encrypted private key, the password can be configured with |
| 166 |
:setting:`security_key_password`. |
| 167 |
|
| 168 |
With these configured it's also necessary to call the |
| 169 |
:func:`celery.setup_security` function. Note that this will also |
| 170 |
disable all insecure serializers so that the worker won't accept |
| 171 |
messages with untrusted content types. |
| 172 |
|
| 173 |
This is an example configuration using the `auth` serializer, |
| 174 |
with the private key and certificate files located in `/etc/ssl`. |
| 175 |
|
| 176 |
.. code-block:: python |
| 177 |
|
| 178 |
app = Celery() |
| 179 |
app.conf.update( |
| 180 |
security_key='/etc/ssl/private/worker.key' |
| 181 |
security_certificate='/etc/ssl/certs/worker.pem' |
| 182 |
security_cert_store='/etc/ssl/certs/*.pem', |
| 183 |
security_digest='sha256', |
| 184 |
task_serializer='auth', |
| 185 |
event_serializer='auth', |
| 186 |
accept_content=['auth'] |
| 187 |
) |
| 188 |
app.setup_security() |
| 189 |
|
| 190 |
.. note:: |
| 191 |
|
| 192 |
While relative paths aren't disallowed, using absolute paths |
| 193 |
is recommended for these files. |
| 194 |
|
| 195 |
Also note that the `auth` serializer won't encrypt the contents of |
| 196 |
a message, so if needed this will have to be enabled separately. |
| 197 |
|
| 198 |
.. _`X.509`: https://en.wikipedia.org/wiki/X.509 |
| 199 |
.. _`Certificate Authority`: |
| 200 |
https://en.wikipedia.org/wiki/Certificate_authority |
| 201 |
|
| 202 |
Intrusion Detection |
| 203 |
=================== |
| 204 |
|
| 205 |
The most important part when defending your systems against |
| 206 |
intruders is being able to detect if the system has been compromised. |
| 207 |
|
| 208 |
Logs |
| 209 |
---- |
| 210 |
|
| 211 |
Logs are usually the first place to look for evidence |
| 212 |
of security breaches, but they're useless if they can be tampered with. |
| 213 |
|
| 214 |
A good solution is to set up centralized logging with a dedicated logging |
| 215 |
server. Access to it should be restricted. |
| 216 |
In addition to having all of the logs in a single place, if configured |
| 217 |
correctly, it can make it harder for intruders to tamper with your logs. |
| 218 |
|
| 219 |
This should be fairly easy to setup using syslog (see also `syslog-ng`_ and |
| 220 |
`rsyslog`_). Celery uses the :mod:`logging` library, and already has |
| 221 |
support for using syslog. |
| 222 |
|
| 223 |
A tip for the paranoid is to send logs using UDP and cut the |
| 224 |
transmit part of the logging server's network cable :-) |
| 225 |
|
| 226 |
.. _`syslog-ng`: https://en.wikipedia.org/wiki/Syslog-ng |
| 227 |
.. _`rsyslog`: http://www.rsyslog.com/ |
| 228 |
|
| 229 |
Tripwire |
| 230 |
-------- |
| 231 |
|
| 232 |
`Tripwire`_ is a (now commercial) data integrity tool, with several |
| 233 |
open source implementations, used to keep |
| 234 |
cryptographic hashes of files in the file-system, so that administrators |
| 235 |
can be alerted when they change. This way when the damage is done and your |
| 236 |
system has been compromised you can tell exactly what files intruders |
| 237 |
have changed (password files, logs, back-doors, root-kits, and so on). |
| 238 |
Often this is the only way you'll be able to detect an intrusion. |
| 239 |
|
| 240 |
Some open source implementations include: |
| 241 |
|
| 242 |
* `OSSEC`_ |
| 243 |
* `Samhain`_ |
| 244 |
* `Open Source Tripwire`_ |
| 245 |
* `AIDE`_ |
| 246 |
|
| 247 |
Also, the `ZFS`_ file-system comes with built-in integrity checks |
| 248 |
that can be used. |
| 249 |
|
| 250 |
.. _`Tripwire`: http://tripwire.com/ |
| 251 |
.. _`OSSEC`: http://www.ossec.net/ |
| 252 |
.. _`Samhain`: http://la-samhna.de/samhain/index.html |
| 253 |
.. _`AIDE`: http://aide.sourceforge.net/ |
| 254 |
.. _`Open Source Tripwire`: https://github.com/Tripwire/tripwire-open-source |
| 255 |
.. _`ZFS`: https://en.wikipedia.org/wiki/ZFS |
| 256 |
|
| 257 |
.. rubric:: Footnotes |
| 258 |
|
| 259 |
.. [*] https://blog.nelhage.com/2011/03/exploiting-pickle/ |