Skip to content

Commit

Permalink
Modify ipinfo 503 Response handling (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-thinkst authored Oct 30, 2024
1 parent 90578d8 commit cb19013
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 4 additions & 1 deletion canarytokens/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ def get_geoinfo_from_ip(ip: str) -> Dict[str, str]:
resp.raise_for_status()
info = resp.json()
except requests.exceptions.HTTPError as e:
log.error(f"ip info error: {e}")
if e.response.status_code == 503:
log.info(f"ip info error: {e}")
else:
log.error(f"ip info error: {e}")
# not strictly Bogon , we have no info
info = models.GeoIPBogonInfo(ip=ip, bogon=True).dict()
return info
Expand Down
22 changes: 21 additions & 1 deletion tests/units/test_queries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import datetime

from twisted.logger import LogLevel, capturedLogs
import pytest
from pydantic import EmailStr
from redis import StrictRedis
import requests

from canarytokens import queries
from canarytokens.canarydrop import Canarydrop
Expand Down Expand Up @@ -327,6 +328,25 @@ def test_get_geoinfo_is_cached():
# to modify the date and check we empty the cache and re-fetch


def test_get_geoinfo_503_response_code_handling(monkeypatch):
ip = "166.73.125.172"

def mock_get(*args, **kwargs):
response = requests.Response()
response.status_code = 503
response.headers = {"Content-Type": "text/plain"}
response._content = b"Service Unavailable"
return response

monkeypatch.setattr(requests, "get", mock_get)

with capturedLogs() as captured:
_ = queries.get_geoinfo_from_ip(ip)

assert "ip info error: 503 Server" in captured[0]["log_format"]
assert captured[0]["log_level"] == LogLevel.info


def test_mail_queue(setup_db):
details = make_token_alert_detail()
mail_key = "some_unique_key"
Expand Down

0 comments on commit cb19013

Please sign in to comment.