2022-07-12 11:09:23 +01:00
|
|
|
import logging
|
|
|
|
from abc import abstractmethod
|
2024-12-06 16:08:48 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
from typing import Any, Callable, List, Optional, Tuple
|
2022-07-12 11:09:23 +01:00
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models.activity import Activity
|
|
|
|
from app.models.bridges import Bridge
|
|
|
|
from app.terraform import BaseAutomation
|
|
|
|
|
|
|
|
|
|
|
|
class BlockBridgeAutomation(BaseAutomation):
|
2022-08-12 12:24:56 +01:00
|
|
|
ips: List[str]
|
|
|
|
fingerprints: List[str]
|
|
|
|
hashed_fingerprints: List[str]
|
2022-07-12 11:09:23 +01:00
|
|
|
patterns: List[str]
|
|
|
|
|
2022-12-07 14:04:33 +00:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2022-07-12 11:09:23 +01:00
|
|
|
"""
|
|
|
|
Constructor method.
|
|
|
|
"""
|
|
|
|
self.ips = []
|
|
|
|
self.fingerprints = []
|
|
|
|
self.hashed_fingerprints = []
|
2022-11-29 11:06:23 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
2022-07-12 11:09:23 +01:00
|
|
|
|
2022-08-12 12:24:56 +01:00
|
|
|
def perform_deprecations(self, ids: List[str], bridge_select_func: Callable[[str], Optional[Bridge]]
|
2024-12-06 16:08:48 +00:00
|
|
|
) -> List[Tuple[Optional[str], Any, Any]]:
|
2022-07-12 11:09:23 +01:00
|
|
|
rotated = []
|
|
|
|
for id_ in ids:
|
|
|
|
bridge = bridge_select_func(id_)
|
2022-07-12 11:43:22 +01:00
|
|
|
if bridge is None:
|
|
|
|
continue
|
2023-02-26 13:45:57 +00:00
|
|
|
logging.debug("Found %s blocked", bridge.hashed_fingerprint)
|
2024-12-06 16:08:48 +00:00
|
|
|
if bridge.added > datetime.now(tz=timezone.utc) - timedelta(hours=3):
|
2022-07-12 11:09:23 +01:00
|
|
|
logging.debug("Not rotating a bridge less than 3 hours old")
|
|
|
|
continue
|
|
|
|
if bridge.deprecate(reason=self.short_name):
|
2023-02-26 13:45:57 +00:00
|
|
|
logging.info("Rotated %s", bridge.hashed_fingerprint)
|
|
|
|
rotated.append((bridge.fingerprint, bridge.cloud_account.provider, bridge.cloud_account.description))
|
2022-07-12 11:09:23 +01:00
|
|
|
else:
|
|
|
|
logging.debug("Not rotating a bridge that is already deprecated")
|
|
|
|
return rotated
|
|
|
|
|
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
|
|
|
self.fetch()
|
|
|
|
logging.debug("Fetch complete")
|
|
|
|
self.parse()
|
|
|
|
logging.debug("Parse complete")
|
|
|
|
rotated = []
|
|
|
|
rotated.extend(self.perform_deprecations(self.ips, get_bridge_by_ip))
|
2022-07-12 11:14:17 +01:00
|
|
|
logging.debug("Blocked by IP")
|
2022-07-12 11:09:23 +01:00
|
|
|
rotated.extend(self.perform_deprecations(self.fingerprints, get_bridge_by_fingerprint))
|
2022-07-12 11:14:17 +01:00
|
|
|
logging.debug("Blocked by fingerprint")
|
2022-09-01 16:26:19 +01:00
|
|
|
rotated.extend(self.perform_deprecations(self.hashed_fingerprints, get_bridge_by_hashed_fingerprint))
|
2022-07-12 11:14:17 +01:00
|
|
|
logging.debug("Blocked by hashed fingerprint")
|
2022-07-12 11:09:23 +01:00
|
|
|
if rotated:
|
|
|
|
activity = Activity(
|
|
|
|
activity_type="block",
|
|
|
|
text=(f"[{self.short_name}] ♻ Rotated {len(rotated)} bridges: \n"
|
2023-02-26 13:45:57 +00:00
|
|
|
+ "\n".join([f"* {fingerprint} ({provider}: {provider_description})" for fingerprint, provider, provider_description in rotated]))
|
2022-07-12 11:09:23 +01:00
|
|
|
)
|
|
|
|
db.session.add(activity)
|
|
|
|
activity.notify()
|
|
|
|
db.session.commit()
|
|
|
|
return True, ""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def fetch(self) -> None:
|
|
|
|
"""
|
|
|
|
Fetch the blocklist data. It is the responsibility of the automation task
|
|
|
|
to persist this within the object for the parse step.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def parse(self) -> None:
|
|
|
|
"""
|
|
|
|
Parse the blocklist data.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-08-12 12:24:56 +01:00
|
|
|
def get_bridge_by_ip(ip: str) -> Optional[Bridge]:
|
2022-07-12 11:09:23 +01:00
|
|
|
return Bridge.query.filter( # type: ignore[no-any-return]
|
|
|
|
Bridge.deprecated.is_(None),
|
|
|
|
Bridge.destroyed.is_(None),
|
|
|
|
Bridge.bridgeline.contains(f" {ip} ")
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
2022-08-12 12:24:56 +01:00
|
|
|
def get_bridge_by_fingerprint(fingerprint: str) -> Optional[Bridge]:
|
2022-07-12 11:09:23 +01:00
|
|
|
return Bridge.query.filter( # type: ignore[no-any-return]
|
|
|
|
Bridge.deprecated.is_(None),
|
|
|
|
Bridge.destroyed.is_(None),
|
|
|
|
Bridge.fingerprint == fingerprint
|
|
|
|
).first()
|
|
|
|
|
|
|
|
|
2022-08-12 12:24:56 +01:00
|
|
|
def get_bridge_by_hashed_fingerprint(hashed_fingerprint: str) -> Optional[Bridge]:
|
2022-07-12 11:09:23 +01:00
|
|
|
return Bridge.query.filter( # type: ignore[no-any-return]
|
|
|
|
Bridge.deprecated.is_(None),
|
|
|
|
Bridge.destroyed.is_(None),
|
|
|
|
Bridge.hashed_fingerprint == hashed_fingerprint
|
|
|
|
).first()
|