From 04f1a90745991a0402f15b3ff292b1225ececec1 Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 14 Aug 2021 01:30:14 +0200 Subject: [PATCH 1/3] cli commands to add and remove keys --- hubgrep_indexer/cli_blueprint/hosters.py | 98 +++++++++++++++++++---- hubgrep_indexer/models/hosting_service.py | 20 +++++ tests/lib/models/test_hosting_service.py | 38 +++++++++ 3 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 tests/lib/models/test_hosting_service.py diff --git a/hubgrep_indexer/cli_blueprint/hosters.py b/hubgrep_indexer/cli_blueprint/hosters.py index c542fcf..336d9f3 100644 --- a/hubgrep_indexer/cli_blueprint/hosters.py +++ b/hubgrep_indexer/cli_blueprint/hosters.py @@ -9,12 +9,23 @@ logger = logging.getLogger(__name__) +def _release_api_key(hosting_service: HostingService, api_key: str): + machine_id = state_manager.remove_machine_api_key( + hosting_service_id=hosting_service.id, api_key=api_key + ) + return machine_id + + @cli_bp.cli.command(help="export hosting_service objects as json (printed out)") -@click.option('--include-exports', '-e', is_flag=True) +@click.option("--include-exports", "-e", is_flag=True) def export_hosters(include_exports: bool = False): services = [] for hosting_service in HostingService.query.all(): - services.append(hosting_service.to_dict(include_secrets=True, include_exports=include_exports)) + services.append( + hosting_service.to_dict( + include_secrets=True, include_exports=include_exports + ) + ) print(json.dumps(services, indent=2)) @@ -32,7 +43,7 @@ def import_hosters(json_path): try: hosting_service = HostingService.from_dict(hoster) if not HostingService.query.filter_by( - api_url=hosting_service.api_url + api_url=hosting_service.api_url ).first(): logger.info(f"adding {hosting_service.api_url}") db.session.add(hosting_service) @@ -48,30 +59,89 @@ def import_hosters(json_path): def release_api_key(api_key): lines = [] for hosting_service in HostingService.query.all(): - if isinstance(hosting_service.api_keys, list) and api_key in hosting_service.api_keys: - machine_id = state_manager.remove_machine_api_key(hosting_service_id=hosting_service.id, api_key=api_key) + if api_key in hosting_service.api_keys: + machine_id = _release_api_key(hosting_service, api_key) if machine_id is not None: # since we search, if for whatever reason there are duplicate api_keys, multiple will be released # it would then be good to know that this happened - lines.append(f"- released api_key from machine_id: {machine_id} for {hosting_service}") + lines.append( + f"- released api_key from machine_id: {machine_id} for {hosting_service}" + ) if len(lines) > 0: print("\n".join(lines)) else: print("- no active api-key to release was found! -") -@cli_bp.cli.command(help="print all currently active api_keys (attached to a machine_id)") +@cli_bp.cli.command( + help="print all currently active api_keys (attached to a machine_id)" +) def active_api_keys(): lines = [] for hosting_service in HostingService.query.all(): - if isinstance(hosting_service.api_keys, list): - for api_key in hosting_service.api_keys: - if state_manager.is_api_key_active(hosting_service_id=hosting_service.id, api_key=api_key): - machine_id = state_manager.get_machine_id_by_api_key(hosting_service_id=hosting_service.id, - api_key=api_key) - lines.append( - f"- machine_id: {machine_id} holds api_key: {api_key} for {hosting_service}") + for api_key in hosting_service.api_keys: + if state_manager.is_api_key_active( + hosting_service_id=hosting_service.id, api_key=api_key + ): + machine_id = state_manager.get_machine_id_by_api_key( + hosting_service_id=hosting_service.id, api_key=api_key + ) + lines.append( + f"- machine_id: {machine_id} holds api_key: {api_key} for {hosting_service}" + ) if len(lines) > 0: print("\n- ".join(lines)) else: print("- no currently active api_keys! -") + + +@cli_bp.cli.command() +def list_api_keys(): + for hosting_service in HostingService.query.all(): + print(hosting_service.api_url) + for api_key in hosting_service.api_keys: + machine_id = state_manager.get_machine_id_by_api_key( + hosting_service_id=hosting_service.id, api_key=api_key + ) + print(f"\t{api_key} - used by {machine_id}") + + +@cli_bp.cli.command(help="add api key") +def add_api_key(api_url, api_key): + hosting_service = HostingService.query.filter_by(api_url=api_url).first() + if not hosting_service: + print(f"couldnt find api url {api_url}!") + exit() + + if api_key in hosting_service.api_keys: + print("hoster already has this api key!") + exit() + + hosting_service.add_api_key(api_key) + print("key added!") + + +@cli_bp.cli.command(help="delete an api key") +def delete_api_key(api_url, api_key): + hosting_service = HostingService.query.filter_by(api_url=api_url).first() + if not hosting_service: + print(f"couldnt find api url {api_url}!") + exit() + + if api_key not in hosting_service.api_keys: + print("hoster doesnt have this api key!") + exit() + + machine_id = state_manager.get_machine_id_by_api_key( + hosting_service_id=hosting_service.id, api_key=api_key + ) + if machine_id is not None: + if not click.confirm("api key is in use. really delete?"): + print("not doing it then...") + exit(1) + + print("released api key") + _release_api_key(hosting_service, api_key) + + hosting_service.delete_api_key(api_key) + print("deleted api key") diff --git a/hubgrep_indexer/models/hosting_service.py b/hubgrep_indexer/models/hosting_service.py index 8a408a5..b8c82f7 100644 --- a/hubgrep_indexer/models/hosting_service.py +++ b/hubgrep_indexer/models/hosting_service.py @@ -37,6 +37,26 @@ def __str__(self) -> str: def hoster_name(self): return str(urlparse(self.landingpage_url).netloc) + def delete_api_key(self, api_key: str): + """ + Add an api key to this hoster. + Dont forget to commit afterwards! + """ + # postgres arrays are immutable, so we have to re-set the whole list + api_keys = list(self.api_keys) + api_keys.remove(api_key) + self.api_keys = api_keys + + def add_api_key(self, api_key: str): + """ + Remove an api key from this hoster. + Dont forget to commit afterwards! + """ + # postgres arrays are immutable, so we have to re-set the whole list + api_keys = list(self.api_keys) + api_keys.append(api_key) + self.api_keys = api_keys + def get_exports_dict(self, unified=False) -> List[Dict]: """ Shorthand for the query to this hosters exports, sorted by datetime (newest first). diff --git a/tests/lib/models/test_hosting_service.py b/tests/lib/models/test_hosting_service.py new file mode 100644 index 0000000..5802839 --- /dev/null +++ b/tests/lib/models/test_hosting_service.py @@ -0,0 +1,38 @@ +import pytest + +from hubgrep_indexer.models.hosting_service import HostingService +from hubgrep_indexer import db +from hubgrep_indexer.constants import HOST_TYPE_GITEA + + +class TestHostingService: + @pytest.mark.parametrize( + "hosting_service", # matched against hosting_service fixture in conftest.py + [HOST_TYPE_GITEA], + indirect=True, + ) + def test_add_api_key(self, test_client, hosting_service): + with test_client: + # throw out potential existing keys + hosting_service.api_keys = [] + # add a new key + hosting_service.add_api_key("key") + db.session.add(hosting_service) + db.session.commit() + + same_hosting_service = HostingService.query.filter_by( + api_url=hosting_service.api_url + ).first() + assert len(same_hosting_service.api_keys) == 1 + + # and delete again.. + # + print(hosting_service.api_keys) + hosting_service.delete_api_key("key") + db.session.add(hosting_service) + db.session.commit() + + same_hosting_service = HostingService.query.filter_by( + api_url=hosting_service.api_url + ).first() + assert len(same_hosting_service.api_keys) == 0 From cc23ee4e21546922789bb130ded7258237489c7e Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 14 Aug 2021 01:31:59 +0200 Subject: [PATCH 2/3] actually commit keys --- hubgrep_indexer/cli_blueprint/hosters.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hubgrep_indexer/cli_blueprint/hosters.py b/hubgrep_indexer/cli_blueprint/hosters.py index 336d9f3..4a03bae 100644 --- a/hubgrep_indexer/cli_blueprint/hosters.py +++ b/hubgrep_indexer/cli_blueprint/hosters.py @@ -118,6 +118,8 @@ def add_api_key(api_url, api_key): exit() hosting_service.add_api_key(api_key) + db.session.add(hosting_service) + db.session.commit() print("key added!") @@ -144,4 +146,6 @@ def delete_api_key(api_url, api_key): _release_api_key(hosting_service, api_key) hosting_service.delete_api_key(api_key) + db.session.add(hosting_service) + db.session.commit() print("deleted api key") From 869a461e4fecaa1e9c713000ec44278194b432a0 Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 16 Aug 2021 11:07:11 +0200 Subject: [PATCH 3/3] fix cli --- hubgrep_indexer/cli_blueprint/hosters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/hubgrep_indexer/cli_blueprint/hosters.py b/hubgrep_indexer/cli_blueprint/hosters.py index 4a03bae..ceba5cd 100644 --- a/hubgrep_indexer/cli_blueprint/hosters.py +++ b/hubgrep_indexer/cli_blueprint/hosters.py @@ -103,10 +103,12 @@ def list_api_keys(): machine_id = state_manager.get_machine_id_by_api_key( hosting_service_id=hosting_service.id, api_key=api_key ) - print(f"\t{api_key} - used by {machine_id}") + print(f"\t'{api_key}' - used by {machine_id}") @cli_bp.cli.command(help="add api key") +@click.argument("api_url") +@click.argument("api_key") def add_api_key(api_url, api_key): hosting_service = HostingService.query.filter_by(api_url=api_url).first() if not hosting_service: @@ -124,6 +126,8 @@ def add_api_key(api_url, api_key): @cli_bp.cli.command(help="delete an api key") +@click.argument("api_url") +@click.argument("api_key") def delete_api_key(api_url, api_key): hosting_service = HostingService.query.filter_by(api_url=api_url).first() if not hosting_service: