mirror of https://github.com/dcarrillo/digaws.git
Compare commits
24 Commits
Author | SHA1 | Date |
---|---|---|
Daniel Carrillo | e5685511ba | |
Daniel Carrillo | d1d01b242c | |
Daniel Carrillo | e2f0f4f63c | |
Daniel Carrillo | b657c4a4d3 | |
Daniel Carrillo | 87618a960d | |
Daniel Carrillo | b1f48d5534 | |
Daniel Carrillo | ca98c678b2 | |
Daniel Carrillo | 5b58df2b3a | |
Daniel Carrillo | ad80450442 | |
Daniel Carrillo | 61970f6679 | |
Daniel Carrillo | 613727ef5a | |
Daniel Carrillo | a3517a25ab | |
Daniel Carrillo | 204ec8f9ff | |
Daniel Carrillo | ef31af865a | |
Daniel Carrillo | fda5957a04 | |
Daniel Carrillo | 0b2f98fa4f | |
Daniel Carrillo | e6756888e7 | |
Daniel Carrillo | 3e03e532fd | |
Daniel Carrillo | ae7bf28702 | |
Daniel Carrillo | 971ead7ed2 | |
Daniel Carrillo | d9c8738e9a | |
Daniel Carrillo | 1bd1f91e45 | |
Daniel Carrillo | 352b9da211 | |
Daniel Carrillo | 4edcc42440 |
|
@ -4,6 +4,8 @@ on:
|
|||
push:
|
||||
branches:
|
||||
- main
|
||||
tags:
|
||||
- '*'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
|
@ -12,12 +14,12 @@ jobs:
|
|||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest, windows-latest]
|
||||
python-version: [3.7, 3.8, 3.9]
|
||||
python-version: ["3.8", "3.9", "3.10", "3.11"]
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v2
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
|
||||
|
@ -36,19 +38,20 @@ jobs:
|
|||
|
||||
build_publish:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'push'
|
||||
needs: tests
|
||||
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Python 3.8
|
||||
uses: actions/setup-python@v2
|
||||
- name: Set up Python 3.11
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: 3.8
|
||||
python-version: 3.11
|
||||
|
||||
- name: Install tools
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install twine
|
||||
pip install twine wheel
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
|
|
33
README.md
33
README.md
|
@ -1,25 +1,36 @@
|
|||
# digaws
|
||||
|
||||
The digaws lookup tool displays information for a given IP address (v4 o v6) or a CIDR, sourced from the AWS official IP ranges.
|
||||
The digaws lookup tool displays information for a given IP address (both v4 and v6) or a CIDR, sourced from the AWS official IP ranges.
|
||||
In order to save bandwidth and time this tool requests the [AWS IP ranges](https://ip-ranges.amazonaws.com/ip-ranges.json) and keeps
|
||||
a cached version until a new version is published.
|
||||
|
||||
[![PyPI version](https://badge.fury.io/py/digaws.svg)](https://pypi.org/project/digaws/)
|
||||
[![CI](https://github.com/dcarrillo/digaws/workflows/CI/badge.svg)](https://github.com/dcarrillo/digaws/actions)
|
||||
|
||||
## Requirements
|
||||
|
||||
Python >= 3.7
|
||||
Python >= 3.8
|
||||
|
||||
Tests are verified on Linux, macos and Windows.
|
||||
|
||||
## Install
|
||||
|
||||
### Using [pipx](https://pypa.github.io/pipx/#install-pipx) (this is the preferred way)
|
||||
|
||||
```bash
|
||||
pip install digaws
|
||||
pipx install digaws
|
||||
```
|
||||
|
||||
### Using pip
|
||||
|
||||
```bash
|
||||
pip install digaws --user
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```text
|
||||
usage: digaws [-h] [--output <plain|json>] [--debug] <ip address|cidr> [<ip address|cidr> ...]
|
||||
usage: digaws [-h] [--output <plain|json>] [--output-fields [{prefix,region,service,network_border_group} ...]] [--debug] [--version] <ip address|cidr> [<ip address|cidr> ...]
|
||||
|
||||
Look up canonical information for AWS IP addresses and networks
|
||||
|
||||
|
@ -30,7 +41,10 @@ optional arguments:
|
|||
-h, --help show this help message and exit
|
||||
--output <plain|json>
|
||||
Formatting style for command output, by default plain
|
||||
--output-fields [{prefix,region,service,network_border_group} ...]
|
||||
Print only the given fields
|
||||
--debug Enable debug
|
||||
--version show program's version number and exit
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
@ -104,3 +118,14 @@ Network border group: sa-east-1
|
|||
}
|
||||
]
|
||||
```
|
||||
|
||||
- Choose output fields
|
||||
|
||||
```text
|
||||
~ » digaws 34.255.166.63 --output-fields service
|
||||
|
||||
Service: AMAZON
|
||||
|
||||
Service: EC2
|
||||
|
||||
```
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
__version__ = '1.0.1'
|
||||
__version__ = '1.0.6'
|
||||
__description__ = 'Look up canonical information for AWS IP addresses and networks'
|
||||
|
|
|
@ -20,6 +20,7 @@ from . import __version__
|
|||
AWS_IP_RANGES_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
|
||||
CACHE_DIR = Path(Path.home() / '.digaws')
|
||||
CACHE_FILE = CACHE_DIR / 'ip-ranges.json'
|
||||
OUTPUT_FIELDS = ['prefix', 'region', 'service', 'network_border_group']
|
||||
|
||||
logger = logging.getLogger()
|
||||
handler = logging.StreamHandler(sys.stderr)
|
||||
|
@ -87,19 +88,23 @@ class UnexpectedRequestException(Exception):
|
|||
|
||||
|
||||
class DigAWSPrettyPrinter:
|
||||
def __init__(self, data: List[Dict], output_filter: List[str] = []):
|
||||
def __init__(self, data: List[Dict], output_fields: List[str] = []):
|
||||
self.data = data
|
||||
self.output_filter = output_filter
|
||||
self.output_fields = output_fields
|
||||
|
||||
def plain_print(self) -> None:
|
||||
for prefix in self.data:
|
||||
try:
|
||||
print(f'Prefix: {prefix["ip_prefix"]}')
|
||||
except KeyError:
|
||||
print(f'IPv6 Prefix: {prefix["ipv6_prefix"]}')
|
||||
print(f'Region: {prefix["region"]}')
|
||||
print(f'Service: {prefix["service"]}')
|
||||
print(f'Network border group: {prefix["network_border_group"]}')
|
||||
if 'prefix' in self.output_fields:
|
||||
try:
|
||||
print(f'Prefix: {prefix["ip_prefix"]}')
|
||||
except KeyError:
|
||||
print(f'IPv6 Prefix: {prefix["ipv6_prefix"]}')
|
||||
if 'region' in self.output_fields:
|
||||
print(f'Region: {prefix["region"]}')
|
||||
if 'service' in self.output_fields:
|
||||
print(f'Service: {prefix["service"]}')
|
||||
if 'network_border_group' in self.output_fields:
|
||||
print(f'Network border group: {prefix["network_border_group"]}')
|
||||
print('')
|
||||
|
||||
def json_print(self) -> None:
|
||||
|
@ -110,22 +115,25 @@ class DigAWSPrettyPrinter:
|
|||
prefix_type = 'ip_prefix'
|
||||
except KeyError:
|
||||
prefix_type = 'ipv6_prefix'
|
||||
data.append(
|
||||
{
|
||||
prefix_type: str(prefix[prefix_type]),
|
||||
'region': prefix['region'],
|
||||
'service': prefix['service'],
|
||||
'network_border_group': prefix['network_border_group']
|
||||
}
|
||||
)
|
||||
if data:
|
||||
print(json.dumps(data, indent=2))
|
||||
|
||||
item_dict = {}
|
||||
if 'prefix' in self.output_fields:
|
||||
item_dict.update({prefix_type: str(prefix[prefix_type])})
|
||||
if 'region' in self.output_fields:
|
||||
item_dict.update({'region': prefix['region']})
|
||||
if 'service' in self.output_fields:
|
||||
item_dict.update({'service': prefix['service']})
|
||||
if 'network_border_group' in self.output_fields:
|
||||
item_dict.update({'network_border_group': prefix['network_border_group']})
|
||||
data.append(item_dict)
|
||||
|
||||
print(json.dumps(data, indent=2))
|
||||
|
||||
|
||||
class DigAWS():
|
||||
def __init__(self, ip_ranges: Dict, output: str = 'plain', output_filter: List[str] = []):
|
||||
def __init__(self, *, ip_ranges: Dict, output: str = 'plain', output_fields: List[str] = []):
|
||||
self.output = output
|
||||
self.output_filter = output_filter
|
||||
self.output_fields = output_fields
|
||||
self.ip_prefixes = [
|
||||
{
|
||||
'ip_prefix': ipaddress.IPv4Network(prefix['ip_prefix']),
|
||||
|
@ -148,7 +156,7 @@ class DigAWS():
|
|||
def lookup(self, address: str) -> DigAWSPrettyPrinter:
|
||||
return DigAWSPrettyPrinter(
|
||||
self._lookup_data(address),
|
||||
self.output_filter
|
||||
self.output_fields
|
||||
)
|
||||
|
||||
def _lookup_data(self, address: str) -> List[Dict]:
|
||||
|
@ -173,7 +181,7 @@ class DigAWS():
|
|||
data = [prefix for prefix in self.ipv6_prefixes
|
||||
if addr.subnet_of(prefix['ipv6_prefix'])]
|
||||
except (ipaddress.AddressValueError, ValueError):
|
||||
raise(ValueError(f'Wrong IP or CIDR format: {address}'))
|
||||
raise ValueError(f'Wrong IP or CIDR format: {address}')
|
||||
|
||||
return data
|
||||
|
||||
|
@ -193,6 +201,15 @@ def arguments_parser() -> argparse.ArgumentParser:
|
|||
default='plain',
|
||||
help='Formatting style for command output, by default %(default)s'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--output-fields',
|
||||
nargs='*',
|
||||
choices=OUTPUT_FIELDS,
|
||||
required=False,
|
||||
dest='output_fields',
|
||||
default=OUTPUT_FIELDS,
|
||||
help='Print only the given fields'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--debug',
|
||||
action='store_true',
|
||||
|
@ -225,7 +242,7 @@ def main():
|
|||
|
||||
try:
|
||||
ip_ranges = get_aws_ip_ranges()
|
||||
dig = DigAWS(ip_ranges)
|
||||
dig = DigAWS(ip_ranges=ip_ranges, output_fields=args.output_fields)
|
||||
|
||||
responses = []
|
||||
for address in args.addresses:
|
||||
|
@ -242,7 +259,7 @@ def main():
|
|||
for response in responses:
|
||||
joined += response.data
|
||||
|
||||
DigAWSPrettyPrinter(joined).json_print()
|
||||
DigAWSPrettyPrinter(joined, args.output_fields).json_print()
|
||||
except (
|
||||
RequestException,
|
||||
ipaddress.AddressValueError,
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
import nox
|
||||
|
||||
|
||||
nox.options.sessions = ['lint', 'typing', 'tests']
|
||||
locations = ['noxfile.py', 'setup.py', 'digaws/', 'tests/']
|
||||
|
||||
lint_common_args = ['--max-line-length', '120']
|
||||
mypy_args = ['--ignore-missing-imports']
|
||||
mypy_args = ['--ignore-missing-imports', '--install-types', '--non-interactive']
|
||||
|
||||
pytest_args = ['--cov=digaws', '--cov-report=', 'tests/']
|
||||
coverage_args = ['report', '--show-missing', '--fail-under=80']
|
||||
|
||||
|
|
4
setup.py
4
setup.py
|
@ -17,7 +17,7 @@ setup(
|
|||
url='http://github.com/dcarrillo/digaws',
|
||||
author='Daniel Carrillo',
|
||||
author_email='daniel.carrillo@gmail.com',
|
||||
license='MIT',
|
||||
license='Apache Software License',
|
||||
packages=['digaws'],
|
||||
zip_safe=False,
|
||||
classifiers=[
|
||||
|
@ -25,7 +25,7 @@ setup(
|
|||
'License :: OSI Approved :: Apache Software License',
|
||||
'Operating System :: OS Independent',
|
||||
],
|
||||
python_requires='>=3.7',
|
||||
python_requires='>=3.8',
|
||||
entry_points={
|
||||
'console_scripts': ['digaws=digaws.digaws:main']
|
||||
},
|
||||
|
|
|
@ -111,6 +111,18 @@ RESPONSE_JSON_PRINT = '''[
|
|||
]
|
||||
'''
|
||||
|
||||
RESPONSE_JSON_FIELDS_PRINT = '''[
|
||||
{
|
||||
"service": "ROUTE53_HEALTHCHECKS",
|
||||
"network_border_group": "us-west-2"
|
||||
},
|
||||
{
|
||||
"service": "EC2",
|
||||
"network_border_group": "us-west-2"
|
||||
}
|
||||
]
|
||||
'''
|
||||
|
||||
RESPONSE_JSON_JOINED_PRINT = '''[
|
||||
{
|
||||
"ip_prefix": "52.94.76.0/22",
|
||||
|
|
|
@ -11,7 +11,10 @@ import tests
|
|||
|
||||
@pytest.fixture
|
||||
def test_dig():
|
||||
return digaws.DigAWS(json.loads(tests.AWS_IP_RANGES))
|
||||
return digaws.DigAWS(ip_ranges=json.loads(
|
||||
tests.AWS_IP_RANGES),
|
||||
output_fields=digaws.OUTPUT_FIELDS
|
||||
)
|
||||
|
||||
|
||||
def test_cli(capsys):
|
||||
|
@ -44,6 +47,25 @@ def test_cli_invocation(capsys, mocker):
|
|||
assert out == tests.RESPONSE_JSON_JOINED_PRINT
|
||||
|
||||
|
||||
def test_cli_output_plain_fields_invocation(capsys, mocker):
|
||||
sys.argv = ['digaws', '52.94.76.0/22', '--output=plain', '--output-fields', 'region']
|
||||
mocker.patch('digaws.digaws.get_aws_ip_ranges', return_value=json.loads(tests.AWS_IP_RANGES))
|
||||
digaws.main()
|
||||
out, _ = capsys.readouterr()
|
||||
|
||||
assert out == 'Region: us-west-2\n\n'
|
||||
|
||||
|
||||
def test_cli_output_json_fields_invocation(capsys, mocker):
|
||||
sys.argv = ['digaws', '2600:1f14:fff:f810:a1c1:f507:a2d1:2dd8', '--output=json',
|
||||
'--output-fields', 'service', 'network_border_group']
|
||||
mocker.patch('digaws.digaws.get_aws_ip_ranges', return_value=json.loads(tests.AWS_IP_RANGES))
|
||||
digaws.main()
|
||||
out, _ = capsys.readouterr()
|
||||
|
||||
assert out == tests.RESPONSE_JSON_FIELDS_PRINT
|
||||
|
||||
|
||||
def test_dig_aws_construct(test_dig):
|
||||
assert test_dig.ip_prefixes == tests.AWS_IPV4_RANGES_OBJ
|
||||
assert test_dig.ipv6_prefixes == tests.AWS_IPV6_RANGES_OBJ
|
||||
|
|
|
@ -84,4 +84,5 @@ def test_get_aws_ip_ranges_invalid_status(mocker, fs, create_cache_dir) -> None:
|
|||
|
||||
with pytest.raises(digaws.UnexpectedRequestException) as e:
|
||||
digaws.get_aws_ip_ranges()
|
||||
assert e.startswith('Unexpected response from')
|
||||
|
||||
assert e.match('^Unexpected response from')
|
||||
|
|
Loading…
Reference in New Issue