Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 1 | # Copyright (C) 2019 Garmin Ltd. |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
| 5 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 6 | from datetime import datetime, timedelta |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 7 | import asyncio |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 8 | import logging |
| 9 | import math |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 10 | import time |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 11 | import os |
| 12 | import base64 |
| 13 | import hashlib |
| 14 | from . import create_async_client |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 15 | import bb.asyncrpc |
| 16 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 17 | logger = logging.getLogger("hashserv.server") |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 18 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 19 | |
| 20 | # This permission only exists to match nothing |
| 21 | NONE_PERM = "@none" |
| 22 | |
| 23 | READ_PERM = "@read" |
| 24 | REPORT_PERM = "@report" |
| 25 | DB_ADMIN_PERM = "@db-admin" |
| 26 | USER_ADMIN_PERM = "@user-admin" |
| 27 | ALL_PERM = "@all" |
| 28 | |
| 29 | ALL_PERMISSIONS = { |
| 30 | READ_PERM, |
| 31 | REPORT_PERM, |
| 32 | DB_ADMIN_PERM, |
| 33 | USER_ADMIN_PERM, |
| 34 | ALL_PERM, |
| 35 | } |
| 36 | |
| 37 | DEFAULT_ANON_PERMS = ( |
| 38 | READ_PERM, |
| 39 | REPORT_PERM, |
| 40 | DB_ADMIN_PERM, |
| 41 | ) |
| 42 | |
| 43 | TOKEN_ALGORITHM = "sha256" |
| 44 | |
| 45 | # 48 bytes of random data will result in 64 characters when base64 |
| 46 | # encoded. This number also ensures that the base64 encoding won't have any |
| 47 | # trailing '=' characters. |
| 48 | TOKEN_SIZE = 48 |
| 49 | |
| 50 | SALT_SIZE = 8 |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class Measurement(object): |
| 54 | def __init__(self, sample): |
| 55 | self.sample = sample |
| 56 | |
| 57 | def start(self): |
| 58 | self.start_time = time.perf_counter() |
| 59 | |
| 60 | def end(self): |
| 61 | self.sample.add(time.perf_counter() - self.start_time) |
| 62 | |
| 63 | def __enter__(self): |
| 64 | self.start() |
| 65 | return self |
| 66 | |
| 67 | def __exit__(self, *args, **kwargs): |
| 68 | self.end() |
| 69 | |
| 70 | |
| 71 | class Sample(object): |
| 72 | def __init__(self, stats): |
| 73 | self.stats = stats |
| 74 | self.num_samples = 0 |
| 75 | self.elapsed = 0 |
| 76 | |
| 77 | def measure(self): |
| 78 | return Measurement(self) |
| 79 | |
| 80 | def __enter__(self): |
| 81 | return self |
| 82 | |
| 83 | def __exit__(self, *args, **kwargs): |
| 84 | self.end() |
| 85 | |
| 86 | def add(self, elapsed): |
| 87 | self.num_samples += 1 |
| 88 | self.elapsed += elapsed |
| 89 | |
| 90 | def end(self): |
| 91 | if self.num_samples: |
| 92 | self.stats.add(self.elapsed) |
| 93 | self.num_samples = 0 |
| 94 | self.elapsed = 0 |
| 95 | |
| 96 | |
| 97 | class Stats(object): |
| 98 | def __init__(self): |
| 99 | self.reset() |
| 100 | |
| 101 | def reset(self): |
| 102 | self.num = 0 |
| 103 | self.total_time = 0 |
| 104 | self.max_time = 0 |
| 105 | self.m = 0 |
| 106 | self.s = 0 |
| 107 | self.current_elapsed = None |
| 108 | |
| 109 | def add(self, elapsed): |
| 110 | self.num += 1 |
| 111 | if self.num == 1: |
| 112 | self.m = elapsed |
| 113 | self.s = 0 |
| 114 | else: |
| 115 | last_m = self.m |
| 116 | self.m = last_m + (elapsed - last_m) / self.num |
| 117 | self.s = self.s + (elapsed - last_m) * (elapsed - self.m) |
| 118 | |
| 119 | self.total_time += elapsed |
| 120 | |
| 121 | if self.max_time < elapsed: |
| 122 | self.max_time = elapsed |
| 123 | |
| 124 | def start_sample(self): |
| 125 | return Sample(self) |
| 126 | |
| 127 | @property |
| 128 | def average(self): |
| 129 | if self.num == 0: |
| 130 | return 0 |
| 131 | return self.total_time / self.num |
| 132 | |
| 133 | @property |
| 134 | def stdev(self): |
| 135 | if self.num <= 1: |
| 136 | return 0 |
| 137 | return math.sqrt(self.s / (self.num - 1)) |
| 138 | |
| 139 | def todict(self): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 140 | return { |
| 141 | k: getattr(self, k) |
| 142 | for k in ("num", "total_time", "max_time", "average", "stdev") |
| 143 | } |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 144 | |
| 145 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 146 | token_refresh_semaphore = asyncio.Lock() |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 147 | |
| 148 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 149 | async def new_token(): |
| 150 | # Prevent malicious users from using this API to deduce the entropy |
| 151 | # pool on the server and thus be able to guess a token. *All* token |
| 152 | # refresh requests lock the same global semaphore and then sleep for a |
| 153 | # short time. The effectively rate limits the total number of requests |
| 154 | # than can be made across all clients to 10/second, which should be enough |
| 155 | # since you have to be an authenticated users to make the request in the |
| 156 | # first place |
| 157 | async with token_refresh_semaphore: |
| 158 | await asyncio.sleep(0.1) |
| 159 | raw = os.getrandom(TOKEN_SIZE, os.GRND_NONBLOCK) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 160 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 161 | return base64.b64encode(raw, b"._").decode("utf-8") |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 162 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 163 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 164 | def new_salt(): |
| 165 | return os.getrandom(SALT_SIZE, os.GRND_NONBLOCK).hex() |
| 166 | |
| 167 | |
| 168 | def hash_token(algo, salt, token): |
| 169 | h = hashlib.new(algo) |
| 170 | h.update(salt.encode("utf-8")) |
| 171 | h.update(token.encode("utf-8")) |
| 172 | return ":".join([algo, salt, h.hexdigest()]) |
| 173 | |
| 174 | |
| 175 | def permissions(*permissions, allow_anon=True, allow_self_service=False): |
| 176 | """ |
| 177 | Function decorator that can be used to decorate an RPC function call and |
| 178 | check that the current users permissions match the require permissions. |
| 179 | |
| 180 | If allow_anon is True, the user will also be allowed to make the RPC call |
| 181 | if the anonymous user permissions match the permissions. |
| 182 | |
| 183 | If allow_self_service is True, and the "username" property in the request |
| 184 | is the currently logged in user, or not specified, the user will also be |
| 185 | allowed to make the request. This allows users to access normal privileged |
| 186 | API, as long as they are only modifying their own user properties (e.g. |
| 187 | users can be allowed to reset their own token without @user-admin |
| 188 | permissions, but not the token for any other user. |
| 189 | """ |
| 190 | |
| 191 | def wrapper(func): |
| 192 | async def wrap(self, request): |
| 193 | if allow_self_service and self.user is not None: |
| 194 | username = request.get("username", self.user.username) |
| 195 | if username == self.user.username: |
| 196 | request["username"] = self.user.username |
| 197 | return await func(self, request) |
| 198 | |
| 199 | if not self.user_has_permissions(*permissions, allow_anon=allow_anon): |
| 200 | if not self.user: |
| 201 | username = "Anonymous user" |
| 202 | user_perms = self.anon_perms |
| 203 | else: |
| 204 | username = self.user.username |
| 205 | user_perms = self.user.permissions |
| 206 | |
| 207 | self.logger.info( |
| 208 | "User %s with permissions %r denied from calling %s. Missing permissions(s) %r", |
| 209 | username, |
| 210 | ", ".join(user_perms), |
| 211 | func.__name__, |
| 212 | ", ".join(permissions), |
| 213 | ) |
| 214 | raise bb.asyncrpc.InvokeError( |
| 215 | f"{username} is not allowed to access permissions(s) {', '.join(permissions)}" |
| 216 | ) |
| 217 | |
| 218 | return await func(self, request) |
| 219 | |
| 220 | return wrap |
| 221 | |
| 222 | return wrapper |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 223 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 224 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 225 | class ServerClient(bb.asyncrpc.AsyncServerConnection): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 226 | def __init__( |
| 227 | self, |
| 228 | socket, |
| 229 | db_engine, |
| 230 | request_stats, |
| 231 | backfill_queue, |
| 232 | upstream, |
| 233 | read_only, |
| 234 | anon_perms, |
| 235 | ): |
| 236 | super().__init__(socket, "OEHASHEQUIV", logger) |
| 237 | self.db_engine = db_engine |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 238 | self.request_stats = request_stats |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 239 | self.max_chunk = bb.asyncrpc.DEFAULT_MAX_CHUNK |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 240 | self.backfill_queue = backfill_queue |
| 241 | self.upstream = upstream |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 242 | self.read_only = read_only |
| 243 | self.user = None |
| 244 | self.anon_perms = anon_perms |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 245 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 246 | self.handlers.update( |
| 247 | { |
| 248 | "get": self.handle_get, |
| 249 | "get-outhash": self.handle_get_outhash, |
| 250 | "get-stream": self.handle_get_stream, |
| 251 | "get-stats": self.handle_get_stats, |
| 252 | "get-db-usage": self.handle_get_db_usage, |
| 253 | "get-db-query-columns": self.handle_get_db_query_columns, |
| 254 | # Not always read-only, but internally checks if the server is |
| 255 | # read-only |
| 256 | "report": self.handle_report, |
| 257 | "auth": self.handle_auth, |
| 258 | "get-user": self.handle_get_user, |
| 259 | "get-all-users": self.handle_get_all_users, |
| 260 | "become-user": self.handle_become_user, |
| 261 | } |
| 262 | ) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 263 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 264 | if not read_only: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 265 | self.handlers.update( |
| 266 | { |
| 267 | "report-equiv": self.handle_equivreport, |
| 268 | "reset-stats": self.handle_reset_stats, |
| 269 | "backfill-wait": self.handle_backfill_wait, |
| 270 | "remove": self.handle_remove, |
| 271 | "clean-unused": self.handle_clean_unused, |
| 272 | "refresh-token": self.handle_refresh_token, |
| 273 | "set-user-perms": self.handle_set_perms, |
| 274 | "new-user": self.handle_new_user, |
| 275 | "delete-user": self.handle_delete_user, |
| 276 | } |
| 277 | ) |
| 278 | |
| 279 | def raise_no_user_error(self, username): |
| 280 | raise bb.asyncrpc.InvokeError(f"No user named '{username}' exists") |
| 281 | |
| 282 | def user_has_permissions(self, *permissions, allow_anon=True): |
| 283 | permissions = set(permissions) |
| 284 | if allow_anon: |
| 285 | if ALL_PERM in self.anon_perms: |
| 286 | return True |
| 287 | |
| 288 | if not permissions - self.anon_perms: |
| 289 | return True |
| 290 | |
| 291 | if self.user is None: |
| 292 | return False |
| 293 | |
| 294 | if ALL_PERM in self.user.permissions: |
| 295 | return True |
| 296 | |
| 297 | if not permissions - self.user.permissions: |
| 298 | return True |
| 299 | |
| 300 | return False |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 301 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 302 | def validate_proto_version(self): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 303 | return self.proto_version > (1, 0) and self.proto_version <= (1, 1) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 304 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 305 | async def process_requests(self): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 306 | async with self.db_engine.connect(self.logger) as db: |
| 307 | self.db = db |
| 308 | if self.upstream is not None: |
| 309 | self.upstream_client = await create_async_client(self.upstream) |
| 310 | else: |
| 311 | self.upstream_client = None |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 312 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 313 | try: |
| 314 | await super().process_requests() |
| 315 | finally: |
| 316 | if self.upstream_client is not None: |
| 317 | await self.upstream_client.close() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 318 | |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 319 | async def dispatch_message(self, msg): |
| 320 | for k in self.handlers.keys(): |
| 321 | if k in msg: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 322 | self.logger.debug("Handling %s" % k) |
| 323 | if "stream" in k: |
| 324 | return await self.handlers[k](msg[k]) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 325 | else: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 326 | with self.request_stats.start_sample() as self.request_sample, self.request_sample.measure(): |
| 327 | return await self.handlers[k](msg[k]) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 328 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 329 | raise bb.asyncrpc.ClientError("Unrecognized command %r" % msg) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 330 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 331 | @permissions(READ_PERM) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 332 | async def handle_get(self, request): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 333 | method = request["method"] |
| 334 | taskhash = request["taskhash"] |
| 335 | fetch_all = request.get("all", False) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 336 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 337 | return await self.get_unihash(method, taskhash, fetch_all) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 338 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 339 | async def get_unihash(self, method, taskhash, fetch_all=False): |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 340 | d = None |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 341 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 342 | if fetch_all: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 343 | row = await self.db.get_unihash_by_taskhash_full(method, taskhash) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 344 | if row is not None: |
| 345 | d = {k: row[k] for k in row.keys()} |
| 346 | elif self.upstream_client is not None: |
| 347 | d = await self.upstream_client.get_taskhash(method, taskhash, True) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 348 | await self.update_unified(d) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 349 | else: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 350 | row = await self.db.get_equivalent(method, taskhash) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 351 | |
| 352 | if row is not None: |
| 353 | d = {k: row[k] for k in row.keys()} |
| 354 | elif self.upstream_client is not None: |
| 355 | d = await self.upstream_client.get_taskhash(method, taskhash) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 356 | await self.db.insert_unihash(d["method"], d["taskhash"], d["unihash"]) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 357 | |
| 358 | return d |
| 359 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 360 | @permissions(READ_PERM) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 361 | async def handle_get_outhash(self, request): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 362 | method = request["method"] |
| 363 | outhash = request["outhash"] |
| 364 | taskhash = request["taskhash"] |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 365 | with_unihash = request.get("with_unihash", True) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 366 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 367 | return await self.get_outhash(method, outhash, taskhash, with_unihash) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 368 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 369 | async def get_outhash(self, method, outhash, taskhash, with_unihash=True): |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 370 | d = None |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 371 | if with_unihash: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 372 | row = await self.db.get_unihash_by_outhash(method, outhash) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 373 | else: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 374 | row = await self.db.get_outhash(method, outhash) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 375 | |
| 376 | if row is not None: |
| 377 | d = {k: row[k] for k in row.keys()} |
| 378 | elif self.upstream_client is not None: |
| 379 | d = await self.upstream_client.get_outhash(method, outhash, taskhash) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 380 | await self.update_unified(d) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 381 | |
| 382 | return d |
| 383 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 384 | async def update_unified(self, data): |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 385 | if data is None: |
| 386 | return |
| 387 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 388 | await self.db.insert_unihash(data["method"], data["taskhash"], data["unihash"]) |
| 389 | await self.db.insert_outhash(data) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 390 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 391 | @permissions(READ_PERM) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 392 | async def handle_get_stream(self, request): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 393 | await self.socket.send_message("ok") |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 394 | |
| 395 | while True: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 396 | upstream = None |
| 397 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 398 | l = await self.socket.recv() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 399 | if not l: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 400 | break |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 401 | |
| 402 | try: |
| 403 | # This inner loop is very sensitive and must be as fast as |
| 404 | # possible (which is why the request sample is handled manually |
| 405 | # instead of using 'with', and also why logging statements are |
| 406 | # commented out. |
| 407 | self.request_sample = self.request_stats.start_sample() |
| 408 | request_measure = self.request_sample.measure() |
| 409 | request_measure.start() |
| 410 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 411 | if l == "END": |
| 412 | break |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 413 | |
| 414 | (method, taskhash) = l.split() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 415 | # self.logger.debug('Looking up %s %s' % (method, taskhash)) |
| 416 | row = await self.db.get_equivalent(method, taskhash) |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 417 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 418 | if row is not None: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 419 | msg = row["unihash"] |
| 420 | # self.logger.debug('Found equivalent task %s -> %s', (row['taskhash'], row['unihash'])) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 421 | elif self.upstream_client is not None: |
| 422 | upstream = await self.upstream_client.get_unihash(method, taskhash) |
| 423 | if upstream: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 424 | msg = upstream |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 425 | else: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 426 | msg = "" |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 427 | else: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 428 | msg = "" |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 429 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 430 | await self.socket.send(msg) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 431 | finally: |
| 432 | request_measure.end() |
| 433 | self.request_sample.end() |
| 434 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 435 | # Post to the backfill queue after writing the result to minimize |
| 436 | # the turn around time on a request |
| 437 | if upstream is not None: |
| 438 | await self.backfill_queue.put((method, taskhash)) |
| 439 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 440 | await self.socket.send("ok") |
| 441 | return self.NO_RESPONSE |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 442 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 443 | async def report_readonly(self, data): |
| 444 | method = data["method"] |
| 445 | outhash = data["outhash"] |
| 446 | taskhash = data["taskhash"] |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 447 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 448 | info = await self.get_outhash(method, outhash, taskhash) |
| 449 | if info: |
| 450 | unihash = info["unihash"] |
| 451 | else: |
| 452 | unihash = data["unihash"] |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 453 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 454 | return { |
| 455 | "taskhash": taskhash, |
| 456 | "method": method, |
| 457 | "unihash": unihash, |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 460 | # Since this can be called either read only or to report, the check to |
| 461 | # report is made inside the function |
| 462 | @permissions(READ_PERM) |
| 463 | async def handle_report(self, data): |
| 464 | if self.read_only or not self.user_has_permissions(REPORT_PERM): |
| 465 | return await self.report_readonly(data) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 466 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 467 | outhash_data = { |
| 468 | "method": data["method"], |
| 469 | "outhash": data["outhash"], |
| 470 | "taskhash": data["taskhash"], |
| 471 | "created": datetime.now(), |
| 472 | } |
| 473 | |
| 474 | for k in ("owner", "PN", "PV", "PR", "task", "outhash_siginfo"): |
| 475 | if k in data: |
| 476 | outhash_data[k] = data[k] |
| 477 | |
| 478 | if self.user: |
| 479 | outhash_data["owner"] = self.user.username |
| 480 | |
| 481 | # Insert the new entry, unless it already exists |
| 482 | if await self.db.insert_outhash(outhash_data): |
| 483 | # If this row is new, check if it is equivalent to another |
| 484 | # output hash |
| 485 | row = await self.db.get_equivalent_for_outhash( |
| 486 | data["method"], data["outhash"], data["taskhash"] |
| 487 | ) |
| 488 | |
| 489 | if row is not None: |
| 490 | # A matching output hash was found. Set our taskhash to the |
| 491 | # same unihash since they are equivalent |
| 492 | unihash = row["unihash"] |
| 493 | else: |
| 494 | # No matching output hash was found. This is probably the |
| 495 | # first outhash to be added. |
| 496 | unihash = data["unihash"] |
| 497 | |
| 498 | # Query upstream to see if it has a unihash we can use |
| 499 | if self.upstream_client is not None: |
| 500 | upstream_data = await self.upstream_client.get_outhash( |
| 501 | data["method"], data["outhash"], data["taskhash"] |
| 502 | ) |
| 503 | if upstream_data is not None: |
| 504 | unihash = upstream_data["unihash"] |
| 505 | |
| 506 | await self.db.insert_unihash(data["method"], data["taskhash"], unihash) |
| 507 | |
| 508 | unihash_data = await self.get_unihash(data["method"], data["taskhash"]) |
| 509 | if unihash_data is not None: |
| 510 | unihash = unihash_data["unihash"] |
| 511 | else: |
| 512 | unihash = data["unihash"] |
| 513 | |
| 514 | return { |
| 515 | "taskhash": data["taskhash"], |
| 516 | "method": data["method"], |
| 517 | "unihash": unihash, |
| 518 | } |
| 519 | |
| 520 | @permissions(READ_PERM, REPORT_PERM) |
| 521 | async def handle_equivreport(self, data): |
| 522 | await self.db.insert_unihash(data["method"], data["taskhash"], data["unihash"]) |
| 523 | |
| 524 | # Fetch the unihash that will be reported for the taskhash. If the |
| 525 | # unihash matches, it means this row was inserted (or the mapping |
| 526 | # was already valid) |
| 527 | row = await self.db.get_equivalent(data["method"], data["taskhash"]) |
| 528 | |
| 529 | if row["unihash"] == data["unihash"]: |
| 530 | self.logger.info( |
| 531 | "Adding taskhash equivalence for %s with unihash %s", |
| 532 | data["taskhash"], |
| 533 | row["unihash"], |
| 534 | ) |
| 535 | |
| 536 | return {k: row[k] for k in ("taskhash", "method", "unihash")} |
| 537 | |
| 538 | @permissions(READ_PERM) |
| 539 | async def handle_get_stats(self, request): |
| 540 | return { |
| 541 | "requests": self.request_stats.todict(), |
| 542 | } |
| 543 | |
| 544 | @permissions(DB_ADMIN_PERM) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 545 | async def handle_reset_stats(self, request): |
| 546 | d = { |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 547 | "requests": self.request_stats.todict(), |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | self.request_stats.reset() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 551 | return d |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 552 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 553 | @permissions(READ_PERM) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 554 | async def handle_backfill_wait(self, request): |
| 555 | d = { |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 556 | "tasks": self.backfill_queue.qsize(), |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 557 | } |
| 558 | await self.backfill_queue.join() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 559 | return d |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 560 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 561 | @permissions(DB_ADMIN_PERM) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 562 | async def handle_remove(self, request): |
| 563 | condition = request["where"] |
| 564 | if not isinstance(condition, dict): |
| 565 | raise TypeError("Bad condition type %s" % type(condition)) |
| 566 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 567 | return {"count": await self.db.remove(condition)} |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 568 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 569 | @permissions(DB_ADMIN_PERM) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 570 | async def handle_clean_unused(self, request): |
| 571 | max_age = request["max_age_seconds"] |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 572 | oldest = datetime.now() - timedelta(seconds=-max_age) |
| 573 | return {"count": await self.db.clean_unused(oldest)} |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 574 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 575 | @permissions(DB_ADMIN_PERM) |
| 576 | async def handle_get_db_usage(self, request): |
| 577 | return {"usage": await self.db.get_usage()} |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 578 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 579 | @permissions(DB_ADMIN_PERM) |
| 580 | async def handle_get_db_query_columns(self, request): |
| 581 | return {"columns": await self.db.get_query_columns()} |
| 582 | |
| 583 | # The authentication API is always allowed |
| 584 | async def handle_auth(self, request): |
| 585 | username = str(request["username"]) |
| 586 | token = str(request["token"]) |
| 587 | |
| 588 | async def fail_auth(): |
| 589 | nonlocal username |
| 590 | # Rate limit bad login attempts |
| 591 | await asyncio.sleep(1) |
| 592 | raise bb.asyncrpc.InvokeError(f"Unable to authenticate as {username}") |
| 593 | |
| 594 | user, db_token = await self.db.lookup_user_token(username) |
| 595 | |
| 596 | if not user or not db_token: |
| 597 | await fail_auth() |
| 598 | |
| 599 | try: |
| 600 | algo, salt, _ = db_token.split(":") |
| 601 | except ValueError: |
| 602 | await fail_auth() |
| 603 | |
| 604 | if hash_token(algo, salt, token) != db_token: |
| 605 | await fail_auth() |
| 606 | |
| 607 | self.user = user |
| 608 | |
| 609 | self.logger.info("Authenticated as %s", username) |
| 610 | |
| 611 | return { |
| 612 | "result": True, |
| 613 | "username": self.user.username, |
| 614 | "permissions": sorted(list(self.user.permissions)), |
| 615 | } |
| 616 | |
| 617 | @permissions(USER_ADMIN_PERM, allow_self_service=True, allow_anon=False) |
| 618 | async def handle_refresh_token(self, request): |
| 619 | username = str(request["username"]) |
| 620 | |
| 621 | token = await new_token() |
| 622 | |
| 623 | updated = await self.db.set_user_token( |
| 624 | username, |
| 625 | hash_token(TOKEN_ALGORITHM, new_salt(), token), |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 626 | ) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 627 | if not updated: |
| 628 | self.raise_no_user_error(username) |
| 629 | |
| 630 | return {"username": username, "token": token} |
| 631 | |
| 632 | def get_perm_arg(self, arg): |
| 633 | if not isinstance(arg, list): |
| 634 | raise bb.asyncrpc.InvokeError("Unexpected type for permissions") |
| 635 | |
| 636 | arg = set(arg) |
| 637 | try: |
| 638 | arg.remove(NONE_PERM) |
| 639 | except KeyError: |
| 640 | pass |
| 641 | |
| 642 | unknown_perms = arg - ALL_PERMISSIONS |
| 643 | if unknown_perms: |
| 644 | raise bb.asyncrpc.InvokeError( |
| 645 | "Unknown permissions %s" % ", ".join(sorted(list(unknown_perms))) |
| 646 | ) |
| 647 | |
| 648 | return sorted(list(arg)) |
| 649 | |
| 650 | def return_perms(self, permissions): |
| 651 | if ALL_PERM in permissions: |
| 652 | return sorted(list(ALL_PERMISSIONS)) |
| 653 | return sorted(list(permissions)) |
| 654 | |
| 655 | @permissions(USER_ADMIN_PERM, allow_anon=False) |
| 656 | async def handle_set_perms(self, request): |
| 657 | username = str(request["username"]) |
| 658 | permissions = self.get_perm_arg(request["permissions"]) |
| 659 | |
| 660 | if not await self.db.set_user_perms(username, permissions): |
| 661 | self.raise_no_user_error(username) |
| 662 | |
| 663 | return { |
| 664 | "username": username, |
| 665 | "permissions": self.return_perms(permissions), |
| 666 | } |
| 667 | |
| 668 | @permissions(USER_ADMIN_PERM, allow_self_service=True, allow_anon=False) |
| 669 | async def handle_get_user(self, request): |
| 670 | username = str(request["username"]) |
| 671 | |
| 672 | user = await self.db.lookup_user(username) |
| 673 | if user is None: |
| 674 | return None |
| 675 | |
| 676 | return { |
| 677 | "username": user.username, |
| 678 | "permissions": self.return_perms(user.permissions), |
| 679 | } |
| 680 | |
| 681 | @permissions(USER_ADMIN_PERM, allow_anon=False) |
| 682 | async def handle_get_all_users(self, request): |
| 683 | users = await self.db.get_all_users() |
| 684 | return { |
| 685 | "users": [ |
| 686 | { |
| 687 | "username": u.username, |
| 688 | "permissions": self.return_perms(u.permissions), |
| 689 | } |
| 690 | for u in users |
| 691 | ] |
| 692 | } |
| 693 | |
| 694 | @permissions(USER_ADMIN_PERM, allow_anon=False) |
| 695 | async def handle_new_user(self, request): |
| 696 | username = str(request["username"]) |
| 697 | permissions = self.get_perm_arg(request["permissions"]) |
| 698 | |
| 699 | token = await new_token() |
| 700 | |
| 701 | inserted = await self.db.new_user( |
| 702 | username, |
| 703 | permissions, |
| 704 | hash_token(TOKEN_ALGORITHM, new_salt(), token), |
| 705 | ) |
| 706 | if not inserted: |
| 707 | raise bb.asyncrpc.InvokeError(f"Cannot create new user '{username}'") |
| 708 | |
| 709 | return { |
| 710 | "username": username, |
| 711 | "permissions": self.return_perms(permissions), |
| 712 | "token": token, |
| 713 | } |
| 714 | |
| 715 | @permissions(USER_ADMIN_PERM, allow_self_service=True, allow_anon=False) |
| 716 | async def handle_delete_user(self, request): |
| 717 | username = str(request["username"]) |
| 718 | |
| 719 | if not await self.db.delete_user(username): |
| 720 | self.raise_no_user_error(username) |
| 721 | |
| 722 | return {"username": username} |
| 723 | |
| 724 | @permissions(USER_ADMIN_PERM, allow_anon=False) |
| 725 | async def handle_become_user(self, request): |
| 726 | username = str(request["username"]) |
| 727 | |
| 728 | user = await self.db.lookup_user(username) |
| 729 | if user is None: |
| 730 | raise bb.asyncrpc.InvokeError(f"User {username} doesn't exist") |
| 731 | |
| 732 | self.user = user |
| 733 | |
| 734 | self.logger.info("Became user %s", username) |
| 735 | |
| 736 | return { |
| 737 | "username": self.user.username, |
| 738 | "permissions": self.return_perms(self.user.permissions), |
| 739 | } |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 740 | |
| 741 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 742 | class Server(bb.asyncrpc.AsyncServer): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 743 | def __init__( |
| 744 | self, |
| 745 | db_engine, |
| 746 | upstream=None, |
| 747 | read_only=False, |
| 748 | anon_perms=DEFAULT_ANON_PERMS, |
| 749 | admin_username=None, |
| 750 | admin_password=None, |
| 751 | ): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 752 | if upstream and read_only: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 753 | raise bb.asyncrpc.ServerError( |
| 754 | "Read-only hashserv cannot pull from an upstream server" |
| 755 | ) |
| 756 | |
| 757 | disallowed_perms = set(anon_perms) - set( |
| 758 | [NONE_PERM, READ_PERM, REPORT_PERM, DB_ADMIN_PERM] |
| 759 | ) |
| 760 | |
| 761 | if disallowed_perms: |
| 762 | raise bb.asyncrpc.ServerError( |
| 763 | f"Permission(s) {' '.join(disallowed_perms)} are not allowed for anonymous users" |
| 764 | ) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 765 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 766 | super().__init__(logger) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 767 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 768 | self.request_stats = Stats() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 769 | self.db_engine = db_engine |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 770 | self.upstream = upstream |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 771 | self.read_only = read_only |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 772 | self.backfill_queue = None |
| 773 | self.anon_perms = set(anon_perms) |
| 774 | self.admin_username = admin_username |
| 775 | self.admin_password = admin_password |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 776 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 777 | self.logger.info( |
| 778 | "Anonymous user permissions are: %s", ", ".join(self.anon_perms) |
| 779 | ) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 780 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 781 | def accept_client(self, socket): |
| 782 | return ServerClient( |
| 783 | socket, |
| 784 | self.db_engine, |
| 785 | self.request_stats, |
| 786 | self.backfill_queue, |
| 787 | self.upstream, |
| 788 | self.read_only, |
| 789 | self.anon_perms, |
| 790 | ) |
| 791 | |
| 792 | async def create_admin_user(self): |
| 793 | admin_permissions = (ALL_PERM,) |
| 794 | async with self.db_engine.connect(self.logger) as db: |
| 795 | added = await db.new_user( |
| 796 | self.admin_username, |
| 797 | admin_permissions, |
| 798 | hash_token(TOKEN_ALGORITHM, new_salt(), self.admin_password), |
| 799 | ) |
| 800 | if added: |
| 801 | self.logger.info("Created admin user '%s'", self.admin_username) |
| 802 | else: |
| 803 | await db.set_user_perms( |
| 804 | self.admin_username, |
| 805 | admin_permissions, |
| 806 | ) |
| 807 | await db.set_user_token( |
| 808 | self.admin_username, |
| 809 | hash_token(TOKEN_ALGORITHM, new_salt(), self.admin_password), |
| 810 | ) |
| 811 | self.logger.info("Admin user '%s' updated", self.admin_username) |
| 812 | |
| 813 | async def backfill_worker_task(self): |
| 814 | async with await create_async_client( |
| 815 | self.upstream |
| 816 | ) as client, self.db_engine.connect(self.logger) as db: |
| 817 | while True: |
| 818 | item = await self.backfill_queue.get() |
| 819 | if item is None: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 820 | self.backfill_queue.task_done() |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 821 | break |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 822 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 823 | method, taskhash = item |
| 824 | d = await client.get_taskhash(method, taskhash) |
| 825 | if d is not None: |
| 826 | await db.insert_unihash(d["method"], d["taskhash"], d["unihash"]) |
| 827 | self.backfill_queue.task_done() |
| 828 | |
| 829 | def start(self): |
| 830 | tasks = super().start() |
| 831 | if self.upstream: |
| 832 | self.backfill_queue = asyncio.Queue() |
| 833 | tasks += [self.backfill_worker_task()] |
| 834 | |
| 835 | self.loop.run_until_complete(self.db_engine.create()) |
| 836 | |
| 837 | if self.admin_username: |
| 838 | self.loop.run_until_complete(self.create_admin_user()) |
| 839 | |
| 840 | return tasks |
| 841 | |
| 842 | async def stop(self): |
| 843 | if self.backfill_queue is not None: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 844 | await self.backfill_queue.put(None) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 845 | await super().stop() |