test(grpc-data): Gateway origin backend for the history rig — read-only AVEVA bindings, reverse/end-only H07, strict-ceiling H08 (#923)
This commit is contained in:
@@ -8,6 +8,7 @@ import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import socket
|
||||
import tempfile
|
||||
import time
|
||||
from urllib.parse import urljoin, urlsplit
|
||||
@@ -147,17 +148,40 @@ def _plain_host(value):
|
||||
return re.split(r"[,:/\\]", value, maxsplit=1)[0]
|
||||
|
||||
|
||||
def validate_fixture_endpoints(document, *, local_services):
|
||||
# #923: the one non-run-owned endpoint a `--history-backend gateway` run may name — the HOST-run read-only HistorianGateway,
|
||||
# reached from the containers through the Docker host alias, and only under this exact key.
|
||||
HOST_GATEWAY_ALIAS = "host.docker.internal"
|
||||
GATEWAY_ENDPOINT_KEY = "ServerHistorian__Endpoint"
|
||||
|
||||
|
||||
def validate_host_gateway_endpoint(value):
|
||||
"""A plaintext h2c URL `http://host.docker.internal:<port>` — no credentials, path, query or fragment."""
|
||||
try:
|
||||
parsed = urlsplit(value) if isinstance(value, str) else None
|
||||
port = parsed.port if parsed is not None else None
|
||||
except ValueError:
|
||||
parsed, port = None, None
|
||||
if (parsed is None or parsed.scheme != "http" or parsed.hostname != HOST_GATEWAY_ALIAS or port is None
|
||||
or parsed.username is not None or parsed.password is not None or parsed.path not in ("", "/")
|
||||
or parsed.query or parsed.fragment):
|
||||
raise RigError("the gateway endpoint must be http://host.docker.internal:<port>")
|
||||
return value
|
||||
|
||||
|
||||
def validate_fixture_endpoints(document, *, local_services, host_gateway_keys=()):
|
||||
"""Reject endpoint escape in resolved Compose/config/seed data, without DNS.
|
||||
|
||||
Pass resolved data, never a YAML file with uninterpreted environment expansion.
|
||||
Image registries are build inputs; endpoint URLs, SQL connection hosts, and
|
||||
host/server/address configuration values must name an explicit local service
|
||||
or loopback. No arbitrary private subnet is treated as local.
|
||||
or loopback. No arbitrary private subnet is treated as local. The only exception
|
||||
is `host_gateway_keys` (a gateway-backend run passes `ServerHistorian__Endpoint`):
|
||||
a value under exactly such a key must be the host-run gateway URL and nothing else.
|
||||
"""
|
||||
services = set(local_services)
|
||||
if not services or any(not isinstance(name, str) or not _SERVICE_PATTERN.fullmatch(name) for name in services):
|
||||
raise RigError("an explicit local service allowlist is required")
|
||||
host_gateway_keys = frozenset(host_gateway_keys)
|
||||
|
||||
def check_host(host):
|
||||
if not _host_allowed(host, services):
|
||||
@@ -174,6 +198,9 @@ def validate_fixture_endpoints(document, *, local_services):
|
||||
elif isinstance(value, str):
|
||||
if "${" in value:
|
||||
raise RigError("fixture configuration must be fully resolved")
|
||||
if key in host_gateway_keys:
|
||||
validate_host_gateway_endpoint(value)
|
||||
return
|
||||
urls = _URL_PATTERN.findall(value)
|
||||
for url in urls:
|
||||
try:
|
||||
@@ -476,7 +503,11 @@ NODES = tuple(f"{pair}-{n}" for pair in ("control", "source", "consumer") for n
|
||||
# `questdb` exists only on a `--history-backend questdb` run (compose.history.questdb.yml), `opcplc` only on a `--conditions`
|
||||
# run (compose.conditions.yml); listing them is the DNS allowlist.
|
||||
SERVICES = ("sql", "migrator", "device", "seeder", "questdb", "opcplc", *NODES)
|
||||
HISTORY_BACKENDS = ("sqlserver", "questdb")
|
||||
HISTORY_BACKENDS = ("sqlserver", "questdb", "gateway")
|
||||
# #923 gateway backend defaults: the host-run read-only gateway's loopback gRPC port, and the fixture tags bound read-only to
|
||||
# real historian system tags (1 Hz UInt2 seconds counter, a 10 s Float, a string). Overridable through the same env names.
|
||||
GATEWAY_DEFAULT_ENDPOINT = "http://host.docker.internal:5231"
|
||||
GATEWAY_DEFAULT_TAGS = "counter=SysTimeSec,flt=SysDataAcqOverallItemsPerSec,text=SysString"
|
||||
|
||||
|
||||
def run_command(argv, *, timeout=120, environment=None):
|
||||
@@ -676,6 +707,8 @@ class LocalRig:
|
||||
files.extend(["--file", str(HERE / "compose.history.yml")])
|
||||
if self.environment.get("GRPC_DATA_HISTORY_BACKEND") == "questdb":
|
||||
files.extend(["--file", str(HERE / "compose.history.questdb.yml")])
|
||||
if self.environment.get("GRPC_DATA_HISTORY_BACKEND") == "gateway":
|
||||
files.extend(["--file", str(HERE / "compose.history.gateway.yml")])
|
||||
if self.environment.get("GRPC_DATA_CONDITIONS") == "1":
|
||||
files.extend(["--file", str(HERE / "compose.conditions.yml")])
|
||||
return files
|
||||
@@ -873,6 +906,18 @@ class LocalRig:
|
||||
raise RigError("unknown history backend")
|
||||
settings["GRPC_DATA_HISTORY"] = "1"
|
||||
settings["GRPC_DATA_HISTORY_BACKEND"] = history_backend
|
||||
if history_backend == "gateway":
|
||||
endpoint = validate_host_gateway_endpoint(os.environ.get("GRPC_DATA_HISTORY_GATEWAY_ENDPOINT") or GATEWAY_DEFAULT_ENDPOINT)
|
||||
tags = os.environ.get("GRPC_DATA_HISTORY_GATEWAY_TAGS") or GATEWAY_DEFAULT_TAGS
|
||||
if not re.fullmatch(r"[a-z]+=[A-Za-z0-9_.]+(?:,[a-z]+=[A-Za-z0-9_.]+)*", tags):
|
||||
raise RigError("gateway tag bindings must be name=historianTag[,...]")
|
||||
# Fail before any build: the gateway is not run-owned, so `up` cannot start it.
|
||||
try:
|
||||
socket.create_connection(("127.0.0.1", urlsplit(endpoint).port), timeout=5).close()
|
||||
except OSError:
|
||||
raise RigError("the host-run HistorianGateway is not listening on the host loopback") from None
|
||||
settings["GRPC_DATA_HISTORY_GATEWAY_ENDPOINT"] = endpoint
|
||||
settings["GRPC_DATA_HISTORY_GATEWAY_TAGS"] = tags
|
||||
if conditions:
|
||||
settings["GRPC_DATA_CONDITIONS"] = "1"
|
||||
atomic_write_secret(self.folder / "environment.json", json.dumps(settings))
|
||||
@@ -889,7 +934,8 @@ class LocalRig:
|
||||
# builds can still be collected by exact observed object IDs on `down`.
|
||||
atomic_write_secret(self.folder / "setup.json", json.dumps({"run_id": self.run_id, "docker_context": self.context["Name"], "project": self.project}))
|
||||
resolved = json.loads(self.compose("config", "--format", "json"))
|
||||
validate_fixture_endpoints(resolved, local_services=SERVICES)
|
||||
validate_fixture_endpoints(resolved, local_services=SERVICES,
|
||||
host_gateway_keys={GATEWAY_ENDPOINT_KEY} if settings.get("GRPC_DATA_HISTORY_BACKEND") == "gateway" else ())
|
||||
try:
|
||||
self.compose("build", "--pull", "migrator", "device", "control-1", "seeder", timeout=1800)
|
||||
self.compose("up", "--detach", "sql", "device", "migrator", *NODES, timeout=timeout)
|
||||
@@ -1350,7 +1396,8 @@ def main(argv=None):
|
||||
parser.add_argument("--conditions", action="store_true", help="up only: add the A2a conditions overlay (AlarmSubscribe exports + one scripted alarm)")
|
||||
parser.add_argument("--history", action="store_true", help="up only: add the H2 history overlay (SOURCE historian + history seed profile)")
|
||||
parser.add_argument("--history-backend", choices=HISTORY_BACKENDS, default=None,
|
||||
help="up --history only: the SOURCE historian backend (sqlserver = the run's SQL container, default; questdb = a run-owned QuestDB)")
|
||||
help="up --history only: the SOURCE historian backend (sqlserver = the run's SQL container, default; questdb = a run-owned QuestDB; "
|
||||
"gateway = a host-run read-only HistorianGateway, README)")
|
||||
arguments = parser.parse_args(argv)
|
||||
try:
|
||||
if arguments.action == "preflight":
|
||||
|
||||
Reference in New Issue
Block a user