diff --git a/.github/workflows/ci-branch-main.yml b/.github/workflows/ci-branch-main.yml index c59412b..6040e95 100644 --- a/.github/workflows/ci-branch-main.yml +++ b/.github/workflows/ci-branch-main.yml @@ -7,6 +7,7 @@ on: paths-ignore: - 'README.md' - 'LICENSE.md' + - 'CHANGELOG.md' - '.gitignore' - 'renovate.json' diff --git a/.github/workflows/ci-development.yml b/.github/workflows/ci-development.yml index f31d988..382f942 100644 --- a/.github/workflows/ci-development.yml +++ b/.github/workflows/ci-development.yml @@ -7,6 +7,7 @@ on: paths-ignore: - 'README.md' - 'LICENSE.md' + - 'CHANGELOG.md' - '.gitignore' - 'renovate.json' diff --git a/.github/workflows/ci-pull-request.yml b/.github/workflows/ci-pull-request.yml index 7de9d84..c4728b2 100644 --- a/.github/workflows/ci-pull-request.yml +++ b/.github/workflows/ci-pull-request.yml @@ -4,6 +4,7 @@ on: paths-ignore: - 'README.md' - 'LICENSE.md' + - 'CHANGELOG.md' - '.gitignore' - 'renovate.json' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b6cc0c3 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# 1.1 +- Added checks to ensure API responses are as expected + +# 1.0 +- Initial release diff --git a/app/_version.py b/app/_version.py index c51437b..71a2ee0 100644 --- a/app/_version.py +++ b/app/_version.py @@ -2,4 +2,4 @@ """MODULE: Specifies app version.""" -VERSION = "1.0" +VERSION = "1.1" diff --git a/app/ip_info.py b/app/ip_info.py index ade9dde..9e41e1a 100644 --- a/app/ip_info.py +++ b/app/ip_info.py @@ -9,8 +9,13 @@ import requests def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> dict: """Retrieves information about a given IPv4 address from IP-API.com.""" api_endpoint = f"http://ip-api.com/json/{ipv4_address}" - resp = requests.get(api_endpoint).json() - return resp + try: + resp = requests.get(api_endpoint) + resp.raise_for_status() + ret = resp.json() if resp.json().get("status") == "success" else None + except (requests.exceptions.JSONDecodeError, requests.exceptions.HTTPError): + ret = None + return ret def get_autonomous_system_number(as_info: str) -> str: @@ -22,7 +27,11 @@ def get_autonomous_system_number(as_info: str) -> str: def get_prefix_information(autonomous_system: int) -> list: """Retrieves prefix information about a given autonomous system.""" api_endpoint = f"https://api.hackertarget.com/aslookup/?q={str(autonomous_system)}" - resp = requests.get(api_endpoint).text - prefixes = resp.split("\n") + try: + resp = requests.get(api_endpoint) + resp.raise_for_status() + except requests.exceptions.HTTPError: + return None + prefixes = resp.text.split("\n") prefixes.pop(0) return prefixes diff --git a/app/main.py b/app/main.py index 898afb8..6223415 100644 --- a/app/main.py +++ b/app/main.py @@ -38,6 +38,9 @@ def main(): # Get information from API ip_info = get_ip_information(ip_address) + if not ip_info: + print("ERROR: could not retrieve IP information from API.") + sys.exit(1) as_number = get_autonomous_system_number(ip_info.get("as")) # Assemble list for table generation @@ -56,7 +59,11 @@ def main(): # If wanted, get prefix information if args.prefixes: prefix_info = get_prefix_information(as_number) - table_data.append(["Prefixes", generate_prefix_string(prefix_info)]) + if not prefix_info: + print("ERROR: could not retrieve prefix information from API.") + sys.exit(1) + else: + table_data.append(["Prefixes", generate_prefix_string(prefix_info)]) print_table(table_data)