|
1 | 1 | # SPDX-License-Identifier: MIT |
2 | 2 | import logging |
3 | 3 | import click |
| 4 | +from collectoss.application.config import SystemConfig |
| 5 | +from collectoss.application.db.models.operations import WorkerOauth |
| 6 | +from collectoss.application.db.session import DatabaseSession |
| 7 | +from keyman.KeyClient import KeyPublisher |
4 | 8 | import sqlalchemy as s |
| 9 | +from sqlalchemy import func |
5 | 10 | from datetime import datetime |
6 | 11 | import httpx |
7 | 12 | from collections import Counter |
@@ -87,6 +92,69 @@ def update_api_key(): |
87 | 92 |
|
88 | 93 | engine.dispose() |
89 | 94 |
|
| 95 | + |
| 96 | +def validate_github_token(api_key: str) -> bool: |
| 97 | + """ check if a github API key is likely to be valid """ |
| 98 | + with httpx.Client() as client: |
| 99 | + rest, gql = GithubApiKeyHandler.get_key_rate_limit(client, api_key) |
| 100 | + return rest is not None and gql is not None |
| 101 | + |
| 102 | + |
| 103 | +@cli.command("add-key") |
| 104 | +@test_connection |
| 105 | +@test_db_connection |
| 106 | +def add_api_key(): |
| 107 | + """ |
| 108 | + Add a new github API key, potentially mid-collection |
| 109 | + """ |
| 110 | + # we prompt this way so that it is provided interactively and not likely to be stored somewhere like shell history |
| 111 | + incoming_api_key = click.prompt('Please enter the new API key (wont show typing)', type=str, hide_input=True) |
| 112 | + |
| 113 | + if not incoming_api_key or len(incoming_api_key) == 0: |
| 114 | + logger.error("No API key provided") |
| 115 | + return |
| 116 | + |
| 117 | + if not validate_github_token(incoming_api_key): |
| 118 | + logger.error("The provided API key is not valid") |
| 119 | + return |
| 120 | + |
| 121 | + |
| 122 | + with DatabaseSession(logger) as session: |
| 123 | + platform = "github" |
| 124 | + |
| 125 | + config = SystemConfig(logger, session) |
| 126 | + |
| 127 | + config_key = config.get_value("Keys", "github_api_key") |
| 128 | + |
| 129 | + existing_key_check = session.query(WorkerOauth).filter(WorkerOauth.platform == platform, WorkerOauth.access_token == incoming_api_key).first() |
| 130 | + |
| 131 | + if existing_key_check or (config_key is not None and config_key == incoming_api_key): |
| 132 | + logger.error("The provided API key already exists in the system") |
| 133 | + return |
| 134 | + |
| 135 | + new_key = WorkerOauth( |
| 136 | + platform=platform, |
| 137 | + access_token=incoming_api_key, |
| 138 | + name=f"New API Key {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} added via CLI", |
| 139 | + consumer_key=0, |
| 140 | + consumer_secret=0, |
| 141 | + access_token_secret=0, |
| 142 | + ) |
| 143 | + session.add(new_key) |
| 144 | + session.commit() |
| 145 | + |
| 146 | + logger.info(f"The new API key has been added to the DB.") |
| 147 | + |
| 148 | + keypub = KeyPublisher() |
| 149 | + |
| 150 | + keypub.publish(incoming_api_key, "github_rest") |
| 151 | + keypub.publish(incoming_api_key, "github_graphql") |
| 152 | + keypub.publish(incoming_api_key, "github_search") |
| 153 | + |
| 154 | + logger.info(f"The new API key has been published to the orchestrator.") |
| 155 | + |
| 156 | + |
| 157 | + |
90 | 158 | def epoch_to_local_time_with_am_pm(epoch): |
91 | 159 | # Convert epoch to local time with timezone awareness |
92 | 160 | local_time = datetime.fromtimestamp(epoch).astimezone() |
|
0 commit comments