| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
import collections |
| 4 |
import datetime |
| 5 |
import json |
| 6 |
|
| 7 |
import dateutil |
| 8 |
import urllib3 |
| 9 |
|
| 10 |
try: |
| 11 |
from unittest import mock |
| 12 |
except ImportError: |
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
import pyfakefs.fake_filesystem_unittest |
| 16 |
|
| 17 |
from cryptodatahub.common.types import CryptoDataEnumBase |
| 18 |
from cryptodatahub.common.entity import Entity |
| 19 |
from cryptodatahub.common.stores import ( |
| 20 |
CertificateTransparencyLogParams, |
| 21 |
CertificateTransparencyLogState, |
| 22 |
CertificateTransparencyLogStateType, |
| 23 |
CertificateTransparencyLogTemporalInterval, |
| 24 |
CertificateTransparencyLogTrustStore, |
| 25 |
) |
| 26 |
|
| 27 |
from cryptodatahub.common.fetcher import ( |
| 28 |
FetcherCertificateTransparencyLogs, |
| 29 |
FetcherCertificateTransparencyLogStoreApple, |
| 30 |
FetcherCertificateTransparencyLogStoreGoogle, |
| 31 |
) |
| 32 |
|
| 33 |
from updaters.ct_log import UpdaterCertificateTransparencyLogs |
| 34 |
|
| 35 |
|
| 36 |
class TestUpdaterCertificateTransparencyLogs(pyfakefs.fake_filesystem_unittest.TestCase): |
| 37 |
CT_LOG_LIST_JSON_SAMPLE = """ |
| 38 |
{ |
| 39 |
"operators": [ |
| 40 |
{ |
| 41 |
"name": "Up In The Air Consulting", |
| 42 |
"email": [ |
| 43 |
"[email protected]" |
| 44 |
], |
| 45 |
"logs": [ |
| 46 |
{ |
| 47 |
"description": "description", |
| 48 |
"log_id": "bG9nX2lk", |
| 49 |
"key": "a2V5", |
| 50 |
"url": "https://log.id/", |
| 51 |
"mmd": 86400, |
| 52 |
"state": { |
| 53 |
"rejected": { |
| 54 |
"timestamp": "1970-01-01T00:00:00Z" |
| 55 |
} |
| 56 |
}, |
| 57 |
"temporal_interval": { |
| 58 |
"start_inclusive": "1970-01-01T00:00:00Z", |
| 59 |
"end_exclusive": "1970-01-01T00:00:00Z" |
| 60 |
} |
| 61 |
} |
| 62 |
], |
| 63 |
"tiled_logs": [ |
| 64 |
{ |
| 65 |
"description": "tiled description", |
| 66 |
"log_id": "dGlsZWRfaWQ=", |
| 67 |
"key": "dGlsZWRfa2V5", |
| 68 |
"mmd": 60, |
| 69 |
"monitoring_url": "https://tiled.example/monitor/", |
| 70 |
"submission_url": "https://tiled.example/submit/", |
| 71 |
"tls_only": true, |
| 72 |
"state": { |
| 73 |
"usable": { |
| 74 |
"timestamp": "1970-01-01T00:00:00Z" |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
] |
| 79 |
} |
| 80 |
] |
| 81 |
} |
| 82 |
""" |
| 83 |
_LOG_STATE = CertificateTransparencyLogState( |
| 84 |
state_type=CertificateTransparencyLogStateType.REJECTED, |
| 85 |
timestamp=datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=dateutil.tz.UTC), |
| 86 |
) |
| 87 |
CT_LOG_ITEM_OBJECT = CertificateTransparencyLogParams( |
| 88 |
log_id='bG9nX2lk', |
| 89 |
operator='FILIPPO_VALSORDA', |
| 90 |
key='a2V5', |
| 91 |
url='https://log.id/', |
| 92 |
mmd=86400, |
| 93 |
description='description', |
| 94 |
temporal_interval=CertificateTransparencyLogTemporalInterval( |
| 95 |
start_inclusive=datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=dateutil.tz.UTC), |
| 96 |
end_exclusive=datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=dateutil.tz.UTC), |
| 97 |
), |
| 98 |
trust_stores=( |
| 99 |
CertificateTransparencyLogTrustStore(owner=Entity.APPLE, log_state=_LOG_STATE), |
| 100 |
CertificateTransparencyLogTrustStore(owner=Entity.GOOGLE, log_state=_LOG_STATE), |
| 101 |
), |
| 102 |
) |
| 103 |
_TILED_LOG_STATE = CertificateTransparencyLogState( |
| 104 |
state_type=CertificateTransparencyLogStateType.USABLE, |
| 105 |
timestamp=datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=dateutil.tz.UTC), |
| 106 |
) |
| 107 |
CT_TILED_LOG_ITEM_OBJECT = CertificateTransparencyLogParams( |
| 108 |
log_id='dGlsZWRfaWQ=', |
| 109 |
operator='FILIPPO_VALSORDA', |
| 110 |
key='dGlsZWRfa2V5', |
| 111 |
mmd=60, |
| 112 |
description='tiled description', |
| 113 |
trust_stores=( |
| 114 |
CertificateTransparencyLogTrustStore(owner=Entity.APPLE, log_state=_TILED_LOG_STATE), |
| 115 |
CertificateTransparencyLogTrustStore(owner=Entity.GOOGLE, log_state=_TILED_LOG_STATE), |
| 116 |
), |
| 117 |
) |
| 118 |
# a log no longer present in any fetched store (retired everywhere) |
| 119 |
_REMOVED_LOG = CertificateTransparencyLogParams( |
| 120 |
log_id='cmV0aXJlZA==', |
| 121 |
operator='FILIPPO_VALSORDA', |
| 122 |
key='a2V5', |
| 123 |
url='https://removed.log/', |
| 124 |
mmd=86400, |
| 125 |
description='removed description', |
| 126 |
trust_stores=( |
| 127 |
CertificateTransparencyLogTrustStore(owner=Entity.GOOGLE, log_state=_LOG_STATE), |
| 128 |
), |
| 129 |
) |
| 130 |
# an already-stored log sharing the fetched sample's log_id but with a different name/description |
| 131 |
_EXISTING_LOG = CertificateTransparencyLogParams( |
| 132 |
log_id='bG9nX2lk', |
| 133 |
operator='FILIPPO_VALSORDA', |
| 134 |
key='a2V5', |
| 135 |
url='https://existing.log/', |
| 136 |
mmd=86400, |
| 137 |
description='existing description', |
| 138 |
trust_stores=( |
| 139 |
CertificateTransparencyLogTrustStore(owner=Entity.GOOGLE, log_state=_LOG_STATE), |
| 140 |
), |
| 141 |
) |
| 142 |
|
| 143 |
def setUp(self): |
| 144 |
self.setUpPyfakefs() |
| 145 |
|
| 146 |
@mock.patch.object(urllib3.poolmanager.PoolManager, 'request') |
| 147 |
def test_log_list_parsing(self, mock_request): |
| 148 |
mock_request.return_value = urllib3.response.HTTPResponse(self.CT_LOG_LIST_JSON_SAMPLE) |
| 149 |
current_data = FetcherCertificateTransparencyLogs.from_current_data() |
| 150 |
self.assertTrue(isinstance(current_data, FetcherCertificateTransparencyLogs)) |
| 151 |
parsed_by_log_id = {p.log_id.value: p for p in current_data.parsed_data} |
| 152 |
self.assertEqual( |
| 153 |
parsed_by_log_id[self.CT_LOG_ITEM_OBJECT.log_id.value], |
| 154 |
self.CT_LOG_ITEM_OBJECT, |
| 155 |
) |
| 156 |
self.assertEqual( |
| 157 |
parsed_by_log_id[self.CT_TILED_LOG_ITEM_OBJECT.log_id.value], |
| 158 |
self.CT_TILED_LOG_ITEM_OBJECT, |
| 159 |
) |
| 160 |
|
| 161 |
def test_get_ct_log_store_updaters(self): |
| 162 |
updaters = FetcherCertificateTransparencyLogs.get_ct_log_store_updaters() |
| 163 |
self.assertEqual(updaters[Entity.GOOGLE], FetcherCertificateTransparencyLogStoreGoogle) |
| 164 |
self.assertEqual(updaters[Entity.APPLE], FetcherCertificateTransparencyLogStoreApple) |
| 165 |
|
| 166 |
@mock.patch.object(urllib3.poolmanager.PoolManager, 'request') |
| 167 |
def test_log_list_update(self, mock_request): |
| 168 |
mock_request.return_value = urllib3.response.HTTPResponse(self.CT_LOG_LIST_JSON_SAMPLE) |
| 169 |
json_file_path = str(CryptoDataEnumBase.get_json_path(CertificateTransparencyLogParams)) |
| 170 |
self.fs.create_file(json_file_path, contents='{}') |
| 171 |
|
| 172 |
UpdaterCertificateTransparencyLogs()() |
| 173 |
with open(json_file_path, encoding='ascii') as json_file: |
| 174 |
self.assertEqual( |
| 175 |
json_file.read(), |
| 176 |
CryptoDataEnumBase.dump_json({ |
| 177 |
'DESCRIPTION': self.CT_LOG_ITEM_OBJECT._asdict(), |
| 178 |
'TILED_DESCRIPTION': self.CT_TILED_LOG_ITEM_OBJECT._asdict(), |
| 179 |
}), |
| 180 |
) |
| 181 |
|
| 182 |
@mock.patch.object(urllib3.poolmanager.PoolManager, 'request') |
| 183 |
def test_log_list_update_preserves_removed_log(self, mock_request): |
| 184 |
mock_request.return_value = urllib3.response.HTTPResponse(self.CT_LOG_LIST_JSON_SAMPLE) |
| 185 |
json_file_path = str(CryptoDataEnumBase.get_json_path(CertificateTransparencyLogParams)) |
| 186 |
self.fs.create_file(json_file_path, contents=CryptoDataEnumBase.dump_json(collections.OrderedDict([ |
| 187 |
(self._REMOVED_LOG.identifier, self._REMOVED_LOG._asdict()), |
| 188 |
]))) |
| 189 |
|
| 190 |
UpdaterCertificateTransparencyLogs()() |
| 191 |
|
| 192 |
with open(json_file_path, encoding='ascii') as json_file: |
| 193 |
result = json.load(json_file) |
| 194 |
self.assertIn(self._REMOVED_LOG.identifier, result) |
| 195 |
self.assertEqual(result[self._REMOVED_LOG.identifier]['description'], 'removed description') |
| 196 |
self.assertIn('DESCRIPTION', result) |
| 197 |
self.assertIn('TILED_DESCRIPTION', result) |
| 198 |
|
| 199 |
@mock.patch.object(urllib3.poolmanager.PoolManager, 'request') |
| 200 |
def test_log_list_update_keeps_existing_log_and_refreshes_only_trust_stores(self, mock_request): |
| 201 |
mock_request.return_value = urllib3.response.HTTPResponse(self.CT_LOG_LIST_JSON_SAMPLE) |
| 202 |
json_file_path = str(CryptoDataEnumBase.get_json_path(CertificateTransparencyLogParams)) |
| 203 |
self.fs.create_file(json_file_path, contents=CryptoDataEnumBase.dump_json(collections.OrderedDict([ |
| 204 |
(self._EXISTING_LOG.identifier, self._EXISTING_LOG._asdict()), |
| 205 |
]))) |
| 206 |
|
| 207 |
UpdaterCertificateTransparencyLogs()() |
| 208 |
|
| 209 |
with open(json_file_path, encoding='ascii') as json_file: |
| 210 |
result = json.load(json_file) |
| 211 |
self.assertIn(self._EXISTING_LOG.identifier, result) |
| 212 |
self.assertNotIn('DESCRIPTION', result) |
| 213 |
self.assertEqual(result[self._EXISTING_LOG.identifier]['description'], 'existing description') |
| 214 |
self.assertEqual( |
| 215 |
result[self._EXISTING_LOG.identifier]['trust_stores'], |
| 216 |
json.loads(CryptoDataEnumBase.dump_json(self.CT_LOG_ITEM_OBJECT._asdict()))['trust_stores'], |
| 217 |
) |
| 218 |
log_ids = [entry['log_id'] for entry in result.values()] |
| 219 |
self.assertEqual(len(log_ids), len(set(log_ids))) |