Skip to content

Commit

Permalink
issue-#52 Implement tox, isort, pytest, and shellcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
James Bensley committed Oct 19, 2023
1 parent 069b018 commit 3884c9c
Show file tree
Hide file tree
Showing 45 changed files with 2,114 additions and 2,119 deletions.
19 changes: 3 additions & 16 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,10 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r dnas/requirements.txt
sudo apt-get install -y whois
- name: Run the Python unit tests
sudo apt-get install -y whois shellcheck
- name: Run the tox tests
working-directory: /home/runner/work/dfz_name_and_shame/dfz_name_and_shame/
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
run: |
sudo mkdir /media/usb0
sudo chmod 777 /media/usb0
python3 dnas/tests/test_git.py -vv
python3 dnas/tests/test_bogon_asn.py -vv
python3 dnas/tests/test_bogon_ip.py -vv
python3 dnas/tests/test_mrt_archive.py -vv
python3 dnas/tests/test_mrt_archives.py -vv
python3 dnas/tests/test_mrt_entry.py -vv
python3 dnas/tests/test_mrt_getter.py -vv
python3 dnas/tests/test_mrt_parser.py -vv
python3 dnas/tests/test_mrt_splitter.py -vv
python3 dnas/tests/test_mrt_stats.py -vv
python3 dnas/tests/test_whois.py -vv
tox
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ redis/data/*
# Don't sync pycache
**/__pycache__/

# Don't sync tox cache
.tox

# Don't sync virtualenv
**/venv/
**/.venv/
25 changes: 12 additions & 13 deletions dnas/dnas/bogon_asn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,38 @@

from dnas.config import config as cfg


class bogon_asn:
"""
Class to check if an ASN is a bogon ASN (meaning reserved by an RFC,
the IETF, or IANA).
"""

@staticmethod
def is_bogon(asn: int = None) -> bool:
def is_bogon(asn: int) -> bool:
"""
Return True if ASN is a bogon ASN, else False.
"""
if type(asn) != int:
raise TypeError(
f"{asn} is not an int: {type(asn)}"
)
raise TypeError(f"{asn} is not an int: {type(asn)}")

if asn == 0: # RFC 7607
if asn == 0: # RFC 7607
return True
elif asn == 23456: # RFC 4893
elif asn == 23456: # RFC 4893
return True
elif asn in range(64496, 64512): # RFC 5398
elif asn in range(64496, 64512): # RFC 5398
return True
elif asn in range(65536, 65552): # RFC 5398
elif asn in range(65536, 65552): # RFC 5398
return True
elif asn in range(64512, 65535): # RFC 6996
elif asn in range(64512, 65535): # RFC 6996
return True
elif asn in range(4200000000, 4294967296): # RFC 6996
elif asn in range(4200000000, 4294967296): # RFC 6996
return True
elif asn == 65535: # RFC 6996
elif asn == 65535: # RFC 6996
return True
elif asn == 4294967295: # RFC 6996
elif asn == 4294967295: # RFC 6996
return True
elif asn in range(65552, 131072): # IANA reserved
elif asn in range(65552, 131072): # IANA reserved
return True
else:
return False
87 changes: 43 additions & 44 deletions dnas/dnas/bogon_attr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict
import typing


