From 1197228650ca63a19bd44b607782e59ffeb1837e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian-Samuel=20Geb=C3=BChr?= Date: Mon, 9 Jan 2023 11:48:44 +0100 Subject: [PATCH] Add deploy option --- cli.py | 8 +++++--- models.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/cli.py b/cli.py index 1f4282f..4291e3b 100644 --- a/cli.py +++ b/cli.py @@ -62,9 +62,11 @@ def cli(): remote_blocklist = blocklist_json_to_instances(json.load(f)) else: remote_blocklist = load_remote_blocklist(server=args.server, token=args.token) - - Instance.show_diff(local_blocklist, remote_blocklist) - + if args.action == "diff": + Instance.show_diffs(local_blocklist, remote_blocklist) + elif args.action == "deploy": + diffs = Instance.list_diffs(local_blocklist, remote_blocklist) + Instance.apply_blocks_from_diff(diffs, args.server, args.token) if __name__ == "__main__": cli() diff --git a/models.py b/models.py index f3b9a07..b97b5ff 100644 --- a/models.py +++ b/models.py @@ -1,3 +1,6 @@ +import requests + + class Instance: def __init__(self, instance_dict): """If obfuscate, reject_media or reject_reports are not specified default to False""" @@ -28,7 +31,9 @@ class Instance: self.severity = instance_dict["severity"] self.public_comment = instance_dict["public_comment"] self.private_comment = instance_dict["private_comment"] - self.remote = True + self.obfuscate = instance_dict["obfuscate"] + self.reject_media = instance_dict["reject_media"] + self.reject_reports = instance_dict["reject_reports"] def parse_local_block(self, instance_dict): self.name = instance_dict["name"] @@ -36,13 +41,40 @@ class Instance: self.severity = instance_dict["severity"] self.public_comment = instance_dict["public_comment"] self.private_comment = instance_dict["private_comment"] - self.remote = False + try: + self.obfuscate = instance_dict["obfuscate"] + except KeyError: + pass + try: + self.reject_media = instance_dict["reject_media"] + except KeyError: + pass + try: + self.reject_reports = instance_dict["reject_reports"] + except KeyError: + pass - def apply(self, instance, token): - pass + def apply(self, server, token, block_id=None): + headers = { + f'Authorization': f'Bearer {token}', + } + data = {"domain": self.domain, + "severity": self.severity, + "reject_media": self.reject_media, + "reject_reports": self.reject_reports, + "private_comment": self.private_comment, + "public_comment": self.public_comment, + "obfuscate": self.obfuscate} + """If no id is given add a new block, else update the existing block""" + if block_id is None: + response = requests.post(f'https://{server}/api/v1/admin/domain_blocks', data=data, headers=headers) + else: + response = requests.post(f'https://{server}/api/v1/admin/domain_blocks/{block_id}', data=data, headers=headers) + if response.status_code != 200: + raise ConnectionError(f"Could not apply block ({response.status_code}: {response.reason})") @staticmethod - def list_diff(local_blocklist, remote_blocklist): + def list_diffs(local_blocklist, remote_blocklist): diffs = [] for local_instance in local_blocklist: instance_found = False @@ -65,7 +97,17 @@ class Instance: return diffs @staticmethod - def show_diff(local_blocklist, remote_blocklist): + def apply_blocks_from_diff(diffs, server, token): + for diff in diffs: + if diff["local"] is None: + pass + elif diff["remote"] is None: + diff["local"].apply(server, token) + else: + diff["local"].apply(server, token, block_id=diff["remote"].id) + + @staticmethod + def show_diffs(local_blocklist, remote_blocklist): from rich.table import Table from rich.console import Console table = Table(title="Differences", expand=True, show_lines=True) @@ -73,7 +115,7 @@ class Instance: table.add_column("Domain", style="cyan") table.add_column("Current remote status", style="magenta") table.add_column("Local status", style="green") - diffs = Instance.list_diff(local_blocklist, remote_blocklist) + diffs = Instance.list_diffs(local_blocklist, remote_blocklist) for diff in diffs: if diff["local"] is None: table.add_row(diff["remote"].domain, None, diff["remote"].status_str()) @@ -82,4 +124,4 @@ class Instance: else: table.add_row(diff["local"].domain, diff["local"].status_str(), diff["remote"].status_str()) console = Console() - console.print(table) \ No newline at end of file + console.print(table)