Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechromancer committed Jan 29, 2024
1 parent 724d955 commit f8ede5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cloudcheck/cidr.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class CidrRanges:
https://github.com/yl2chen/cidranger
"""

def __init__(self, ranges):
def __init__(self, ranges=None):
self.cidrs = set()
if ranges is not None:
self.update(ranges)

def update(self, ranges):
for r in ranges:
self.cidrs.add(ipaddress.ip_network(r, strict=False))

Expand Down
8 changes: 5 additions & 3 deletions cloudcheck/providers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CloudProviderJSON(BaseModel):
regexes: Dict[str, List[str]] = {}
provider_type: str = "cloud"
ips_url: str = ""
asns: List[str] = []
asns: List[int] = []

@field_validator("cidrs")
@classmethod
Expand All @@ -51,12 +51,13 @@ class BaseCloudProvider:
def __init__(self, j, httpx_client=None):
self._httpx_client = httpx_client
self._log = None
self.ranges = CidrRanges()
if j is not None:
p = CloudProviderJSON(**j)
self.domains = set(
[d.lower() for d in set(list(self.domains) + list(p.domains))]
)
self.ranges = CidrRanges(p.cidrs)
self.ranges.update(p.cidrs)
self.last_updated = p.last_updated

self._bucket_name_regex = re.compile("^" + self.bucket_name_regex + "$", re.I)
Expand All @@ -80,7 +81,7 @@ async def update(self):
)
ranges = self.parse_response(response)
if ranges:
self.ranges.cidrs.update(ranges)
self.ranges.update(ranges)
except Exception as e:
log.warning(f"Error retrieving {self.ips_url}: {e}")
log.warning(traceback.format_exc())
Expand Down Expand Up @@ -111,6 +112,7 @@ def to_json(self):
regexes=self.regexes,
provider_type=self.provider_type,
ips_url=self.ips_url,
asns=self.asns,
bucket_name_regex=self.bucket_name_regex,
).dict()

Expand Down

0 comments on commit f8ede5b

Please sign in to comment.