diff --git a/cracklab/server/services/job_issuance.py b/cracklab/server/services/job_issuance.py index 4b4aaa9..9a48faa 100644 --- a/cracklab/server/services/job_issuance.py +++ b/cracklab/server/services/job_issuance.py @@ -16,7 +16,10 @@ from cracklab.server.services.method_definition import ( MethodDefinitionError, validate_method_definition, ) - +from cracklab.server.services.search_context import ( + SearchContextError, + SearchContextService, +) class JobIssuanceError(ValueError): pass @@ -109,6 +112,7 @@ class JobIssuanceService: "unsupported_count": matched_count - valid_hash_count, } + def issue_job( self, method_id: str, @@ -120,23 +124,10 @@ class JobIssuanceService: if limit is not None and limit <= 0: raise ValueError("limit must be greater than zero") - method_version_row = self._methods.get_method_version( + steps = self._prepare_steps( method_id, method_version, ) - if method_version_row is None: - raise JobIssuanceError( - f"Method version not found: {method_id} v{method_version}" - ) - - definition = self._decode_method_definition( - method_version_row["definition_json"], - ) - steps = self._build_steps( - method_id, - method_version, - definition, - ) self._conn.execute("BEGIN IMMEDIATE") @@ -152,95 +143,200 @@ class JobIssuanceService: self._conn.rollback() return None - job_id = f"JOB-{uuid.uuid4().hex[:12].upper()}" - hash_file_name = f"{job_id}.22000" - - handshake_ids = tuple( - int(row["handshake_id"]) - for row in handshake_rows + return self._issue_job_with_handshakes( + method_id, + method_version, + client_id, + steps, + handshake_rows, ) - unique_hashes: list[str] = [] - seen_hashes: set[str] = set() - - for row in handshake_rows: - hash22000 = str(row["hash22000"]).strip() - - if hash22000 not in seen_hashes: - seen_hashes.add(hash22000) - unique_hashes.append(hash22000) - - hash_content = "\n".join(unique_hashes) + "\n" - hash_file_sha256 = hashlib.sha256( - hash_content.encode("utf-8"), - ).hexdigest() - - created_at = self._jobs.create_job( - job_id=job_id, - method_id=method_id, - method_version=method_version, - status="QUEUED", - hash_file_name=hash_file_name, - hash_file_sha256=hash_file_sha256, - hash_count=len(unique_hashes), - step_count=len(steps), - client_id=client_id, - ) - - for row in handshake_rows: - self._jobs.add_job_handshake( - job_id=job_id, - handshake_id=int(row["handshake_id"]), - hash22000=str(row["hash22000"]).strip(), - access_point_id=( - int(row["access_point_id"]) - if row["access_point_id"] is not None - else None - ), - ) - - self._reservations.create_reservation( - reservation_id=uuid.uuid4().hex, - handshake_id=int(row["handshake_id"]), - method_id=method_id, - method_version=method_version, - job_id=job_id, - client_id=client_id, - ) - - for step_no, step in enumerate(steps, start=1): - self._jobs.create_job_step( - job_id=job_id, - step_no=step_no, - step_id=step["step_id"], - status="PENDING", - session_name=self._session_name( - job_id, - step["step_id"], - ), - definition=step, - ) - - self._audit.create_event( - event_type="JOB_ISSUED", - job_id=job_id, - client_id=client_id, - method_id=method_id, - method_version=method_version, - details={ - "handshake_count": len(handshake_rows), - "hash_count": len(unique_hashes), - "step_count": len(steps), - "hash_file_name": hash_file_name, - }, - ) - - self._conn.commit() - except Exception: self._conn.rollback() raise + def issue_job_from_search_context( + self, + token: str, + method_id: str, + method_version: int, + client_id: str | None = None, + ) -> IssuedJob | None: + if not isinstance(token, str) or not token: + raise JobIssuanceError( + "Invalid search context token." + ) + + steps = self._prepare_steps( + method_id, + method_version, + ) + + token_hash = hashlib.sha256( + token.encode("utf-8"), + ).hexdigest() + + self._conn.execute("BEGIN IMMEDIATE") + + try: + context = SearchContextService( + self._conn, + ).get_context(token) + + if context is None: + raise JobIssuanceError( + "Search context not found." + ) + + if context["expired"]: + raise JobIssuanceError( + "Search context expired. Return to Search " + "and export the results again." + ) + + handshake_rows = ( + self._handshakes.list_eligible_for_search_context( + token_hash, + method_id, + method_version, + None, + ) + ) + + if not handshake_rows: + self._conn.rollback() + return None + + return self._issue_job_with_handshakes( + method_id, + method_version, + client_id, + steps, + handshake_rows, + ) + + except Exception: + self._conn.rollback() + raise + + def _prepare_steps( + self, + method_id: str, + method_version: int, + ) -> list[dict[str, Any]]: + method_version_row = self._methods.get_method_version( + method_id, + method_version, + ) + + if method_version_row is None: + raise JobIssuanceError( + f"Method version not found: {method_id} v{method_version}" + ) + + definition = self._decode_method_definition( + method_version_row["definition_json"], + ) + + return self._build_steps( + method_id, + method_version, + definition, + ) + + def _issue_job_with_handshakes( + self, + method_id: str, + method_version: int, + client_id: str | None, + steps: list[dict[str, Any]], + handshake_rows: list[sqlite3.Row], + ) -> IssuedJob: + job_id = f"JOB-{uuid.uuid4().hex[:12].upper()}" + hash_file_name = f"{job_id}.22000" + + handshake_ids = tuple( + int(row["handshake_id"]) + for row in handshake_rows + ) + + unique_hashes: list[str] = [] + seen_hashes: set[str] = set() + + for row in handshake_rows: + hash22000 = str(row["hash22000"]).strip() + + if hash22000 not in seen_hashes: + seen_hashes.add(hash22000) + unique_hashes.append(hash22000) + + hash_content = "\n".join(unique_hashes) + "\n" + hash_file_sha256 = hashlib.sha256( + hash_content.encode("utf-8"), + ).hexdigest() + + created_at = self._jobs.create_job( + job_id=job_id, + method_id=method_id, + method_version=method_version, + status="QUEUED", + hash_file_name=hash_file_name, + hash_file_sha256=hash_file_sha256, + hash_count=len(unique_hashes), + step_count=len(steps), + client_id=client_id, + ) + + for row in handshake_rows: + self._jobs.add_job_handshake( + job_id=job_id, + handshake_id=int(row["handshake_id"]), + hash22000=str(row["hash22000"]).strip(), + access_point_id=( + int(row["access_point_id"]) + if row["access_point_id"] is not None + else None + ), + ) + + self._reservations.create_reservation( + reservation_id=uuid.uuid4().hex, + handshake_id=int(row["handshake_id"]), + method_id=method_id, + method_version=method_version, + job_id=job_id, + client_id=client_id, + ) + + for step_no, step in enumerate(steps, start=1): + self._jobs.create_job_step( + job_id=job_id, + step_no=step_no, + step_id=step["step_id"], + status="PENDING", + session_name=self._session_name( + job_id, + step["step_id"], + ), + definition=step, + ) + + self._audit.create_event( + event_type="JOB_ISSUED", + job_id=job_id, + client_id=client_id, + method_id=method_id, + method_version=method_version, + details={ + "handshake_count": len(handshake_rows), + "hash_count": len(unique_hashes), + "step_count": len(steps), + "hash_file_name": hash_file_name, + }, + ) + + self._conn.commit() + return IssuedJob( job_id=job_id, method_id=method_id,