From 642ba1decfdb882711cf7addc671a38d603d502d Mon Sep 17 00:00:00 2001 From: d-robotics Date: Sun, 27 Sep 2026 02:33:28 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20isolated=20internal=20test?= =?UTF-8?q?=20lane=20preparation=20and=20seed=20tooling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../issues/01-environment.md | 11 + .scratch/internal-test-lane/issues/02-seed.md | 11 + .../internal-test-lane/issues/03-lifecycle.md | 11 + .../issues/04-integration.md | 11 + .scratch/internal-test-lane/map.md | 6 +- .scratch/internal-test-lane/spec.md | 88 +++ cmd/internal-test-seed/main.go | 146 +++++ docs/internal-test-lane.md | 52 ++ internal/testlane/apply.go | 345 ++++++++++ internal/testlane/plan.go | 619 ++++++++++++++++++ internal/testlane/plan_test.go | 223 +++++++ scripts/internal_test_lane.py | 96 +++ scripts/internal_test_lane/__init__.py | 1 + .../internal_test_lane/config.example.json | 48 ++ scripts/internal_test_lane/credentials.py | 70 ++ scripts/internal_test_lane/environment.py | 453 +++++++++++++ scripts/internal_test_lane/lifecycle.py | 457 +++++++++++++ .../make_synthetic_bundle.py | 63 ++ tests/internal_test_lane/test_credentials.py | 79 +++ tests/internal_test_lane/test_environment.py | 142 ++++ tests/internal_test_lane/test_lifecycle.py | 176 +++++ 21 files changed, 3107 insertions(+), 1 deletion(-) create mode 100644 .scratch/internal-test-lane/issues/01-environment.md create mode 100644 .scratch/internal-test-lane/issues/02-seed.md create mode 100644 .scratch/internal-test-lane/issues/03-lifecycle.md create mode 100644 .scratch/internal-test-lane/issues/04-integration.md create mode 100644 .scratch/internal-test-lane/spec.md create mode 100644 cmd/internal-test-seed/main.go create mode 100644 docs/internal-test-lane.md create mode 100644 internal/testlane/apply.go create mode 100644 internal/testlane/plan.go create mode 100644 internal/testlane/plan_test.go create mode 100644 scripts/internal_test_lane.py create mode 100644 scripts/internal_test_lane/__init__.py create mode 100644 scripts/internal_test_lane/config.example.json create mode 100644 scripts/internal_test_lane/credentials.py create mode 100644 scripts/internal_test_lane/environment.py create mode 100644 scripts/internal_test_lane/lifecycle.py create mode 100644 tests/internal_test_lane/make_synthetic_bundle.py create mode 100644 tests/internal_test_lane/test_credentials.py create mode 100644 tests/internal_test_lane/test_environment.py create mode 100644 tests/internal_test_lane/test_lifecycle.py diff --git a/.scratch/internal-test-lane/issues/01-environment.md b/.scratch/internal-test-lane/issues/01-environment.md new file mode 100644 index 00000000..00ec400a --- /dev/null +++ b/.scratch/internal-test-lane/issues/01-environment.md @@ -0,0 +1,11 @@ +# Render the isolated environment + +Type: task +Status: claimed +Owner: test_environment (gpt-6-sol, high) +Blocked by: none + +Implement spec component 1 using Python stdlib. Own environment renderer and its +public-boundary tests. Do not touch existing production templates or access any cluster. +Validate test identity, pinned images, separate authenticated DB/workspace, secret +references, bounded processor and resource/network constraints. diff --git a/.scratch/internal-test-lane/issues/02-seed.md b/.scratch/internal-test-lane/issues/02-seed.md new file mode 100644 index 00000000..217adb57 --- /dev/null +++ b/.scratch/internal-test-lane/issues/02-seed.md @@ -0,0 +1,11 @@ +# Seed independent task and role identities + +Type: task +Status: claimed +Owner: test_seed (gpt-6-sol, high) +Blocked by: none + +Implement spec component 3. Own cmd/internal-test-seed and internal/testlane. +Use normalized offline sample bundles, not production collection cloning. Add +fresh-target guard, plan/confirm, real application-compatible identities/dictionaries, +five private role credentials and scratch/snapshot support. Test synthetic inputs only. diff --git a/.scratch/internal-test-lane/issues/03-lifecycle.md b/.scratch/internal-test-lane/issues/03-lifecycle.md new file mode 100644 index 00000000..39924f44 --- /dev/null +++ b/.scratch/internal-test-lane/issues/03-lifecycle.md @@ -0,0 +1,11 @@ +# Import, prepare, archive and clean bounded test batches + +Type: task +Status: claimed +Owner: test_lifecycle (gpt-6-sol, high) +Blocked by: none + +Implement spec component 2. Own CLI and lifecycle module/tests. Coordinate public +interfaces with tickets 01/02. No network or apply commands. Validate ownership, +paths, digest and batch state; keep templates and latest three results; confirm +old batch cleanup without following symlinks or touching unowned data. diff --git a/.scratch/internal-test-lane/issues/04-integration.md b/.scratch/internal-test-lane/issues/04-integration.md new file mode 100644 index 00000000..be163a1e --- /dev/null +++ b/.scratch/internal-test-lane/issues/04-integration.md @@ -0,0 +1,11 @@ +# Integrate, verify and document the test lane + +Type: task +Status: claimed +Owner: root +Blocked by: 01, 02, 03 (final integration) + +Prepare independent synthetic assets and a new local Mongo target; verify commands +and real business API flows after integration. Add usage/recovery/limitations guide, +run targeted and full checks, obtain parallel standards/spec review from 17e7de7e, +resolve findings and commit locally. No production data reads/writes or deployment. diff --git a/.scratch/internal-test-lane/map.md b/.scratch/internal-test-lane/map.md index 5e14e62b..f009e114 100644 --- a/.scratch/internal-test-lane/map.md +++ b/.scratch/internal-test-lane/map.md @@ -1,7 +1,7 @@ # 内部测试链路:需求讨论 Type: grilling -Status: awaiting-final-confirmation +Status: claimed ## Notes @@ -118,3 +118,7 @@ Q1–Q3 均已采用推荐项。 - 取得针对样本读取/复制、测试资源/账号创建及部署的具体授权后才执行。设计回答不等于对未知清单或资源成本的授权。 - 测试账号与数据只在测试环境产生;任何生产代码、工作流、账号、成员授权或客户任务修改不在此方案范围内。 - 自动清理、自动客户池补领、多供应商授权功能、多人压力测试及自动生产部署不在首版范围。 + +## Implementation — 2026-09-27 + +用户已要求本地实现,并确认从 `17e7de7e` 审查;核验受控命令、部署清单及独立本地合成业务流程。实际客户样本复制和集群部署仍需另行批准。实现票见 [issues](issues/),范围见 [spec.md](spec.md)。 diff --git a/.scratch/internal-test-lane/spec.md b/.scratch/internal-test-lane/spec.md new file mode 100644 index 00000000..cbb6a7d6 --- /dev/null +++ b/.scratch/internal-test-lane/spec.md @@ -0,0 +1,88 @@ +# Internal test lane — implementation baseline + +Status: ready-for-agent +Baseline: 17e7de7e + +The user confirmed Q1–Q13 and requested implementation on 2026-09-27. See +[map.md](map.md) and [ADR-0004](../../docs/adr/0004-isolated-internal-test-lane.md). +The current turn authorizes local implementation and synthetic validation only: +no customer database, BOS assets, production deployment or live credentials are accessed. +Actual cluster provisioning and the six real sample transfers remain separately approved operations. + +## User-facing outcome + +An internal developer can prepare a small, independently runnable environment +using the same application/business UI, five isolated role accounts and a fixed +six-source sample pack (three BLF-data, three SL-data). They can reproduce a +known state or start from slicing, archive results and explicitly prepare the +next batch/version without putting customer work into a test queue. + +## Delivery components + +1. Offline environment renderer and configuration validation. Fixed test namespace, + distinct host, authenticated dedicated Mongo, per-batch database and PVCs, + separate API and bounded annotation processor, no automatic upstream jobs. + Secrets are supplied privately, never embedded in tracked config or status. + Production deployment templates and routing remain unchanged. +2. Offline approved-bundle importer and batch lifecycle CLI. Six samples, content + hashes, local paths and ownership are validated before copying. No network + source discovery, no implicit customer data fetch, no arbitrary raw Mongo restore. +3. Guarded Go seed command. Validate and plan before connecting, fresh test target + only; create full independent project/run/batch/task/artifact/revision/member + identities and dictionary snapshots. Five passwords supplied through a private + file. No production account/session/lease/callback/source-output reuse. +4. Local synthetic integration verification and operating guide. Distinguish + prepared, seeded and business-verified states; no claim of deployed/accepted + solely because manifests were rendered or records inserted. + +## Explicit behavior + +- Test supplier `test`; platform admin plus supplier manager, annotator, quality + reviewer and acceptance reviewer. Customer identities are not cloned. +- Templates retain source metadata only for traceability. Work IDs and output + paths are new. Scratch templates start unclaimed at slicing. Snapshot templates + preserve reviewed sample slice/action content without copying live leases; + dependent media must be reconstructed or imported consistently before review. +- Required media includes both camera videos, both PTS series and the IMU data + required by materialization. Every referenced input is a copied local test asset. +- Public production hostname/IP, database names and mount credentials cannot be + selected by the controlled tool. Changing test project name alone is not isolation. +- At most six active test tasks per environment. No auto-refill from customer pool. +- No public production apply command in the tooling. Renderer outputs a reviewable + package; authorized operator deployment is a separate operation with target checks. +- Test APIs and processors must not start before fixture initialization is complete. +- Version changes use a fresh per-batch database/PVC mapping, preserving the previous + batch; old code is not automatically pointed at a newer migrated database. +- Archive requires explicit evidence of stopped/detached processing. Preparing or + archiving local files is not proof the remote environment is idle. The guide must + explain this human verification limit. +- Keep fixed templates and latest three completed batch results. Earlier batch + local package cleanup is previewed and hash-confirmed; do not erase source + templates, active/unknown batches, newer results or anything outside owned paths. + Cluster PVC/database removal is a separately approved, documented operation. +- One slice-processing worker. CPU, memory and storage quotas are configurable + within safe finite bounds. If resources are insufficient, testing waits/stops. + No pressure testing, production resource reduction, customer delivery/notification. +- Same immutable app image is used for eventual promotion, with production runtime + settings retained. Candidate success does not authorize production release. + +## Confirmed verification seams + +The user explicitly accepted these on 2026-09-27: + +- Controlled CLI behavior and refusal of unsafe targets/paths/states. +- Generated Kubernetes resource manifests and isolation/resource/auth invariants. +- Real API workflows against a new local synthetic database and synthetic assets. + +Use meaningful public-boundary tests. Run scoped tests during implementation, +typecheck/build, then full relevant suites at the end. Review against baseline +17e7de7e along standards and specification axes. Existing full-suite failures +must be reported separately; do not claim a wholly green repository. + +## Current-turn acceptance + +Local commands, manifests, initialization, lifecycle and synthetic workflow evidence +are runnable and documented. No company access is required to demonstrate refusal +checks and business behavior. Real sample inventory, exact company allocation, +external ingress/storage prerequisites, cluster deployment and capacity validation +remain explicitly unverified until separately authorized; do not fabricate them. diff --git a/cmd/internal-test-seed/main.go b/cmd/internal-test-seed/main.go new file mode 100644 index 00000000..f8b9e14a --- /dev/null +++ b/cmd/internal-test-seed/main.go @@ -0,0 +1,146 @@ +package main + +import ( + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "os" + "strings" + "time" + + "droboticflow/internal/testlane" +) + +func main() { + if err := run(os.Args[1:]); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } +} + +func run(args []string) error { + if len(args) == 0 { + return errors.New("usage: internal-test-seed plan|apply [flags]") + } + switch args[0] { + case "plan": + return runPlan(args[1:]) + case "apply": + return runApply(args[1:]) + default: + return errors.New("usage: internal-test-seed plan|apply [flags]") + } +} + +func runPlan(args []string) error { + f := flag.NewFlagSet("plan", flag.ContinueOnError) + manifestPath := f.String("manifest", "", "offline normalized manifest") + assetsRoot := f.String("assets-root", "", "copied asset import root") + workspaceRoot := f.String("workspace-root", testlane.RuntimeAssetsRoot, "isolated runtime workspace root") + batch := f.String("batch", "", "unique internal test batch ID") + out := f.String("out", "", "private plan file") + if err := f.Parse(args); err != nil { + return err + } + if f.NArg() != 0 || *manifestPath == "" || *assetsRoot == "" || *batch == "" || *out == "" { + return errors.New("plan requires --manifest, --assets-root, --batch and --out") + } + m, err := loadManifest(*manifestPath) + if err != nil { + return err + } + p, err := testlane.BuildPlan(*batch, m, *assetsRoot, *workspaceRoot) + if err != nil { + return err + } + data, err := json.MarshalIndent(p, "", " ") + if err != nil { + return err + } + data = append(data, '\n') + file, err := os.OpenFile(*out, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return err + } + if _, err = file.Write(data); err != nil { + file.Close() + return err + } + if err = file.Close(); err != nil { + return err + } + return json.NewEncoder(os.Stdout).Encode(map[string]any{"batchId": p.BatchID, "database": p.Database, "planSha256": p.SHA256, "projectIds": p.ProjectIDs, "sampleCount": len(p.Samples), "assetCount": len(p.Assets), "userIds": p.UserIDs}) +} + +func runApply(args []string) error { + f := flag.NewFlagSet("apply", flag.ContinueOnError) + planPath := f.String("plan", "", "plan file") + manifestPath := f.String("manifest", "", "same normalized manifest") + assetsRoot := f.String("assets-root", "", "copied asset import root") + accountsPath := f.String("accounts-file", "", "private generated account credentials file") + mongoURIPath := f.String("mongo-uri-file", "", "private dedicated Mongo URI file") + confirm := f.String("confirm", "", "exact plan SHA256 from plan output") + if err := f.Parse(args); err != nil { + return err + } + if f.NArg() != 0 || *planPath == "" || *manifestPath == "" || *assetsRoot == "" || *accountsPath == "" || *mongoURIPath == "" || *confirm == "" { + return errors.New("apply requires --plan, --manifest, --assets-root, --accounts-file, --mongo-uri-file and --confirm") + } + data, err := os.ReadFile(*planPath) + if err != nil { + return err + } + var p testlane.Plan + if err := decodeExact(data, &p); err != nil { + return err + } + if *confirm != p.SHA256 { + return errors.New("--confirm does not match plan SHA256") + } + m, err := loadManifest(*manifestPath) + if err != nil { + return err + } + accountsData, err := testlane.ReadPrivateFile(*accountsPath) + if err != nil { + return err + } + var accounts testlane.AccountsFile + if err := decodeExact(accountsData, &accounts); err != nil { + return err + } + uriData, err := testlane.ReadPrivateFile(*mongoURIPath) + if err != nil { + return err + } + uri := strings.TrimSpace(string(uriData)) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + result, err := testlane.Apply(ctx, p, m, *assetsRoot, uri, accounts) + if err != nil { + return err + } + return json.NewEncoder(os.Stdout).Encode(result) +} + +func loadManifest(path string) (testlane.Manifest, error) { + data, err := os.ReadFile(path) + if err != nil { + return testlane.Manifest{}, err + } + return testlane.DecodeManifest(data) +} +func decodeExact(data []byte, target any) error { + decoder := json.NewDecoder(strings.NewReader(string(data))) + decoder.DisallowUnknownFields() + if err := decoder.Decode(target); err != nil { + return err + } + if decoder.Decode(&struct{}{}) != io.EOF { + return errors.New("JSON has trailing data") + } + return nil +} diff --git a/docs/internal-test-lane.md b/docs/internal-test-lane.md new file mode 100644 index 00000000..3f0afee4 --- /dev/null +++ b/docs/internal-test-lane.md @@ -0,0 +1,52 @@ +# 内部测试环境操作指南 + +本指南对应 [ADR-0004](adr/0004-isolated-internal-test-lane.md) 和[实施基线](../.scratch/internal-test-lane/spec.md)。受控命令只在本地验证六份已批准材料、生成环境清单和批次计划;它不会访问客户数据库、BOS 或集群,也不会部署、创建账号或证明业务验收。实际样本读取与复制、集群资源创建和部署须分别取得具体授权。部署前再次核对 kubeconfig 的集群、namespace 和资源额度;不要从配置默认值推断线上状态。 + +## 离线准备 + +使用仓库内的 `local-data/internal-test-lane/` 作为私有根目录。它被 Git 忽略,导入的原始模板和批次记录不能加入提交。一次批次的配置为 JSON:`schemaVersion: 1`、独立测试 `batchId` 和 `hostname`、生产入口列表 `productionOrigins`、以 SHA256 固定的 `image` 与 `mongoImage`、完整 `buildCommit`、`workspaceGi`、`mongoGi`,以及 API、processor、Mongo 各自的 CPU/内存 requests 和 limits;还需要 TLS Secret 名、镜像拉取 Secret 名和入口控制器 namespace。`verify-config` 会拒绝已知生产入口、IP 主机名、其他 namespace、非固定镜像及越界资源值。资源值和外部入口、存储类能力仍需获准只读盘点后确认。 + +素材包 `bundle.json` 的 `schemaVersion` 为 `droboticflow.internal-test-seed/v1`。它含 `dictionaries`、六个 `samples` 和 `assets` 清单。BLF-data、SL-data 各三条完整 part,样本键使用小写字母、数字和连字符;至少各有一种 `scratch` 与 `snapshot` 模板。每个样本包含 `source.captureId`、正数 `durationSeconds` 和 `media` 中的双目视频、双目 PTS、IMU 路径;可额外含 `sourceManifest`。所有路径相对素材包目录,位于 `templates//`。`assets` 为每个引用文件列出相同路径、字节数和 SHA256。快照还需明确阶段、切片和动作;舍弃切片的原因必须存在于所属项目的舍弃原因字典快照中。字典和快照使用获准样本的真实内容,不能伪造已有审查或交付结果。导入前另行审查六个来源 ID/版本、文件清单与总大小。 + +以下命令仅处理本地文件;示例路径由操作者替换为已审查文件: + +```sh +python3 scripts/internal_test_lane.py verify-config --config local-data/internal-test-lane/inputs/batch-config.json +python3 scripts/internal_test_lane.py import-bundle --bundle local-data/internal-test-lane/inputs/bundle.json +python3 scripts/internal_test_lane.py prepare --batch --import-id --config local-data/internal-test-lane/inputs/batch-config.json +python3 scripts/internal_test_lane.py status +python3 scripts/internal_test_lane.py credentials --config local-data/internal-test-lane/inputs/batch-config.json --out local-data/internal-test-lane/batches//credentials +``` + +`import-bundle` 校验目录与文件均非符号链接、文件大小和 SHA256,然后将原件复制到私有 `imports//`;再次使用同一包会验证现有导入,不覆盖。`prepare` 在批次唯一且上批已归档时生成 `environment.json`、`initializer.json`、`config.json`、`seed-plan.json` 和 `batch.json`。前两个是供审查的 Kubernetes 资源清单和临时初始化 Pod 清单;本地 seed plan 只用于预览身份、素材和目标,不可直接用于集群初始化。`batch.json` 绑定源码提交、镜像、配置、导入版本和清单摘要,状态仅为 `prepared`,远端状态为 `unknown`。未完成的目录必须人工调查,工具不将其当作成功。 + +`credentials` 在新的私有目录中生成 0600 的 `secret.json`、`accounts.json` 和 `mongo-uri.txt`,标准输出仅返回路径。`secret.json` 含测试 Mongo root/app 凭证及 API token;`accounts.json` 含平台管理员、`test` 供应商管理员、标注员、质检员和终审员五个独立账号。不要打印、提交或转存这些内容到工单。账号使用者由审批清单指定。数据库 URI 指向本批次 Mongo 服务的完整集群 DNS 名和独立数据库;运行服务只应持有该测试权限。 + +## 经另行批准后的集群顺序 + +以下是操作顺序,不是当前授权,也不是自动发布脚本。集群写入前须有该次部署的明确批准,并立即核对目标集群和 `droboticflow-internal-test` namespace;样本读取、测试存储写入和凭证处理分别遵循已批准范围。先核实 namespace、网络策略、配额、Ingress/TLS、存储类与节点可用资源。空间不足时保持暂停,不缩减生产资源。 + +1. 审查 `environment.json`、`initializer.json`、私有 Secret 清单及 source/inventory 记录。环境清单建立独立 namespace、配额与网络策略、数据库和工作区 PVC;Mongo 目标副本为 1,API 与 processor 初始副本为 0。不要提前打开业务入口。测试 PVC 和数据库按批次独立,旧版本不能自动指向新批次迁移后的数据库。 +2. 在获准测试 namespace 创建清单与私有 Secret,等待独立 Mongo 就绪。只在隔离工作区准备临时 initializer Pod,上传**已复制并核验**的模板文件、规范化 `manifest.json`、匹配源码版本的 Linux seed 可执行文件及必要的私有账号/URI 文件。临时 Pod 不是长期服务;测试 API、processor 不带客户读取凭证。 +3. 在 initializer 内对 `/workspace` 的文件重新运行 `internal-test-seed plan --manifest --assets-root /workspace --workspace-root /workspace --batch --out `。本地预览计划绑定的是本地导入路径,不能跨机器复用。核对新计划的六个样本、两项目、五账号、目标数据库和摘要后,以**同一** manifest、素材根目录与计划摘要运行 `internal-test-seed apply --plan --manifest --assets-root /workspace --accounts-file --mongo-uri-file --confirm `。该命令只接受专用测试 URI 和空数据库;若中途失败,保持停机并调查部分写入,不绕过拒绝检查。 +4. 核实 seed 完成标记及素材路径/哈希,删除 initializer Pod 和暂存的私有文件,然后才将 API 与 processor 调至各 1 个副本。处理器切片并发为 1。确认入口明确标为测试、五角色登录和越权拒绝,再通过原业务界面完成领取、切片、标注、送检、质检、终审和测试交付。输出留在测试工作区,不进入客户出口。 + +`snapshot` 模板保留经审查的切片/动作内容,但 seed 不伪造送检、质检结论或切片生成产物。尤其是 `quality_review` 快照的切片媒体与正式审核证据必须通过真实业务提交和处理流程生成并核验;不能仅凭 `requestedStage` 字段宣称已进入质检。正常、退回和恢复路径的结果及未验证项应写入批次验收记录。测试通过不等于获准发布生产。 + +## 归档与清理 + +换版本前停止新测试并等待在途处理结束,在集群中确认服务停用、相关处理和工作区已从旧批次分离;保留结果及对应提交、镜像和配置。然后用本地证据 JSON(仅 `batchId`、`tasksComplete: true`、`environmentStopped: true`、`detached: true`、`reviewer`)归档: + +```sh +python3 scripts/internal_test_lane.py archive --batch --evidence local-data/internal-test-lane/inputs/-archive.json +python3 scripts/internal_test_lane.py status +``` + +这是操作者陈述的记录,工具无法从本地证明远端已经停止或验收完成。固定模板和最新三个已归档批次保留;更旧批次先预览再确认: + +```sh +python3 scripts/internal_test_lane.py cleanup-plan --batch +python3 scripts/internal_test_lane.py clean --batch --confirm +``` + +`clean` 重新校验预览中文件的路径、大小和 SHA256,只删除该批次拥有的本地目录;导入模板不被清理。远端 PVC、数据库、Secret、Ingress 或工作区**没有自动清理**,不得因本地 `clean` 成功声称集群资源已删除。远端清理由内部管理员另拟精确清单、备份/保留判断及授权后实施;不删除或修改客户数据。 diff --git a/internal/testlane/apply.go b/internal/testlane/apply.go new file mode 100644 index 00000000..8d5c8a8c --- /dev/null +++ b/internal/testlane/apply.go @@ -0,0 +1,345 @@ +package testlane + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "net" + "net/url" + "os" + "path/filepath" + "regexp" + "strings" + "time" + + "droboticflow/internal/domain" + "droboticflow/internal/store" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "golang.org/x/crypto/bcrypt" +) + +type Account struct { + Role string `json:"role"` + Name string `json:"name"` + Phone string `json:"phone"` + Password string `json:"password"` +} +type AccountsFile struct { + Accounts []Account `json:"accounts"` +} +type ApplyResult struct { + Database string `json:"database"` + PlanSHA256 string `json:"planSha256"` + Created bool `json:"created"` + Projects int `json:"projects"` + Samples int `json:"samples"` + Accounts int `json:"accounts"` +} + +func ValidateMongoURI(raw, database, batch string) error { + u, err := url.Parse(raw) + if err != nil || u.Scheme != "mongodb" || u.RawQuery == "" && u.Host == "" { + return errors.New("Mongo URI must use a guarded mongodb:// target") + } + if u.Fragment != "" || u.Opaque != "" || strings.Contains(u.Host, ",") { + return errors.New("Mongo URI target must be a single host") + } + host, port, err := net.SplitHostPort(u.Host) + if err != nil { + return errors.New("Mongo URI must specify an allowed host and port") + } + clusterHost := "internal-test-" + batch + "-mongo.droboticflow-internal-test.svc.cluster.local" + local := (host == "localhost" || host == "127.0.0.1") && port == "27019" + cluster := host == clusterHost && port == "27017" + if !(local || cluster) { + return errors.New("Mongo URI target is outside the internal test lane") + } + if strings.TrimPrefix(u.Path, "/") != database { + return errors.New("Mongo URI database differs from batch database") + } + for key := range u.Query() { + switch strings.ToLower(key) { + case "authsource", "directconnection", "serverselectiontimeoutms", "connecttimeoutms", "timeoutms", "tls": + default: + return errors.New("Mongo URI contains unsupported option") + } + } + if auth := u.Query().Get("authSource"); auth != "" && !((local && auth == "admin") || (cluster && auth == database)) { + return errors.New("Mongo URI authSource outside the internal test lane") + } + return nil +} + +func ReadPrivateFile(path string) ([]byte, error) { + absolute, err := filepath.Abs(path) + if err != nil { + return nil, err + } + for current := absolute; ; current = filepath.Dir(current) { + info, err := os.Lstat(current) + if err != nil { + return nil, err + } + if info.Mode()&os.ModeSymlink != 0 { + return nil, errors.New("private file path contains symlink") + } + if current == filepath.Dir(current) { + break + } + } + info, err := os.Lstat(path) + if err != nil { + return nil, err + } + if !info.Mode().IsRegular() || info.Mode().Perm()&0o077 != 0 { + return nil, errors.New("private file must be a regular file with mode 0600 or stricter") + } + return os.ReadFile(path) +} + +func ValidateAccounts(a AccountsFile) error { + if len(a.Accounts) != 5 { + return errors.New("exactly five accounts are required") + } + roles := map[string]bool{"admin": false, "supplier_manager": false, "annotator": false, "quality_reviewer": false, "acceptance_reviewer": false} + phones := map[string]bool{} + phonePattern := regexp.MustCompile(`^1[3-9][0-9]{9}$`) + for _, account := range a.Accounts { + seen, okay := roles[account.Role] + if !okay || seen { + return errors.New("account roles must be the five distinct test roles") + } + roles[account.Role] = true + if strings.TrimSpace(account.Name) == "" || !phonePattern.MatchString(account.Phone) || phones[account.Phone] || len(account.Password) < 16 { + return errors.New("account name, unique phone, or generated password invalid") + } + phones[account.Phone] = true + } + return nil +} + +func Apply(ctx context.Context, p Plan, m Manifest, assetsRoot, mongoURI string, accounts AccountsFile) (ApplyResult, error) { + result := ApplyResult{Database: p.Database, PlanSHA256: p.SHA256, Projects: 2, Samples: 6, Accounts: 5} + if err := VerifyPlan(p, m, assetsRoot); err != nil { + return ApplyResult{}, err + } + if err := ValidateAccounts(accounts); err != nil { + return ApplyResult{}, err + } + if err := ValidateMongoURI(mongoURI, p.Database, p.BatchID); err != nil { + return ApplyResult{}, err + } + client, err := mongo.Connect(ctx, options.Client().ApplyURI(mongoURI).SetServerSelectionTimeout(5*time.Second)) + if err != nil { + return ApplyResult{}, errors.New("internal test Mongo connection failed") + } + defer client.Disconnect(context.Background()) + db := client.Database(p.Database) + collections, err := db.ListCollectionNames(ctx, bson.D{}) + if err != nil { + return ApplyResult{}, errors.New("internal test database preflight failed") + } + if len(collections) > 0 { + var marker struct { + Kind string `bson:"kind"` + PlanSHA256 string `bson:"planSha256"` + BatchID string `bson:"batchId"` + State string `bson:"state"` + } + if err := db.Collection("_internalTestSeed").FindOne(ctx, bson.M{"_id": "environment"}).Decode(&marker); err == nil && marker.Kind == "droboticflow-internal-test-lane" && marker.PlanSHA256 == p.SHA256 && marker.BatchID == p.BatchID && marker.State == "complete" { + for _, check := range []struct { + name string + want int64 + }{{"projects", 2}, {"users", 5}, {"annotationTasks", 6}, {"humanItems", 6}, {"humanBatches", 6}} { + count, err := db.Collection(check.name).CountDocuments(ctx, bson.D{}) + if err != nil || count != check.want { + return ApplyResult{}, errors.New("completed seed marker has inconsistent record counts") + } + } + result.Created = false + return result, nil + } + return ApplyResult{}, errors.New("target database is populated; internal test seed only accepts a fresh empty database") + } + if _, err := db.Collection("_internalTestSeed").InsertOne(ctx, bson.M{"_id": "environment", "kind": "droboticflow-internal-test-lane", "planSha256": p.SHA256, "batchId": p.BatchID, "state": "seeding", "createdAt": time.Now().UTC().Format(time.RFC3339)}); err != nil { + return ApplyResult{}, errors.New("internal test database marker creation failed") + } + if err := insertSeed(ctx, db, p, m, accounts); err != nil { + return ApplyResult{}, errors.New("seed stopped; partial test database must be inspected before retry") + } + _, err = db.Collection("_internalTestSeed").UpdateOne(ctx, bson.M{"_id": "environment", "planSha256": p.SHA256, "state": "seeding"}, bson.M{"$set": bson.M{"state": "complete", "completedAt": time.Now().UTC().Format(time.RFC3339)}}) + if err != nil { + return ApplyResult{}, errors.New("internal test database marker completion failed") + } + result.Created = true + return result, nil +} + +func insertSeed(ctx context.Context, db *mongo.Database, p Plan, m Manifest, accounts AccountsFile) error { + s := store.NewMongoStore(db) + now := time.Now().UTC().Format(time.RFC3339) + if err := s.UpsertOrganization(ctx, domain.OrganizationRecord{ID: p.OrganizationID, Name: "Internal test lane", Slug: "internal-test-" + p.BatchID, Status: domain.OrganizationStatusActive, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + if err := s.UpsertSupplier(ctx, domain.SupplierRecord{ID: p.SupplierID, OrganizationID: p.OrganizationID, Name: "test", Status: "active", CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + for _, a := range accounts.Accounts { + hash, err := bcrypt.GenerateFromPassword([]byte(a.Password), bcrypt.DefaultCost) + if err != nil { + return err + } + supplier := p.SupplierID + if a.Role == "admin" { + supplier = "" + } + if err := s.UpsertUser(ctx, domain.UserRecord{ID: p.UserIDs[a.Role], OrganizationID: p.OrganizationID, Name: a.Name, Phone: a.Phone, Role: domain.UserRole(a.Role), SupplierID: supplier, PasswordHash: string(hash), Status: domain.UserStatusActive, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + } + adminID := p.UserIDs["admin"] + for _, name := range []string{"BLF-data", "SL-data"} { + projectID := p.ProjectIDs[name] + if err := s.UpsertProject(ctx, domain.ProjectRecord{ID: projectID, OrganizationID: p.OrganizationID, Name: "Internal test " + name, Description: "Isolated internal test samples", OwnerUserID: adminID, Status: domain.ProjectStatusActive, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + for _, role := range []string{"supplier_manager", "annotator", "quality_reviewer", "acceptance_reviewer"} { + member := domain.ProjectMemberRecord{ID: newID("member"), OrganizationID: p.OrganizationID, ProjectID: projectID, UserID: p.UserIDs[role], SupplierID: p.SupplierID, Roles: domain.AccountProductionRoles(domain.UserRecord{Role: domain.UserRole(role)}), CreatedAt: now, UpdatedAt: now} + if err := s.UpsertProjectMember(ctx, member); err != nil { + return err + } + } + labels := domain.ProjectAnnotationLabelsRecord{ID: newID("project-labels"), OrganizationID: p.OrganizationID, ProjectID: projectID, UpdatedByUserID: adminID, UpdatedAt: now} + d := m.Dictionaries[name] + for _, group := range []struct { + kind string + values []string + target *[]string + }{{"slice", d.SliceNames, &labels.SliceLabelIDs}, {"action", d.Descriptions, &labels.LabelIDs}, {"discard_reason", d.DiscardReasons, &labels.DiscardReasonLabelIDs}} { + for _, text := range group.values { + id := stableLabelID(group.kind, text) + record := domain.AnnotationLabelRecord{ID: id, OrganizationID: p.OrganizationID, Kind: group.kind, Code: id, Name: text, NameKey: domain.DictionaryNameKey(group.kind, text), Status: domain.AnnotationLabelActive, Version: 1, CreatedByUserID: adminID, UpdatedByUserID: adminID, CreatedAt: now, UpdatedAt: now} + if err := s.UpsertAnnotationLabel(ctx, record); err != nil { + return err + } + *group.target = append(*group.target, id) + } + } + if err := s.UpsertProjectAnnotationLabels(ctx, labels); err != nil { + return err + } + if err := s.UpsertProjectSliceLabels(ctx, labels); err != nil { + return err + } + if err := s.UpsertProjectDiscardReasonLabels(ctx, labels); err != nil { + return err + } + } + assets := map[string]Asset{} + for _, a := range p.Assets { + assets[a.Path] = a + } + for i, sample := range m.Samples { + ids := p.Samples[i] + projectID := p.ProjectIDs[sample.Project] + workflowName := "Internal test " + sample.Key + if err := s.UpsertWorkflow(ctx, domain.WorkflowRecord{ID: ids.WorkflowID, OrganizationID: p.OrganizationID, ProjectID: projectID, Name: workflowName, Description: "Manual internal test sample", Type: domain.WorkflowTypeBatch, Status: domain.WorkflowStatus("idle"), Nodes: []domain.WorkflowNode{}, Edges: []domain.WorkflowEdge{}, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + ports := []struct { + role, rel, port string + typ domain.PortType + }{{"leftVideo", sample.Media.LeftVideo, "fullLeftVideos", domain.PortTypeVideo}, {"rightVideo", sample.Media.RightVideo, "fullRightVideos", domain.PortTypeVideo}, {"leftPTS", sample.Media.LeftPTS, "leftTimestamps", domain.PortTypeFile}, {"rightPTS", sample.Media.RightPTS, "rightTimestamps", domain.PortTypeFile}, {"imu", sample.Media.IMU, "imu", domain.PortTypeFile}} + if sample.Media.SourceManifest != "" { + ports = append(ports, struct { + role, rel, port string + typ domain.PortType + }{"sourceManifest", sample.Media.SourceManifest, "sourceManifest", domain.PortTypeFile}) + } + artifacts := make([]domain.ExecutionArtifact, 0, len(ports)) + bindings := make([]domain.NodeInputBinding, 0, len(ports)) + for _, port := range ports { + asset := assets[port.rel] + artifact := domain.ExecutionArtifact{ID: ids.ArtifactIDs[port.role], RunID: ids.RunID, NodeID: "source", PortID: port.port, Name: filepath.Base(port.rel), Type: port.typ, Path: filepath.Join(p.WorkspaceRoot, filepath.FromSlash(port.rel)), SizeBytes: asset.Size, Metadata: &domain.ArtifactMetadata{SHA256: asset.SHA256}, Data: map[string]any{"collectionId": sample.Key}} + artifacts = append(artifacts, artifact) + bindings = append(bindings, domain.NodeInputBinding{TargetPortID: port.port, Artifacts: []domain.ExecutionArtifact{artifact}}) + } + run := domain.RunRecord{ID: ids.RunID, OrganizationID: p.OrganizationID, WorkflowID: ids.WorkflowID, WorkflowName: workflowName, ProjectID: projectID, ProjectName: "Internal test " + sample.Project, Status: domain.RunStatusWaitingForHuman, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentNodeID: "human", CaptureDirectories: []string{sample.Key}, Artifacts: artifacts, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "human", Status: domain.NodeExecutionStatusWaitingForHuman, InputArtifacts: artifacts, InputBindings: bindings}}, StartedAt: now, CreatedAt: now, UpdatedAt: now} + if err := s.UpsertRun(ctx, run); err != nil { + return err + } + dictionary := m.Dictionaries[sample.Project] + labels := make([]domain.SemanticLabelDefinition, 0, len(dictionary.Descriptions)) + for _, description := range dictionary.Descriptions { + labels = append(labels, domain.SemanticLabelDefinition{ID: stableLabelID("action", description), Name: description}) + } + if err := s.UpsertAnnotationTemplate(ctx, domain.AnnotationTemplateRecord{ID: ids.TemplateID, OrganizationID: p.OrganizationID, ProjectID: projectID, Name: "Test snapshot " + sample.Key, Version: 6, Status: domain.AnnotationTemplatePublished, Labels: labels, Phrases: dictionary.Descriptions, HumanStages: domain.DefaultFanyuV58HumanStages(), CreatedByUserID: adminID, PublishedByUserID: adminID, PublishedAt: now, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + if err := s.UpsertHumanBatch(ctx, domain.HumanBatchRecord{ID: ids.BatchID, OrganizationID: p.OrganizationID, PluginID: "annotation-qa", ProjectID: projectID, SupplierID: p.SupplierID, WorkflowID: ids.WorkflowID, RunID: ids.RunID, GateNodeID: "human", ProcessingProfile: domain.ProcessingProfileRawHuman, Status: domain.HumanBatchActive, Stages: domain.DefaultFanyuV58HumanStages(), ReleasePolicy: domain.HumanReleaseAllAccepted, MaxReworkRounds: 5, ItemCount: 1, RequirePrePackageAcceptance: true, CreatedAt: now, UpdatedAt: now}); err != nil { + return err + } + status, stage, revision := domain.AnnotationTaskStatusPendingAnnotation, domain.FanyuV58StageAnnotationCombined, 0 + itemStatus := domain.HumanItemReady + leftURL := "/api/runs/" + ids.RunID + "/artifacts/" + ids.ArtifactIDs["leftVideo"] + "/content" + rightURL := "/api/runs/" + ids.RunID + "/artifacts/" + ids.ArtifactIDs["rightVideo"] + "/content" + task := domain.AnnotationTaskRecord{ID: ids.TaskID, OrganizationID: p.OrganizationID, ProjectID: projectID, WorkflowID: ids.WorkflowID, RunID: ids.RunID, NodeID: "human", ArtifactID: ids.ArtifactIDs["leftVideo"], RightArtifactID: ids.ArtifactIDs["rightVideo"], SourceVideo: leftURL, RightSourceVideo: rightURL, ClipStart: 0, ClipEnd: sample.DurationSeconds, SourceIsFullVideo: true, SourceDurationSeconds: sample.DurationSeconds, CoverageStartSecond: 0, CoverageEndSecond: sample.DurationSeconds, CoverageMode: "full_common_range", TemplateID: ids.TemplateID, TemplateVersion: 6, SupplierID: p.SupplierID, ProcessingProfile: domain.ProcessingProfileRawHuman, Status: status, CurrentRevision: revision, CreatedAt: now, UpdatedAt: now} + if err := s.UpsertAnnotationTask(ctx, task); err != nil { + return err + } + payload := map[string]any{"rightArtifactId": ids.ArtifactIDs["rightVideo"], "captureDirectory": sample.Key, "sliceWorkflow": map[string]any{"status": "editing", "phase": "slice"}} + if sample.Snapshot != nil { + slices := make([]domain.SliceSegment, 0, len(sample.Snapshot.Slices)) + actions := make([]domain.AnnotationSegment, 0, len(sample.Snapshot.Actions)) + for _, v := range sample.Snapshot.Slices { + slice := domain.SliceSegment{ID: newID("slice"), Name: v.Name, Start: v.Start, End: v.End, DeliveryDisposition: v.DeliveryDisposition, DiscardReason: v.DiscardReason, AnnotationStatus: domain.SliceAnnotationPending} + if v.DeliveryDisposition == "discard" { + slice.DiscardReasonLabelID = stableLabelID("discard_reason", v.DiscardReason) + slice.DiscardReasonVersion = 1 + slice.DiscardReasonName = v.DiscardReason + } + slices = append(slices, slice) + } + for _, v := range sample.Snapshot.Actions { + labelID := "" + if !v.IsInvalid && contains(dictionary.Descriptions, v.Description) { + labelID = stableLabelID("action", v.Description) + } + status := "pending" + if v.IsInvalid { + status = "invalid" + } else if strings.TrimSpace(v.Description) != "" { + status = "described" + } + actions = append(actions, domain.AnnotationSegment{ID: newID("action"), Status: status, LabelID: labelID, Label: v.Description, Description: v.Description, Start: v.Start, End: v.End, IsInvalid: v.IsInvalid, Source: "internal-test-snapshot"}) + } + payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices, "segments": actions, "operations": []domain.SliceOperation{}, "statuses": map[string]any{}} + if sample.Snapshot.Stage == "quality_review" { + payload["requestedStage"] = "quality_review" + } + if err := s.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: ids.SliceRevisionID, OrganizationID: p.OrganizationID, TaskID: ids.TaskID, Revision: 0, Segments: slices, AuthorUserID: p.UserIDs["annotator"], CreatedAt: now}); err != nil { + return err + } + if err := s.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: ids.ActionRevisionID, OrganizationID: p.OrganizationID, TaskID: ids.TaskID, Revision: 0, TemplateVersion: 6, Segments: actions, AuthorUserID: p.UserIDs["annotator"], CreatedAt: now}); err != nil { + return err + } + } + item := domain.HumanItemRecord{ID: ids.ItemID, OrganizationID: p.OrganizationID, PluginID: "annotation-qa", BatchID: ids.BatchID, ProjectID: projectID, SupplierID: p.SupplierID, WorkflowID: ids.WorkflowID, RunID: ids.RunID, GateNodeID: "human", LegacyTaskID: ids.TaskID, ProcessingProfile: domain.ProcessingProfileRawHuman, Subject: domain.HumanSubjectRef{ArtifactID: ids.ArtifactIDs["leftVideo"], ItemKey: sample.Key, MediaType: "video/mp4", Payload: payload}, CurrentStageID: stage, Status: itemStatus, CurrentRevision: revision, MaxReworkRounds: 5, Version: 1, CreatedAt: now, UpdatedAt: now} + if err := s.UpsertHumanItem(ctx, item); err != nil { + return err + } + // Source IDs are audit-only metadata. They never occupy an active record ID. + if _, err := db.Collection("_internalTestSources").InsertOne(ctx, bson.M{"sampleKey": sample.Key, "project": sample.Project, "source": sample.Source, "newTaskId": ids.TaskID, "newRunId": ids.RunID}); err != nil { + return err + } + } + return nil +} + +func stableLabelID(project, value string) string { + h := sha256.Sum256([]byte(project + "\x00" + value)) + return "internal-test-label-" + hex.EncodeToString(h[:8]) +} diff --git a/internal/testlane/plan.go b/internal/testlane/plan.go new file mode 100644 index 00000000..03f862c0 --- /dev/null +++ b/internal/testlane/plan.go @@ -0,0 +1,619 @@ +// Package testlane constructs an isolated internal-test seed from a bounded, +// offline manifest. It never reads a customer database or object store. +package testlane + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "math" + "os" + "path" + "path/filepath" + "regexp" + "sort" + "strings" +) + +const SchemaVersion = "droboticflow.internal-test-seed/v1" +const RuntimeAssetsRoot = "/workspace" + +var batchPattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,31}$`) +var keyPattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,39}$`) + +type Dictionary struct { + SliceNames []string `json:"sliceNames"` + Descriptions []string `json:"descriptions"` + DiscardReasons []string `json:"discardReasons"` +} +type Source struct { + CaptureID string `json:"captureId"` + TaskID string `json:"taskId,omitempty"` + Version string `json:"version,omitempty"` +} +type Media struct { + LeftVideo string `json:"leftVideo"` + RightVideo string `json:"rightVideo"` + LeftPTS string `json:"leftPTS"` + RightPTS string `json:"rightPTS"` + IMU string `json:"imu"` + SourceManifest string `json:"sourceManifest,omitempty"` +} +type SnapshotSlice struct { + Start float64 `json:"start"` + End float64 `json:"end"` + Name string `json:"name,omitempty"` + DeliveryDisposition string `json:"deliveryDisposition,omitempty"` + DiscardReason string `json:"discardReason,omitempty"` +} +type SnapshotAction struct { + Start float64 `json:"start"` + End float64 `json:"end"` + Description string `json:"description,omitempty"` + IsInvalid bool `json:"isInvalid,omitempty"` +} +type Snapshot struct { + Stage string `json:"stage"` + Slices []SnapshotSlice `json:"slices"` + Actions []SnapshotAction `json:"actions"` +} +type Sample struct { + Key string `json:"key"` + Project string `json:"project"` + Mode string `json:"mode"` + Source Source `json:"source"` + Media Media `json:"media"` + DurationSeconds float64 `json:"durationSeconds"` + Snapshot *Snapshot `json:"snapshot,omitempty"` +} +type Manifest struct { + SchemaVersion string `json:"schemaVersion"` + Dictionaries map[string]Dictionary `json:"dictionaries"` + Samples []Sample `json:"samples"` +} +type Asset struct { + Path string `json:"path"` + SHA256 string `json:"sha256"` + Size int64 `json:"size"` +} +type SampleIDs struct { + Key string `json:"key"` + Project string `json:"project"` + Mode string `json:"mode"` + WorkflowID string `json:"workflowId"` + RunID string `json:"runId"` + BatchID string `json:"batchId"` + ItemID string `json:"itemId"` + TaskID string `json:"taskId"` + TemplateID string `json:"templateId"` + ArtifactIDs map[string]string `json:"artifactIds"` + SliceRevisionID string `json:"sliceRevisionId,omitempty"` + ActionRevisionID string `json:"actionRevisionId,omitempty"` +} +type Plan struct { + SchemaVersion string `json:"schemaVersion"` + BatchID string `json:"batchId"` + Database string `json:"database"` + AssetsRoot string `json:"assetsRoot"` + WorkspaceRoot string `json:"workspaceRoot"` + ManifestSHA256 string `json:"manifestSha256"` + OrganizationID string `json:"organizationId"` + SupplierID string `json:"supplierId"` + ProjectIDs map[string]string `json:"projectIds"` + UserIDs map[string]string `json:"userIds"` + Samples []SampleIDs `json:"samples"` + Assets []Asset `json:"assets"` + SHA256 string `json:"sha256"` +} + +func DecodeManifest(data []byte) (Manifest, error) { + var m Manifest + dec := json.NewDecoder(strings.NewReader(string(data))) + dec.DisallowUnknownFields() + if err := dec.Decode(&m); err != nil { + return m, err + } + if dec.Decode(&struct{}{}) != io.EOF { + return m, errors.New("manifest has trailing data") + } + if err := ValidateManifest(m); err != nil { + return m, err + } + return m, nil +} + +func ValidateManifest(m Manifest) error { + if m.SchemaVersion != SchemaVersion { + return fmt.Errorf("unsupported manifest schema") + } + if len(m.Samples) != 6 { + return fmt.Errorf("manifest must have exactly six samples") + } + if len(m.Dictionaries) != 2 { + return fmt.Errorf("manifest must have dictionaries for two projects") + } + counts := map[string]int{"BLF-data": 0, "SL-data": 0} + for name := range counts { + d, ok := m.Dictionaries[name] + if !ok || len(d.SliceNames) == 0 || len(d.Descriptions) == 0 || len(d.DiscardReasons) == 0 { + return fmt.Errorf("%s dictionaries incomplete", name) + } + for _, list := range [][]string{d.SliceNames, d.Descriptions, d.DiscardReasons} { + seen := map[string]bool{} + for _, v := range list { + if strings.TrimSpace(v) == "" || seen[v] { + return fmt.Errorf("%s dictionary contains empty/duplicate entry", name) + } + seen[v] = true + } + } + } + keys := map[string]bool{} + paths := map[string]bool{} + for _, s := range m.Samples { + if !keyPattern.MatchString(s.Key) || keys[s.Key] { + return fmt.Errorf("sample key invalid or duplicate: %q", s.Key) + } + keys[s.Key] = true + if _, ok := counts[s.Project]; !ok { + return fmt.Errorf("unsupported project %q", s.Project) + } + counts[s.Project]++ + if s.Mode != "scratch" && s.Mode != "snapshot" { + return fmt.Errorf("%s: unsupported mode", s.Key) + } + if s.Source.CaptureID == "" { + return fmt.Errorf("%s: source capture ID required", s.Key) + } + if s.DurationSeconds <= 0 || s.DurationSeconds > 86400 { + return fmt.Errorf("%s: duration out of bounds", s.Key) + } + if s.Mode == "scratch" && s.Snapshot != nil { + return fmt.Errorf("%s: scratch must not carry snapshot", s.Key) + } + if s.Mode == "snapshot" { + if s.Snapshot == nil || (s.Snapshot.Stage != "annotation" && s.Snapshot.Stage != "quality_review") || len(s.Snapshot.Slices) == 0 { + return fmt.Errorf("%s: snapshot incomplete", s.Key) + } + cursor := float64(0) + for _, slice := range s.Snapshot.Slices { + if !finiteRange(slice.Start, slice.End, s.DurationSeconds) || math.Abs(slice.Start-cursor) > 1e-6 { + return fmt.Errorf("%s: invalid snapshot slice", s.Key) + } + if slice.DeliveryDisposition != "" && slice.DeliveryDisposition != "keep" && slice.DeliveryDisposition != "discard" { + return fmt.Errorf("%s: invalid slice disposition", s.Key) + } + if slice.DeliveryDisposition == "discard" { + if slice.Name != "" || !contains(m.Dictionaries[s.Project].DiscardReasons, slice.DiscardReason) { + return fmt.Errorf("%s: discard slice must have a reason and no name", s.Key) + } + } else if strings.TrimSpace(slice.Name) == "" || slice.DiscardReason != "" { + return fmt.Errorf("%s: kept slice must have a name and no discard reason", s.Key) + } + cursor = slice.End + } + if math.Abs(cursor-s.DurationSeconds) > 1e-6 { + return fmt.Errorf("%s: snapshot slices must cover full sample", s.Key) + } + for _, slice := range s.Snapshot.Slices { + if slice.DeliveryDisposition == "discard" { + continue + } + within := make([]SnapshotAction, 0) + for _, action := range s.Snapshot.Actions { + if action.Start >= slice.Start && action.End <= slice.End { + within = append(within, action) + } + } + cursor = slice.Start + for _, action := range within { + if math.Abs(action.Start-cursor) > 1e-6 { + return fmt.Errorf("%s: action coverage gap or overlap", s.Key) + } + cursor = action.End + } + if math.Abs(cursor-slice.End) > 1e-6 { + return fmt.Errorf("%s: actions must cover kept slice", s.Key) + } + } + for _, action := range s.Snapshot.Actions { + if !finiteRange(action.Start, action.End, s.DurationSeconds) { + return fmt.Errorf("%s: invalid snapshot action", s.Key) + } + contained := 0 + for _, slice := range s.Snapshot.Slices { + if slice.DeliveryDisposition != "discard" && action.Start >= slice.Start && action.End <= slice.End { + contained++ + } + } + if contained != 1 { + return fmt.Errorf("%s: action must be inside one kept slice", s.Key) + } + } + } + media := []string{s.Media.LeftVideo, s.Media.RightVideo, s.Media.LeftPTS, s.Media.RightPTS, s.Media.IMU} + if s.Media.SourceManifest != "" { + media = append(media, s.Media.SourceManifest) + } + for _, p := range media { + if err := validateAssetPath(s.Key, p); err != nil { + return err + } + if paths[p] { + return fmt.Errorf("asset referenced twice: %s", p) + } + paths[p] = true + } + } + if counts["BLF-data"] != 3 || counts["SL-data"] != 3 { + return fmt.Errorf("each project must have three samples") + } + return nil +} + +func contains(values []string, needle string) bool { + for _, v := range values { + if v == needle { + return true + } + } + return false +} +func finiteRange(start, end, max float64) bool { + return !math.IsNaN(start) && !math.IsNaN(end) && !math.IsInf(start, 0) && !math.IsInf(end, 0) && start >= 0 && end > start && end <= max +} +func validateAssetPath(key, p string) error { + if p == "" || strings.Contains(p, "\\") || path.IsAbs(p) || path.Clean(p) != p || !strings.HasPrefix(p, "templates/"+key+"/") || strings.Contains(p, "../") { + return fmt.Errorf("%s: asset path must be under templates/%s", key, key) + } + return nil +} + +func DatabaseName(batch string) (string, error) { + if !batchPattern.MatchString(batch) { + return "", fmt.Errorf("batch ID must be lowercase hyphenated, 1-32 characters") + } + return "droboticflow_internal_test_" + strings.ReplaceAll(batch, "-", "_"), nil +} + +func BuildPlan(batch string, m Manifest, assetsRoot, workspaceRoot string) (Plan, error) { + db, err := DatabaseName(batch) + if err != nil { + return Plan{}, err + } + if err := ValidateWorkspaceRoot(workspaceRoot); err != nil { + return Plan{}, err + } + if err := ValidateManifest(m); err != nil { + return Plan{}, err + } + if err := validateAssetsRoot(assetsRoot); err != nil { + return Plan{}, err + } + manifestJSON, _ := json.Marshal(m) + p := Plan{SchemaVersion: SchemaVersion, BatchID: batch, Database: db, AssetsRoot: assetsRoot, WorkspaceRoot: workspaceRoot, ManifestSHA256: sum(manifestJSON), OrganizationID: newID("org"), SupplierID: newID("supplier"), ProjectIDs: map[string]string{}, UserIDs: map[string]string{}, Assets: []Asset{}} + for _, name := range []string{"BLF-data", "SL-data"} { + p.ProjectIDs[name] = newID("project") + } + for _, role := range []string{"admin", "supplier_manager", "annotator", "quality_reviewer", "acceptance_reviewer"} { + p.UserIDs[role] = newID("user") + } + for _, s := range m.Samples { + ids := SampleIDs{Key: s.Key, Project: s.Project, Mode: s.Mode, WorkflowID: newID("workflow"), RunID: newID("run"), BatchID: newID("batch"), ItemID: newID("item"), TaskID: newID("task"), TemplateID: newID("template"), ArtifactIDs: map[string]string{}} + for _, role := range []string{"leftVideo", "rightVideo", "leftPTS", "rightPTS", "imu"} { + ids.ArtifactIDs[role] = newID("artifact") + } + if s.Media.SourceManifest != "" { + ids.ArtifactIDs["sourceManifest"] = newID("artifact") + } + if s.Mode == "snapshot" { + ids.SliceRevisionID, ids.ActionRevisionID = newID("slice-revision"), newID("action-revision") + } + p.Samples = append(p.Samples, ids) + for _, rel := range []string{s.Media.LeftVideo, s.Media.RightVideo, s.Media.LeftPTS, s.Media.RightPTS, s.Media.IMU, s.Media.SourceManifest} { + if rel == "" { + continue + } + full := filepath.Join(assetsRoot, filepath.FromSlash(rel)) + if err := rejectAssetSymlinks(assetsRoot, rel); err != nil { + return Plan{}, err + } + info, err := os.Lstat(full) + if err != nil { + return Plan{}, fmt.Errorf("asset %s: %w", rel, err) + } + if !info.Mode().IsRegular() || info.Size() == 0 { + return Plan{}, fmt.Errorf("asset %s must be a regular file", rel) + } + f, err := os.Open(full) + if err != nil { + return Plan{}, err + } + h := sha256.New() + _, err = io.Copy(h, f) + closeErr := f.Close() + if err != nil { + return Plan{}, err + } + if closeErr != nil { + return Plan{}, closeErr + } + p.Assets = append(p.Assets, Asset{Path: rel, SHA256: hex.EncodeToString(h.Sum(nil)), Size: info.Size()}) + } + } + sort.Slice(p.Assets, func(i, j int) bool { return p.Assets[i].Path < p.Assets[j].Path }) + p.SHA256 = planHash(p) + return p, nil +} + +func VerifyPlan(p Plan, m Manifest, assetsRoot string) error { + if p.SchemaVersion != SchemaVersion || p.SHA256 == "" || p.SHA256 != planHash(p) { + return errors.New("plan hash mismatch") + } + if err := ValidateWorkspaceRoot(p.WorkspaceRoot); err != nil { + return err + } + if err := validateAssetsRoot(assetsRoot); err != nil { + return err + } + if p.AssetsRoot != assetsRoot { + return errors.New("assets root differs from approved plan") + } + db, err := DatabaseName(p.BatchID) + if err != nil || db != p.Database { + return errors.New("plan database mismatch") + } + manifestJSON, _ := json.Marshal(m) + if sum(manifestJSON) != p.ManifestSHA256 { + return errors.New("manifest changed since plan") + } + // Rehash every asset before the first database connection. + for _, a := range p.Assets { + if err := rejectAssetSymlinks(assetsRoot, a.Path); err != nil { + return err + } + full := filepath.Join(assetsRoot, filepath.FromSlash(a.Path)) + info, err := os.Lstat(full) + if err != nil || !info.Mode().IsRegular() || info.Size() != a.Size { + return fmt.Errorf("asset changed: %s", a.Path) + } + f, err := os.Open(full) + if err != nil { + return err + } + h := sha256.New() + _, err = io.Copy(h, f) + f.Close() + if err != nil || hex.EncodeToString(h.Sum(nil)) != a.SHA256 { + return fmt.Errorf("asset checksum changed: %s", a.Path) + } + } + if len(p.Samples) != 6 || len(p.Assets) < 30 { + return errors.New("plan incomplete") + } + if err := validatePlanIdentity(p, m); err != nil { + return err + } + return nil +} + +func validateAssetsRoot(root string) error { + if !filepath.IsAbs(root) || filepath.Clean(root) != root { + return errors.New("assets root must be absolute") + } + if root != RuntimeAssetsRoot { + base, err := internalTestLocalRoot() + if err != nil { + return err + } + rel, err := filepath.Rel(base, root) + if err != nil || rel == "." || strings.HasPrefix(rel, "..") { + return errors.New("assets root outside dedicated internal-test-lane directory") + } + if err := rejectLocalPathSymlinks(base, rel); err != nil { + return err + } + } + info, err := os.Lstat(root) + if err != nil { + return err + } + if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 { + return errors.New("assets root must be a real directory") + } + return nil +} + +func internalTestLocalRoot() (string, error) { + wd, err := os.Getwd() + if err != nil { + return "", err + } + for { + if _, err := os.Stat(filepath.Join(wd, "go.mod")); err == nil { + return filepath.Join(wd, "local-data", "internal-test-lane"), nil + } + parent := filepath.Dir(wd) + if parent == wd { + return "", errors.New("repository root unavailable") + } + wd = parent + } +} + +func rejectLocalPathSymlinks(base, relative string) error { + for _, prefix := range []string{filepath.Dir(base), base} { + info, err := os.Lstat(prefix) + if err == nil && info.Mode()&os.ModeSymlink != 0 { + return errors.New("internal test path contains symlink") + } + if err != nil && !os.IsNotExist(err) { + return err + } + } + current := base + for _, part := range strings.Split(relative, string(filepath.Separator)) { + current = filepath.Join(current, part) + info, err := os.Lstat(current) + if err == nil && info.Mode()&os.ModeSymlink != 0 { + return errors.New("internal test path contains symlink") + } + if err != nil && !os.IsNotExist(err) { + return err + } + } + return nil +} + +func rejectAssetSymlinks(root, relative string) error { + current := root + for _, part := range strings.Split(filepath.FromSlash(relative), string(filepath.Separator)) { + current = filepath.Join(current, part) + info, err := os.Lstat(current) + if err != nil { + return err + } + if info.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("asset path contains symlink: %s", relative) + } + } + return nil +} + +func validatePlanIdentity(p Plan, m Manifest) error { + active := map[string]bool{} + source := map[string]bool{} + for _, s := range m.Samples { + for _, id := range []string{s.Source.CaptureID, s.Source.TaskID} { + if id != "" { + source[id] = true + } + } + } + check := func(id string) error { + if !strings.HasPrefix(id, "internal-test-") || active[id] || source[id] { + return errors.New("plan contains duplicate, source, or non-test identity") + } + active[id] = true + return nil + } + for _, id := range []string{p.OrganizationID, p.SupplierID} { + if err := check(id); err != nil { + return err + } + } + for _, name := range []string{"BLF-data", "SL-data"} { + if err := check(p.ProjectIDs[name]); err != nil { + return err + } + } + for _, role := range []string{"admin", "supplier_manager", "annotator", "quality_reviewer", "acceptance_reviewer"} { + if err := check(p.UserIDs[role]); err != nil { + return err + } + } + if len(p.Samples) != len(m.Samples) { + return errors.New("plan sample count mismatch") + } + expectedAssets := map[string]bool{} + for i, s := range m.Samples { + ids := p.Samples[i] + if ids.Key != s.Key || ids.Project != s.Project || ids.Mode != s.Mode { + return errors.New("plan sample mapping mismatch") + } + for _, id := range []string{ids.WorkflowID, ids.RunID, ids.BatchID, ids.ItemID, ids.TaskID, ids.TemplateID} { + if err := check(id); err != nil { + return err + } + } + roles := map[string]string{"leftVideo": s.Media.LeftVideo, "rightVideo": s.Media.RightVideo, "leftPTS": s.Media.LeftPTS, "rightPTS": s.Media.RightPTS, "imu": s.Media.IMU} + if s.Media.SourceManifest != "" { + roles["sourceManifest"] = s.Media.SourceManifest + } + if len(ids.ArtifactIDs) != len(roles) { + return errors.New("plan artifact mapping mismatch") + } + for role, rel := range roles { + if err := check(ids.ArtifactIDs[role]); err != nil { + return err + } + expectedAssets[rel] = true + } + if s.Mode == "snapshot" { + for _, id := range []string{ids.SliceRevisionID, ids.ActionRevisionID} { + if err := check(id); err != nil { + return err + } + } + } else if ids.SliceRevisionID != "" || ids.ActionRevisionID != "" { + return errors.New("scratch plan carries revisions") + } + } + if len(p.Assets) != len(expectedAssets) { + return errors.New("plan asset count mismatch") + } + for _, asset := range p.Assets { + if !expectedAssets[asset.Path] { + return errors.New("plan has unreferenced asset") + } + delete(expectedAssets, asset.Path) + } + return nil +} + +func planHash(p Plan) string { p.SHA256 = ""; data, _ := json.Marshal(p); return sum(data) } +func sum(data []byte) string { h := sha256.Sum256(data); return hex.EncodeToString(h[:]) } +func newID(kind string) string { + var bytes [16]byte + if _, err := rand.Read(bytes[:]); err != nil { + panic(err) + } + return "internal-test-" + kind + "-" + hex.EncodeToString(bytes[:]) +} + +func ValidateWorkspaceRoot(root string) error { + if root == RuntimeAssetsRoot { + return nil + } + if !filepath.IsAbs(root) || filepath.Clean(root) != root || filepath.Base(root) != "workspace" { + return errors.New("workspace root must be /workspace or a dedicated local workspace") + } + wd, err := os.Getwd() + if err != nil { + return err + } + for { + if _, err := os.Stat(filepath.Join(wd, "go.mod")); err == nil { + break + } + parent := filepath.Dir(wd) + if parent == wd { + return errors.New("repository root unavailable") + } + wd = parent + } + allowed := filepath.Join(wd, "local-data", "internal-test-lane") + rel, err := filepath.Rel(allowed, root) + if err != nil || rel == "." || strings.HasPrefix(rel, "..") || !strings.HasSuffix(rel, string(filepath.Separator)+"workspace") { + return errors.New("workspace root outside dedicated internal-test-lane directory") + } + if err := rejectLocalPathSymlinks(allowed, rel); err != nil { + return err + } + current := allowed + for _, part := range strings.Split(rel, string(filepath.Separator)) { + current = filepath.Join(current, part) + info, err := os.Lstat(current) + if err == nil && info.Mode()&os.ModeSymlink != 0 { + return errors.New("workspace path contains symlink") + } + if err != nil && !os.IsNotExist(err) { + return err + } + } + return nil +} diff --git a/internal/testlane/plan_test.go b/internal/testlane/plan_test.go new file mode 100644 index 00000000..584eefe5 --- /dev/null +++ b/internal/testlane/plan_test.go @@ -0,0 +1,223 @@ +package testlane + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func fixtureManifest(t *testing.T) (Manifest, string) { + t.Helper() + base, err := internalTestLocalRoot() + if err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(base, 0o700); err != nil { + t.Fatal(err) + } + root, err := os.MkdirTemp(base, "seed-plan-test-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(root) }) + m := Manifest{SchemaVersion: SchemaVersion, Dictionaries: map[string]Dictionary{ + "BLF-data": {SliceNames: []string{"pick"}, Descriptions: []string{"pick item"}, DiscardReasons: []string{"bad camera"}}, + "SL-data": {SliceNames: []string{"place"}, Descriptions: []string{"place item"}, DiscardReasons: []string{"bad camera"}}, + }} + for _, project := range []string{"BLF-data", "SL-data"} { + for n := 1; n <= 3; n++ { + key := strings.ToLower(strings.Split(project, "-")[0]) + "-" + string(rune('0'+n)) + prefix := "templates/" + key + "/" + s := Sample{Key: key, Project: project, Mode: "scratch", Source: Source{CaptureID: "source-" + key}, DurationSeconds: 2, + Media: Media{LeftVideo: prefix + "left.mp4", RightVideo: prefix + "right.mp4", LeftPTS: prefix + "left_pts.csv", RightPTS: prefix + "right_pts.csv", IMU: prefix + "imu.csv"}} + for _, rel := range []string{s.Media.LeftVideo, s.Media.RightVideo, s.Media.LeftPTS, s.Media.RightPTS, s.Media.IMU} { + full := filepath.Join(root, filepath.FromSlash(rel)) + if err := os.MkdirAll(filepath.Dir(full), 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(full, []byte("fixture"), 0o600); err != nil { + t.Fatal(err) + } + } + m.Samples = append(m.Samples, s) + } + } + return m, root +} + +func TestPlanBindsManifestAssetsAndFreshIDs(t *testing.T) { + m, root := fixtureManifest(t) + p, err := BuildPlan("batch-one", m, root, RuntimeAssetsRoot) + if err != nil { + t.Fatal(err) + } + if p.Database != "droboticflow_internal_test_batch_one" || len(p.Assets) != 30 || len(p.Samples) != 6 { + t.Fatalf("incomplete plan: %+v", p) + } + if p.Samples[0].TaskID == m.Samples[0].Source.CaptureID || p.Samples[0].RunID == m.Samples[0].Source.CaptureID { + t.Fatal("source ID reused") + } + if err := VerifyPlan(p, m, root); err != nil { + t.Fatal(err) + } + if err := VerifyPlan(p, m, t.TempDir()); err == nil { + t.Fatal("external assets root accepted") + } + if err := VerifyPlan(p, m, root+string(filepath.Separator)+"."); err == nil { + t.Fatal("substituted assets root accepted") + } + p.Database = "droboticflow" + if err := VerifyPlan(p, m, root); err == nil { + t.Fatal("tampered target accepted") + } + p.Database = "droboticflow_internal_test_batch_one" + if err := os.WriteFile(filepath.Join(root, "templates", "blf-1", "left.mp4"), []byte("changed"), 0o600); err != nil { + t.Fatal(err) + } + if err := VerifyPlan(p, m, root); err == nil { + t.Fatal("changed asset accepted") + } +} + +func TestPlanRejectsSymlinkedMedia(t *testing.T) { + m, root := fixtureManifest(t) + p, err := BuildPlan("symlink-test", m, root, RuntimeAssetsRoot) + if err != nil { + t.Fatal(err) + } + left := filepath.Join(root, "templates", "blf-1", "left.mp4") + copyPath := filepath.Join(root, "templates", "blf-1", "copy.mp4") + if err := os.Rename(left, copyPath); err != nil { + t.Fatal(err) + } + if err := os.Symlink(copyPath, left); err != nil { + t.Fatal(err) + } + if err := VerifyPlan(p, m, root); err == nil { + t.Fatal("symlinked media accepted") + } +} + +func TestManifestRejectsUnboundedOrExternalInput(t *testing.T) { + m, _ := fixtureManifest(t) + m.Samples[0].Media.LeftVideo = "../../production/video.mp4" + if err := ValidateManifest(m); err == nil { + t.Fatal("external asset path accepted") + } + m, _ = fixtureManifest(t) + m.Samples[0].Snapshot = &Snapshot{Stage: "quality_review", Slices: []SnapshotSlice{{Start: 0, End: 2, Name: "pick"}}} + if err := ValidateManifest(m); err == nil { + t.Fatal("scratch carried a snapshot") + } + m, _ = fixtureManifest(t) + m.Samples = m.Samples[:5] + if err := ValidateManifest(m); err == nil { + t.Fatal("unbounded sample count accepted") + } +} + +func TestSnapshotAcceptsBlankManualDescriptionAndDiscard(t *testing.T) { + m, root := fixtureManifest(t) + s := &m.Samples[0] + s.Mode = "snapshot" + s.Snapshot = &Snapshot{ + Stage: "quality_review", + Slices: []SnapshotSlice{ + {Start: 0, End: 1, Name: "pick", DeliveryDisposition: "keep"}, + {Start: 1, End: 2, DeliveryDisposition: "discard", DiscardReason: "bad camera"}, + }, + Actions: []SnapshotAction{ + {Start: 0, End: 0.5, Description: "a manually written description"}, + {Start: 0.5, End: 1, Description: ""}, + }, + } + if err := ValidateManifest(m); err != nil { + t.Fatal(err) + } + if _, err := BuildPlan("snapshot-one", m, root, RuntimeAssetsRoot); err != nil { + t.Fatal(err) + } + s.Snapshot.Actions[1].End = 0.9 + if err := ValidateManifest(m); err == nil { + t.Fatal("action gap accepted") + } + s.Snapshot.Actions[1].End = 1 + s.Snapshot.Slices[1].Name = "must-not-name-discard" + if err := ValidateManifest(m); err == nil { + t.Fatal("discard slice with name accepted") + } +} + +func TestMongoTargetGuard(t *testing.T) { + db := "droboticflow_internal_test_batch_one" + allowed := []string{ + "mongodb://localhost:27019/" + db + "?authSource=admin", + "mongodb://127.0.0.1:27019/" + db + "?authSource=admin", + "mongodb://seed:private@internal-test-batch-one-mongo.droboticflow-internal-test.svc.cluster.local:27017/" + db + "?authSource=" + db, + } + for _, uri := range allowed { + if err := ValidateMongoURI(uri, db, "batch-one"); err != nil { + t.Fatal(err) + } + } + denied := []string{ + "mongodb://localhost:27018/" + db, + "mongodb://127.0.0.1:27018/" + db, + "mongodb://internal-test-other-mongo.droboticflow-internal-test.svc.cluster.local:27017/" + db, + "mongodb://localhost:27019/droboticflow?authSource=admin", + "mongodb+srv://internal-test-batch-one-mongo.droboticflow-internal-test.svc.cluster.local/" + db, + } + for _, uri := range denied { + if err := ValidateMongoURI(uri, db, "batch-one"); err == nil { + t.Fatalf("unsafe Mongo target accepted: %s", uri) + } + } + secret := "never-print-this-password" + err := ValidateMongoURI("mongodb://user:"+secret+"@prod.example:27017/"+db, db, "batch-one") + if err == nil || strings.Contains(err.Error(), secret) { + t.Fatal("unsafe URI accepted or credentials leaked") + } +} + +func TestPrivateAccountFileAndRoles(t *testing.T) { + dir, err := os.MkdirTemp("/private/tmp", "testlane-accounts-") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.RemoveAll(dir) }) + path := filepath.Join(dir, "accounts.json") + if err := os.WriteFile(path, []byte("private"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := ReadPrivateFile(path); err == nil { + t.Fatal("world-readable credentials accepted") + } + if err := os.Chmod(path, 0o600); err != nil { + t.Fatal(err) + } + if _, err := ReadPrivateFile(path); err != nil { + t.Fatal(err) + } + alias := filepath.Join(dir, "alias") + if err := os.Symlink(dir, alias); err != nil { + t.Fatal(err) + } + if _, err := ReadPrivateFile(filepath.Join(alias, "accounts.json")); err == nil { + t.Fatal("symlinked private file ancestor accepted") + } + a := AccountsFile{Accounts: []Account{ + {Role: "admin", Name: "Admin", Phone: "13800000001", Password: "generated-password-1"}, + {Role: "supplier_manager", Name: "Manager", Phone: "13800000002", Password: "generated-password-2"}, + {Role: "annotator", Name: "Annotator", Phone: "13800000003", Password: "generated-password-3"}, + {Role: "quality_reviewer", Name: "Quality", Phone: "13800000004", Password: "generated-password-4"}, + {Role: "acceptance_reviewer", Name: "Acceptance", Phone: "13800000005", Password: "generated-password-5"}, + }} + if err := ValidateAccounts(a); err != nil { + t.Fatal(err) + } + a.Accounts[4].Role = "admin" + if err := ValidateAccounts(a); err == nil { + t.Fatal("duplicate role accepted") + } +} diff --git a/scripts/internal_test_lane.py b/scripts/internal_test_lane.py new file mode 100644 index 00000000..953f641f --- /dev/null +++ b/scripts/internal_test_lane.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +"""Prepare and track the internal test lane using only local files. + +No command in this entry point deploys, accesses MongoDB, reads BOS, or changes +customer records. Prepared manifests and plans require separate review. +""" + +from __future__ import annotations + +import argparse +import json +import os +import sys +from pathlib import Path + + +REPO = Path(__file__).resolve().parent.parent +if str(REPO) not in sys.path: + sys.path.insert(0, str(REPO)) + +from scripts.internal_test_lane.lifecycle import Lane, LaneError, _check_no_symlink, _read_json # noqa: E402 + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--root", type=Path, default=REPO / "local-data" / "internal-test-lane", help="private local lane root") + sub = parser.add_subparsers(dest="command", required=True) + + verify = sub.add_parser("verify-config", help="validate a dedicated environment config and print safe derived names") + verify.add_argument("--config", type=Path, required=True) + + importer = sub.add_parser("import-bundle", help="verify six locally approved samples and copy immutable originals") + importer.add_argument("--bundle", type=Path, required=True) + + prepared = sub.add_parser("prepare", help="render a batch package and offline seed plan") + prepared.add_argument("--batch", required=True) + prepared.add_argument("--import-id", required=True) + prepared.add_argument("--config", type=Path, required=True) + + creds = sub.add_parser("credentials", help="generate private test-only credentials and a Secret manifest") + creds.add_argument("--config", type=Path, required=True) + creds.add_argument("--out", type=Path, required=True) + + sub.add_parser("status", help="list local imports and batches without claiming remote success") + + archive = sub.add_parser("archive", help="record operator evidence that a batch is complete, stopped, and detached") + archive.add_argument("--batch", required=True) + archive.add_argument("--evidence", type=Path, required=True) + + cleanup = sub.add_parser("cleanup-plan", help="preview local files eligible for explicit cleanup") + cleanup.add_argument("--batch", required=True) + + clean = sub.add_parser("clean", help="remove only previewed owned local files of an old archived batch") + clean.add_argument("--batch", required=True) + clean.add_argument("--confirm", required=True, help="confirmSha256 from cleanup-plan") + + args = parser.parse_args(argv) + lane = Lane(args.root) + try: + if args.command == "verify-config": + from scripts.internal_test_lane.environment import validate_config + config = validate_config(_read_json(args.config)) + result = {key: config[key] for key in ("batchId", "namespace", "databaseName", "resourcePrefix", "credentialsSecretName", "mongoServiceName", "workspacePvcName", "mongoPvcName")} + elif args.command == "import-bundle": + result = lane.import_bundle(args.bundle) + elif args.command == "prepare": + result = lane.prepare(args.batch, args.import_id, args.config, REPO) + elif args.command == "credentials": + from scripts.internal_test_lane.credentials import generate_credentials + output = Path(os.path.abspath(args.out)) + root = lane.root + _check_no_symlink(output) + if output != root and root not in output.parents: + raise LaneError("credential output directory must be inside the private lane root") + with lane.locked(): + result = generate_credentials(_read_json(args.config), output) + elif args.command == "status": + result = lane.status() + elif args.command == "archive": + result = lane.archive(args.batch, args.evidence) + elif args.command == "cleanup-plan": + result = lane.cleanup_plan(args.batch) + elif args.command == "clean": + result = lane.clean(args.batch, args.confirm) + else: + parser.error("unknown command") + return 2 + except (LaneError, ValueError, OSError) as exc: + print(f"refused: {exc}", file=sys.stderr) + return 2 + print(json.dumps(result, indent=2, sort_keys=True)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/internal_test_lane/__init__.py b/scripts/internal_test_lane/__init__.py new file mode 100644 index 00000000..24552361 --- /dev/null +++ b/scripts/internal_test_lane/__init__.py @@ -0,0 +1 @@ +"""Offline management of the isolated internal test lane.""" diff --git a/scripts/internal_test_lane/config.example.json b/scripts/internal_test_lane/config.example.json new file mode 100644 index 00000000..3b6da1c2 --- /dev/null +++ b/scripts/internal_test_lane/config.example.json @@ -0,0 +1,48 @@ +{ + "schemaVersion": 1, + "batchId": "candidate-001", + "hostname": "test.example.test", + "productionOrigins": [ + "http://120.48.180.80" + ], + "image": "registry.example.test/droboticflow-api@sha256:0000000000000000000000000000000000000000000000000000000000000000", + "mongoImage": "registry.example.test/mongo@sha256:0000000000000000000000000000000000000000000000000000000000000000", + "buildCommit": "0000000000000000000000000000000000000000", + "workspaceGi": 20, + "mongoGi": 5, + "tlsSecretName": "internal-test-tls", + "imagePullSecretName": "internal-test-registry", + "ingressNamespace": "ingress-nginx", + "resources": { + "api": { + "requests": { + "cpu": "100m", + "memory": "128Mi" + }, + "limits": { + "cpu": "500m", + "memory": "512Mi" + } + }, + "processor": { + "requests": { + "cpu": "500m", + "memory": "1Gi" + }, + "limits": { + "cpu": "2", + "memory": "8Gi" + } + }, + "mongo": { + "requests": { + "cpu": "100m", + "memory": "128Mi" + }, + "limits": { + "cpu": "500m", + "memory": "512Mi" + } + } + } +} diff --git a/scripts/internal_test_lane/credentials.py b/scripts/internal_test_lane/credentials.py new file mode 100644 index 00000000..114a2a33 --- /dev/null +++ b/scripts/internal_test_lane/credentials.py @@ -0,0 +1,70 @@ +"""Generate isolated test credentials into new, private local files only.""" + +import json +import os +from pathlib import Path +import secrets +from urllib.parse import quote + +from .environment import validate_config + + +def _write_private(path, value): + descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + with os.fdopen(descriptor, "w", encoding="utf-8") as stream: + json.dump(value, stream, ensure_ascii=False, indent=2) + stream.write("\n") + stream.flush() + os.fsync(stream.fileno()) + + +def generate_credentials(config, outdir): + """Return file paths, never credential contents; refuse replacing any output.""" + normalized = validate_config(config) + output = Path(os.path.abspath(outdir)) + for path in (output, *output.parents): + if path.is_symlink(): + raise ValueError("credential output must not traverse a symbolic link") + if output.exists(): + raise FileExistsError("credential output must be a new directory") + output.parent.mkdir(parents=True, exist_ok=True, mode=0o700) + output.mkdir(mode=0o700) + + root_password = secrets.token_urlsafe(36) + app_password = secrets.token_urlsafe(36) + namespace = normalized["namespace"] + database = normalized["databaseName"] + host = f"{normalized['mongoServiceName']}.{namespace}.svc.cluster.local" + values = { + "MONGO_ROOT_USERNAME": "test_root", + "MONGO_ROOT_PASSWORD": root_password, + "MONGO_APP_USERNAME": "test_app", + "MONGO_APP_PASSWORD": app_password, + "MONGO_URI": f"mongodb://test_app:{quote(app_password, safe='')}@{host}:27017/{database}?authSource={database}", + "DROBOTICFLOW_API_TOKEN": secrets.token_urlsafe(48), + } + secret = { + "apiVersion": "v1", "kind": "Secret", "type": "Opaque", + "metadata": {"name": normalized["credentialsSecretName"], "namespace": namespace, + "labels": {"app.kubernetes.io/part-of": "internal-test-lane"}}, + "stringData": values, + } + roles = [("admin", "平台管理员"), ("supplier_manager", "供应商管理员"), + ("annotator", "标注员"), ("quality_reviewer", "质检员"), + ("acceptance_reviewer", "最终验收员")] + accounts = {"accounts": [ + {"role": role, "name": f"内部测试·{name}", "phone": f"1999999000{number}", + "password": secrets.token_urlsafe(36)} + for number, (role, name) in enumerate(roles, start=1) + ]} + secret_file, accounts_file = output / "secret.json", output / "accounts.json" + _write_private(secret_file, secret) + _write_private(accounts_file, accounts) + uri_file = output / "mongo-uri.txt" + descriptor = os.open(uri_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) + with os.fdopen(descriptor, "w", encoding="utf-8") as stream: + stream.write(values["MONGO_URI"] + "\n") + stream.flush() + os.fsync(stream.fileno()) + return {"secretManifest": str(secret_file), "accountsFile": str(accounts_file), + "mongoUriFile": str(uri_file)} diff --git a/scripts/internal_test_lane/environment.py b/scripts/internal_test_lane/environment.py new file mode 100644 index 00000000..1b59cef6 --- /dev/null +++ b/scripts/internal_test_lane/environment.py @@ -0,0 +1,453 @@ +"""Pure, offline Kubernetes renderer for one internal test batch. + +The result is a review artifact, not an apply command. Credentials, source +captures, and cluster state never enter this module. +""" + +from __future__ import annotations + +import ipaddress +import re +from urllib.parse import urlsplit + + +NAMESPACE = "droboticflow-internal-test" +REQUIRED_SECRET_KEYS = ( + "MONGO_ROOT_USERNAME", + "MONGO_ROOT_PASSWORD", + "MONGO_APP_USERNAME", + "MONGO_APP_PASSWORD", + "MONGO_URI", + "DROBOTICFLOW_API_TOKEN", +) +_PRODUCTION_HOSTS = {"120.48.180.80"} +_CONFIG_KEYS = { + "schemaVersion", "namespace", "batchId", "hostname", "productionOrigins", + "image", "mongoImage", "buildCommit", "workspaceGi", "mongoGi", + "resources", "storageClass", "tlsSecretName", "imagePullSecretName", + "ingressNamespace", "ingressClassName", +} +_DNS_LABEL = re.compile(r"^[a-z](?:[a-z0-9-]*[a-z0-9])?$") +_BATCH = re.compile(r"^[a-z](?:[a-z0-9-]{0,30}[a-z0-9])?$") +_IMAGE = re.compile(r"^[^\s@]+@sha256:[0-9a-f]{64}$") +_CPU = re.compile(r"^(?:[1-9][0-9]*m|[1-9][0-9]*)$") +_MEM = re.compile(r"^([1-9][0-9]*)(Mi|Gi)$") + + +def _required_string(value: object, field: str) -> str: + if not isinstance(value, str) or not value or value != value.strip(): + raise ValueError(f"{field} must be a nonblank string without surrounding whitespace") + return value + + +def _dns_label(value: object, field: str) -> str: + value = _required_string(value, field) + if len(value) > 63 or not _DNS_LABEL.fullmatch(value): + raise ValueError(f"{field} must be a lowercase DNS label") + return value + + +def _quantity(value: object, field: str, *, cpu: bool) -> int: + value = _required_string(value, field) + if cpu: + if not _CPU.fullmatch(value): + raise ValueError(f"{field} must be an integer core or positive millicore quantity") + return int(value[:-1]) if value.endswith("m") else 1000 * int(value) + match = _MEM.fullmatch(value) + if not match: + raise ValueError(f"{field} must be a positive Mi or Gi quantity") + return int(match[1]) * (1024 if match[2] == "Gi" else 1) + + +def _origin_host(value: object) -> str: + value = _required_string(value, "productionOrigins entry") + parsed = urlsplit(value) + if parsed.scheme not in ("http", "https") or not parsed.hostname or parsed.username or parsed.password: + raise ValueError("productionOrigins must contain absolute HTTP(S) origins") + if parsed.path not in ("", "/") or parsed.query or parsed.fragment: + raise ValueError("productionOrigins entries must be origins without paths or queries") + try: + _ = parsed.port + except ValueError as exc: + raise ValueError("productionOrigins contains an invalid port") from exc + return parsed.hostname.lower().rstrip(".") + + +def validate_config(raw: dict) -> dict: + """Reject ambiguous or unsafe deployment input and return derived names.""" + if not isinstance(raw, dict): + raise ValueError("config must be a JSON object") + unknown = set(raw) - _CONFIG_KEYS - { + "databaseName", "resourcePrefix", "credentialsSecretName", "mongoServiceName", + "workspacePvcName", "mongoPvcName", + } + if unknown: + raise ValueError(f"unknown config fields: {', '.join(sorted(unknown))}") + if type(raw.get("schemaVersion")) is not int or raw["schemaVersion"] != 1: + raise ValueError("schemaVersion must be 1") + if raw.get("namespace", NAMESPACE) != NAMESPACE: + raise ValueError(f"namespace must be {NAMESPACE}") + batch = _required_string(raw.get("batchId"), "batchId") + if not _BATCH.fullmatch(batch): + raise ValueError("batchId must be 1-32 lowercase letters, digits, or hyphens") + hostname = _required_string(raw.get("hostname"), "hostname").lower() + if hostname != raw["hostname"] or hostname.endswith(".") or len(hostname) > 253: + raise ValueError("hostname must be a lowercase DNS name") + if len(hostname.split(".")) < 3 or any(not _DNS_LABEL.fullmatch(part) or len(part) > 63 for part in hostname.split(".")): + raise ValueError("hostname must be a dedicated fully qualified DNS name") + try: + ipaddress.ip_address(hostname) + except ValueError: + pass + else: + raise ValueError("hostname cannot be an IP address") + if hostname == "localhost" or hostname.endswith(".localhost"): + raise ValueError("hostname cannot be localhost") + origins = raw.get("productionOrigins") + if not isinstance(origins, list) or not origins: + raise ValueError("productionOrigins must be a nonempty list") + origin_hosts = {_origin_host(origin) for origin in origins} + if hostname in origin_hosts or hostname in _PRODUCTION_HOSTS: + raise ValueError("hostname must not be a production origin") + for field in ("image", "mongoImage"): + if not _IMAGE.fullmatch(_required_string(raw.get(field), field)): + raise ValueError(f"{field} must be pinned by a sha256 digest") + commit = _required_string(raw.get("buildCommit"), "buildCommit") + if not re.fullmatch(r"[0-9a-f]{40}", commit): + raise ValueError("buildCommit must be a full 40-character lowercase Git SHA") + for field, maximum in (("workspaceGi", 500), ("mongoGi", 100)): + value = raw.get(field) + if type(value) is not int or not 1 <= value <= maximum: + raise ValueError(f"{field} must be an integer between 1 and {maximum}") + resources = raw.get("resources") + if not isinstance(resources, dict) or set(resources) != {"api", "processor", "mongo"}: + raise ValueError("resources must specify api, processor, and mongo") + for component, value in resources.items(): + if not isinstance(value, dict) or set(value) != {"requests", "limits"}: + raise ValueError(f"resources.{component} needs requests and limits") + for category in ("requests", "limits"): + quantities = value[category] + if not isinstance(quantities, dict) or set(quantities) != {"cpu", "memory"}: + raise ValueError(f"resources.{component}.{category} needs cpu and memory") + for key, cpu in (("cpu", True), ("memory", False)): + request = _quantity(value["requests"][key], f"resources.{component}.requests.{key}", cpu=cpu) + limit = _quantity(value["limits"][key], f"resources.{component}.limits.{key}", cpu=cpu) + maximum = (4000 if cpu else 8192) if component == "processor" else (2000 if cpu else 4096) + if request > limit or limit > maximum: + raise ValueError(f"resources.{component}.{key} request/limit exceeds the bounded lane") + for field in ("tlsSecretName", "imagePullSecretName", "ingressNamespace"): + _dns_label(raw.get(field), field) + if "storageClass" in raw: + _dns_label(raw["storageClass"], "storageClass") + if "ingressClassName" in raw: + _dns_label(raw["ingressClassName"], "ingressClassName") + prefix = f"internal-test-{batch}" + derived = { + "namespace": NAMESPACE, + "databaseName": "droboticflow_internal_test_" + batch.replace("-", "_"), + "resourcePrefix": prefix, + "credentialsSecretName": prefix + "-credentials", + "mongoServiceName": prefix + "-mongo", + "workspacePvcName": prefix + "-workspace", + "mongoPvcName": prefix + "-mongo-data", + } + for field, expected in derived.items(): + if field in raw and raw[field] != expected: + raise ValueError(f"{field} must equal the batch-scoped derived value") + result = dict(raw) + result.update(derived) + return result + + +def render_environment(config: dict) -> dict: + """Render one batch as a Kubernetes List; never talks to Kubernetes.""" + c = validate_config(config) + ns, prefix, db = c["namespace"], c["resourcePrefix"], c["databaseName"] + secret, mongo = c["credentialsSecretName"], c["mongoServiceName"] + labels = {"app.kubernetes.io/part-of": "droboticflow-internal-test", "internal-test/batch": c["batchId"]} + + def obj(api_version: str, kind: str, name: str, spec: dict | None = None, *, data: dict | None = None) -> dict: + item = {"apiVersion": api_version, "kind": kind, "metadata": {"name": name, "namespace": ns, "labels": labels}} + if spec is not None: + item["spec"] = spec + if data is not None: + item["data"] = data + return item + + def env(name: str, *, key: str | None = None, value: str | None = None) -> dict: + if key is not None: + return {"name": name, "valueFrom": {"secretKeyRef": {"name": secret, "key": key}}} + return {"name": name, "value": value} + + def pod_spec(component: str, container: dict, *, workspace: bool = False, init: list | None = None) -> dict: + spec = { + "serviceAccountName": prefix + "-" + component, + "automountServiceAccountToken": False, + "imagePullSecrets": [{"name": c["imagePullSecretName"]}], + "securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001, "fsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}}, + "containers": [container], + } + if init: + spec["initContainers"] = init + if workspace: + spec["volumes"] = [{"name": "workspace", "persistentVolumeClaim": {"claimName": c["workspacePvcName"]}}] + return spec + + def deployment(component: str, container: dict, *, workspace: bool = False, init: list | None = None) -> dict: + name = prefix + "-" + component + selector = {"app": name} + spec = { + "replicas": 0, + "strategy": {"type": "Recreate"}, + "selector": {"matchLabels": selector}, + "template": {"metadata": {"labels": {**labels, **selector}}, "spec": pod_spec(component, container, workspace=workspace, init=init)}, + } + return obj("apps/v1", "Deployment", name, spec) + + items = [{"apiVersion": "v1", "kind": "Namespace", "metadata": {"name": ns, "labels": {"app.kubernetes.io/part-of": "droboticflow-internal-test"}}}] + for component in ("api", "processor", "mongo", "initializer"): + items.append(obj("v1", "ServiceAccount", prefix + "-" + component)) + items.append(obj("v1", "ConfigMap", prefix + "-config", data={ + "MONGO_DB": db, + "PORT": "3001", + "WORKSPACE_MOUNT_PATH": "/workspace", + "ANNOTATION_PROCESSOR_MODE": "external", + "DROBOTICFLOW_REQUIRE_SESSION_AUTH": "true", + "DROBOTICFLOW_SESSION_COOKIE_SECURE": "true", + "DROBOTICFLOW_OBJECT_SYNC_ENABLED": "false", + "K8S_NAMESPACE": ns, + "WORKSPACE_PVC_NAME": c["workspacePvcName"], + "DROBOTICFLOW_BUILD_COMMIT": c["buildCommit"], + "INTERNAL_TEST_BATCH_ID": c["batchId"], + "INTERNAL_TEST_ENVIRONMENT": "true", + })) + for kind, size in (("workspace", c["workspaceGi"]), ("mongo-data", c["mongoGi"])): + name = c["workspacePvcName"] if kind == "workspace" else c["mongoPvcName"] + pvc = obj("v1", "PersistentVolumeClaim", name, { + "accessModes": ["ReadWriteMany" if kind == "workspace" else "ReadWriteOnce"], + "resources": {"requests": {"storage": f"{size}Gi"}}, + }) + pvc["metadata"]["annotations"] = {"helm.sh/resource-policy": "keep", "internal-test/retention": "manual-review-only"} + if "storageClass" in c: + pvc["spec"]["storageClassName"] = c["storageClass"] + items.append(pvc) + mongo_bootstrap = ( + "const name = process.env.MONGO_INITDB_DATABASE;\n" + "if (!/^droboticflow_internal_test_[a-z0-9_]+$/.test(name)) throw Error('unsafe database name');\n" + "db.getSiblingDB(name).createUser({user: process.env.MONGO_APP_USERNAME," + " pwd: process.env.MONGO_APP_PASSWORD, roles: [{role: 'readWrite', db: name}]});\n" + ) + items.append(obj("v1", "ConfigMap", prefix + "-mongo-init", data={"01-create-app-user.js": mongo_bootstrap})) + items.append(obj("v1", "Service", mongo, { + "type": "ClusterIP", "selector": {"app": mongo}, + "ports": [{"name": "mongo", "port": 27017, "targetPort": 27017}], + })) + mongo_container = { + "name": "mongo", "image": c["mongoImage"], "imagePullPolicy": "IfNotPresent", + "ports": [{"name": "mongo", "containerPort": 27017}], + "env": [ + env("MONGO_INITDB_DATABASE", value=db), + env("MONGO_INITDB_ROOT_USERNAME", key="MONGO_ROOT_USERNAME"), + env("MONGO_INITDB_ROOT_PASSWORD", key="MONGO_ROOT_PASSWORD"), + env("MONGO_APP_USERNAME", key="MONGO_APP_USERNAME"), + env("MONGO_APP_PASSWORD", key="MONGO_APP_PASSWORD"), + ], + "volumeMounts": [{"name": "data", "mountPath": "/data/db"}, {"name": "mongo-init", "mountPath": "/docker-entrypoint-initdb.d", "readOnly": True}], + "resources": c["resources"]["mongo"], + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + "readinessProbe": {"tcpSocket": {"port": "mongo"}, "periodSeconds": 10}, + } + mongo_pod = pod_spec("mongo", mongo_container) + mongo_pod["volumes"] = [ + {"name": "data", "persistentVolumeClaim": {"claimName": c["mongoPvcName"]}}, + {"name": "mongo-init", "configMap": {"name": prefix + "-mongo-init"}}, + ] + # The official Mongo entrypoint can initialize as its pre-created uid 999. + # Kubernetes fsGroup makes a fresh PVC writable without root chown/gosu. + mongo_pod["securityContext"] = { + "runAsNonRoot": True, "runAsUser": 999, "runAsGroup": 999, + "fsGroup": 999, "seccompProfile": {"type": "RuntimeDefault"}, + } + items.append(obj("apps/v1", "Deployment", mongo, { + "replicas": 1, "strategy": {"type": "Recreate"}, + "selector": {"matchLabels": {"app": mongo}}, + "template": {"metadata": {"labels": {**labels, "app": mongo}}, "spec": mongo_pod}, + })) + app_mongo_env = [env("MONGO_DB", value=db), env("MONGO_URI", key="MONGO_URI")] + expected_uri_suffix = f"@{mongo}.{ns}.svc.cluster.local:27017/{db}?authSource={db}" + guard = { + "name": "validate-mongo-target", "image": c["image"], "imagePullPolicy": "IfNotPresent", + "command": ["/bin/sh", "-ec", """ +case "$MONGO_URI" in mongodb://*"$EXPECTED_URI_SUFFIX") ;; *) + echo 'Mongo target must be the isolated test database' >&2; exit 1;; esac +uri_body=${MONGO_URI#mongodb://} +userinfo=${uri_body%%@*} +case "$userinfo" in *:*) ;; *) exit 1;; esac +case "${uri_body#*@}" in *'@'*) exit 1;; esac +"""], + "env": [env("MONGO_URI", key="MONGO_URI"), env("EXPECTED_URI_SUFFIX", value=expected_uri_suffix)], + "resources": {"requests": {"cpu": "10m", "memory": "16Mi"}, "limits": {"cpu": "100m", "memory": "64Mi"}}, + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + } + seed_gate = { + "name": "wait-for-seed", "image": c["mongoImage"], "imagePullPolicy": "IfNotPresent", + "command": ["/bin/sh", "-ec", """ +i=0 +while [ "$i" -lt 120 ]; do + if mongosh "$MONGO_URI" --quiet --eval ' + const ready = db.getCollection("_internalTestSeed").findOne({ + _id: "environment", kind: "droboticflow-internal-test-lane", + batchId: process.env.EXPECTED_BATCH, state: 'complete' + }); + quit(ready ? 0 : 1); + ' >/dev/null 2>&1; then exit 0; fi + i=$((i + 1)) + sleep 5 +done +echo 'Internal test seed is not complete' >&2 +exit 1 +"""], + "env": [env("MONGO_URI", key="MONGO_URI"), env("EXPECTED_BATCH", value=c["batchId"])], + "resources": {"requests": {"cpu": "10m", "memory": "32Mi"}, "limits": {"cpu": "100m", "memory": "128Mi"}}, + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + } + api_container = { + "name": "api", "image": c["image"], "imagePullPolicy": "IfNotPresent", + "ports": [{"name": "http", "containerPort": 3001}], + "env": app_mongo_env + [ + env("PORT", value="3001"), env("DIST_DIR", value="/app/dist"), + env("WORKSPACE_MOUNT_PATH", value="/workspace"), + env("ANNOTATION_PROCESSOR_MODE", value="external"), + env("DROBOTICFLOW_REQUIRE_SESSION_AUTH", value="true"), + env("DROBOTICFLOW_SESSION_COOKIE_SECURE", value="true"), + env("DROBOTICFLOW_OBJECT_SYNC_ENABLED", value="false"), + env("K8S_NAMESPACE", value=ns), env("WORKSPACE_PVC_NAME", value=c["workspacePvcName"]), + env("DROBOTICFLOW_BUILD_COMMIT", value=c["buildCommit"]), + env("DROBOTICFLOW_API_TOKEN", key="DROBOTICFLOW_API_TOKEN"), + ], + "volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}], + "resources": c["resources"]["api"], + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + "readinessProbe": {"httpGet": {"path": "/api/ready", "port": "http"}, "periodSeconds": 10}, + } + items.append(deployment("api", api_container, workspace=True, init=[guard, seed_gate])) + processor_container = { + "name": "processor", "image": c["image"], "imagePullPolicy": "IfNotPresent", + "command": ["/usr/local/bin/droboticflow-api"], + "ports": [{"name": "health", "containerPort": 3003}], + "env": app_mongo_env + [env("ANNOTATION_PROCESSOR_MODE", value="processor"), + env("ANNOTATION_PROCESSOR_CONCURRENCY", value="1"), + env("ANNOTATION_INDEX_BACKFILL", value="0"), + env("WORKSPACE_MOUNT_PATH", value="/workspace")], + "volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}], + "resources": c["resources"]["processor"], + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + "readinessProbe": {"httpGet": {"path": "/ready", "port": "health"}, "periodSeconds": 10}, + } + items.append(deployment("processor", processor_container, workspace=True, init=[guard, seed_gate])) + items.append(obj("v1", "Service", prefix + "-api", { + "type": "ClusterIP", "selector": {"app": prefix + "-api"}, + "ports": [{"name": "http", "port": 80, "targetPort": 3001}], + })) + ingress = obj("networking.k8s.io/v1", "Ingress", prefix + "-api", { + "tls": [{"hosts": [c["hostname"]], "secretName": c["tlsSecretName"]}], + "rules": [{"host": c["hostname"], "http": {"paths": [{"path": "/", "pathType": "Prefix", "backend": {"service": {"name": prefix + "-api", "port": {"number": 80}}}}]}}], + }) + ingress["metadata"]["annotations"] = {"nginx.ingress.kubernetes.io/ssl-redirect": "true"} + if "ingressClassName" in c: + ingress["spec"]["ingressClassName"] = c["ingressClassName"] + items.append(ingress) + items.append(obj("v1", "ResourceQuota", prefix + "-quota", {"hard": { + "pods": "3", "persistentvolumeclaims": "6", + "requests.storage": f"{3 * (c['workspaceGi'] + c['mongoGi'])}Gi", + "requests.cpu": _millicores(sum(_quantity(c["resources"][component]["requests"]["cpu"], "cpu", cpu=True) for component in ("api", "processor", "mongo"))), + "limits.cpu": _millicores(sum(_quantity(c["resources"][component]["limits"]["cpu"], "cpu", cpu=True) for component in ("api", "processor", "mongo"))), + "requests.memory": f"{sum(_quantity(c['resources'][component]['requests']['memory'], 'memory', cpu=False) for component in ('api', 'processor', 'mongo'))}Mi", + "limits.memory": f"{sum(_quantity(c['resources'][component]['limits']['memory'], 'memory', cpu=False) for component in ('api', 'processor', 'mongo'))}Mi", + }})) + items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-default-deny", { + "podSelector": {}, "policyTypes": ["Ingress", "Egress"], + })) + items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-mongo", { + "podSelector": {"matchLabels": {"app": mongo}}, "policyTypes": ["Ingress"], + "ingress": [{"from": [{"podSelector": {"matchLabels": {"app": prefix + "-api"}}}, + {"podSelector": {"matchLabels": {"app": prefix + "-processor"}}}, + {"podSelector": {"matchLabels": {"app": prefix + "-initializer"}}}], + "ports": [{"protocol": "TCP", "port": 27017}]}], + })) + items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-app-egress", { + "podSelector": {"matchExpressions": [{"key": "app", "operator": "In", "values": [prefix + "-api", prefix + "-processor", prefix + "-initializer"]}]}, + "policyTypes": ["Egress"], + "egress": [ + {"to": [{"podSelector": {"matchLabels": {"app": mongo}}}], "ports": [{"protocol": "TCP", "port": 27017}]}, + {"to": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": "kube-system"}}}], "ports": [{"protocol": "UDP", "port": 53}, {"protocol": "TCP", "port": 53}]}, + ], + })) + items.append(obj("networking.k8s.io/v1", "NetworkPolicy", prefix + "-allow-ingress", { + "podSelector": {"matchLabels": {"app": prefix + "-api"}}, "policyTypes": ["Ingress"], + "ingress": [{"from": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": c["ingressNamespace"]}}}], + "ports": [{"protocol": "TCP", "port": 3001}]}], + })) + # Apply policies and quotas before the first workload can become runnable. + priority = { + "Namespace": 0, "ResourceQuota": 1, "NetworkPolicy": 2, + "ServiceAccount": 3, "ConfigMap": 4, "PersistentVolumeClaim": 5, + "Service": 6, "Deployment": 7, "Ingress": 8, + } + items.sort(key=lambda item: priority[item["kind"]]) + return {"apiVersion": "v1", "kind": "List", "items": items} + + +def render_initializer(config: dict) -> dict: + """Render a temporary pod for an approved seed operation. + + The pod is deliberately absent from render_environment. An operator + creates it only after review, then removes it before enabling the API and + processor. The application image supplies the same Debian runtime as the + seed executable, but this manifest does not embed assets or credentials. + """ + c = validate_config(config) + prefix = c["resourcePrefix"] + suffix = f"@{c['mongoServiceName']}.{c['namespace']}.svc.cluster.local:27017/{c['databaseName']}?authSource={c['databaseName']}" + return { + "apiVersion": "v1", "kind": "Pod", + "metadata": { + "name": prefix + "-initializer", "namespace": c["namespace"], + "labels": {"app.kubernetes.io/part-of": "droboticflow-internal-test", "internal-test/batch": c["batchId"], "app": prefix + "-initializer"}, + "annotations": {"internal-test/purpose": "temporary-approved-seed"}, + }, + "spec": { + "serviceAccountName": prefix + "-initializer", "automountServiceAccountToken": False, + "restartPolicy": "Never", "activeDeadlineSeconds": 28800, + "imagePullSecrets": [{"name": c["imagePullSecretName"]}], + "securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001, "fsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}}, + "containers": [{ + "name": "initializer", "image": c["image"], "imagePullPolicy": "IfNotPresent", + "command": ["/bin/sh", "-ec", """ +case "$MONGO_URI" in mongodb://*"$EXPECTED_URI_SUFFIX") ;; *) + echo 'Mongo target must be the isolated test database' >&2; exit 1;; esac +uri_body=${MONGO_URI#mongodb://} +case "${uri_body#*@}" in *'@'*) exit 1;; esac +sleep 28800 +"""], + "env": [ + {"name": "MONGO_URI", "valueFrom": {"secretKeyRef": {"name": c["credentialsSecretName"], "key": "MONGO_URI"}}}, + {"name": "EXPECTED_URI_SUFFIX", "value": suffix}, + {"name": "MONGO_DB", "value": c["databaseName"]}, + {"name": "WORKSPACE_MOUNT_PATH", "value": "/workspace"}, + {"name": "INTERNAL_TEST_BATCH_ID", "value": c["batchId"]}, + ], + "resources": c["resources"]["api"], + "securityContext": {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}, + "volumeMounts": [{"name": "workspace", "mountPath": "/workspace"}, {"name": "scratch", "mountPath": "/tmp"}], + }], + "volumes": [ + {"name": "workspace", "persistentVolumeClaim": {"claimName": c["workspacePvcName"]}}, + {"name": "scratch", "emptyDir": {"sizeLimit": "2Gi"}}, + ], + }, + } + + +def _millicores(value: int) -> str: + return f"{value}m" diff --git a/scripts/internal_test_lane/lifecycle.py b/scripts/internal_test_lane/lifecycle.py new file mode 100644 index 00000000..e3ec0e02 --- /dev/null +++ b/scripts/internal_test_lane/lifecycle.py @@ -0,0 +1,457 @@ +"""Local, offline lifecycle for vetted internal-test samples and batches. + +This module never connects to a cluster, object store, or database. Its records +describe prepared artifacts and operator-supplied evidence, not remote state. +""" + +from __future__ import annotations + +import fcntl +import hashlib +import json +import math +import os +import re +import shutil +import stat +import subprocess +import tempfile +from contextlib import contextmanager +from datetime import datetime, timezone +from pathlib import Path, PurePosixPath + + +OWNER = "droboticflow-internal-test-lane" +SEED_SCHEMA = "droboticflow.internal-test-seed/v1" +SHA_RE = re.compile(r"^[0-9a-f]{64}$") +BATCH_RE = re.compile(r"^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$") +KEY_RE = re.compile(r"^[a-z][a-z0-9-]{0,39}$") +MEDIA_REQUIRED = {"leftVideo", "rightVideo", "leftPTS", "rightPTS", "imu"} +MEDIA_OPTIONAL = {"sourceManifest"} + + +class LaneError(ValueError): + """A refused lane operation with a safe, actionable explanation.""" + + +def _canonical(data: object) -> bytes: + return (json.dumps(data, sort_keys=True, ensure_ascii=False, separators=(",", ":")) + "\n").encode() + + +def _digest(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def _import_identity(manifest: dict, inventory: dict[str, tuple[str, int]]) -> str: + stable_inventory = {path: {"sha256": digest, "size": size} for path, (digest, size) in sorted(inventory.items())} + return _digest(_canonical({"manifest": manifest, "inventory": stable_inventory})) + + +def _read_json(path: Path) -> dict: + _check_no_symlink(Path(os.path.abspath(path))) + try: + with path.open("r", encoding="utf-8") as file: + value = json.load(file) + except (OSError, ValueError) as exc: + raise LaneError(f"cannot read JSON at {path}: {exc}") from exc + if not isinstance(value, dict): + raise LaneError(f"JSON object required at {path}") + return value + + +def _atomic_json(path: Path, value: object) -> None: + fd, name = tempfile.mkstemp(prefix=".writing-", dir=path.parent) + try: + os.fchmod(fd, 0o600) + with os.fdopen(fd, "wb") as file: + file.write(_canonical(value)) + file.flush() + os.fsync(file.fileno()) + os.replace(name, path) + finally: + if os.path.exists(name): + os.unlink(name) + + +def _check_no_symlink(path: Path) -> None: + current = Path(path.anchor) + for part in path.parts[1:]: + current /= part + try: + mode = current.lstat().st_mode + except FileNotFoundError: + continue + if stat.S_ISLNK(mode): + raise LaneError(f"symlink is forbidden in lane path: {current}") + + +def _safe_relative(raw: object) -> Path: + if not isinstance(raw, str) or not raw or "\\" in raw: + raise LaneError("asset path must be a nonempty POSIX relative path") + pure = PurePosixPath(raw) + if pure.is_absolute() or any(part in {"", ".", ".."} for part in raw.split("/")): + raise LaneError(f"unsafe asset path: {raw}") + if any(part.startswith(".") for part in pure.parts): + raise LaneError(f"hidden asset path is forbidden: {raw}") + return Path(*pure.parts) + + +def _valid_batch_id(batch_id: object) -> str: + if not isinstance(batch_id, str) or len(batch_id) > 32 or not BATCH_RE.fullmatch(batch_id): + raise LaneError("batch ID must be lowercase hyphenated, start with a letter, and be <=32 characters") + return batch_id + + +def _hash_file(path: Path) -> tuple[str, int]: + _check_no_symlink(path) + mode = path.stat().st_mode + if not stat.S_ISREG(mode): + raise LaneError(f"regular file required: {path}") + digest = hashlib.sha256() + size = 0 + with path.open("rb") as file: + while block := file.read(1024 * 1024): + digest.update(block) + size += len(block) + return digest.hexdigest(), size + + +def _validate_bundle(bundle: dict) -> tuple[dict, dict[str, tuple[str, int]]]: + if bundle.get("schemaVersion") != SEED_SCHEMA: + raise LaneError(f"bundle schemaVersion must be {SEED_SCHEMA}") + if set(bundle) != {"schemaVersion", "dictionaries", "samples", "assets"}: + raise LaneError("bundle permits only schemaVersion, dictionaries, samples, and assets") + samples = bundle.get("samples") + if not isinstance(samples, list) or len(samples) != 6: + raise LaneError("bundle must contain exactly six samples") + counts = {"BLF-data": 0, "SL-data": 0} + keys: set[str] = set() + refs: set[str] = set() + modes: set[str] = set() + for sample in samples: + if not isinstance(sample, dict): + raise LaneError("each sample must be an object") + allowed_sample = {"key", "project", "mode", "source", "media", "durationSeconds", "snapshot"} + if not set(sample) <= allowed_sample: + raise LaneError("unrecognized sample fields are forbidden") + key, project, mode = sample.get("key"), sample.get("project"), sample.get("mode") + if not isinstance(key, str) or not KEY_RE.fullmatch(key) or key in keys: + raise LaneError(f"invalid or duplicate sample key: {key}") + keys.add(key) + if project not in counts: + raise LaneError(f"unapproved sample project: {project}") + counts[project] += 1 + if mode not in {"scratch", "snapshot"}: + raise LaneError(f"invalid sample mode: {mode}") + modes.add(mode) + source = sample.get("source") + if not isinstance(source, dict) or not isinstance(source.get("captureId"), str) or not source["captureId"]: + raise LaneError(f"source captureId required for {key}") + if not set(source) <= {"captureId", "taskId", "version"}: + raise LaneError(f"unrecognized source fields for {key}") + media = sample.get("media") + if not isinstance(media, dict) or not MEDIA_REQUIRED <= media.keys() or not media.keys() <= MEDIA_REQUIRED | MEDIA_OPTIONAL: + raise LaneError(f"five media fields required for {key}; only sourceManifest is optional") + for field, raw_path in media.items(): + rel = _safe_relative(raw_path) + if len(rel.parts) < 3 or rel.parts[:2] != ("templates", key): + raise LaneError(f"{key}.{field} must live below templates/{key}/") + if rel.as_posix() in refs: + raise LaneError(f"asset referenced more than once: {rel}") + refs.add(rel.as_posix()) + duration = sample.get("durationSeconds") + if isinstance(duration, bool) or not isinstance(duration, (int, float)) or not math.isfinite(duration) or not 0 < duration <= 86400: + raise LaneError(f"positive durationSeconds required for {key}") + if mode == "snapshot" and not isinstance(sample.get("snapshot"), dict): + raise LaneError(f"snapshot slices/actions required for {key}") + if mode == "scratch" and sample.get("snapshot") is not None: + raise LaneError(f"scratch sample cannot carry a snapshot: {key}") + if mode == "snapshot" and not set(sample["snapshot"]) <= {"stage", "slices", "actions"}: + raise LaneError(f"unrecognized snapshot fields for {key}") + if counts != {"BLF-data": 3, "SL-data": 3} or modes != {"scratch", "snapshot"}: + raise LaneError("bundle needs three samples per project and both template modes") + dictionaries = bundle.get("dictionaries") + if not isinstance(dictionaries, dict) or set(dictionaries) != set(counts): + raise LaneError("both project dictionary snapshots are required") + for project, dictionary in dictionaries.items(): + if not isinstance(dictionary, dict) or set(dictionary) != {"sliceNames", "descriptions", "discardReasons"}: + raise LaneError(f"dictionary snapshot incomplete for {project}") + for entries in dictionary.values(): + if not isinstance(entries, list) or not entries or any(not isinstance(entry, str) or not entry.strip() for entry in entries) or len(entries) != len(set(entries)): + raise LaneError(f"dictionary entries invalid for {project}") + assets = bundle.get("assets") + if not isinstance(assets, list): + raise LaneError("assets inventory with size and SHA256 is required") + inventory: dict[str, tuple[str, int]] = {} + for asset in assets: + if not isinstance(asset, dict): + raise LaneError("asset inventory entries must be objects") + if set(asset) != {"path", "sha256", "size"}: + raise LaneError("asset entries permit only path, sha256, and size") + rel = _safe_relative(asset.get("path")).as_posix() + sha, size = asset.get("sha256"), asset.get("size") + if rel in inventory or not isinstance(sha, str) or not SHA_RE.fullmatch(sha) or isinstance(size, bool) or not isinstance(size, int) or size <= 0: + raise LaneError(f"invalid asset inventory entry: {rel}") + inventory[rel] = (sha, size) + if set(inventory) != refs: + raise LaneError("asset inventory must exactly match sample media references") + normalized = {key: value for key, value in bundle.items() if key != "assets"} + return normalized, inventory + + +def _safe_tree_files(directory: Path) -> dict[str, tuple[str, int]]: + _check_no_symlink(directory) + result: dict[str, tuple[str, int]] = {} + for parent, dirs, files in os.walk(directory, followlinks=False): + for name in dirs: + child = Path(parent, name) + if not stat.S_ISDIR(child.lstat().st_mode): + raise LaneError(f"unsafe directory in batch: {child}") + for name in files: + child = Path(parent, name) + result[child.relative_to(directory).as_posix()] = _hash_file(child) + return result + + +def _remove_tree_no_follow(parent: Path, name: str) -> None: + """Delete one validated directory using dirfds and O_NOFOLLOW.""" + flags = os.O_RDONLY | os.O_DIRECTORY | getattr(os, "O_NOFOLLOW", 0) + parent_fd = os.open(parent, flags) + try: + def visit(fd: int) -> None: + with os.scandir(fd) as entries: + children = list(entries) + for entry in children: + info = os.stat(entry.name, dir_fd=fd, follow_symlinks=False) + if stat.S_ISDIR(info.st_mode): + child_fd = os.open(entry.name, flags, dir_fd=fd) + try: + visit(child_fd) + finally: + os.close(child_fd) + os.rmdir(entry.name, dir_fd=fd) + elif stat.S_ISREG(info.st_mode): + os.unlink(entry.name, dir_fd=fd) + else: + raise LaneError(f"refusing unsafe entry during cleanup: {entry.name}") + child_fd = os.open(name, flags, dir_fd=parent_fd) + try: + visit(child_fd) + finally: + os.close(child_fd) + os.rmdir(name, dir_fd=parent_fd) + finally: + os.close(parent_fd) + + +class Lane: + def __init__(self, root: Path): + self.root = Path(os.path.abspath(root)) + _check_no_symlink(self.root) + + @contextmanager + def locked(self): + self.root.mkdir(parents=True, exist_ok=True, mode=0o700) + marker = self.root / ".internal-test-lane.json" + if not marker.exists(): + if any(self.root.iterdir()): + raise LaneError("lane root must be empty before initialization") + os.chmod(self.root, 0o700) + _atomic_json(marker, {"schemaVersion": 1, "owner": OWNER, "root": str(self.root)}) + record = _read_json(marker) + if record != {"schemaVersion": 1, "owner": OWNER, "root": str(self.root)}: + raise LaneError("lane ownership marker does not match this root") + lock_path = self.root / ".lock" + _check_no_symlink(lock_path) + fd = os.open(lock_path, os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0), 0o600) + try: + fcntl.flock(fd, fcntl.LOCK_EX) + yield + finally: + fcntl.flock(fd, fcntl.LOCK_UN) + os.close(fd) + + def import_bundle(self, bundle_path: Path) -> dict: + bundle_path = Path(os.path.abspath(bundle_path)) + bundle = _read_json(bundle_path) + normalized, inventory = _validate_bundle(bundle) + source_root = bundle_path.parent + for rel, expected in inventory.items(): + actual = _hash_file(source_root / rel) + if actual != expected: + raise LaneError(f"source asset hash/size mismatch: {rel}") + digest = _import_identity(normalized, inventory) + with self.locked(): + imports = self.root / "imports" + imports.mkdir(exist_ok=True, mode=0o700) + target = imports / digest + if target.exists(): + self._verify_import(digest) + return {"importId": digest, "samples": 6, "bytes": sum(size for _, size in inventory.values()), "reused": True} + staging = Path(tempfile.mkdtemp(prefix=".pending-", dir=imports)) + try: + for rel, expected in inventory.items(): + dst = staging / rel + dst.parent.mkdir(parents=True, exist_ok=True, mode=0o700) + with (source_root / rel).open("rb") as src, dst.open("xb") as out: + os.chmod(dst, 0o600) + shutil.copyfileobj(src, out, 1024 * 1024) + if _hash_file(dst) != expected: + raise LaneError(f"asset changed during import: {rel}") + _atomic_json(staging / "manifest.json", normalized) + _atomic_json(staging / "inventory.json", {k: {"sha256": v[0], "size": v[1]} for k, v in inventory.items()}) + _atomic_json(staging / "import.json", {"importId": digest, "bytes": sum(size for _, size in inventory.values()), "createdAt": datetime.now(timezone.utc).isoformat(), "state": "imported"}) + os.rename(staging, target) + except BaseException: + shutil.rmtree(staging) + raise + return {"importId": digest, "samples": 6, "bytes": sum(size for _, size in inventory.values()), "reused": False} + + def _verify_import(self, import_id: str) -> tuple[Path, dict]: + if not SHA_RE.fullmatch(import_id): + raise LaneError("import ID must be a SHA256 digest") + directory = self.root / "imports" / import_id + _check_no_symlink(directory) + if not directory.is_dir(): + raise LaneError(f"unknown import: {import_id}") + manifest = _read_json(directory / "manifest.json") + inventory = _read_json(directory / "inventory.json") + if not isinstance(inventory, dict): + raise LaneError("import inventory must be an object") + bundle = {**manifest, "assets": [{"path": path, **value} for path, value in inventory.items() if isinstance(value, dict)]} + normalized, refs = _validate_bundle(bundle) + if normalized != manifest or _import_identity(normalized, refs) != import_id: + raise LaneError("import manifest/inventory digest no longer matches import ID") + import_record = _read_json(directory / "import.json") + if import_record.get("importId") != import_id or import_record.get("state") != "imported": + raise LaneError("import record does not match import ID") + if set(_safe_tree_files(directory)) != set(refs) | {"manifest.json", "inventory.json", "import.json"}: + raise LaneError("import has unexpected or missing files") + for rel, expected in refs.items(): + if _hash_file(directory / rel) != expected: + raise LaneError(f"imported asset changed: {rel}") + return directory, manifest + + def prepare(self, batch_id: str, import_id: str, config_path: Path, repo: Path) -> dict: + _valid_batch_id(batch_id) + from scripts.internal_test_lane.environment import render_environment, render_initializer, validate_config + + raw = _read_json(config_path) + if raw.get("batchId") != batch_id: + raise LaneError("config batchId must match requested batch") + config = validate_config(raw) + environment = render_environment(config) + initializer = render_initializer(config) + with self.locked(): + source, _ = self._verify_import(import_id) + batches = self.root / "batches" + batches.mkdir(exist_ok=True, mode=0o700) + if (batches / batch_id).exists(): + raise LaneError("batch already exists; batch IDs are never overwritten") + for child in batches.iterdir(): + if child.name.startswith(".pending-"): + raise LaneError("unfinished batch preparation needs operator review") + if child.is_dir() and _read_json(child / "batch.json").get("state") != "archived": + raise LaneError("one batch at a time: archive the current batch before preparing another") + staging = Path(tempfile.mkdtemp(prefix=".pending-", dir=batches)) + try: + _atomic_json(staging / "environment.json", environment) + _atomic_json(staging / "initializer.json", initializer) + _atomic_json(staging / "config.json", config) + plan_path = staging / "seed-plan.json" + cmd = ["go", "run", "./cmd/internal-test-seed", "plan", "--manifest", str(source / "manifest.json"), "--assets-root", str(source), "--batch", batch_id, "--out", str(plan_path)] + go_cache = self.root / "go-cache" + go_cache.mkdir(exist_ok=True, mode=0o700) + result = subprocess.run(cmd, cwd=repo, env={**os.environ, "GOPROXY": "off", "GOSUMDB": "off", "GOCACHE": str(go_cache)}, text=True, capture_output=True, check=False) + if result.returncode: + raise LaneError(f"offline seed planner rejected bundle: {result.stderr.strip() or result.stdout.strip()}") + if not plan_path.is_file(): + raise LaneError("seed planner did not write a plan") + plan_hash, _ = _hash_file(plan_path) + env_hash, _ = _hash_file(staging / "environment.json") + initializer_hash, _ = _hash_file(staging / "initializer.json") + record = {"batchId": batch_id, "importId": import_id, "state": "prepared", "preparedAt": datetime.now(timezone.utc).isoformat(), "buildCommit": config["buildCommit"], "image": config["image"], "mongoImage": config["mongoImage"], "configSha256": _digest(_canonical(config)), "environmentSha256": env_hash, "initializerSha256": initializer_hash, "seedPlanSha256": plan_hash, "remoteState": "unknown", "businessAcceptance": "not-recorded"} + _atomic_json(staging / "batch.json", record) + os.rename(staging, batches / batch_id) + return record + except BaseException: + shutil.rmtree(staging) + raise + + def status(self) -> dict: + with self.locked(): + imports = self.root / "imports" + batches = self.root / "batches" + import_ids = sorted(p.name for p in imports.iterdir() if p.is_dir() and not p.name.startswith(".pending-")) if imports.exists() else [] + pending_imports = sorted(p.name for p in imports.iterdir() if p.name.startswith(".pending-")) if imports.exists() else [] + records = [] + pending = [] + if batches.exists(): + for child in sorted(batches.iterdir()): + if child.name.startswith(".pending-"): + pending.append(child.name) + elif child.is_dir(): + records.append(_read_json(child / "batch.json")) + return {"owner": OWNER, "imports": import_ids, "batches": records, "unfinished": {"imports": pending_imports, "batches": pending}, "remoteState": "not-verified-by-this-tool"} + + def archive(self, batch_id: str, evidence_path: Path) -> dict: + _valid_batch_id(batch_id) + evidence = _read_json(evidence_path) + if set(evidence) != {"batchId", "tasksComplete", "environmentStopped", "detached", "reviewer"}: + raise LaneError("archive evidence permits only the five required fields") + if evidence.get("batchId") != batch_id or evidence.get("tasksComplete") is not True or evidence.get("environmentStopped") is not True or evidence.get("detached") is not True or not isinstance(evidence.get("reviewer"), str) or not evidence["reviewer"].strip(): + raise LaneError("archive evidence must bind batchId, confirm tasksComplete/environmentStopped/detached, and name reviewer") + with self.locked(): + directory = self.root / "batches" / batch_id + _check_no_symlink(directory) + record = _read_json(directory / "batch.json") + if record.get("state") != "prepared": + raise LaneError("only a prepared batch can be archived") + _atomic_json(directory / "archive-evidence.json", evidence) + record.update({"state": "archived", "archivedAt": datetime.now(timezone.utc).isoformat(), "evidenceSha256": _digest(_canonical(evidence)), "remoteState": "operator-attested-stopped", "businessAcceptance": "not-inferred"}) + _atomic_json(directory / "batch.json", record) + return record + + def cleanup_plan(self, batch_id: str) -> dict: + _valid_batch_id(batch_id) + with self.locked(): + batches = self.root / "batches" + records = [_read_json(p / "batch.json") for p in batches.iterdir() if p.is_dir() and not p.name.startswith(".pending-")] + archived = sorted((r for r in records if r.get("state") == "archived"), key=lambda r: (r["archivedAt"], r["batchId"]), reverse=True) + if batch_id in {r["batchId"] for r in archived[:3]}: + raise LaneError("the latest three completed batches are retained") + if batch_id not in {r["batchId"] for r in archived}: + raise LaneError("cleanup requires an archived, detached batch") + directory = batches / batch_id + files = _safe_tree_files(directory) + files.pop("cleanup-plan.json", None) + plan = {"schemaVersion": 1, "owner": OWNER, "batchId": batch_id, "scope": "local-batch-record-only", "clusterCleanup": "manual-not-performed", "files": [{"path": path, "sha256": digest, "size": size} for path, (digest, size) in sorted(files.items())]} + plan["confirmSha256"] = _digest(_canonical(plan)) + _atomic_json(directory / "cleanup-plan.json", plan) + return plan + + def clean(self, batch_id: str, confirm: str) -> dict: + _valid_batch_id(batch_id) + if not SHA_RE.fullmatch(confirm): + raise LaneError("--confirm requires the cleanup plan SHA256") + with self.locked(): + batches = self.root / "batches" + directory = batches / batch_id + _check_no_symlink(directory) + plan = _read_json(directory / "cleanup-plan.json") + if plan.get("confirmSha256") != confirm: + raise LaneError("confirmation digest does not match cleanup plan") + base = {k: v for k, v in plan.items() if k != "confirmSha256"} + if _digest(_canonical(base)) != confirm or plan.get("batchId") != batch_id or plan.get("owner") != OWNER: + raise LaneError("cleanup plan integrity check failed") + records = [_read_json(p / "batch.json") for p in batches.iterdir() if p.is_dir() and not p.name.startswith(".pending-")] + archived = sorted((r for r in records if r.get("state") == "archived"), key=lambda r: (r["archivedAt"], r["batchId"]), reverse=True) + if batch_id in {r["batchId"] for r in archived[:3]} or batch_id not in {r["batchId"] for r in archived}: + raise LaneError("batch is active or belongs to the latest three completed batches") + expected = {entry["path"]: (entry["sha256"], entry["size"]) for entry in plan["files"]} + actual = _safe_tree_files(directory) + actual.pop("cleanup-plan.json", None) + if actual != expected: + raise LaneError("batch files changed after cleanup preview; create a new plan") + _remove_tree_no_follow(batches, batch_id) + return {"batchId": batch_id, "removed": True, "scope": "local-batch-record-only", "clusterCleanup": "manual-not-performed"} diff --git a/tests/internal_test_lane/make_synthetic_bundle.py b/tests/internal_test_lane/make_synthetic_bundle.py new file mode 100644 index 00000000..98ae6fb9 --- /dev/null +++ b/tests/internal_test_lane/make_synthetic_bundle.py @@ -0,0 +1,63 @@ +"""Create six tiny generated captures; never reads customer media or credentials.""" + +import argparse +import hashlib +import json +from pathlib import Path +import shutil +import subprocess + + +def make_bundle(output): + root = Path(output).resolve() + root.mkdir(parents=True, exist_ok=False, mode=0o700) + video = root / "generated.mp4" + subprocess.run(["ffmpeg", "-nostdin", "-hide_banner", "-loglevel", "error", + "-f", "lavfi", "-i", "testsrc2=size=96x64:rate=4:duration=2", + "-c:v", "libx264", "-pix_fmt", "yuv420p", str(video)], check=True) + samples, assets = [], [] + for project in ("BLF-data", "SL-data"): + for number in range(1, 4): + key = project.split("-")[0].lower() + f"-{number}" + directory = root / "templates" / key + directory.mkdir(parents=True, mode=0o700) + media = {} + for camera in ("left", "right"): + name = f"camera_{camera}.mp4" + shutil.copyfile(video, directory / name) + media[f"{camera}Video"] = f"templates/{key}/{name}" + pts_name = f"camera_{camera}_pts.csv" + (directory / pts_name).write_text("frame_index,timestamp_us\n" + "".join( + f"{frame},{1000000 + frame * 250000}\n" for frame in range(8))) + media[f"{camera}PTS"] = f"templates/{key}/{pts_name}" + (directory / "imu.csv").write_text("timestamp_us,x,y,z,type\n" + "".join( + f"{1000000 + frame * 250000},0,0,9.8,accel\n{1000000 + frame * 250000},0,0,0,gyro\n" + for frame in range(8))) + media["imu"] = f"templates/{key}/imu.csv" + sample = {"key": key, "project": project, "mode": "scratch" if number == 1 else "snapshot", + "source": {"captureId": f"synthetic-{key}", "taskId": f"synthetic-source-{key}", "version": "1"}, + "media": media, "durationSeconds": 2} + if number > 1: + sample["snapshot"] = { + "stage": "annotation" if number == 2 else "quality_review", + "slices": [{"start": 0, "end": 2, "name": "测试切片"}], + "actions": [{"start": 0, "end": 1, "description": "测试动作"}, + {"start": 1, "end": 2, "description": "", "isInvalid": False}], + } + samples.append(sample) + for path in media.values(): + content = (root / path).read_bytes() + assets.append({"path": path, "size": len(content), "sha256": hashlib.sha256(content).hexdigest()}) + video.unlink() # Only the generator's own temporary synthetic file. + dictionaries = {p: {"sliceNames": ["测试切片"], "descriptions": ["测试动作"], + "discardReasons": ["测试遮挡"]} for p in ("BLF-data", "SL-data")} + manifest = {"schemaVersion": "droboticflow.internal-test-seed/v1", "dictionaries": dictionaries, + "samples": samples, "assets": assets} + (root / "manifest.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n") + return root / "manifest.json" + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--out", required=True) + print(make_bundle(parser.parse_args().out)) diff --git a/tests/internal_test_lane/test_credentials.py b/tests/internal_test_lane/test_credentials.py new file mode 100644 index 00000000..9663b625 --- /dev/null +++ b/tests/internal_test_lane/test_credentials.py @@ -0,0 +1,79 @@ +"""Private output contract for test-lane credential generation.""" + +import json +import os +from pathlib import Path +import tempfile +import unittest +from urllib.parse import urlparse, unquote + +from scripts.internal_test_lane.credentials import generate_credentials + + +def config(): + return { + "schemaVersion": 1, "batchId": "secret-check", "hostname": "test.example.test", + "productionOrigins": ["http://120.48.180.80"], + "image": "example.test/api@sha256:" + "a" * 64, + "mongoImage": "example.test/mongo@sha256:" + "b" * 64, + "buildCommit": "c" * 40, "workspaceGi": 10, "mongoGi": 5, + "tlsSecretName": "internal-test-tls", "imagePullSecretName": "internal-test-registry", + "ingressNamespace": "ingress-nginx", + "resources": {name: {"requests": {"cpu": "100m", "memory": "128Mi"}, + "limits": {"cpu": "500m", "memory": "512Mi"}} + for name in ("api", "processor", "mongo")}, + } + + +class CredentialTests(unittest.TestCase): + def test_private_outputs_bind_only_test_target_and_do_not_echo_passwords(self): + with tempfile.TemporaryDirectory() as directory: + output = Path(directory).resolve() / "credentials" + result = generate_credentials(config(), output) + secret_file = Path(result["secretManifest"]) + accounts_file = Path(result["accountsFile"]) + self.assertEqual(os.stat(secret_file).st_mode & 0o777, 0o600) + self.assertEqual(os.stat(accounts_file).st_mode & 0o777, 0o600) + self.assertEqual(os.stat(output).st_mode & 0o777, 0o700) + secret = json.loads(secret_file.read_text()) + self.assertEqual(secret["metadata"]["namespace"], "droboticflow-internal-test") + data = secret["stringData"] + uri = urlparse(data["MONGO_URI"]) + self.assertEqual(uri.hostname, "internal-test-secret-check-mongo.droboticflow-internal-test.svc.cluster.local") + self.assertEqual(uri.path, "/droboticflow_internal_test_secret_check") + self.assertEqual(unquote(uri.password), data["MONGO_APP_PASSWORD"]) + self.assertEqual(Path(result["mongoUriFile"]).read_text().strip(), data["MONGO_URI"]) + self.assertEqual(os.stat(result["mongoUriFile"]).st_mode & 0o777, 0o600) + accounts = json.loads(accounts_file.read_text())["accounts"] + self.assertEqual({a["role"] for a in accounts}, + {"admin", "supplier_manager", "annotator", "quality_reviewer", "acceptance_reviewer"}) + passwords = [a["password"] for a in accounts] + self.assertEqual(len(set(passwords)), 5) + self.assertTrue(all(len(password) >= 32 for password in passwords)) + self.assertTrue(all(password not in json.dumps(result) for password in passwords + list(data.values()))) + + def test_generation_refuses_overwrite_or_symlinked_destination(self): + with tempfile.TemporaryDirectory() as directory: + output = Path(directory).resolve() / "credentials" + generate_credentials(config(), output) + before = (output / "accounts.json").read_bytes() + with self.assertRaises((ValueError, FileExistsError)): + generate_credentials(config(), output) + self.assertEqual((output / "accounts.json").read_bytes(), before) + linked = Path(directory).resolve() / "linked" + linked.symlink_to(output, target_is_directory=True) + with self.assertRaises((ValueError, FileExistsError)): + generate_credentials(config(), linked / "nested") + + def test_invalid_environment_is_rejected_before_any_output(self): + with tempfile.TemporaryDirectory() as directory: + output = Path(directory).resolve() / "credentials" + unsafe = config() + unsafe["namespace"] = "droboticflow-fanyu" + with self.assertRaises(ValueError): + generate_credentials(unsafe, output) + self.assertFalse(output.exists()) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/internal_test_lane/test_environment.py b/tests/internal_test_lane/test_environment.py new file mode 100644 index 00000000..25f139f2 --- /dev/null +++ b/tests/internal_test_lane/test_environment.py @@ -0,0 +1,142 @@ +"""Offline contract tests for the internal test environment renderer.""" + +import copy +import os +import subprocess +import unittest + +from scripts.internal_test_lane.environment import render_environment, render_initializer, validate_config + + +def config(): + pair = {"requests": {"cpu": "100m", "memory": "128Mi"}, "limits": {"cpu": "500m", "memory": "512Mi"}} + return { + "schemaVersion": 1, + "batchId": "acceptance-1", + "hostname": "test.example.test", + "productionOrigins": ["http://120.48.180.80", "https://prod.example.test"], + "image": "registry.example.test/droboticflow/api@sha256:" + "a" * 64, + "mongoImage": "registry.example.test/mongo@sha256:" + "b" * 64, + "buildCommit": "c" * 40, + "workspaceGi": 10, + "mongoGi": 5, + "resources": {name: copy.deepcopy(pair) for name in ("api", "processor", "mongo")}, + "tlsSecretName": "internal-test-tls", + "imagePullSecretName": "internal-test-registry", + "ingressNamespace": "ingress-nginx", + } + + +class EnvironmentTests(unittest.TestCase): + def test_rendered_batch_uses_dedicated_database_and_retained_pvcs(self): + rendered = render_environment(config()) + self.assertEqual(rendered["kind"], "List") + items = rendered["items"] + pvc_names = {x["metadata"]["name"] for x in items if x["kind"] == "PersistentVolumeClaim"} + self.assertEqual(pvc_names, {"internal-test-acceptance-1-workspace", "internal-test-acceptance-1-mongo-data"}) + cfg = next(x for x in items if x["kind"] == "ConfigMap" and x["metadata"]["name"] == "internal-test-acceptance-1-config") + self.assertEqual(cfg["data"]["MONGO_DB"], "droboticflow_internal_test_acceptance_1") + self.assertEqual(cfg["data"]["ANNOTATION_PROCESSOR_MODE"], "external") + self.assertEqual(cfg["data"]["DROBOTICFLOW_REQUIRE_SESSION_AUTH"], "true") + deployments = {x["metadata"]["name"]: x for x in items if x["kind"] == "Deployment"} + self.assertEqual(deployments["internal-test-acceptance-1-mongo"]["spec"]["replicas"], 1) + self.assertEqual(deployments["internal-test-acceptance-1-api"]["spec"]["replicas"], 0) + self.assertEqual(deployments["internal-test-acceptance-1-processor"]["spec"]["replicas"], 0) + mongo_pod = deployments["internal-test-acceptance-1-mongo"]["spec"]["template"]["spec"] + mongo_env = mongo_pod["containers"][0]["env"] + refs = {entry["name"]: entry["valueFrom"]["secretKeyRef"]["key"] for entry in mongo_env if "valueFrom" in entry} + self.assertEqual(refs, { + "MONGO_INITDB_ROOT_USERNAME": "MONGO_ROOT_USERNAME", + "MONGO_INITDB_ROOT_PASSWORD": "MONGO_ROOT_PASSWORD", + "MONGO_APP_USERNAME": "MONGO_APP_USERNAME", + "MONGO_APP_PASSWORD": "MONGO_APP_PASSWORD", + }) + self.assertEqual(mongo_pod["securityContext"]["runAsUser"], 999) + self.assertEqual(mongo_pod["securityContext"]["fsGroup"], 999) + self.assertTrue(mongo_pod["securityContext"]["runAsNonRoot"]) + + def test_initializer_is_a_separate_ephemeral_pod_with_only_test_scope(self): + manifest = render_environment(config()) + self.assertFalse(any(x["kind"] == "Pod" for x in manifest["items"])) + initializer = render_initializer(config()) + self.assertEqual(initializer["kind"], "Pod") + self.assertEqual(initializer["metadata"]["name"], "internal-test-acceptance-1-initializer") + pod = initializer["spec"] + self.assertFalse(pod["automountServiceAccountToken"]) + self.assertEqual(pod["restartPolicy"], "Never") + self.assertEqual(pod["volumes"][0]["persistentVolumeClaim"]["claimName"], + "internal-test-acceptance-1-workspace") + refs = {x["name"]: x["valueFrom"]["secretKeyRef"] for x in pod["containers"][0]["env"] if "valueFrom" in x} + self.assertEqual(refs, {"MONGO_URI": {"name": "internal-test-acceptance-1-credentials", "key": "MONGO_URI"}}) + + def test_validation_rejects_a_production_or_ip_hostname(self): + for hostname in ("prod.example.test", "120.48.180.80", "localhost"): + candidate = config() + candidate["hostname"] = hostname + with self.subTest(hostname=hostname), self.assertRaises(ValueError): + validate_config(candidate) + + def test_runtime_has_only_batch_scoped_secret_refs_and_no_cluster_credential(self): + items = render_environment(config())["items"] + deployments = {x["metadata"]["name"]: x for x in items if x["kind"] == "Deployment"} + for component in ("api", "processor"): + pod = deployments[f"internal-test-acceptance-1-{component}"]["spec"]["template"]["spec"] + self.assertFalse(pod["automountServiceAccountToken"]) + environment = pod["containers"][0]["env"] + refs = {item["name"]: item["valueFrom"]["secretKeyRef"] for item in environment if "valueFrom" in item} + self.assertEqual(refs["MONGO_URI"]["name"], "internal-test-acceptance-1-credentials") + self.assertNotIn("MONGO_ROOT_PASSWORD", refs) + self.assertNotIn("MONGO_APP_PASSWORD", refs) + self.assertEqual([part["name"] for part in pod["initContainers"]], + ["validate-mongo-target", "wait-for-seed"]) + gate = pod["initContainers"][1] + self.assertIn("_internalTestSeed", gate["command"][-1]) + self.assertIn("state: 'complete'", gate["command"][-1]) + self.assertFalse(any(x["kind"] in ("RoleBinding", "ClusterRoleBinding", "Secret") for x in items)) + + def test_workspace_is_shareable_and_network_denies_unapproved_egress(self): + items = render_environment(config())["items"] + workspace = next(x for x in items if x["kind"] == "PersistentVolumeClaim" and x["metadata"]["name"].endswith("-workspace")) + self.assertEqual(workspace["spec"]["accessModes"], ["ReadWriteMany"]) + policies = {x["metadata"]["name"]: x["spec"] for x in items if x["kind"] == "NetworkPolicy"} + self.assertEqual(policies["internal-test-acceptance-1-default-deny"]["policyTypes"], ["Ingress", "Egress"]) + self.assertEqual(len(policies["internal-test-acceptance-1-allow-app-egress"]["egress"]), 2) + first_pod = next(i for i, item in enumerate(items) if item["kind"] == "Deployment") + self.assertLess(max(i for i, item in enumerate(items) if item["kind"] == "NetworkPolicy"), first_pod) + + def test_startup_guard_rejects_secret_uri_outside_batch_database(self): + items = render_environment(config())["items"] + api = next(x for x in items if x["kind"] == "Deployment" and x["metadata"]["name"].endswith("-api")) + guard = api["spec"]["template"]["spec"]["initContainers"][0] + suffix = next(item["value"] for item in guard["env"] if item["name"] == "EXPECTED_URI_SUFFIX") + + def guard_accepts(uri): + run = subprocess.run(guard["command"], env={**os.environ, "MONGO_URI": uri, "EXPECTED_URI_SUFFIX": suffix}, + capture_output=True, text=True, check=False) + return run.returncode == 0 + + self.assertTrue(guard_accepts("mongodb://tester:encoded-secret" + suffix)) + self.assertFalse(guard_accepts("mongodb://tester:secret@prod.example.test:27017/droboticflow")) + self.assertFalse(guard_accepts("mongodb://tester:secret@prod.example.test:27017/other@" + suffix[1:])) + + def test_processor_can_use_bounded_eight_gibibytes_without_raising_api_cap(self): + candidate = config() + candidate["resources"]["processor"]["limits"] = {"cpu": "4", "memory": "8Gi"} + self.assertEqual(validate_config(candidate)["resources"]["processor"]["limits"]["memory"], "8Gi") + candidate["resources"]["api"]["limits"]["memory"] = "8Gi" + with self.assertRaises(ValueError): + validate_config(candidate) + + def test_forged_derived_names_and_aliases_are_rejected(self): + forged = config() + forged["databaseName"] = "customer_database" + with self.assertRaises(ValueError): + validate_config(forged) + aliased = config() + aliased["mongoUri"] = "mongodb://customer.example.test" + with self.assertRaises(ValueError): + validate_config(aliased) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/internal_test_lane/test_lifecycle.py b/tests/internal_test_lane/test_lifecycle.py new file mode 100644 index 00000000..d0bf9c85 --- /dev/null +++ b/tests/internal_test_lane/test_lifecycle.py @@ -0,0 +1,176 @@ +"""Public CLI and lifecycle refusal tests using only synthetic local files.""" + +import hashlib +import json +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path +from unittest import mock + +from scripts.internal_test_lane.lifecycle import Lane, LaneError + + +REPO = Path(__file__).resolve().parents[2] +CLI = REPO / "scripts" / "internal_test_lane.py" + + +def synthetic_bundle(directory: Path) -> Path: + samples = [] + assets = [] + for project in ("BLF-data", "SL-data"): + for number in range(3): + key = f"{project.lower()}-{number}" + media = {} + for field in ("leftVideo", "rightVideo", "leftPTS", "rightPTS", "imu", "sourceManifest"): + rel = f"templates/{key}/{field}.bin" + payload = f"synthetic:{key}:{field}".encode() + path = directory / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(payload) + media[field] = rel + assets.append({"path": rel, "sha256": hashlib.sha256(payload).hexdigest(), "size": len(payload)}) + mode = "scratch" if number == 0 else "snapshot" + sample = {"key": key, "project": project, "mode": mode, "source": {"captureId": f"synthetic-{key}", "version": "v1"}, "media": media, "durationSeconds": 4} + if mode == "snapshot": + sample["snapshot"] = {"stage": "annotation", "slices": [{"start": 0, "end": 4, "name": "test"}], "actions": [{"start": 0, "end": 4, "description": "synthetic", "isInvalid": False}]} + samples.append(sample) + dictionaries = {name: {"sliceNames": ["test"], "descriptions": ["synthetic"], "discardReasons": ["bad"]} for name in ("BLF-data", "SL-data")} + bundle = directory / "bundle.json" + bundle.write_text(json.dumps({"schemaVersion": "droboticflow.internal-test-seed/v1", "dictionaries": dictionaries, "samples": samples, "assets": assets}), encoding="utf-8") + return bundle + + +class LifecycleTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.base = Path(self.temp.name).resolve() + self.bundle = synthetic_bundle(self.base / "source") + self.lane = Lane(self.base / "lane") + + def test_import_is_immutable_and_rejects_hash_change(self): + first = self.lane.import_bundle(self.bundle) + self.assertEqual(first["samples"], 6) + self.assertFalse(first["reused"]) + self.assertTrue(self.lane.import_bundle(self.bundle)["reused"]) + asset = self.base / "source" / "templates" / "blf-data-0" / "imu.bin" + asset.write_bytes(b"changed") + with self.assertRaisesRegex(LaneError, "hash/size mismatch"): + self.lane.import_bundle(self.bundle) + imported_asset = self.lane.root / "imports" / first["importId"] / "templates" / "blf-data-0" / "imu.bin" + self.assertNotEqual(imported_asset.read_bytes(), b"changed") + + def test_import_refuses_escape_and_symlink(self): + value = json.loads(self.bundle.read_text()) + value["samples"][0]["media"]["leftVideo"] = "../outside" + self.bundle.write_text(json.dumps(value)) + with self.assertRaisesRegex(LaneError, "unsafe asset path"): + self.lane.import_bundle(self.bundle) + + self.bundle = synthetic_bundle(self.base / "source") + path = self.base / "source" / "templates" / "blf-data-0" / "leftVideo.bin" + path.unlink() + path.symlink_to(self.base / "source" / "templates" / "blf-data-0" / "rightVideo.bin") + with self.assertRaisesRegex(LaneError, "symlink"): + self.lane.import_bundle(self.bundle) + + def test_cli_status_reports_local_preparation_only(self): + self.lane.import_bundle(self.bundle) + completed = subprocess.run([sys.executable, str(CLI), "--root", str(self.lane.root), "status"], cwd=REPO, capture_output=True, text=True, check=False) + self.assertEqual(completed.returncode, 0, completed.stderr) + status = json.loads(completed.stdout) + self.assertEqual(status["remoteState"], "not-verified-by-this-tool") + self.assertEqual(len(status["imports"]), 1) + + def test_cli_credentials_rejects_symlink_ancestor_inside_owned_root(self): + from test_environment import config + + self.lane.import_bundle(self.bundle) + config_path = self.base / "config.json" + config_path.write_text(json.dumps(config())) + inside = self.lane.root / "inside" + inside.mkdir() + (self.lane.root / "link").symlink_to(inside, target_is_directory=True) + output = self.lane.root / "link" / "credentials" + completed = subprocess.run([sys.executable, str(CLI), "--root", str(self.lane.root), "credentials", "--config", str(config_path), "--out", str(output)], cwd=REPO, capture_output=True, text=True, check=False) + self.assertEqual(completed.returncode, 2) + self.assertIn("symlink", completed.stderr) + self.assertFalse((inside / "credentials").exists()) + + def test_prepare_rejects_modified_import_manifest_or_inventory(self): + from test_environment import config + + imported = self.lane.import_bundle(self.bundle) + config_path = self.base / "config.json" + config_path.write_text(json.dumps(config())) + source = self.lane.root / "imports" / imported["importId"] + manifest_path = source / "manifest.json" + original_manifest = manifest_path.read_text() + manifest = json.loads(original_manifest) + manifest["samples"][0]["source"]["version"] = "tampered" + manifest_path.write_text(json.dumps(manifest)) + with self.assertRaisesRegex(LaneError, "digest no longer matches"): + self.lane.prepare("acceptance-1", imported["importId"], config_path, REPO) + manifest_path.write_text(original_manifest) + inventory_path = source / "inventory.json" + inventory = json.loads(inventory_path.read_text()) + first = next(iter(inventory)) + inventory[first]["sha256"] = "0" * 64 + inventory_path.write_text(json.dumps(inventory)) + with self.assertRaisesRegex(LaneError, "digest no longer matches"): + self.lane.prepare("acceptance-1", imported["importId"], config_path, REPO) + + def test_cleanup_retains_latest_three_and_checks_preview(self): + self.lane.import_bundle(self.bundle) + batches = self.lane.root / "batches" + batches.mkdir() + for number in range(4): + batch = batches / f"test-{number}" + batch.mkdir() + (batch / "batch.json").write_text(json.dumps({"batchId": batch.name, "state": "archived", "archivedAt": f"2026-09-0{number + 1}T00:00:00Z"})) + (batch / "result.txt").write_text(f"synthetic-{number}") + with self.assertRaisesRegex(LaneError, "latest three"): + self.lane.cleanup_plan("test-3") + plan = self.lane.cleanup_plan("test-0") + self.assertEqual(plan["clusterCleanup"], "manual-not-performed") + with self.assertRaisesRegex(LaneError, "confirmation"): + self.lane.clean("test-0", "0" * 64) + (batches / "test-0" / "result.txt").write_text("changed") + with self.assertRaisesRegex(LaneError, "changed after cleanup"): + self.lane.clean("test-0", plan["confirmSha256"]) + plan = self.lane.cleanup_plan("test-0") + result = self.lane.clean("test-0", plan["confirmSha256"]) + self.assertTrue(result["removed"]) + self.assertFalse((batches / "test-0").exists()) + self.assertTrue((self.lane.root / "imports").exists()) + + def test_prepare_is_new_batch_only_and_never_infers_remote_state(self): + from tests.internal_test_lane.test_environment import config + + imported = self.lane.import_bundle(self.bundle) + candidate = config() + config_path = self.base / "config.json" + config_path.write_text(json.dumps(candidate)) + + def fake_run(args, **kwargs): + Path(args[args.index("--out") + 1]).write_text('{"synthetic":true}') + return subprocess.CompletedProcess(args, 0, "", "") + + with mock.patch("scripts.internal_test_lane.lifecycle.subprocess.run", side_effect=fake_run) as call: + record = self.lane.prepare("acceptance-1", imported["importId"], config_path, REPO) + self.assertEqual(record["state"], "prepared") + self.assertEqual(record["remoteState"], "unknown") + package = self.lane.root / "batches" / "acceptance-1" + initializer = json.loads((package / "initializer.json").read_text()) + self.assertEqual(initializer["kind"], "Pod") + self.assertEqual(initializer["metadata"]["namespace"], "droboticflow-internal-test") + self.assertEqual(record["initializerSha256"], hashlib.sha256((package / "initializer.json").read_bytes()).hexdigest()) + self.assertEqual(call.call_args.kwargs["env"]["GOPROXY"], "off") + with self.assertRaisesRegex(LaneError, "already exists"): + self.lane.prepare("acceptance-1", imported["importId"], config_path, REPO) + + +if __name__ == "__main__": + unittest.main()