Skip to content

Commit

Permalink
feat: improve user-agent details
Browse files Browse the repository at this point in the history
  • Loading branch information
kshychko committed Jan 14, 2024
1 parent bd95688 commit bb01f9e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Binary file modified alb-s3-sqs-es/alb_s3/geoip/GeoLite2-City_20210518/GeoLite2-City.mmdb
Binary file not shown.
28 changes: 20 additions & 8 deletions alb-s3-sqs-es/alb_s3/ingester/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,22 @@ def __init__(self, log_msg):
if "user_agent" in self.message:
user_agent = parse(self.message["user_agent"])
user_agent_details = {}
user_agent_details["browser"] = user_agent.get_browser()
user_agent_details["os"] = user_agent.get_os()
user_agent_details["device"] = user_agent.get_device()
user_agent_details["browser"] = user_agent.browser.family
user_agent_details["browser_version"] = user_agent.browser.version_string
user_agent_details["os"] = user_agent.os.family
user_agent_details["os_version"] = user_agent.os.version_string
user_agent_details["device_family"] = user_agent.device.family
user_agent_details["device_brand"] = user_agent.device.brand
user_agent_details["device_model"] = user_agent.device.model
user_agent_details["is_pc"] = user_agent.is_pc
user_agent_details["is_bot"] = user_agent.is_bot
user_agent_details["is_mobile"] = user_agent.is_mobile
user_agent_details["is_tablet"] = user_agent.is_tablet
user_agent_details["user_agent"] = user_agent.ua_string

self.message["user_agent_details"] = user_agent_details
#pop the old key
del self.message['user_agent']

# GeoIP Lookup
if self.message["client_ip"] is not None:
Expand Down Expand Up @@ -108,11 +119,12 @@ def __init__(self, log_msg):
] = geo_ip_response.subdivisions.most_specific.name

if geo_ip_response.location is not None:
location = {
"lon": geo_ip_response.location.longitude,
"lat": geo_ip_response.location.latitude,
}
geoip_details["location"] = location
if geo_ip_response.location.longitude is not None:
location = {
"lon": geo_ip_response.location.longitude,
"lat": geo_ip_response.location.latitude,
}
geoip_details["location"] = location
self.message["client_geoip"] = geoip_details

# Expand Request details
Expand Down
15 changes: 11 additions & 4 deletions alb-s3-sqs-es/tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ def test_app_loadbalancer_log_https_useragent(loadbalancer_https_entry):
msg = loadbalancer_https_entry.payload()

assert msg['type'] == 'https'
assert msg['user_agent'].startswith('Mozilla/5.0 (Macintosh;')
assert msg['user_agent_details']['browser'] == 'Firefox 88.0'
assert msg['user_agent_details']['os'] == 'Mac OS X 10.15'
assert msg['user_agent_details']['device'] == 'PC'
assert msg['user_agent_details']['user_agent'].startswith('Mozilla/5.0 (Macintosh;')
assert msg['user_agent_details']['browser'] == 'Firefox'
assert msg['user_agent_details']['browser_version'] == '88.0'
assert msg['user_agent_details']['os'] == 'Mac OS X'
assert msg['user_agent_details']['os_version'] == '10.15'
assert msg['user_agent_details']['device_family'] == 'Mac'
assert msg['user_agent_details']['device_brand'] == 'Apple'
assert msg['user_agent_details']['device_model'] == 'Mac'
assert msg['user_agent_details']['is_pc'] is True
assert msg['user_agent_details']['is_bot'] is False
assert msg['user_agent_details']['is_tablet'] is False
assert msg['user_agent_details']['is_mobile'] is False

0 comments on commit bb01f9e

Please sign in to comment.