harborrag-runtime 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harborrag_runtime/__init__.py +22 -0
- harborrag_runtime/application/README.md +39 -0
- harborrag_runtime/application/__init__.py +39 -0
- harborrag_runtime/application/chat/__init__.py +7 -0
- harborrag_runtime/application/chat/citations.py +149 -0
- harborrag_runtime/application/chat/client.py +201 -0
- harborrag_runtime/application/chat/completion.py +92 -0
- harborrag_runtime/application/chat/completion_request.py +130 -0
- harborrag_runtime/application/chat/evidence.py +202 -0
- harborrag_runtime/application/chat/options.py +43 -0
- harborrag_runtime/application/chat/preparation.py +135 -0
- harborrag_runtime/application/chat/presenters.py +130 -0
- harborrag_runtime/application/chat/prompting.py +127 -0
- harborrag_runtime/application/chat/query_scope.py +146 -0
- harborrag_runtime/application/chat/replay.py +141 -0
- harborrag_runtime/application/chat/request_filters.py +134 -0
- harborrag_runtime/application/chat/retrieval.py +127 -0
- harborrag_runtime/application/chat/service.py +371 -0
- harborrag_runtime/application/composition/__init__.py +1 -0
- harborrag_runtime/application/composition/factories.py +106 -0
- harborrag_runtime/application/composition/resources.py +141 -0
- harborrag_runtime/application/composition/selection.py +65 -0
- harborrag_runtime/application/composition/service.py +550 -0
- harborrag_runtime/application/composition/tenant_models.py +45 -0
- harborrag_runtime/application/control_plane/__init__.py +1 -0
- harborrag_runtime/application/control_plane/active_runs.py +38 -0
- harborrag_runtime/application/control_plane/connection_models.py +109 -0
- harborrag_runtime/application/control_plane/connection_testing.py +91 -0
- harborrag_runtime/application/control_plane/connection_writes.py +227 -0
- harborrag_runtime/application/control_plane/connections.py +749 -0
- harborrag_runtime/application/control_plane/effect_recovery.py +235 -0
- harborrag_runtime/application/control_plane/projects.py +313 -0
- harborrag_runtime/application/control_plane/settings_import.py +239 -0
- harborrag_runtime/application/control_plane/settings_service.py +233 -0
- harborrag_runtime/application/control_plane/tenants.py +55 -0
- harborrag_runtime/application/errors.py +110 -0
- harborrag_runtime/application/ingestion/__init__.py +1 -0
- harborrag_runtime/application/ingestion/client.py +65 -0
- harborrag_runtime/application/ingestion/connection_sources.py +96 -0
- harborrag_runtime/application/ingestion/direct.py +120 -0
- harborrag_runtime/application/ingestion/follower.py +83 -0
- harborrag_runtime/application/ingestion/ports.py +174 -0
- harborrag_runtime/application/ingestion/progress_bridge.py +221 -0
- harborrag_runtime/application/ingestion/retry_selection.py +71 -0
- harborrag_runtime/application/ingestion/service.py +564 -0
- harborrag_runtime/application/ingestion/starter.py +78 -0
- harborrag_runtime/application/memory/__init__.py +21 -0
- harborrag_runtime/application/memory/client.py +114 -0
- harborrag_runtime/application/memory/composition.py +90 -0
- harborrag_runtime/application/memory/context.py +45 -0
- harborrag_runtime/application/memory/sessions.py +117 -0
- harborrag_runtime/application/ports.py +133 -0
- harborrag_runtime/application/retrieval/__init__.py +1 -0
- harborrag_runtime/application/retrieval/access.py +26 -0
- harborrag_runtime/application/scheduling/service.py +58 -0
- harborrag_runtime/application/schemas.py +15 -0
- harborrag_runtime/chat/README.md +40 -0
- harborrag_runtime/chat/__init__.py +18 -0
- harborrag_runtime/chat/composition.py +53 -0
- harborrag_runtime/chat/facade.py +46 -0
- harborrag_runtime/chat/service.py +183 -0
- harborrag_runtime/chat/tenant_clients.py +341 -0
- harborrag_runtime/chat/tenant_config.py +235 -0
- harborrag_runtime/chat/tenant_models.py +22 -0
- harborrag_runtime/composition/__init__.py +5 -0
- harborrag_runtime/composition/config_store.py +53 -0
- harborrag_runtime/composition/connection_removal.py +120 -0
- harborrag_runtime/composition/connection_stores.py +107 -0
- harborrag_runtime/composition/control_plane.py +401 -0
- harborrag_runtime/composition/mcp_auth.py +39 -0
- harborrag_runtime/composition/readers.py +113 -0
- harborrag_runtime/composition/resources.py +336 -0
- harborrag_runtime/composition/storage_providers.py +113 -0
- harborrag_runtime/config/__init__.py +84 -0
- harborrag_runtime/config/checkout.py +212 -0
- harborrag_runtime/config/chunking.py +167 -0
- harborrag_runtime/config/connectors/__init__.py +19 -0
- harborrag_runtime/config/connectors/fingerprints.py +54 -0
- harborrag_runtime/config/connectors/importing.py +611 -0
- harborrag_runtime/config/connectors/loader.py +95 -0
- harborrag_runtime/config/connectors/models/__init__.py +30 -0
- harborrag_runtime/config/connectors/models/atlassian.py +199 -0
- harborrag_runtime/config/connectors/models/builder.py +208 -0
- harborrag_runtime/config/connectors/models/common.py +45 -0
- harborrag_runtime/config/connectors/models/fields.py +134 -0
- harborrag_runtime/config/connectors/models/github.py +143 -0
- harborrag_runtime/config/connectors/models/local_file.py +119 -0
- harborrag_runtime/config/connectors/models/registry.py +149 -0
- harborrag_runtime/config/connectors/models/sharepoint.py +186 -0
- harborrag_runtime/config/connectors/models/spec.py +563 -0
- harborrag_runtime/config/connectors/parsing.py +275 -0
- harborrag_runtime/config/connectors/providers.py +177 -0
- harborrag_runtime/config/connectors/schemas.py +215 -0
- harborrag_runtime/config/errors.py +26 -0
- harborrag_runtime/config/graph_build.py +335 -0
- harborrag_runtime/config/graph_build_loading.py +38 -0
- harborrag_runtime/config/loading.py +245 -0
- harborrag_runtime/config/models.py +95 -0
- harborrag_runtime/config/ontology_loading.py +76 -0
- harborrag_runtime/config/parsers/__init__.py +19 -0
- harborrag_runtime/config/parsers/loader.py +85 -0
- harborrag_runtime/config/parsers/parsing.py +285 -0
- harborrag_runtime/config/parsers/providers.py +305 -0
- harborrag_runtime/config/parsers/schemas.py +614 -0
- harborrag_runtime/config/settings.py +378 -0
- harborrag_runtime/config/settings_document.py +133 -0
- harborrag_runtime/config/temporal.py +331 -0
- harborrag_runtime/config/temporal_loading.py +310 -0
- harborrag_runtime/config_store/__init__.py +5 -0
- harborrag_runtime/config_store/defaults.py +47 -0
- harborrag_runtime/config_store/provider.py +153 -0
- harborrag_runtime/config_store/run_settings.py +72 -0
- harborrag_runtime/connections/__init__.py +27 -0
- harborrag_runtime/connections/secrets.py +348 -0
- harborrag_runtime/contracts.py +237 -0
- harborrag_runtime/document_stage_catalog.py +82 -0
- harborrag_runtime/errors.py +64 -0
- harborrag_runtime/events/__init__.py +3 -0
- harborrag_runtime/events/in_process.py +100 -0
- harborrag_runtime/execution/__init__.py +26 -0
- harborrag_runtime/execution/contracts.py +32 -0
- harborrag_runtime/execution/direct.py +169 -0
- harborrag_runtime/execution/gateway.py +97 -0
- harborrag_runtime/execution/source_conversion.py +61 -0
- harborrag_runtime/execution/source_submission.py +278 -0
- harborrag_runtime/execution/submission.py +64 -0
- harborrag_runtime/execution/task_registry.py +387 -0
- harborrag_runtime/execution/temporal.py +165 -0
- harborrag_runtime/ingestion/README.md +79 -0
- harborrag_runtime/ingestion/__init__.py +59 -0
- harborrag_runtime/ingestion/chunking_profile.py +97 -0
- harborrag_runtime/ingestion/composition.py +279 -0
- harborrag_runtime/ingestion/connection_checks.py +239 -0
- harborrag_runtime/ingestion/connections.py +507 -0
- harborrag_runtime/ingestion/document/__init__.py +1 -0
- harborrag_runtime/ingestion/document/normalization.py +14 -0
- harborrag_runtime/ingestion/document/normalizers/__init__.py +9 -0
- harborrag_runtime/ingestion/document/normalizers/builtins.py +39 -0
- harborrag_runtime/ingestion/document/normalizers/transform.py +30 -0
- harborrag_runtime/ingestion/document/run_tools.py +152 -0
- harborrag_runtime/ingestion/failure_classification.py +82 -0
- harborrag_runtime/ingestion/field_indexes.py +56 -0
- harborrag_runtime/ingestion/maintenance/__init__.py +1 -0
- harborrag_runtime/ingestion/maintenance/connection_removal.py +115 -0
- harborrag_runtime/ingestion/maintenance/projection_admin.py +180 -0
- harborrag_runtime/ingestion/maintenance/relation_repair.py +555 -0
- harborrag_runtime/ingestion/maintenance/retention.py +353 -0
- harborrag_runtime/ingestion/observability.py +3 -0
- harborrag_runtime/ingestion/profiles.py +127 -0
- harborrag_runtime/ingestion/runtime_builder.py +339 -0
- harborrag_runtime/ingestion/source/__init__.py +1 -0
- harborrag_runtime/ingestion/source/descriptor_mapping.py +144 -0
- harborrag_runtime/ingestion/source/discovery.py +290 -0
- harborrag_runtime/ingestion/source/documents.py +184 -0
- harborrag_runtime/ingestion/source/finalization.py +288 -0
- harborrag_runtime/ingestion/source/operations.py +175 -0
- harborrag_runtime/ingestion/source/plan.py +413 -0
- harborrag_runtime/ingestion/source/retry.py +150 -0
- harborrag_runtime/ingestion/source/service.py +470 -0
- harborrag_runtime/ingestion/source/skips.py +172 -0
- harborrag_runtime/ingestion_contracts.py +107 -0
- harborrag_runtime/memory/__init__.py +81 -0
- harborrag_runtime/memory/access.py +47 -0
- harborrag_runtime/memory/administration.py +460 -0
- harborrag_runtime/memory/context/__init__.py +5 -0
- harborrag_runtime/memory/context/result.py +20 -0
- harborrag_runtime/memory/directory.py +166 -0
- harborrag_runtime/memory/erasure.py +122 -0
- harborrag_runtime/memory/locks.py +170 -0
- harborrag_runtime/memory/messages.py +202 -0
- harborrag_runtime/memory/presenters.py +117 -0
- harborrag_runtime/memory/projects.py +38 -0
- harborrag_runtime/memory/tiers/__init__.py +1 -0
- harborrag_runtime/memory/tiers/in_memory.py +491 -0
- harborrag_runtime/memory/titles.py +32 -0
- harborrag_runtime/memory/usage.py +131 -0
- harborrag_runtime/observability/__init__.py +1 -0
- harborrag_runtime/observability/connector_metrics.py +14 -0
- harborrag_runtime/observability/discovery_metrics.py +46 -0
- harborrag_runtime/observability/model.py +410 -0
- harborrag_runtime/observability/observability_types.py +47 -0
- harborrag_runtime/observability/stage_observation.py +96 -0
- harborrag_runtime/observability/tool_audit.py +17 -0
- harborrag_runtime/plugins.py +56 -0
- harborrag_runtime/rate_limiting.py +39 -0
- harborrag_runtime/reader_contracts.py +149 -0
- harborrag_runtime/retrieval/__init__.py +1 -0
- harborrag_runtime/retrieval/composition.py +174 -0
- harborrag_runtime/retrieval/facades.py +224 -0
- harborrag_runtime/scheduling/__init__.py +48 -0
- harborrag_runtime/scheduling/config.py +185 -0
- harborrag_runtime/scheduling/errors.py +37 -0
- harborrag_runtime/scheduling/models.py +146 -0
- harborrag_runtime/scheduling/service.py +297 -0
- harborrag_runtime/scheduling/validation.py +213 -0
- harborrag_runtime/sdk/__init__.py +86 -0
- harborrag_runtime/sdk/configuration.py +44 -0
- harborrag_runtime/sdk/facades.py +47 -0
- harborrag_runtime/sdk/runtime.py +242 -0
- harborrag_runtime/security/__init__.py +1 -0
- harborrag_runtime/security/api_key_operations.py +236 -0
- harborrag_runtime/serialization.py +35 -0
- harborrag_runtime/temporal/__init__.py +19 -0
- harborrag_runtime/temporal/activity_observability.py +313 -0
- harborrag_runtime/temporal/client.py +580 -0
- harborrag_runtime/temporal/connection.py +39 -0
- harborrag_runtime/temporal/conversion.py +147 -0
- harborrag_runtime/temporal/dispatch.py +38 -0
- harborrag_runtime/temporal/document_workflow.py +154 -0
- harborrag_runtime/temporal/failure_handling.py +68 -0
- harborrag_runtime/temporal/gateway.py +121 -0
- harborrag_runtime/temporal/heartbeats.py +67 -0
- harborrag_runtime/temporal/identity.py +14 -0
- harborrag_runtime/temporal/ingestion_activities.py +435 -0
- harborrag_runtime/temporal/maintenance_activities.py +251 -0
- harborrag_runtime/temporal/maintenance_schemas.py +88 -0
- harborrag_runtime/temporal/optional.py +19 -0
- harborrag_runtime/temporal/plan_resolver.py +149 -0
- harborrag_runtime/temporal/policies.py +18 -0
- harborrag_runtime/temporal/reindex_workflow.py +71 -0
- harborrag_runtime/temporal/retry_activities.py +147 -0
- harborrag_runtime/temporal/retry_workflow.py +222 -0
- harborrag_runtime/temporal/sandbox.py +51 -0
- harborrag_runtime/temporal/schedules.py +341 -0
- harborrag_runtime/temporal/schemas.py +482 -0
- harborrag_runtime/temporal/source_activities.py +590 -0
- harborrag_runtime/temporal/source_batch_workflow.py +226 -0
- harborrag_runtime/temporal/source_workflow.py +908 -0
- harborrag_runtime/temporal/submission.py +23 -0
- harborrag_runtime/temporal/worker.py +478 -0
- harborrag_runtime/temporal/worker_registry.py +141 -0
- harborrag_runtime/temporal_models.py +227 -0
- harborrag_runtime/testing/chat_fakes.py +309 -0
- harborrag_runtime/testing/chat_service_double.py +126 -0
- harborrag_runtime/testing/connection_fakes.py +278 -0
- harborrag_runtime/testing/memory_fakes.py +128 -0
- harborrag_runtime/topology/__init__.py +1 -0
- harborrag_runtime/topology/budgeted_extractor.py +110 -0
- harborrag_runtime/topology/chunk_runner.py +41 -0
- harborrag_runtime/topology/composition.py +168 -0
- harborrag_runtime/topology/configuration.py +126 -0
- harborrag_runtime/topology/contextual.py +140 -0
- harborrag_runtime/topology/derived.py +238 -0
- harborrag_runtime/topology/derived_coverage.py +5 -0
- harborrag_runtime/topology/derived_dispatch.py +63 -0
- harborrag_runtime/topology/derived_factory.py +175 -0
- harborrag_runtime/topology/derived_models.py +251 -0
- harborrag_runtime/topology/derived_projection.py +127 -0
- harborrag_runtime/topology/description_factory.py +81 -0
- harborrag_runtime/topology/embedding_profile.py +85 -0
- harborrag_runtime/topology/entity_summary_index.py +304 -0
- harborrag_runtime/topology/maintenance.py +126 -0
- harborrag_runtime/topology/operations.py +277 -0
- harborrag_runtime/topology/parent_materializer.py +167 -0
- harborrag_runtime/topology/run_cost.py +43 -0
- harborrag_runtime/topology/security_operations.py +46 -0
- harborrag_runtime/topology/service.py +349 -0
- harborrag_runtime/topology/summary_factory.py +164 -0
- harborrag_runtime/topology/summary_inputs.py +129 -0
- harborrag_runtime/topology/summary_operations.py +128 -0
- harborrag_runtime/topology/summary_policy.py +39 -0
- harborrag_runtime/topology/summary_products.py +43 -0
- harborrag_runtime/topology/summary_service.py +625 -0
- harborrag_runtime/topology/summary_worker.py +129 -0
- harborrag_runtime/topology/summary_workflow.py +32 -0
- harborrag_runtime/topology/worker.py +101 -0
- harborrag_runtime/topology/workflow.py +32 -0
- harborrag_runtime-2.0.0.dist-info/METADATA +267 -0
- harborrag_runtime-2.0.0.dist-info/RECORD +271 -0
- harborrag_runtime-2.0.0.dist-info/WHEEL +4 -0
- harborrag_runtime-2.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Value types of the connection service: who writes, what is written and what it returns.
|
|
2
|
+
|
|
3
|
+
Leaf module of the connection use cases; it imports none of them.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping, Sequence
|
|
9
|
+
from dataclasses import dataclass
|
|
10
|
+
from typing import Any, Literal, Protocol
|
|
11
|
+
|
|
12
|
+
from harborrag_core.contracts.errors import HarborError
|
|
13
|
+
from harborrag_core.domain.connection import Connection
|
|
14
|
+
from harborrag_core.ingestion import IngestionTask
|
|
15
|
+
from harborrag_core.security import AccessContext
|
|
16
|
+
from harborrag_engine.ingestion.models import IngestionCreateCommand
|
|
17
|
+
|
|
18
|
+
WriteOrigin = Literal["api", "operator"]
|
|
19
|
+
"""Who writes: ``api`` callers may not set new ``env:`` references or operator-only
|
|
20
|
+
fields such as a worker file path; the CLI (``operator``) may."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConnectionRuns(Protocol):
|
|
24
|
+
"""Ingestion runs and published-document counts of one connection."""
|
|
25
|
+
|
|
26
|
+
async def latest_for_connection(
|
|
27
|
+
self, *, tenant_id: str, connection_id: str
|
|
28
|
+
) -> IngestionTask | None:
|
|
29
|
+
"""Return the connection's most recently submitted run, or ``None`` when it has none."""
|
|
30
|
+
|
|
31
|
+
async def active_for_connection(
|
|
32
|
+
self, *, tenant_id: str, connection_id: str
|
|
33
|
+
) -> IngestionTask | None:
|
|
34
|
+
"""Return the connection's run that has not ended, or ``None`` when it has none."""
|
|
35
|
+
|
|
36
|
+
async def connection_document_count(self, *, tenant_id: str, connection_id: str) -> int:
|
|
37
|
+
"""Count the documents the connection's runs have recorded."""
|
|
38
|
+
|
|
39
|
+
async def get(self, task_id: str) -> IngestionTask | None:
|
|
40
|
+
"""Return the run ``task_id``, or ``None`` when it does not exist."""
|
|
41
|
+
|
|
42
|
+
async def progress(self, task_id: str) -> dict[str, int]:
|
|
43
|
+
"""Return the run's document counts by result status."""
|
|
44
|
+
|
|
45
|
+
async def progress_for_tasks(self, task_ids: Sequence[str]) -> dict[str, dict[str, int]]:
|
|
46
|
+
"""Return the document counts by result status for each of ``task_ids``."""
|
|
47
|
+
|
|
48
|
+
async def source_scope_ids_by_connection(
|
|
49
|
+
self, *, tenant_id: str, connection_ids: Sequence[str]
|
|
50
|
+
) -> dict[str, list[str]]:
|
|
51
|
+
"""Return the source scopes each connection, by ``source_key``, was ingested under."""
|
|
52
|
+
|
|
53
|
+
async def allowed_source_scope_ids(
|
|
54
|
+
self, tenant_id: str, *, access: AccessContext, limit: int = 10000
|
|
55
|
+
) -> tuple[str, ...]:
|
|
56
|
+
"""Return every source scope ``access`` may read in ``tenant_id``.
|
|
57
|
+
|
|
58
|
+
Raises:
|
|
59
|
+
HarborConflictError: The reader may read more than ``limit`` source scopes.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class IngestionSubmitter(Protocol):
|
|
64
|
+
"""Start an ingestion through the shared submission path; tell when a run ended unrecorded."""
|
|
65
|
+
|
|
66
|
+
async def submit(
|
|
67
|
+
self, command: IngestionCreateCommand, *, idempotency_key: str | None
|
|
68
|
+
) -> dict[str, object]:
|
|
69
|
+
"""Register an ingestion and start its workflow; the accepted task as a public document."""
|
|
70
|
+
|
|
71
|
+
async def settle_if_ended(self, task_id: str) -> bool:
|
|
72
|
+
"""Record the end of a run whose workflow ended unrecorded; whether the run is over."""
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass(frozen=True, slots=True)
|
|
76
|
+
class ConnectionWrite:
|
|
77
|
+
"""The body of a connection ``PUT`` or ``test``."""
|
|
78
|
+
|
|
79
|
+
source_type: str
|
|
80
|
+
credentials: Mapping[str, Any]
|
|
81
|
+
config: Mapping[str, Any]
|
|
82
|
+
name: str | None = None
|
|
83
|
+
tuning: Mapping[str, Any] | None = None
|
|
84
|
+
|
|
85
|
+
def effective_tuning(self, existing: Connection | None) -> Mapping[str, Any]:
|
|
86
|
+
"""Return the tuning to apply: as sent, else what is stored, else none."""
|
|
87
|
+
if self.tuning is not None:
|
|
88
|
+
return self.tuning
|
|
89
|
+
return existing.tuning if existing else {}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True, slots=True)
|
|
93
|
+
class ConnectionPutResult:
|
|
94
|
+
"""A stored connection, whether it was new, and what ``sync_now`` started or why it did not."""
|
|
95
|
+
|
|
96
|
+
connection: dict[str, Any]
|
|
97
|
+
created: bool
|
|
98
|
+
version: int
|
|
99
|
+
ingestion: dict[str, Any] | None = None
|
|
100
|
+
sync_error: HarborError | None = None
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass(frozen=True, slots=True)
|
|
104
|
+
class ConnectionKey:
|
|
105
|
+
"""The public identity of a connection inside one tenant."""
|
|
106
|
+
|
|
107
|
+
tenant_id: str
|
|
108
|
+
project_id: str
|
|
109
|
+
connection_id: str
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Run the check chain of a connection test off the event loop, under a deadline.
|
|
2
|
+
|
|
3
|
+
Checks run on a small pool of their own, with no retries and short request timeouts, so a
|
|
4
|
+
source that never answers cannot hold the API's shared threads after the deadline.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
from collections.abc import Mapping
|
|
11
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from harborrag_adapters.connectors.policies.network import DestinationPolicy
|
|
15
|
+
from harborrag_runtime.config.connectors.models import SourceTypeSpec, ValidatedConnection
|
|
16
|
+
from harborrag_runtime.config.errors import ConnectorConfigurationError
|
|
17
|
+
from harborrag_runtime.ingestion.connection_checks import (
|
|
18
|
+
ConnectionTestReport,
|
|
19
|
+
run_connection_checks,
|
|
20
|
+
timed_out_report,
|
|
21
|
+
)
|
|
22
|
+
from harborrag_runtime.ingestion.connections import build_connector_from_parts
|
|
23
|
+
|
|
24
|
+
from .connection_writes import merge_secrets
|
|
25
|
+
|
|
26
|
+
TEST_TIMEOUT_SECONDS = 15.0
|
|
27
|
+
TEST_REQUEST_TIMEOUT_SECONDS = 10.0
|
|
28
|
+
TEST_WORKERS = 4
|
|
29
|
+
_TEST_EXECUTOR = ThreadPoolExecutor(max_workers=TEST_WORKERS, thread_name_prefix="connection-test")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
async def run_checks( # noqa: PLR0913 - one connection test's inputs
|
|
33
|
+
spec: SourceTypeSpec,
|
|
34
|
+
validated: ValidatedConnection,
|
|
35
|
+
effective: Mapping[str, Any],
|
|
36
|
+
secret_values: Mapping[str, str],
|
|
37
|
+
tenant_id: str,
|
|
38
|
+
destination_policy: DestinationPolicy,
|
|
39
|
+
) -> ConnectionTestReport:
|
|
40
|
+
"""Build the connector and run its check chain, ending the wait at the deadline.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
spec: The connection's source type.
|
|
44
|
+
validated: The values that passed validation.
|
|
45
|
+
effective: The credentials as they would be stored, secrets included.
|
|
46
|
+
secret_values: The resolved secret values the connector reads.
|
|
47
|
+
tenant_id: Tenant the connector is built for.
|
|
48
|
+
destination_policy: Hosts the connector may reach beyond public addresses.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
The report of the checks, or a timed-out report when the deadline passed.
|
|
52
|
+
"""
|
|
53
|
+
credentials = merge_secrets(validated.credentials, effective, spec.secret_fields)
|
|
54
|
+
tuning = _test_tuning(spec, validated.tuning)
|
|
55
|
+
|
|
56
|
+
def check() -> ConnectionTestReport:
|
|
57
|
+
try:
|
|
58
|
+
connector = build_connector_from_parts(
|
|
59
|
+
spec.source_type,
|
|
60
|
+
tenant_id=tenant_id,
|
|
61
|
+
credentials=credentials,
|
|
62
|
+
config=validated.config,
|
|
63
|
+
tuning=tuning,
|
|
64
|
+
secret_values=secret_values,
|
|
65
|
+
destination_policy=destination_policy,
|
|
66
|
+
)
|
|
67
|
+
except ConnectorConfigurationError as error:
|
|
68
|
+
report = ConnectionTestReport()
|
|
69
|
+
report.passed("schema")
|
|
70
|
+
report.failed("reachable", "unreachable", str(error))
|
|
71
|
+
return report
|
|
72
|
+
return run_connection_checks(connector)
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
running = asyncio.get_running_loop().run_in_executor(_TEST_EXECUTOR, check)
|
|
76
|
+
return await asyncio.wait_for(running, TEST_TIMEOUT_SECONDS)
|
|
77
|
+
except TimeoutError:
|
|
78
|
+
return timed_out_report()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _test_tuning(spec: SourceTypeSpec, tuning: Mapping[str, Any]) -> dict[str, Any]:
|
|
82
|
+
"""Return ``tuning`` for a connection test: no retries, no request past a short timeout."""
|
|
83
|
+
capped = dict(tuning)
|
|
84
|
+
if "max_retries" in spec.tuning_fields:
|
|
85
|
+
capped["max_retries"] = 0
|
|
86
|
+
if "request_timeout_seconds" in spec.tuning_fields:
|
|
87
|
+
configured = capped.get("request_timeout_seconds")
|
|
88
|
+
capped["request_timeout_seconds"] = min(
|
|
89
|
+
configured or TEST_REQUEST_TIMEOUT_SECONDS, TEST_REQUEST_TIMEOUT_SECONDS
|
|
90
|
+
)
|
|
91
|
+
return capped
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""Rules for writing one connection: preconditions, secret handling, validation and the stored row.
|
|
2
|
+
|
|
3
|
+
Pure functions over a write and the stored connection; the service does the I/O around them.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from harborrag_core.contracts.errors import HarborImmutableFieldError, HarborPreconditionFailedError
|
|
12
|
+
from harborrag_core.domain.connection import Connection, ConnectionBuilder
|
|
13
|
+
from harborrag_core.security.secret_refs import SecretSource, all_refs
|
|
14
|
+
from harborrag_runtime.config.connectors.models import (
|
|
15
|
+
ConnectionValidationError,
|
|
16
|
+
FieldIssue,
|
|
17
|
+
SourceTypeSpec,
|
|
18
|
+
ValidatedConnection,
|
|
19
|
+
)
|
|
20
|
+
from harborrag_runtime.config.connectors.models.builder import validate_connection
|
|
21
|
+
from harborrag_runtime.config.connectors.models.spec import METHOD_KEY
|
|
22
|
+
from harborrag_runtime.connections import reuses_stored_reference
|
|
23
|
+
from harborrag_runtime.ingestion.connections import ConnectionUnavailableError
|
|
24
|
+
|
|
25
|
+
from .connection_models import ConnectionKey, ConnectionWrite, WriteOrigin
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def check_preconditions(
|
|
29
|
+
existing: Connection | None,
|
|
30
|
+
spec: SourceTypeSpec,
|
|
31
|
+
expected_version: int | None,
|
|
32
|
+
create_only: bool,
|
|
33
|
+
) -> None:
|
|
34
|
+
"""Refuse a write that would break an immutable field or a precondition, before any I/O."""
|
|
35
|
+
if existing is None:
|
|
36
|
+
if expected_version is not None:
|
|
37
|
+
raise HarborPreconditionFailedError("connection does not exist")
|
|
38
|
+
return
|
|
39
|
+
if existing.status == "deleting":
|
|
40
|
+
raise ConnectionUnavailableError(f"connection {existing.ref!r} is being deleted")
|
|
41
|
+
if existing.source_type != spec.source_type:
|
|
42
|
+
raise HarborImmutableFieldError(
|
|
43
|
+
"source_type cannot change; delete the connection or pick another connection_id",
|
|
44
|
+
field="source_type",
|
|
45
|
+
)
|
|
46
|
+
if create_only:
|
|
47
|
+
raise HarborPreconditionFailedError("connection already exists")
|
|
48
|
+
if expected_version is not None and expected_version != existing.version:
|
|
49
|
+
raise HarborPreconditionFailedError("connection version does not match")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def plan_credentials(
|
|
53
|
+
incoming: Mapping[str, Any], existing: Connection | None, spec: SourceTypeSpec
|
|
54
|
+
) -> tuple[dict[str, Any], frozenset[str]]:
|
|
55
|
+
"""Return the sent credentials without ``SecretStatus`` echoes, and the stored secrets kept.
|
|
56
|
+
|
|
57
|
+
A secret left out, echoed as its status, or sent back as the reference it
|
|
58
|
+
already holds keeps its stored value only when it belongs to the auth method
|
|
59
|
+
being written and no endpoint field changed.
|
|
60
|
+
|
|
61
|
+
Raises:
|
|
62
|
+
ConnectionValidationError: An endpoint changed while a stored secret would be kept.
|
|
63
|
+
"""
|
|
64
|
+
sent = {name: value for name, value in incoming.items() if not _is_status_echo(value)}
|
|
65
|
+
if existing is None:
|
|
66
|
+
return sent, frozenset()
|
|
67
|
+
try:
|
|
68
|
+
secrets = spec.method_secret_fields(sent.get(METHOD_KEY))
|
|
69
|
+
except KeyError:
|
|
70
|
+
return sent, frozenset()
|
|
71
|
+
keep = frozenset(name for name in secrets if name not in sent and name in existing.credentials)
|
|
72
|
+
reused = frozenset(
|
|
73
|
+
name
|
|
74
|
+
for name in secrets
|
|
75
|
+
if name in sent and reuses_stored_reference(sent[name], existing.credentials.get(name))
|
|
76
|
+
)
|
|
77
|
+
moved = sorted(
|
|
78
|
+
name
|
|
79
|
+
for name in spec.endpoints
|
|
80
|
+
if _endpoint(sent.get(name)) != _endpoint(existing.credentials.get(name))
|
|
81
|
+
)
|
|
82
|
+
if (keep or reused) and moved:
|
|
83
|
+
raise ConnectionValidationError(
|
|
84
|
+
[
|
|
85
|
+
FieldIssue(f"credentials.{name}", f"send it again: {', '.join(moved)} changed")
|
|
86
|
+
for name in sorted(keep | reused)
|
|
87
|
+
]
|
|
88
|
+
)
|
|
89
|
+
return sent, keep
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class ConcurrentWriteError(Exception):
|
|
93
|
+
"""A save without a precondition lost a race with another write."""
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def refuse_operator_fields(
|
|
97
|
+
spec: SourceTypeSpec, write: ConnectionWrite, existing: Connection | None, origin: WriteOrigin
|
|
98
|
+
) -> None:
|
|
99
|
+
"""Refuse an API write that only operators may make, such as setting a worker path.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
ConnectionValidationError: ``origin`` is ``api`` and an operator-only field
|
|
103
|
+
changes or an API-only install rule refuses the config.
|
|
104
|
+
"""
|
|
105
|
+
if origin != "api":
|
|
106
|
+
return
|
|
107
|
+
issues = [
|
|
108
|
+
*spec.operator_field_issues(write.credentials, existing.credentials if existing else None),
|
|
109
|
+
*spec.api_policy_issues(write.config),
|
|
110
|
+
]
|
|
111
|
+
if issues:
|
|
112
|
+
raise ConnectionValidationError(issues)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def secret_sources(origin: WriteOrigin) -> frozenset[SecretSource]:
|
|
116
|
+
"""Return the reference sources a caller may set: ``env:`` only from operator paths."""
|
|
117
|
+
return frozenset({"stored", "env"}) if origin == "operator" else frozenset({"stored"})
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _is_status_echo(value: Any) -> bool:
|
|
121
|
+
"""Report whether ``value`` is a ``SecretStatus`` sent back as read."""
|
|
122
|
+
return isinstance(value, Mapping) and "set" in value and "secret_ref" not in value
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _endpoint(value: Any) -> str | None:
|
|
126
|
+
"""Normalize an endpoint URL for comparison; blank counts as unset."""
|
|
127
|
+
text = str(value).strip().rstrip("/").casefold() if value is not None else ""
|
|
128
|
+
return text or None
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def effective_credentials(
|
|
132
|
+
sent: Mapping[str, Any], existing: Connection | None, keep: frozenset[str]
|
|
133
|
+
) -> dict[str, Any]:
|
|
134
|
+
"""Credentials as they will be once the write applies, for validation only."""
|
|
135
|
+
kept = {name: existing.credentials[name] for name in keep} if existing else {}
|
|
136
|
+
return {**kept, **{name: value for name, value in sent.items() if value is not None}}
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def validate_write(
|
|
140
|
+
spec: SourceTypeSpec,
|
|
141
|
+
credentials: Mapping[str, Any],
|
|
142
|
+
write: ConnectionWrite,
|
|
143
|
+
existing: Connection | None,
|
|
144
|
+
tenant_id: str,
|
|
145
|
+
) -> ValidatedConnection:
|
|
146
|
+
"""Check the write against the source type's schemas, then against its connector.
|
|
147
|
+
|
|
148
|
+
Raises:
|
|
149
|
+
ConnectionValidationError: A schema or the connector rejects the values.
|
|
150
|
+
"""
|
|
151
|
+
return validate_connection(
|
|
152
|
+
spec, credentials, write.config, write.effective_tuning(existing), tenant_id=tenant_id
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def build_connection( # noqa: PLR0913 - the parts of one stored connection
|
|
157
|
+
key: ConnectionKey,
|
|
158
|
+
existing: Connection | None,
|
|
159
|
+
spec: SourceTypeSpec,
|
|
160
|
+
write: ConnectionWrite,
|
|
161
|
+
credentials: dict[str, Any],
|
|
162
|
+
validated: ValidatedConnection,
|
|
163
|
+
actor: str,
|
|
164
|
+
) -> Connection:
|
|
165
|
+
"""Assemble the connection to store; a write that may fix it clears an attention status."""
|
|
166
|
+
builder = (
|
|
167
|
+
ConnectionBuilder.from_existing(existing)
|
|
168
|
+
if existing
|
|
169
|
+
else ConnectionBuilder.for_new(
|
|
170
|
+
tenant_id=key.tenant_id, project_id=key.project_id, connection_id=key.connection_id
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
builder.source_type(spec.source_type).name(write.name).credentials(credentials)
|
|
174
|
+
builder.config(validated.config).tuning(validated.tuning)
|
|
175
|
+
builder.secret_refs(all_refs(credentials)).actor(actor)
|
|
176
|
+
if existing is not None and (
|
|
177
|
+
existing.status == "error"
|
|
178
|
+
or existing.credentials != credentials
|
|
179
|
+
or (existing.status == "needs_reauth" and _sends_secret_value(write, spec))
|
|
180
|
+
):
|
|
181
|
+
builder.status("active")
|
|
182
|
+
return builder.build()
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _sends_secret_value(write: ConnectionWrite, spec: SourceTypeSpec) -> bool:
|
|
186
|
+
"""Report whether the write carries a secret as a value, not a status or reference."""
|
|
187
|
+
return any(
|
|
188
|
+
isinstance(write.credentials.get(name), str) and write.credentials.get(name)
|
|
189
|
+
for name in spec.secret_fields
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def unchanged(before: Connection, after: Connection) -> bool:
|
|
194
|
+
"""Report whether a write would store exactly what ``before`` holds."""
|
|
195
|
+
return (
|
|
196
|
+
before.name == after.name
|
|
197
|
+
and before.status == after.status
|
|
198
|
+
and before.status_detail == after.status_detail
|
|
199
|
+
and not changed_fields(before, after)
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def changed_fields(before: Connection, after: Connection) -> list[str]:
|
|
204
|
+
"""Names of fields a write changed, as ``name`` or ``<part>.<field>``."""
|
|
205
|
+
changed = ["name"] if before.name != after.name else []
|
|
206
|
+
for part, label in (
|
|
207
|
+
("credentials", "credentials"),
|
|
208
|
+
("config", "config"),
|
|
209
|
+
("tuning", "advanced"),
|
|
210
|
+
):
|
|
211
|
+
old: Mapping[str, Any] = getattr(before, part)
|
|
212
|
+
new: Mapping[str, Any] = getattr(after, part)
|
|
213
|
+
changed.extend(
|
|
214
|
+
f"{label}.{name}"
|
|
215
|
+
for name in sorted(set(old) | set(new))
|
|
216
|
+
if old.get(name) != new.get(name)
|
|
217
|
+
)
|
|
218
|
+
return changed
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def merge_secrets(
|
|
222
|
+
validated: Mapping[str, Any], secrets: Mapping[str, Any], secret_fields: frozenset[str]
|
|
223
|
+
) -> dict[str, Any]:
|
|
224
|
+
"""Merge the validated non-secret fields with the secret fields as ``secrets`` holds them."""
|
|
225
|
+
merged = {name: value for name, value in validated.items() if name not in secret_fields}
|
|
226
|
+
merged.update({name: value for name, value in secrets.items() if name in secret_fields})
|
|
227
|
+
return merged
|