| 1 |
import datetime |
| 2 |
import os |
| 3 |
from unittest.mock import Mock, patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from celery.exceptions import SecurityError |
| 8 |
from celery.security.certificate import Certificate, CertStore, FSCertStore |
| 9 |
from t.unit import conftest |
| 10 |
|
| 11 |
from . import CERT1, CERT2, CERT_ECDSA, KEY1 |
| 12 |
from .case import SecurityCase |
| 13 |
|
| 14 |
|
| 15 |
class test_Certificate(SecurityCase): |
| 16 |
|
| 17 |
def test_valid_certificate(self): |
| 18 |
Certificate(CERT1) |
| 19 |
Certificate(CERT2) |
| 20 |
|
| 21 |
def test_invalid_certificate(self): |
| 22 |
with pytest.raises((SecurityError, TypeError)): |
| 23 |
Certificate(None) |
| 24 |
with pytest.raises(SecurityError): |
| 25 |
Certificate('') |
| 26 |
with pytest.raises(SecurityError): |
| 27 |
Certificate('foo') |
| 28 |
with pytest.raises(SecurityError): |
| 29 |
Certificate(CERT1[:20] + CERT1[21:]) |
| 30 |
with pytest.raises(SecurityError): |
| 31 |
Certificate(KEY1) |
| 32 |
with pytest.raises(SecurityError): |
| 33 |
Certificate(CERT_ECDSA) |
| 34 |
|
| 35 |
@pytest.mark.skip('TODO: cert expired') |
| 36 |
def test_has_expired(self): |
| 37 |
assert not Certificate(CERT1).has_expired() |
| 38 |
|
| 39 |
def test_has_expired_mock(self): |
| 40 |
x = Certificate(CERT1) |
| 41 |
|
| 42 |
x._cert = Mock(name='cert') |
| 43 |
time_after = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=-1) |
| 44 |
x._cert.not_valid_after_utc = time_after |
| 45 |
|
| 46 |
assert x.has_expired() is True |
| 47 |
|
| 48 |
def test_has_not_expired_mock(self): |
| 49 |
x = Certificate(CERT1) |
| 50 |
|
| 51 |
x._cert = Mock(name='cert') |
| 52 |
time_after = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=1) |
| 53 |
x._cert.not_valid_after_utc = time_after |
| 54 |
|
| 55 |
assert x.has_expired() is False |
| 56 |
|
| 57 |
|
| 58 |
class test_CertStore(SecurityCase): |
| 59 |
|
| 60 |
def test_itercerts(self): |
| 61 |
cert1 = Certificate(CERT1) |
| 62 |
cert2 = Certificate(CERT2) |
| 63 |
certstore = CertStore() |
| 64 |
for c in certstore.itercerts(): |
| 65 |
assert False |
| 66 |
certstore.add_cert(cert1) |
| 67 |
certstore.add_cert(cert2) |
| 68 |
for c in certstore.itercerts(): |
| 69 |
assert c in (cert1, cert2) |
| 70 |
|
| 71 |
def test_duplicate(self): |
| 72 |
cert1 = Certificate(CERT1) |
| 73 |
certstore = CertStore() |
| 74 |
certstore.add_cert(cert1) |
| 75 |
with pytest.raises(SecurityError): |
| 76 |
certstore.add_cert(cert1) |
| 77 |
|
| 78 |
|
| 79 |
class test_FSCertStore(SecurityCase): |
| 80 |
|
| 81 |
@patch('os.path.isdir') |
| 82 |
@patch('glob.glob') |
| 83 |
@patch('celery.security.certificate.Certificate') |
| 84 |
def test_init(self, Certificate, glob, isdir): |
| 85 |
cert = Certificate.return_value = Mock() |
| 86 |
cert.has_expired.return_value = False |
| 87 |
isdir.return_value = True |
| 88 |
glob.return_value = ['foo.cert'] |
| 89 |
with conftest.open(): |
| 90 |
cert.get_id.return_value = 1 |
| 91 |
|
| 92 |
path = os.path.join('var', 'certs') |
| 93 |
x = FSCertStore(path) |
| 94 |
assert 1 in x._certs |
| 95 |
glob.assert_called_with(os.path.join(path, '*')) |
| 96 |
|
| 97 |
# they both end up with the same id |
| 98 |
glob.return_value = ['foo.cert', 'bar.cert'] |
| 99 |
with pytest.raises(SecurityError): |
| 100 |
x = FSCertStore(path) |
| 101 |
glob.return_value = ['foo.cert'] |
| 102 |
|
| 103 |
cert.has_expired.return_value = True |
| 104 |
with pytest.raises(SecurityError): |
| 105 |
x = FSCertStore(path) |
| 106 |
|
| 107 |
isdir.return_value = False |
| 108 |
with pytest.raises(SecurityError): |
| 109 |
x = FSCertStore(path) |