| 1 |
"""Keys and certificates for tests (KEY1 is a private key of CERT1, etc.) |
| 2 |
|
| 3 |
Generated with: |
| 4 |
|
| 5 |
.. code-block:: console |
| 6 |
|
| 7 |
$ openssl genrsa -des3 -passout pass:test -out key1.key 1024 |
| 8 |
$ openssl req -new -key key1.key -out key1.csr -passin pass:test |
| 9 |
$ cp key1.key key1.key.org |
| 10 |
$ openssl rsa -in key1.key.org -out key1.key -passin pass:test |
| 11 |
$ openssl x509 -req -days 365 -in cert1.csr \ |
| 12 |
-signkey key1.key -out cert1.crt |
| 13 |
$ rm key1.key.org cert1.csr |
| 14 |
""" |
| 15 |
|
| 16 |
import builtins |
| 17 |
import os |
| 18 |
import tempfile |
| 19 |
from unittest.mock import Mock, patch |
| •20 |
import sys |
| 21 |
|
| 22 |
import pytest |
| 23 |
from kombu.exceptions import SerializerNotInstalled |
| 24 |
from kombu.serialization import disable_insecure_serializers, registry |
| 25 |
|
| 26 |
from celery.exceptions import ImproperlyConfigured, SecurityError |
| 27 |
from celery.security import disable_untrusted_serializers, setup_security |
| 28 |
from celery.security.utils import reraise_errors |
| 29 |
from t.unit import conftest |
| 30 |
|
| 31 |
from . import CERT1, ENCKEY1, KEY1, KEYPASSWORD |
| 32 |
from .case import SecurityCase |
| 33 |
|
| 34 |
|
| 35 |
class test_security(SecurityCase): |
| 36 |
|
| 37 |
def teardown_method(self): |
| 38 |
registry._disabled_content_types.clear() |
| 39 |
registry._set_default_serializer('json') |
| 40 |
try: |
| 41 |
registry.unregister('auth') |
| 42 |
except SerializerNotInstalled: |
| 43 |
pass |
| 44 |
|
| 45 |
def test_disable_insecure_serializers(self): |
| 46 |
try: |
| 47 |
disabled = registry._disabled_content_types |
| 48 |
assert disabled |
| 49 |
|
| 50 |
disable_insecure_serializers( |
| 51 |
['application/json', 'application/x-python-serialize'], |
| 52 |
) |
| 53 |
assert 'application/x-yaml' in disabled |
| 54 |
assert 'application/json' not in disabled |
| 55 |
assert 'application/x-python-serialize' not in disabled |
| 56 |
disabled.clear() |
| 57 |
|
| 58 |
disable_insecure_serializers(allowed=None) |
| 59 |
assert 'application/x-yaml' in disabled |
| 60 |
assert 'application/json' in disabled |
| 61 |
assert 'application/x-python-serialize' in disabled |
| 62 |
finally: |
| 63 |
disable_insecure_serializers(allowed=['json']) |
| 64 |
|
| 65 |
@patch('celery.security._disable_insecure_serializers') |
| 66 |
def test_disable_untrusted_serializers(self, disable): |
| 67 |
disable_untrusted_serializers(['foo']) |
| 68 |
disable.assert_called_with(allowed=['foo']) |
| 69 |
|
| 70 |
def test_setup_security(self): |
| 71 |
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_key1: |
| 72 |
tmp_key1.write(KEY1) |
| 73 |
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_cert1: |
| 74 |
tmp_cert1.write(CERT1) |
| 75 |
|
| 76 |
self.app.conf.update( |
| 77 |
task_serializer='auth', |
| 78 |
accept_content=['auth'], |
| 79 |
security_key=tmp_key1.name, |
| 80 |
security_certificate=tmp_cert1.name, |
| 81 |
security_cert_store='*.pem', |
| 82 |
) |
| 83 |
self.app.setup_security() |
| 84 |
|
| 85 |
os.remove(tmp_key1.name) |
| 86 |
os.remove(tmp_cert1.name) |
| 87 |
|
| 88 |
def test_setup_security_encrypted_key_file(self): |
| 89 |
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_key1: |
| 90 |
tmp_key1.write(ENCKEY1) |
| 91 |
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_cert1: |
| 92 |
tmp_cert1.write(CERT1) |
| 93 |
|
| 94 |
self.app.conf.update( |
| 95 |
task_serializer='auth', |
| 96 |
accept_content=['auth'], |
| 97 |
security_key=tmp_key1.name, |
| 98 |
security_key_password=KEYPASSWORD, |
| 99 |
security_certificate=tmp_cert1.name, |
| 100 |
security_cert_store='*.pem', |
| 101 |
) |
| 102 |
self.app.setup_security() |
| 103 |
|
| 104 |
os.remove(tmp_key1.name) |
| 105 |
os.remove(tmp_cert1.name) |
| 106 |
|
| 107 |
def test_setup_security_disabled_serializers(self): |
| 108 |
disabled = registry._disabled_content_types |
| 109 |
assert len(disabled) == 0 |
| 110 |
|
| 111 |
self.app.conf.task_serializer = 'json' |
| 112 |
with pytest.raises(ImproperlyConfigured): |
| 113 |
self.app.setup_security() |
| 114 |
assert 'application/x-python-serialize' in disabled |
| 115 |
disabled.clear() |
| 116 |
|
| 117 |
self.app.conf.task_serializer = 'auth' |
| 118 |
with pytest.raises(ImproperlyConfigured): |
| 119 |
self.app.setup_security() |
| 120 |
assert 'application/json' in disabled |
| 121 |
disabled.clear() |
| 122 |
|
| 123 |
@patch('celery.current_app') |
| •124 |
@pytest.mark.skipif(sys.version_info > (3, 8), reason='breaks with python 3.8') |
| 125 |
def test_setup_security__default_app(self, current_app): |
| 126 |
with pytest.raises(ImproperlyConfigured): |
| 127 |
setup_security() |
| 128 |
|
| 129 |
@patch('celery.security.register_auth') |
| 130 |
@patch('celery.security._disable_insecure_serializers') |
| 131 |
def test_setup_registry_complete(self, dis, reg, key='KEY', cert='CERT'): |
| 132 |
calls = [0] |
| 133 |
|
| 134 |
def effect(*args): |
| 135 |
try: |
| 136 |
m = Mock() |
| 137 |
m.read.return_value = 'B' if calls[0] else 'A' |
| 138 |
return m |
| 139 |
finally: |
| 140 |
calls[0] += 1 |
| 141 |
|
| 142 |
self.app.conf.task_serializer = 'auth' |
| 143 |
self.app.conf.accept_content = ['auth'] |
| 144 |
with conftest.open(side_effect=effect): |
| 145 |
with patch('celery.security.registry') as registry: |
| 146 |
store = Mock() |
| 147 |
self.app.setup_security(['json'], key, None, cert, store) |
| 148 |
dis.assert_called_with(['json']) |
| 149 |
reg.assert_called_with('A', None, 'B', store, 'sha256', 'json') |
| 150 |
registry._set_default_serializer.assert_called_with('auth') |
| 151 |
|
| 152 |
def test_security_conf(self): |
| 153 |
self.app.conf.task_serializer = 'auth' |
| 154 |
with pytest.raises(ImproperlyConfigured): |
| 155 |
self.app.setup_security() |
| 156 |
|
| 157 |
self.app.conf.accept_content = ['auth'] |
| 158 |
with pytest.raises(ImproperlyConfigured): |
| 159 |
self.app.setup_security() |
| 160 |
|
| 161 |
_import = builtins.__import__ |
| 162 |
|
| 163 |
def import_hook(name, *args, **kwargs): |
| 164 |
if name == 'cryptography': |
| 165 |
raise ImportError |
| 166 |
return _import(name, *args, **kwargs) |
| 167 |
|
| 168 |
builtins.__import__ = import_hook |
| 169 |
with pytest.raises(ImproperlyConfigured): |
| 170 |
self.app.setup_security() |
| 171 |
builtins.__import__ = _import |
| 172 |
|
| 173 |
def test_reraise_errors(self): |
| 174 |
with pytest.raises(SecurityError): |
| 175 |
with reraise_errors(errors=(KeyError,)): |
| 176 |
raise KeyError('foo') |
| 177 |
with pytest.raises(KeyError): |
| 178 |
with reraise_errors(errors=(ValueError,)): |
| 179 |
raise KeyError('bar') |