Skip to content

Commit

Permalink
Merge pull request #51 from HubGrep/feature/cli_key_handling
Browse files Browse the repository at this point in the history
cli commands to add and remove keys
  • Loading branch information
geoheelias authored Aug 16, 2021
2 parents 6ca0e4e + 869a461 commit 89412c2
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 14 deletions.
106 changes: 92 additions & 14 deletions hubgrep_indexer/cli_blueprint/hosters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand All @@ -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)
Expand All @@ -48,30 +59,97 @@ 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")
@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:
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)
db.session.add(hosting_service)
db.session.commit()
print("key added!")


@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:
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)
db.session.add(hosting_service)
db.session.commit()
print("deleted api key")
20 changes: 20 additions & 0 deletions hubgrep_indexer/models/hosting_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/models/test_hosting_service.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89412c2

Please sign in to comment.