class bogon_attr:
"""
Expand All @@ -7,57 +8,55 @@ class bogon_attr:

# https://www.iana.org/assignments/bgp-parameters/bgp-parameters.xhtml
known_attrs = {
1: "ORIGIN", # RFC4271
2: "AS_PATH", #RFC4271
3: "NEXT_HOP", # RFC4271
4: "MULTI_EXIT_DISC", # RFC4271
5: "LOCAL_PREF", # RFC4271
6: "ATOMIC_AGGREGATE", # RFC4271
7: "AGGREGATOR", # RFC4271
8: "COMMUNITY", # RFC1997
9: "ORIGINATOR_ID", # RFC4456
10: "CLUSTER_LIST", # RFC4456
#11: "DPA", # Deprecated
#12: "ADVERTISER", # Deprecated
#13: "RCID_PATH/CLUSTER_ID", # Deprecated
14: "MP_REACH_NLRI", # RFC4760
15: "MP_UNREACH_NLRI", # RFC4760
16: "EXTENDED COMMUNITIES", # RFC4360
17: "AS4_PATH", # RFC6793
18: "AS4_AGGREGATOR", # RFC6793
#19: "SAFI Specific Attribute", # Deprecated
#20: "Connector Attribute", # Deprecated
#21: "AS_PATHLIMIT" # Deprecated
22: "PMSI_TUNNEL", # RFC6514
23: "Tunnel Encapsulation Attribute", # RFC5512
24: "Traffic Engineering", # RFC5543
25: "IPv6 Address Specific Extended Community", # RFC5701
26: "AIGP", # RFC7311
27: "PE Distinguisher Labels", # RFC6514
#28: "BGP Entropy Label Capability Attribute", # Deprecated
29: "BGP-LS Attribute", # RFC7752
32: "LARGE_COMMUNITY", # RFC8092
33: "BGPsec_Path", # RFC8205
34:"BGP Community Container Attribute", # draft-ietf-idr-wide-bgp-communities
35:"Only to Customer", # draft-ietf-idr-bgp-open-policy
36:"BGP Domain Path", # draft-ietf-bess-evpn-ipvpn-interworking
37: "SFP attribute", # RFC9015
38: "BFD Discriminator", # RFC9026
40: "BGP Prefix-SID", # RFC8669
128: "ATTR_SET", # RFC6368
255: "Reserved for development", #RFC2042
1: "ORIGIN", # RFC4271
2: "AS_PATH", # RFC4271
3: "NEXT_HOP", # RFC4271
4: "MULTI_EXIT_DISC", # RFC4271
5: "LOCAL_PREF", # RFC4271
6: "ATOMIC_AGGREGATE", # RFC4271
7: "AGGREGATOR", # RFC4271
8: "COMMUNITY", # RFC1997
9: "ORIGINATOR_ID", # RFC4456
10: "CLUSTER_LIST", # RFC4456
# 11: "DPA", # Deprecated
# 12: "ADVERTISER", # Deprecated
# 13: "RCID_PATH/CLUSTER_ID", # Deprecated
14: "MP_REACH_NLRI", # RFC4760
15: "MP_UNREACH_NLRI", # RFC4760
16: "EXTENDED COMMUNITIES", # RFC4360
17: "AS4_PATH", # RFC6793
18: "AS4_AGGREGATOR", # RFC6793
# 19: "SAFI Specific Attribute", # Deprecated
# 20: "Connector Attribute", # Deprecated
# 21: "AS_PATHLIMIT" # Deprecated
22: "PMSI_TUNNEL", # RFC6514
23: "Tunnel Encapsulation Attribute", # RFC5512
24: "Traffic Engineering", # RFC5543
25: "IPv6 Address Specific Extended Community", # RFC5701
26: "AIGP", # RFC7311
27: "PE Distinguisher Labels", # RFC6514
# 28: "BGP Entropy Label Capability Attribute", # Deprecated
29: "BGP-LS Attribute", # RFC7752
32: "LARGE_COMMUNITY", # RFC8092
33: "BGPsec_Path", # RFC8205
34: "BGP Community Container Attribute", # draft-ietf-idr-wide-bgp-communities
35: "Only to Customer", # draft-ietf-idr-bgp-open-policy
36: "BGP Domain Path", # draft-ietf-bess-evpn-ipvpn-interworking
37: "SFP attribute", # RFC9015
38: "BFD Discriminator", # RFC9026
40: "BGP Prefix-SID", # RFC8669
128: "ATTR_SET", # RFC6368
255: "Reserved for development", # RFC2042
}

@staticmethod
def is_unknown(attr: int = None) -> bool:
def is_unknown(attr: int) -> bool:
"""
Return True is BGP attr ID is a unknown/bogon, else False
"""

if type(attr) != int:
raise TypeError(
f"attr is not an int: {type(attr)}"
)
raise TypeError(f"attr is not an int: {type(attr)}")

if attr in bogon_attr.known_attrs:
return False
Expand Down
39 changes: 16 additions & 23 deletions dnas/dnas/bogon_ip.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
import ipaddress
from typing import List
import typing

from dnas.config import config as cfg


class bogon_ip:
"""
Class to check if an IP subnet is a bogon address (meaning reserved by
an RFC, the IETF, or IANA).
"""

BOGON_V4_NETS: List[ipaddress.IPv4Network] = []
for bogon in cfg.BOGONS_V4:
bog_net = ipaddress.ip_network(bogon)
BOGON_V4_NETS: list[ipaddress.IPv4Network] = []
for v4_bogon in cfg.BOGONS_V4:
bog_net = ipaddress.ip_network(v4_bogon)
if type(bog_net) != ipaddress.IPv4Network:
raise TypeError(
f"v4 bogon {bogon} is not a valid IPv4 subnet: {type(bog_net)}"
f"v4 bogon {v4_bogon} is not a valid IPv4 subnet: {type(bog_net)}"
)
BOGON_V4_NETS.append(bog_net)

BOGON_V6_NETS: List[ipaddress.IPv6Network] = []
for bogon in cfg.BOGONS_V6:
bog_net = ipaddress.ip_network(bogon)
BOGON_V6_NETS: list[ipaddress.IPv6Network] = []
for v6_bogon in cfg.BOGONS_V6:
bog_net = ipaddress.ip_network(v6_bogon)
if type(bog_net) != ipaddress.IPv6Network:
raise TypeError(
f"v6 bogon {bogon} is not a valid IPv6 subnet: {type(bog_net)}"
f"v6 bogon {v6_bogon} is not a valid IPv6 subnet: {type(bog_net)}"
)
BOGON_V6_NETS.append(bog_net)

@staticmethod
def is_v4_bogon(subnet: str = None) -> bool:
def is_v4_bogon(subnet: str) -> bool:
"""
Return True if IP prefix is in a v4 bogon range, else False.
Expects CIDR notation as string.
"""
if not subnet:
raise ValueError(
f"Missing required options: subnet={subnet}"
)
raise ValueError(f"Missing required options: subnet={subnet}")

if type(subnet) != str:
raise TypeError(
f"subnet is not a string: {type(subnet)}"
)
raise TypeError(f"subnet is not a string: {type(subnet)}")

ip_net = ipaddress.ip_network(subnet)
if type(ip_net) != ipaddress.IPv4Network:
Expand All @@ -55,20 +52,16 @@ def is_v4_bogon(subnet: str = None) -> bool:
return False

@staticmethod
def is_v6_bogon(subnet: str = None) -> bool:
def is_v6_bogon(subnet: str) -> bool:
"""
Return True is IP prefix is in a v6 bogon range, else False.
Expects CIDR notation as string.
"""
if not subnet:
raise ValueError(
f"Missing required options: subnet={subnet}"
)
raise ValueError(f"Missing required options: subnet={subnet}")

if type(subnet) != str:
raise TypeError(
f"subnet is not a string: {type(subnet)}"
)
raise TypeError(f"subnet is not a string: {type(subnet)}")

ip_net = ipaddress.ip_network(subnet)
if type(ip_net) != ipaddress.IPv6Network:
Expand Down
6 changes: 6 additions & 0 deletions dnas/dnas/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import typing


class config:
Expand All @@ -23,6 +24,11 @@ class config:
TIME_FORMAT = "%Y%m%d.%H%M"
DAY_FORMAT = "%Y%m%d"

# JSON indent when exporting MRT entry to JSON
MRT_ENTRY_JSON_INDENT = 2
# JSON indent when exporting MRT stats to JSON
MRT_STATS_JSON_INDENT = 2

# Log mode, 'a'ppend or over'w'rite
LOG_MODE = "a"
# Standard logging format
Expand Down
Loading

0 comments on commit 3884c9c

Please sign in to comment.