| 1 |
import datetime |
| 2 |
import json |
| 3 |
from copy import deepcopy |
| 4 |
from typing import Any, Optional |
| 5 |
|
| 6 |
import pytest |
| 7 |
import responses |
| 8 |
from responses import matchers |
| 9 |
|
| 10 |
from censys.common.exceptions import CensysInternalServerException |
| 11 |
from censys.search import CensysHosts, SearchClient |
| 12 |
from tests.utils import V2_URL, CensysTestCase |
| 13 |
|
| 14 |
TEST_HOST = "8.8.8.8" |
| 15 |
TEST_SEARCH_QUERY = "services.service_name: HTTP" |
| 16 |
|
| 17 |
VIEW_HOST_JSON = { |
| 18 |
"code": 200, |
| 19 |
"status": "OK", |
| 20 |
"result": { |
| 21 |
"services": [ |
| 22 |
{ |
| 23 |
"transport_protocol": "UDP", |
| 24 |
"truncated": False, |
| 25 |
"service_name": "DNS", |
| 26 |
"_decoded": "dns", |
| 27 |
"source_ip": "167.248.133.40", |
| 28 |
"extended_service_name": "DNS", |
| 29 |
"observed_at": "2021-04-01T13:40:03.755876935Z", |
| 30 |
"dns": {"server_type": "FORWARDING"}, |
| 31 |
"perspective_id": "PERSPECTIVE_NTT", |
| 32 |
"port": 53, |
| 33 |
"software": [], |
| 34 |
} |
| 35 |
], |
| 36 |
"ip": "8.8.8.8", |
| 37 |
"location_updated_at": "2021-03-30T14:53:12.980328Z", |
| 38 |
"location": { |
| 39 |
"country": "United States", |
| 40 |
"coordinates": {"latitude": 37.751, "longitude": -97.822}, |
| 41 |
"registered_country": "United States", |
| 42 |
"registered_country_code": "US", |
| 43 |
"postal_code": "", |
| 44 |
"country_code": "US", |
| 45 |
"timezone": "America/Chicago", |
| 46 |
"continent": "North America", |
| 47 |
}, |
| 48 |
"last_updated_at": "2021-04-01T14:10:10.712Z", |
| 49 |
}, |
| 50 |
} |
| 51 |
|
| 52 |
SEARCH_HOSTS_JSON = { |
| 53 |
"code": 200, |
| 54 |
"status": "OK", |
| 55 |
"result": { |
| 56 |
"query": "services.service_name: HTTP", |
| 57 |
"hits": [ |
| 58 |
{ |
| 59 |
"services": [ |
| 60 |
{"service_name": "HTTP", "port": 443}, |
| 61 |
{"service_name": "HTTP", "port": 80}, |
| 62 |
], |
| 63 |
"ip": "1.0.0.0", |
| 64 |
}, |
| 65 |
{ |
| 66 |
"services": [ |
| 67 |
{"service_name": "HTTP", "port": 443}, |
| 68 |
{"service_name": "HTTP", "port": 80}, |
| 69 |
], |
| 70 |
"ip": "1.0.0.1", |
| 71 |
}, |
| 72 |
], |
| 73 |
"total": 146857082, |
| 74 |
"links": { |
| 75 |
"prev": "eyJBZnRlciI6WyIxIiwiMS4wLjAuMCJdLCJSZXZlcnNlIjp0cnVlfQ==", |
| 76 |
"next": "eyJBZnRlciI6WyIxIiwiMS4wLjAuNDkiXSwiUmV2ZXJzZSI6ZmFsc2V9", |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
AGGREGATE_HOSTS_JSON = { |
| 82 |
"code": 200, |
| 83 |
"status": "OK", |
| 84 |
"result": { |
| 85 |
"total_omitted": 358388380, |
| 86 |
"buckets": [ |
| 87 |
{"count": 47637476, "key": "80"}, |
| 88 |
{"count": 35073802, "key": "443"}, |
| 89 |
{"count": 17256198, "key": "7547"}, |
| 90 |
{"count": 13216884, "key": "22"}, |
| 91 |
], |
| 92 |
"potential_deviation": 605118, |
| 93 |
"field": "services.port", |
| 94 |
"query": "services.service_name: HTTP", |
| 95 |
"total": 149575980, |
| 96 |
}, |
| 97 |
} |
| 98 |
|
| 99 |
VIEW_HOST_NAMES_JSON = { |
| 100 |
"code": 200, |
| 101 |
"status": "OK", |
| 102 |
"result": {"names": ["google.com", "google.co.uk", "google.com.au", "dns.google"]}, |
| 103 |
"links": {"prev": "prevCursorToken", "next": "nextCursorToken"}, |
| 104 |
} |
| 105 |
|
| 106 |
HOST_METADATA_JSON = { |
| 107 |
"code": 200, |
| 108 |
"status": "OK", |
| 109 |
"result": {"services": ["HTTP", "IMAP", "MQTT", "SSH", "..."]}, |
| 110 |
} |
| 111 |
|
| 112 |
VIEW_HOST_DIFF_JSON = { |
| 113 |
"code": 200, |
| 114 |
"status": "OK", |
| 115 |
"result": { |
| 116 |
"a": {"ip": "1.1.1.1", "last_updated_at": "2022-01-10T15:41:27.416Z"}, |
| 117 |
"b": {"ip": "1.1.1.2", "last_updated_at": "2022-01-10T15:41:27.416Z"}, |
| 118 |
"patch": [{}], |
| 119 |
}, |
| 120 |
} |
| 121 |
|
| 122 |
VIEW_HOST_EVENTS_JSON = { |
| 123 |
"code": 200, |
| 124 |
"status": "OK", |
| 125 |
"result": { |
| 126 |
"ip": "8.8.8.8", |
| 127 |
"events": [ |
| 128 |
{ |
| 129 |
"_event": "service_observed", |
| 130 |
"service_observed": { |
| 131 |
"id": { |
| 132 |
"port": 443, |
| 133 |
"transport_protocol": "TCP", |
| 134 |
"service_name": "HTTP", |
| 135 |
}, |
| 136 |
"observed_at": "2021-07-27T18:00:11.296Z", |
| 137 |
"perspective_id": "PERSPECTIVE_NTT", |
| 138 |
"changed_fields": [{"field_name": "services.banner"}], |
| 139 |
}, |
| 140 |
"timestamp": "2021-07-27T18:00:11.296Z", |
| 141 |
}, |
| 142 |
{ |
| 143 |
"_event": "location_updated", |
| 144 |
"location_updated": { |
| 145 |
"location": { |
| 146 |
"continent": "North America", |
| 147 |
"country": "United States", |
| 148 |
"country_code": "US", |
| 149 |
"postal_code": "48104", |
| 150 |
"timezone": "America/Michigan", |
| 151 |
"coordinates": {"latitude": "42.273", "longitude": "-83.751"}, |
| 152 |
"registered_country": "United States", |
| 153 |
"registered_country_code": "US", |
| 154 |
} |
| 155 |
}, |
| 156 |
"timestamp": "2021-07-27T18:00:11.297Z", |
| 157 |
}, |
| 158 |
], |
| 159 |
}, |
| 160 |
} |
| 161 |
RATE_LIMIT_ERROR_JSON = { |
| 162 |
"error": "Rate limit exceeded. See https://search.censys.io/account for rate limit details.", |
| 163 |
"status": "error", |
| 164 |
"error_type": "rate_limit_exceeded", |
| 165 |
} |
| 166 |
TOO_MANY_REQUESTS_ERROR_JSON = { |
| 167 |
"code": 429, |
| 168 |
"status": "Too Many Requests", |
| 169 |
"error": "You have used your full quota for this billing period. Please see https://search.censys.io/account or contact [email protected].", |
| 170 |
} |
| 171 |
SERVER_ERROR_JSON = { |
| 172 |
"code": 500, |
| 173 |
"status": "Internal Server Error", |
| 174 |
"error": "An unexpected error occurred. Please try again later.", |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
class TestHosts(CensysTestCase): |
| 179 |
api: CensysHosts |
| 180 |
|
| 181 |
def setup_method(self): |
| 182 |
self.setUpApi(SearchClient(self.api_id, self.api_secret).v2.hosts) |
| 183 |
|
| 184 |
def test_view(self): |
| 185 |
self.responses.add( |
| 186 |
responses.GET, |
| 187 |
f"{V2_URL}/hosts/{TEST_HOST}", |
| 188 |
status=200, |
| 189 |
json=VIEW_HOST_JSON, |
| 190 |
) |
| 191 |
|
| 192 |
res = self.api.view(TEST_HOST) |
| 193 |
|
| 194 |
assert res == VIEW_HOST_JSON["result"] |
| 195 |
|
| 196 |
def test_view_at_time(self): |
| 197 |
self.responses.add( |
| 198 |
responses.GET, |
| 199 |
f"{V2_URL}/hosts/{TEST_HOST}?at_time=2021-03-01T00:00:00.000000Z", # noqa: E231 |
| 200 |
status=200, |
| 201 |
json=VIEW_HOST_JSON, |
| 202 |
) |
| 203 |
|
| 204 |
date = datetime.date(2021, 3, 1) |
| 205 |
|
| 206 |
res = self.api.view(TEST_HOST, at_time=date) |
| 207 |
|
| 208 |
assert res == VIEW_HOST_JSON["result"] |
| 209 |
|
| 210 |
def test_bulk_view(self): |
| 211 |
ips = ["1.1.1.1", "1.1.1.2", "1.1.1.3"] |
| 212 |
expected = {} |
| 213 |
for ip in ips: |
| 214 |
host_json = deepcopy(VIEW_HOST_JSON) |
| 215 |
host_json["result"]["ip"] = ip |
| 216 |
self.responses.add( |
| 217 |
responses.GET, |
| 218 |
f"{V2_URL}/hosts/{ip}", |
| 219 |
status=200, |
| 220 |
json=host_json, |
| 221 |
) |
| 222 |
expected[ip] = deepcopy(host_json["result"]) |
| 223 |
|
| 224 |
results = self.api.bulk_view(ips) |
| 225 |
assert results == expected |
| 226 |
|
| 227 |
def test_bulk_view_at_time(self): |
| 228 |
ips = ["1.1.1.1", "1.1.1.2", "1.1.1.3"] |
| 229 |
expected = {} |
| 230 |
for ip in ips: |
| 231 |
host_json = deepcopy(VIEW_HOST_JSON) |
| 232 |
host_json["result"]["ip"] = ip |
| 233 |
self.responses.add( |
| 234 |
responses.GET, |
| 235 |
f"{V2_URL}/hosts/{ip}?at_time=2021-03-01T00:00:00.000000Z", # noqa: E231 |
| 236 |
status=200, |
| 237 |
json=host_json, |
| 238 |
) |
| 239 |
expected[ip] = deepcopy(host_json["result"]) |
| 240 |
|
| 241 |
date = datetime.date(2021, 3, 1) |
| 242 |
|
| 243 |
results = self.api.bulk_view(ips, at_time=date) |
| 244 |
assert results == expected |
| 245 |
|
| 246 |
def test_bulk_view_with_error(self): |
| 247 |
ips = ["1.1.1.1", "1.1.1.2", "1.1.1.3"] |
| 248 |
expected = {} |
| 249 |
for ip in ips[:-1]: |
| 250 |
host_json = deepcopy(VIEW_HOST_JSON) |
| 251 |
host_json["result"]["ip"] = ip |
| 252 |
self.responses.add( |
| 253 |
responses.GET, |
| 254 |
f"{V2_URL}/hosts/{ip}", |
| 255 |
status=200, |
| 256 |
json=host_json, |
| 257 |
) |
| 258 |
expected[ip] = deepcopy(host_json["result"]) |
| 259 |
|
| 260 |
self.responses.add( |
| 261 |
responses.GET, |
| 262 |
f"{V2_URL}/hosts/{ips[-1]}", |
| 263 |
status=429, |
| 264 |
json=RATE_LIMIT_ERROR_JSON, |
| 265 |
) |
| 266 |
expected[ips[-1]] = { |
| 267 |
"error": "429 (rate_limit_exceeded): Rate limit exceeded. See https://search.censys.io/account for rate limit details." |
| 268 |
} |
| 269 |
|
| 270 |
results = self.api.bulk_view(ips) |
| 271 |
assert results == expected |
| 272 |
|
| 273 |
@pytest.mark.parametrize( |
| 274 |
("method_name", "raw"), |
| 275 |
[ |
| 276 |
("search_post_raw", True), |
| 277 |
("raw_search", True), |
| 278 |
("search_post", False), |
| 279 |
], |
| 280 |
) |
| 281 |
def test_search_post(self, method_name: str, raw: bool): |
| 282 |
self.responses.add( |
| 283 |
responses.POST, |
| 284 |
f"{V2_URL}/hosts/search", |
| 285 |
status=200, |
| 286 |
json=SEARCH_HOSTS_JSON, |
| 287 |
match=[ |
| 288 |
matchers.json_params_matcher( |
| 289 |
{ |
| 290 |
"q": TEST_SEARCH_QUERY, |
| 291 |
"per_page": 100, |
| 292 |
} |
| 293 |
) |
| 294 |
], |
| 295 |
) |
| 296 |
method = getattr(self.api, method_name) |
| 297 |
result = method(TEST_SEARCH_QUERY) |
| 298 |
if raw: |
| 299 |
assert result == SEARCH_HOSTS_JSON |
| 300 |
else: |
| 301 |
assert result == SEARCH_HOSTS_JSON["result"] |
| 302 |
|
| 303 |
@pytest.mark.parametrize( |
| 304 |
("fields", "sort", "cursor", "virtual_hosts"), |
| 305 |
[ |
| 306 |
(None, None, None, None), |
| 307 |
(["ip", "services.port"], None, None, None), |
| 308 |
(None, "RELEVANCE", None, None), |
| 309 |
(["ip", "services.port"], "RELEVANCE", None, None), |
| 310 |
(None, None, None, "ONLY"), |
| 311 |
(None, None, "nextCursorToken", None), |
| 312 |
], |
| 313 |
) |
| 314 |
def test_search( |
| 315 |
self, |
| 316 |
fields: Optional[list[str]], |
| 317 |
sort: Optional[str], |
| 318 |
cursor: Optional[str], |
| 319 |
virtual_hosts: Optional[str], |
| 320 |
): |
| 321 |
self.responses.add( |
| 322 |
responses.POST, |
| 323 |
f"{V2_URL}/hosts/search", |
| 324 |
status=200, |
| 325 |
json=SEARCH_HOSTS_JSON, |
| 326 |
) |
| 327 |
query = self.api.search( |
| 328 |
TEST_SEARCH_QUERY, |
| 329 |
fields=fields, |
| 330 |
sort=sort, |
| 331 |
cursor=cursor, |
| 332 |
virtual_hosts=virtual_hosts, |
| 333 |
) |
| 334 |
assert next(query) == SEARCH_HOSTS_JSON["result"]["hits"] |
| 335 |
|
| 336 |
def test_search_per_page(self): |
| 337 |
test_per_page = 50 |
| 338 |
self.responses.add( |
| 339 |
responses.POST, |
| 340 |
V2_URL + "/hosts/search", |
| 341 |
status=200, |
| 342 |
json=SEARCH_HOSTS_JSON, |
| 343 |
match=[matchers.json_params_matcher({"q": "services.service_name: HTTP", "per_page": test_per_page})], |
| 344 |
) |
| 345 |
|
| 346 |
query = self.api.search("services.service_name: HTTP", per_page=test_per_page) |
| 347 |
assert next(query) == SEARCH_HOSTS_JSON["result"]["hits"] |
| 348 |
|
| 349 |
@pytest.mark.parametrize( |
| 350 |
("params", "expected_params"), |
| 351 |
[ |
| 352 |
({}, {"q": TEST_SEARCH_QUERY, "per_page": 100}), |
| 353 |
({"per_page": 1}, {"q": TEST_SEARCH_QUERY, "per_page": 1}), |
| 354 |
( |
| 355 |
{"cursor": "nextCursorToken"}, |
| 356 |
{"q": TEST_SEARCH_QUERY, "cursor": "nextCursorToken", "per_page": 100}, |
| 357 |
), |
| 358 |
( |
| 359 |
{"fields": ["ip"]}, |
| 360 |
{"q": TEST_SEARCH_QUERY, "fields": "ip", "per_page": 100}, |
| 361 |
), |
| 362 |
( |
| 363 |
{ |
| 364 |
"fields": [ |
| 365 |
"ip", |
| 366 |
"services.port", |
| 367 |
"services.extended_service_name", |
| 368 |
] |
| 369 |
}, |
| 370 |
{ |
| 371 |
"q": TEST_SEARCH_QUERY, |
| 372 |
"fields": [ |
| 373 |
"ip", |
| 374 |
"services.port", |
| 375 |
"services.extended_service_name", |
| 376 |
], |
| 377 |
"per_page": 100, |
| 378 |
}, |
| 379 |
), |
| 380 |
( |
| 381 |
{"sort": "RELEVANCE"}, |
| 382 |
{ |
| 383 |
"q": TEST_SEARCH_QUERY, |
| 384 |
"sort": "RELEVANCE", |
| 385 |
"per_page": 100, |
| 386 |
}, |
| 387 |
), |
| 388 |
], |
| 389 |
) |
| 390 |
def test_search_get(self, params: dict[str, Any], expected_params: dict[str, Any]): |
| 391 |
self.responses.add( |
| 392 |
responses.GET, |
| 393 |
f"{V2_URL}/hosts/search", |
| 394 |
status=200, |
| 395 |
json=SEARCH_HOSTS_JSON, |
| 396 |
match=[matchers.query_param_matcher(expected_params)], |
| 397 |
) |
| 398 |
result = self.api.search_get(TEST_SEARCH_QUERY, **params) |
| 399 |
assert result == SEARCH_HOSTS_JSON["result"] |
| 400 |
|
| 401 |
def test_search_with_error_retry(self): |
| 402 |
is_first_request = True |
| 403 |
|
| 404 |
def request_callback(_): |
| 405 |
nonlocal is_first_request |
| 406 |
if is_first_request: |
| 407 |
is_first_request = False |
| 408 |
return (429, {}, json.dumps(RATE_LIMIT_ERROR_JSON)) |
| 409 |
return (200, {}, json.dumps(SEARCH_HOSTS_JSON)) |
| 410 |
|
| 411 |
self.responses.add_callback( |
| 412 |
responses.POST, |
| 413 |
V2_URL + "/hosts/search", |
| 414 |
callback=request_callback, |
| 415 |
content_type="application/json", |
| 416 |
) |
| 417 |
|
| 418 |
query = self.api.search("services.service_name: HTTP") |
| 419 |
assert query() == SEARCH_HOSTS_JSON["result"]["hits"] |
| 420 |
|
| 421 |
def test_search_invalid_query(self): |
| 422 |
invalid_query = "some_bad_query" |
| 423 |
no_hosts_json = deepcopy(SEARCH_HOSTS_JSON) |
| 424 |
no_hosts_json["result"]["hits"] = [] |
| 425 |
no_hosts_json["result"]["total"] = 0 |
| 426 |
no_hosts_json["result"]["links"]["next"] = "" |
| 427 |
self.responses.add( |
| 428 |
responses.POST, |
| 429 |
V2_URL + "/hosts/search", |
| 430 |
status=200, |
| 431 |
json=no_hosts_json, |
| 432 |
match=[matchers.json_params_matcher({"q": invalid_query, "per_page": 100})], |
| 433 |
) |
| 434 |
|
| 435 |
query = self.api.search(invalid_query) |
| 436 |
assert next(query) == no_hosts_json["result"]["hits"] |
| 437 |
assert query.pages == 0 |
| 438 |
with pytest.raises(StopIteration): |
| 439 |
next(query) |
| 440 |
|
| 441 |
def test_search_pages(self): |
| 442 |
self.responses.add( |
| 443 |
responses.POST, |
| 444 |
V2_URL + "/hosts/search", |
| 445 |
status=200, |
| 446 |
json=SEARCH_HOSTS_JSON, |
| 447 |
match=[matchers.json_params_matcher({"q": "services.service_name: HTTP", "per_page": 100})], |
| 448 |
) |
| 449 |
page_2_json = deepcopy(SEARCH_HOSTS_JSON) |
| 450 |
hits = page_2_json["result"]["hits"] |
| 451 |
new_hits = [ |
| 452 |
{ |
| 453 |
"services": [ |
| 454 |
{"service_name": "HTTP", "port": 443}, |
| 455 |
{"service_name": "HTTP", "port": 80}, |
| 456 |
], |
| 457 |
"ip": "1.0.0.2", |
| 458 |
} |
| 459 |
] |
| 460 |
next_cursor = SEARCH_HOSTS_JSON["result"]["links"]["next"] |
| 461 |
page_2_json["result"]["hits"] = new_hits |
| 462 |
page_2_json["result"]["links"]["next"] = None |
| 463 |
self.responses.add( |
| 464 |
responses.POST, |
| 465 |
V2_URL + "/hosts/search", |
| 466 |
status=200, |
| 467 |
json=page_2_json, |
| 468 |
match=[ |
| 469 |
matchers.json_params_matcher( |
| 470 |
{ |
| 471 |
"q": "services.service_name: HTTP", |
| 472 |
"per_page": 100, |
| 473 |
"cursor": next_cursor, |
| 474 |
} |
| 475 |
) |
| 476 |
], |
| 477 |
) |
| 478 |
|
| 479 |
expected = [hits, new_hits] |
| 480 |
|
| 481 |
query = self.api.search("services.service_name: HTTP", pages=-1) |
| 482 |
for i, page in enumerate(query): |
| 483 |
assert expected[i] == page |
| 484 |
|
| 485 |
def test_search_pages_retry_with_server_error(self): |
| 486 |
first_request_to_second_page = True |
| 487 |
|
| 488 |
def request_callback(_): |
| 489 |
nonlocal first_request_to_second_page |
| 490 |
if first_request_to_second_page: |
| 491 |
first_request_to_second_page = False |
| 492 |
return (500, {}, json.dumps(SERVER_ERROR_JSON)) |
| 493 |
return (200, {}, json.dumps(SEARCH_HOSTS_JSON)) |
| 494 |
|
| 495 |
self.responses.add( |
| 496 |
responses.POST, |
| 497 |
V2_URL + "/hosts/search", |
| 498 |
status=200, |
| 499 |
json=SEARCH_HOSTS_JSON, |
| 500 |
match=[matchers.json_params_matcher({"q": "services.service_name: HTTP", "per_page": 100})], |
| 501 |
) |
| 502 |
self.responses.add_callback( |
| 503 |
responses.POST, |
| 504 |
V2_URL + "/hosts/search", |
| 505 |
callback=request_callback, |
| 506 |
content_type="application/json", |
| 507 |
) |
| 508 |
|
| 509 |
query = self.api.search("services.service_name: HTTP", pages=2) |
| 510 |
assert next(query) == SEARCH_HOSTS_JSON["result"]["hits"] |
| 511 |
assert next(query) == SEARCH_HOSTS_JSON["result"]["hits"], "Retry did not fail" |
| 512 |
|
| 513 |
def test_search_pages_retry_fail(self): |
| 514 |
self.responses.add( |
| 515 |
responses.POST, |
| 516 |
V2_URL + "/hosts/search", |
| 517 |
status=200, |
| 518 |
json=SEARCH_HOSTS_JSON, |
| 519 |
match=[matchers.json_params_matcher({"q": "services.service_name: HTTP", "per_page": 100})], |
| 520 |
) |
| 521 |
self.responses.add( |
| 522 |
responses.POST, |
| 523 |
V2_URL + "/hosts/search", |
| 524 |
status=500, |
| 525 |
json=SERVER_ERROR_JSON, |
| 526 |
match=[ |
| 527 |
matchers.json_params_matcher( |
| 528 |
{ |
| 529 |
"q": "services.service_name: HTTP", |
| 530 |
"per_page": 100, |
| 531 |
"cursor": "eyJBZnRlciI6WyIxIiwiMS4wLjAuNDkiXSwiUmV2ZXJzZSI6ZmFsc2V9", |
| 532 |
} |
| 533 |
) |
| 534 |
], |
| 535 |
) |
| 536 |
|
| 537 |
query = self.api.search("services.service_name: HTTP", pages=2) |
| 538 |
assert next(query) == SEARCH_HOSTS_JSON["result"]["hits"] |
| 539 |
with pytest.raises(CensysInternalServerException): |
| 540 |
next(query) |
| 541 |
|
| 542 |
def test_search_virtual_hosts(self): |
| 543 |
self.responses.add( |
| 544 |
responses.POST, |
| 545 |
V2_URL + "/hosts/search", |
| 546 |
status=200, |
| 547 |
json=SEARCH_HOSTS_JSON, |
| 548 |
match=[ |
| 549 |
matchers.json_params_matcher( |
| 550 |
{ |
| 551 |
"q": "services.service_name: HTTP", |
| 552 |
"per_page": 100, |
| 553 |
"virtual_hosts": "EXCLUDE", |
| 554 |
} |
| 555 |
) |
| 556 |
], |
| 557 |
) |
| 558 |
|
| 559 |
query = self.api.search("services.service_name: HTTP", virtual_hosts="EXCLUDE") |
| 560 |
assert query() == SEARCH_HOSTS_JSON["result"]["hits"] |
| 561 |
|
| 562 |
def test_search_fields(self): |
| 563 |
self.responses.add( |
| 564 |
responses.POST, |
| 565 |
V2_URL + "/hosts/search", |
| 566 |
status=200, |
| 567 |
json=SEARCH_HOSTS_JSON, |
| 568 |
match=[ |
| 569 |
matchers.json_params_matcher( |
| 570 |
{ |
| 571 |
"q": "services.service_name: HTTP", |
| 572 |
"per_page": 100, |
| 573 |
"fields": ["ip", "services.port"], |
| 574 |
} |
| 575 |
) |
| 576 |
], |
| 577 |
) |
| 578 |
|
| 579 |
query = self.api.search("services.service_name: HTTP", fields=["ip", "services.port"]) |
| 580 |
assert query() == SEARCH_HOSTS_JSON["result"]["hits"] |
| 581 |
|
| 582 |
def test_aggregate(self): |
| 583 |
self.responses.add( |
| 584 |
responses.GET, |
| 585 |
V2_URL + "/hosts/aggregate?field=services.port&q=services.service_name: HTTP&num_buckets=4", |
| 586 |
status=200, |
| 587 |
json=AGGREGATE_HOSTS_JSON, |
| 588 |
) |
| 589 |
self.maxDiff = None |
| 590 |
res = self.api.aggregate("services.service_name: HTTP", "services.port", num_buckets=4) |
| 591 |
|
| 592 |
assert res == AGGREGATE_HOSTS_JSON["result"] |
| 593 |
|
| 594 |
def test_aggregate_virtual_hosts(self): |
| 595 |
self.responses.add( |
| 596 |
responses.GET, |
| 597 |
V2_URL |
| 598 |
+ "/hosts/aggregate?field=services.port&q=services.service_name: HTTP&num_buckets=5" |
| 599 |
+ "&virtual_hosts=INCLUDE", |
| 600 |
status=200, |
| 601 |
json=AGGREGATE_HOSTS_JSON, |
| 602 |
) |
| 603 |
self.maxDiff = None |
| 604 |
res = self.api.aggregate( |
| 605 |
"services.service_name: HTTP", |
| 606 |
"services.port", |
| 607 |
num_buckets=5, |
| 608 |
virtual_hosts="INCLUDE", |
| 609 |
) |
| 610 |
|
| 611 |
assert res == AGGREGATE_HOSTS_JSON["result"] |
| 612 |
|
| 613 |
def test_search_view_all(self): |
| 614 |
test_per_page = 50 |
| 615 |
ips = ["1.1.1.1", "1.1.1.2"] |
| 616 |
search_json = deepcopy(SEARCH_HOSTS_JSON) |
| 617 |
search_json["result"]["hits"] = [{"ip": ip} for ip in ips] |
| 618 |
search_json["result"]["total"] = len(ips) |
| 619 |
search_json["result"]["links"]["next"] = "" |
| 620 |
self.responses.add( |
| 621 |
responses.POST, |
| 622 |
f"{V2_URL}/hosts/search", |
| 623 |
status=200, |
| 624 |
json=search_json, |
| 625 |
match=[ |
| 626 |
matchers.json_params_matcher( |
| 627 |
{ |
| 628 |
"q": "services.service_name: HTTP", |
| 629 |
"per_page": test_per_page, |
| 630 |
} |
| 631 |
) |
| 632 |
], |
| 633 |
) |
| 634 |
|
| 635 |
expected = {} |
| 636 |
for ip in ips: |
| 637 |
view_json = deepcopy(VIEW_HOST_JSON) |
| 638 |
view_json["result"]["ip"] = ip |
| 639 |
self.responses.add( |
| 640 |
responses.GET, |
| 641 |
f"{V2_URL}/hosts/{ip}", |
| 642 |
status=200, |
| 643 |
json=view_json, |
| 644 |
) |
| 645 |
expected[ip] = deepcopy(view_json["result"]) |
| 646 |
|
| 647 |
query = self.api.search("services.service_name: HTTP", per_page=test_per_page) |
| 648 |
results = query.view_all() |
| 649 |
assert results == expected |
| 650 |
|
| 651 |
def test_search_view_all_virtual_hosts(self): |
| 652 |
test_per_page = 50 |
| 653 |
search_json = deepcopy(SEARCH_HOSTS_JSON) |
| 654 |
hits = [{"ip": "1.1.1.1", "name": "one.one.one.one"}, {"ip": "1.0.0.1"}] |
| 655 |
search_json["result"]["hits"] = hits |
| 656 |
search_json["result"]["total"] = len(hits) |
| 657 |
search_json["result"]["links"]["next"] = "" |
| 658 |
self.responses.add( |
| 659 |
responses.POST, |
| 660 |
f"{V2_URL}/hosts/search", |
| 661 |
status=200, |
| 662 |
json=search_json, |
| 663 |
match=[ |
| 664 |
matchers.json_params_matcher( |
| 665 |
{ |
| 666 |
"q": "services.service_name: HTTP", |
| 667 |
"per_page": test_per_page, |
| 668 |
} |
| 669 |
) |
| 670 |
], |
| 671 |
) |
| 672 |
|
| 673 |
expected = {} |
| 674 |
for hit in hits: |
| 675 |
view_json = deepcopy(VIEW_HOST_JSON) |
| 676 |
view_json["result"]["ip"] = hit["ip"] |
| 677 |
document_key = hit["ip"] |
| 678 |
if "name" in hit: |
| 679 |
document_key += "+" + hit["name"] |
| 680 |
self.responses.add( |
| 681 |
responses.GET, |
| 682 |
f"{V2_URL}/hosts/{document_key}", |
| 683 |
status=200, |
| 684 |
json=view_json, |
| 685 |
) |
| 686 |
expected[document_key] = deepcopy(view_json["result"]) |
| 687 |
|
| 688 |
query = self.api.search("services.service_name: HTTP", per_page=test_per_page) |
| 689 |
results = query.view_all() |
| 690 |
assert results == expected |
| 691 |
|
| 692 |
def test_search_view_all_error(self): |
| 693 |
test_per_page = 50 |
| 694 |
ips = ["1.1.1.1", "1.1.1.2", "1.1.1.3"] |
| 695 |
search_json = deepcopy(SEARCH_HOSTS_JSON) |
| 696 |
search_json["result"]["hits"] = [{"ip": ip} for ip in ips] |
| 697 |
search_json["result"]["total"] = len(ips) |
| 698 |
search_json["result"]["links"]["next"] = "" |
| 699 |
self.responses.add( |
| 700 |
responses.POST, |
| 701 |
f"{V2_URL}/hosts/search", |
| 702 |
status=200, |
| 703 |
json=search_json, |
| 704 |
match=[ |
| 705 |
matchers.json_params_matcher( |
| 706 |
{ |
| 707 |
"q": "services.service_name: HTTP", |
| 708 |
"per_page": test_per_page, |
| 709 |
} |
| 710 |
) |
| 711 |
], |
| 712 |
) |
| 713 |
|
| 714 |
expected = {} |
| 715 |
for ip in ips[:-1]: |
| 716 |
view_json = deepcopy(VIEW_HOST_JSON) |
| 717 |
view_json["result"]["ip"] = ip |
| 718 |
self.responses.add( |
| 719 |
responses.GET, |
| 720 |
f"{V2_URL}/hosts/{ip}", |
| 721 |
status=200, |
| 722 |
json=view_json, |
| 723 |
) |
| 724 |
expected[ip] = deepcopy(view_json["result"]) |
| 725 |
|
| 726 |
self.responses.add( |
| 727 |
responses.GET, |
| 728 |
f"{V2_URL}/hosts/{ips[-1]}", |
| 729 |
status=429, |
| 730 |
json=RATE_LIMIT_ERROR_JSON, |
| 731 |
) |
| 732 |
expected[ips[-1]] = { |
| 733 |
"error": "429 (rate_limit_exceeded): Rate limit exceeded. See https://search.censys.io/account for rate limit details." |
| 734 |
} |
| 735 |
|
| 736 |
query = self.api.search("services.service_name: HTTP", per_page=test_per_page) |
| 737 |
results = query.view_all() |
| 738 |
assert results == expected |
| 739 |
|
| 740 |
def test_view_host_names(self): |
| 741 |
self.responses.add( |
| 742 |
responses.GET, |
| 743 |
f"{V2_URL}/hosts/{TEST_HOST}/names", |
| 744 |
status=200, |
| 745 |
json=VIEW_HOST_NAMES_JSON, |
| 746 |
) |
| 747 |
results = self.api.view_host_names(TEST_HOST) |
| 748 |
assert results == VIEW_HOST_NAMES_JSON["result"]["names"] |
| 749 |
|
| 750 |
def test_view_host_names_pages(self): |
| 751 |
self.responses.add( |
| 752 |
responses.GET, |
| 753 |
f"{V2_URL}/hosts/{TEST_HOST}/names?per_page=50", |
| 754 |
status=200, |
| 755 |
json=VIEW_HOST_NAMES_JSON, |
| 756 |
) |
| 757 |
results = self.api.view_host_names(TEST_HOST, per_page=50) |
| 758 |
assert results == VIEW_HOST_NAMES_JSON["result"]["names"] |
| 759 |
|
| 760 |
def test_host_metadata(self): |
| 761 |
self.responses.add( |
| 762 |
responses.GET, |
| 763 |
f"{V2_URL}/metadata/hosts", |
| 764 |
status=200, |
| 765 |
json=HOST_METADATA_JSON, |
| 766 |
) |
| 767 |
results = self.api.metadata() |
| 768 |
assert results == HOST_METADATA_JSON["result"] |
| 769 |
|
| 770 |
def test_view_host_diff(self): |
| 771 |
self.responses.add( |
| 772 |
responses.GET, |
| 773 |
f"{V2_URL}/hosts/{TEST_HOST}/diff", |
| 774 |
status=200, |
| 775 |
json=VIEW_HOST_DIFF_JSON, |
| 776 |
) |
| 777 |
results = self.api.view_host_diff(TEST_HOST) |
| 778 |
assert results == VIEW_HOST_DIFF_JSON["result"] |
| 779 |
|
| 780 |
@pytest.mark.parametrize( |
| 781 |
("kwargs", "query_params"), |
| 782 |
[ |
| 783 |
({"ip_b": "1.1.1.2"}, "ip_b=1.1.1.2"), |
| 784 |
( |
| 785 |
{ |
| 786 |
"at_time": datetime.date(2021, 7, 1), |
| 787 |
"at_time_b": datetime.date(2021, 7, 31), |
| 788 |
}, |
| 789 |
"at_time=2021-07-01T00%3A00%3A00.000000Z&at_time_b=2021-07-31T00%3A00%3A00.000000Z", |
| 790 |
), |
| 791 |
], |
| 792 |
) |
| 793 |
def test_view_host_diff_params(self, kwargs: dict, query_params: str): |
| 794 |
self.responses.add( |
| 795 |
responses.GET, |
| 796 |
f"{V2_URL}/hosts/{TEST_HOST}/diff?{query_params}", |
| 797 |
status=200, |
| 798 |
json=VIEW_HOST_DIFF_JSON, |
| 799 |
) |
| 800 |
results = self.api.view_host_diff(TEST_HOST, **kwargs) |
| 801 |
assert results == VIEW_HOST_DIFF_JSON["result"] |
| 802 |
|
| 803 |
@pytest.mark.parametrize( |
| 804 |
("kwargs", "query_params"), |
| 805 |
[ |
| 806 |
({}, ""), |
| 807 |
({"per_page": 50}, "per_page=50"), |
| 808 |
( |
| 809 |
{ |
| 810 |
"start_time": datetime.date(2021, 7, 1), |
| 811 |
"end_time": datetime.date(2021, 7, 31), |
| 812 |
}, |
| 813 |
"start_time=2021-07-01T00%3A00%3A00.000000Z&end_time=2021-07-31T00%3A00%3A00.000000Z", |
| 814 |
), |
| 815 |
( |
| 816 |
{"cursor": "nextCursor", "reversed": True}, |
| 817 |
"cursor=nextCursor&reversed=True", |
| 818 |
), |
| 819 |
], |
| 820 |
) |
| 821 |
def test_view_host_events_params(self, kwargs: dict, query_params: str): |
| 822 |
url = f"{V2_URL}/experimental/hosts/{TEST_HOST}/events" |
| 823 |
if query_params: |
| 824 |
url += "?" + query_params |
| 825 |
self.responses.add( |
| 826 |
responses.GET, |
| 827 |
url, |
| 828 |
status=200, |
| 829 |
json=VIEW_HOST_EVENTS_JSON, |
| 830 |
) |
| 831 |
results = self.api.view_host_events(TEST_HOST, **kwargs) |
| 832 |
assert results == VIEW_HOST_EVENTS_JSON["result"] |
| 833 |
|
| 834 |
@pytest.mark.parametrize( |
| 835 |
("kwargs", "query_params"), |
| 836 |
[ |
| 837 |
({}, {"per_page": 100}), |
| 838 |
({"per_page": 50}, {"per_page": 50}), |
| 839 |
( |
| 840 |
{ |
| 841 |
"start_time": datetime.date(2021, 7, 1), |
| 842 |
}, |
| 843 |
{"per_page": 100, "start_time": "2021-07-01T00:00:00.000000Z"}, |
| 844 |
), |
| 845 |
( |
| 846 |
{"cursor": "nextCursor"}, |
| 847 |
{"per_page": 100, "cursor": "nextCursor"}, |
| 848 |
), |
| 849 |
], |
| 850 |
) |
| 851 |
def test_view_host_certificates(self, kwargs: dict, query_params: dict): |
| 852 |
self.responses.add( |
| 853 |
responses.GET, |
| 854 |
f"{V2_URL}/hosts/{TEST_HOST}/certificates", |
| 855 |
status=200, |
| 856 |
json=VIEW_HOST_EVENTS_JSON, |
| 857 |
match=[matchers.query_param_matcher(query_params)], |
| 858 |
) |
| 859 |
results = self.api.view_host_certificates(TEST_HOST, **kwargs) |
| 860 |
assert results == VIEW_HOST_EVENTS_JSON["result"] |