Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions cmd/clawctl-hub/operator_admin_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestOperatorAdminScopeCentralAuthorization(t *testing.T) {
allowed = append(allowed, route)
}
}
if len(allowed) != 12 {
if len(allowed) != 13 {
t.Fatalf("operator-admin routes=%d", len(allowed))
}
forbidden := []string{
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestOperatorAdminScheduleAPI(t *testing.T) {
if err != nil {
t.Fatal(err)
}
routes := []string{"POST /v1/operator/schedules/{operation}/{phase}", "POST /v1/operator/schedules/{id}/run-now"}
routes := []string{"POST /v1/operator/schedules/{operation}/{phase}", "POST /v1/operator/schedules/{id}/run-now", "POST /v1/operator/schedule-jobs/{id}/cancel"}
_, secret, err := f.store.CreateServiceToken(store.ServiceToken{Name: "scheduler", Scope: "operator-admin", ExpiresAt: time.Now().Add(time.Hour), Allowlist: routes}, actor.AccountID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -146,4 +146,32 @@ func TestOperatorAdminScheduleAPI(t *testing.T) {
if run.Code != 200 {
t.Fatalf("run %d %s", run.Code, run.Body.String())
}
var ran store.ScheduleRunNowResponse
if err := json.Unmarshal(run.Body.Bytes(), &ran); err != nil || len(ran.JobIDs) != 1 {
t.Fatalf("run receipt %s", run.Body.String())
}
if rec := call("/v1/operator/schedule-jobs/"+ran.JobIDs[0]+"/cancel", store.ScheduleJobCancelRequest{}, "cancel-empty"); rec.Code != 400 {
t.Fatalf("cancel without reason %d %s", rec.Code, rec.Body.String())
}
if rec := call("/v1/operator/schedule-jobs/"+ran.JobIDs[0]+"/cancel", store.ScheduleJobCancelRequest{Reason: "machine offline"}, ""); rec.Code == 200 {
t.Fatal("cancel without idempotency key succeeded")
}
cancel := call("/v1/operator/schedule-jobs/"+ran.JobIDs[0]+"/cancel", store.ScheduleJobCancelRequest{Reason: "machine offline"}, "cancel")
if cancel.Code != 200 {
t.Fatalf("cancel %d %s", cancel.Code, cancel.Body.String())
}
var cancelled store.ScheduleJobCancelResponse
if err := json.Unmarshal(cancel.Body.Bytes(), &cancelled); err != nil || cancelled.State != "rejected" || cancelled.ScheduleID != v.ID || cancelled.Replayed {
t.Fatalf("cancel receipt %s", cancel.Body.String())
}
again := call("/v1/operator/schedule-jobs/"+ran.JobIDs[0]+"/cancel", store.ScheduleJobCancelRequest{Reason: "machine offline"}, "cancel")
if again.Code != 200 || again.Header().Get("Idempotency-Replayed") != "true" {
t.Fatalf("cancel replay %d %s", again.Code, again.Body.String())
}
if rec := call("/v1/operator/schedule-jobs/"+ran.JobIDs[0]+"/cancel", store.ScheduleJobCancelRequest{Reason: "machine offline"}, "cancel-twice"); rec.Code != 412 || !strings.Contains(rec.Body.String(), "PRECONDITION_FAILED") {
t.Fatalf("cancel of a finished job %d %s", rec.Code, rec.Body.String())
}
if rec := call("/v1/operator/schedule-jobs/no-such-job/cancel", store.ScheduleJobCancelRequest{Reason: "machine offline"}, "cancel-missing"); rec.Code != 404 {
t.Fatalf("cancel missing %d %s", rec.Code, rec.Body.String())
}
}
3 changes: 2 additions & 1 deletion cmd/clawctl-hub/operator_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,13 @@ func (h *hub) operatorRoutes(mux *http.ServeMux) []string {
} {
mux.HandleFunc(patterns[diskClean+i], handler)
}
patterns = append(patterns, "GET /v1/operator/schedules", "GET /v1/operator/schedules/{id}", "POST /v1/operator/schedules/{operation}/{phase}", "POST /v1/operator/schedules/{id}/run-now", "GET /v1/operator/script-catalog", "POST /v1/operator/script-runs/preview", "POST /v1/operator/script-runs")
patterns = append(patterns, "GET /v1/operator/schedules", "GET /v1/operator/schedules/{id}", "POST /v1/operator/schedules/{operation}/{phase}", "POST /v1/operator/schedules/{id}/run-now", "POST /v1/operator/schedule-jobs/{id}/cancel", "GET /v1/operator/script-catalog", "POST /v1/operator/script-runs/preview", "POST /v1/operator/script-runs")
mux.HandleFunc("GET /v1/operator/script-catalog", h.handleScriptCatalog)
mux.HandleFunc("GET /v1/operator/schedules", h.handleScheduleList)
mux.HandleFunc("GET /v1/operator/schedules/{id}", h.handleScheduleGet)
mux.HandleFunc("POST /v1/operator/schedules/{operation}/{phase}", h.handleScheduleManage)
mux.HandleFunc("POST /v1/operator/schedules/{id}/run-now", h.handleScheduleRunNow)
mux.HandleFunc("POST /v1/operator/schedule-jobs/{id}/cancel", h.handleScheduleJobCancel)
mux.HandleFunc("POST /v1/operator/script-runs/preview", h.handleScriptPreview)
mux.HandleFunc("POST /v1/operator/script-runs", h.handleScriptApply)
for _, pattern := range []string{"POST /v1/operator/disk-ops/actions/preview", "POST /v1/operator/disk-ops/actions/decide", "GET /v1/operator/disk-ops/quarantine/{machine_id}", "POST /v1/operator/disk-ops/restores/preview", "POST /v1/operator/disk-ops/restores"} {
Expand Down
1 change: 1 addition & 0 deletions cmd/clawctl-hub/operator_boundary.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ var operatorRoutePolicies = map[string]operatorRoutePolicy{
"GET /v1/operator/schedules/{id}": {operatorauth.View, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligible},
"POST /v1/operator/schedules/{operation}/{phase}": {operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin},
"POST /v1/operator/schedules/{id}/run-now": {operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin},
"POST /v1/operator/schedule-jobs/{id}/cancel": {operatorauth.Admin, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin},
"GET /v1/operator/script-catalog": {operatorauth.View, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceIneligible},
"POST /v1/operator/script-runs/preview": {operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleReadScriptsOnly},
"POST /v1/operator/script-runs": {operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleReadScriptsOnly},
Expand Down
21 changes: 11 additions & 10 deletions cmd/clawctl-hub/operator_boundary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ func TestOperatorRouteManifestMatchesAllRegisteredRoutes(t *testing.T) {
t.Fatalf("non-operator registered=%d policies=%d, want 39/39",
len(nonOperatorRegistered), len(nonOperatorRoutePolicies))
}
if len(operatorRegistered) != 325 || len(operatorRoutePolicies) != 325 {
t.Fatalf("operator registered=%d policies=%d, want 325/325",
if len(operatorRegistered) != 326 || len(operatorRoutePolicies) != 326 {
t.Fatalf("operator registered=%d policies=%d, want 326/326",
len(operatorRegistered), len(operatorRoutePolicies))
}
if len(nonOperatorRegistered)+len(operatorRegistered) != 364 {
t.Fatalf("all registered routes=%d, 預期 364", len(nonOperatorRegistered)+len(operatorRegistered))
if len(nonOperatorRegistered)+len(operatorRegistered) != 365 {
t.Fatalf("all registered routes=%d, 預期 365", len(nonOperatorRegistered)+len(operatorRegistered))
}
counts := map[operatorauth.Permission]int{}
representations := map[operatorRepresentation]int{}
Expand All @@ -146,14 +146,14 @@ func TestOperatorRouteManifestMatchesAllRegisteredRoutes(t *testing.T) {
representations[policy.Representation]++
profiles[policy.SecurityProfile]++
}
if counts[operatorauth.View] != 128 || counts[operatorauth.Operate] != 34 || counts[operatorauth.Admin] != 163 {
t.Fatalf("permission counts=%v, want view=128 operate=34 admin=163", counts)
if counts[operatorauth.View] != 128 || counts[operatorauth.Operate] != 34 || counts[operatorauth.Admin] != 164 {
t.Fatalf("permission counts=%v, want view=128 operate=34 admin=164", counts)
}
if representations[operatorJSON] != 159 || representations[operatorHTML] != 165 || representations[operatorPlain] != 1 {
t.Fatalf("representation counts=%v, want JSON=159 HTML=165 plain=1", representations)
if representations[operatorJSON] != 160 || representations[operatorHTML] != 165 || representations[operatorPlain] != 1 {
t.Fatalf("representation counts=%v, want JSON=160 HTML=165 plain=1", representations)
}
if profiles[operatorSecurityLocked] != 324 || profiles[operatorSecurityTerminal] != 1 {
t.Fatalf("security profiles=%v, want locked=324 terminal=1", profiles)
if profiles[operatorSecurityLocked] != 325 || profiles[operatorSecurityTerminal] != 1 {
t.Fatalf("security profiles=%v, want locked=325 terminal=1", profiles)
}
}

Expand Down Expand Up @@ -580,6 +580,7 @@ func TestEveryOperatorRouteRequestsItsExactPermission(t *testing.T) {
{"GET /v1/operator/schedules/{id}", "/v1/operator/schedules/s-1", operatorRoutePolicy{operatorauth.View, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligible}},
{"POST /v1/operator/schedules/{operation}/{phase}", "/v1/operator/schedules/upsert/preview", operatorRoutePolicy{operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin}},
{"POST /v1/operator/schedules/{id}/run-now", "/v1/operator/schedules/s-1/run-now", operatorRoutePolicy{operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin}},
{"POST /v1/operator/schedule-jobs/{id}/cancel", "/v1/operator/schedule-jobs/j-1/cancel", operatorRoutePolicy{operatorauth.Admin, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleOperatorAdmin}},
{"GET /v1/operator/script-catalog", "/v1/operator/script-catalog", operatorRoutePolicy{operatorauth.View, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceIneligible}},
{"POST /v1/operator/script-runs/preview", "/v1/operator/script-runs/preview", operatorRoutePolicy{operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleReadScriptsOnly}},
{"POST /v1/operator/script-runs", "/v1/operator/script-runs", operatorRoutePolicy{operatorauth.Operate, operatorJSON, operator.SourceKindOperatorAPI, operatorSecurityLocked, serviceEligibleReadScriptsOnly}},
Expand Down
41 changes: 41 additions & 0 deletions cmd/clawctl-hub/operator_schedule_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,47 @@ func (h *hub) handleScheduleRunNow(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, result)
}

// handleScheduleJobCancel cancels one scheduled job that is still not_started
// and was never claimed, so a stale job on an offline machine can be cleared
// without a raw database edit. Admin only; reason and Idempotency-Key required.
func (h *hub) handleScheduleJobCancel(w http.ResponseWriter, r *http.Request) {
if !h.admitScheduleMutation(w, r) {
return
}
actor := operatorActor(r)
action := store.AuditScheduleJobCancel
if !h.requireAuditedJSON(w, r, actor, action) {
return
}
p, ok := operatorauth.PrincipalFromContext(r.Context())
if !ok || !p.Has(operatorauth.Admin) {
h.rejectOperatorPolicyTransport(w, r, actor, action, 403, "SCRIPT_SCOPE_REQUIRED", "cancelling a scheduled job requires admin")
return
}
if !h.requireDiskJSONFreshHuman(w, r, action) {
return
}
actor.AuthCapability = p.GrantedCapabilities.Admin
if p.ServiceScope == operatorauth.ServiceScopeOperatorAdmin {
actor.AuthCapability = operatorauth.ServiceScopeOperatorAdmin
}
var body store.ScheduleJobCancelRequest
if rejection := decodeOperatorBody(w, r, &body); rejection != nil {
h.rejectOperatorPolicyTransport(w, r, actor, action, rejection.Status, rejection.Code, rejection.Detail)
return
}
result, e := operator.New(h.store).ScheduleJobCancel(r.PathValue("id"), r.Header.Get("Idempotency-Key"), body, actor)
if e != nil {
writeSettingErr(w, "schedule job cancel", e)
return
}
w.Header().Set("Cache-Control", "no-store")
if result.Replayed {
w.Header().Set("Idempotency-Replayed", "true")
}
writeJSON(w, 200, result)
}

// The registry and stored schedule select the boundary scope, never a caller mode.
func scheduleRequestPermission(r *http.Request, st *store.Store) operatorauth.Permission {
var id, script string
Expand Down
17 changes: 14 additions & 3 deletions docs/API-SURFACE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
> 這是控制面 parity 的帳本,不是行銷功能表。只要正常操作仍只有 CLI,
> 就留在「缺口」欄;高風險不是把入口藏起來,而是補 RBAC、preview、確認、
> 冪等與 audit。共同成功語意見 [CONTROL-PLANE-CONTRACT.md](CONTROL-PLANE-CONTRACT.md)。
> 逐一對照 364 個 HTTP operations(39 non-operator+325 operator;159 條 operator JSON)、
> 逐一對照 365 個 HTTP operations(39 non-operator+326 operator;160 條 operator JSON)、
> native Store、UI/CLI 與 Intune-style submenu 的完整清單,
> 見 [FEATURE-INVENTORY.md](FEATURE-INVENTORY.md)。

Expand All @@ -12,7 +12,7 @@ is off by default and uses its own signature/secret boundary. See [CHANNELS.md](

## Public local-account routes (Autopilot)

These six operations are included in the 364-operation tally above (39 non-operator + 325 operator). The inventory/contract excludes these six public account operations and totals 358. Account routes return 404 in `tailscale` mode. In `local` / `both`, they pin Host to `CLAWCTL_PUBLIC_URL`, use locked security headers and Go CrossOriginProtection, and do not require an existing operator session.
These six operations are included in the 365-operation tally above (39 non-operator + 326 operator). The inventory/contract excludes these six public account operations and totals 359. Account routes return 404 in `tailscale` mode. In `local` / `both`, they pin Host to `CLAWCTL_PUBLIC_URL`, use locked security headers and Go CrossOriginProtection, and do not require an existing operator session.

| Method | Route | Authority / behavior |
|---|---|---|
Expand Down Expand Up @@ -599,6 +599,16 @@ Both require view.
`targets`, `cron`, and `enabled`. `catch_up` must be false.
Preview and apply return a `ScheduleResult` object with `schedule`,
`preview_digest`, and `replayed`.
`POST /v1/operator/schedule-jobs/{id}/cancel` requires admin (or an allowlisted
`operator-admin` service token), `Idempotency-Key`, and `{"reason":"..."}`. It
cancels one scheduled job only while it is `not_started` and has never been
claimed (no lease); the job becomes `rejected` with an audit row
(`schedule-job-cancel`). It returns `{"job_id","schedule_id","machine_id","state","replayed"}`;
an unknown job is `404 SCHEDULE_JOB_NOT_FOUND`, a claimed or finished job is
`412 PRECONDITION_FAILED`. The scheduler also stops counting such a job as an
overlap once it is older than one cron interval of its schedule (plus a
two-minute grace); that target is left out of the run, recorded in audit, and
the other targets still run.
`POST /v1/operator/schedules/{id}/run-now` requires `Idempotency-Key`, accepts `{}`,
and returns `{"job_ids":[...],"replayed":false}` (an empty array when no jobs are enqueued). Replays return the saved job IDs,
`replayed:true`, and `Idempotency-Replayed: true`, without enqueueing. Manual
Expand Down Expand Up @@ -828,7 +838,8 @@ that service identity. Existing bounded rate-limit denial auditing still applies
Schedule list/get accepts explicitly allowlisted `view`, `operate`, and
`operator-admin` tokens. Schedule upsert and set-enabled preview/apply
(`POST /v1/operator/schedules/{operation}/{phase}`), and run-now
(`POST /v1/operator/schedules/{id}/run-now`) accept explicitly allowlisted
(`POST /v1/operator/schedules/{id}/run-now`), and scheduled-job cancel
(`POST /v1/operator/schedule-jobs/{id}/cancel`) accept explicitly allowlisted
`operator-admin` tokens. `view` and `operate` tokens cannot manage schedules.
Catalog approval, revision, preview digest, idempotency and rate-limit checks
still apply to delegated actions.
Expand Down
4 changes: 2 additions & 2 deletions docs/CONTROL-PLANE-CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ Web、operator JSON、strict Go client 與 HTTP CLI 共用同一套 preview/dige
在 mutation 前建立 private canonical recovery receipt,ambiguous response 以原 request 精確重放。

目前 production surface 固定為
358(33 non-operator+325 operator;159 operator JSON+166 HTML/BFF/CSV/download),exact auth tally 是
`view=128`/`operate=34`/`admin=163`。這是由 manifest 測試固定的 contract;實機紀錄是私人工作筆記,不在這個公開倉庫。
359(33 non-operator+326 operator;160 operator JSON+166 HTML/BFF/CSV/download),exact auth tally 是
`view=128`/`operate=34`/`admin=164`。這是由 manifest 測試固定的 contract;實機紀錄是私人工作筆記,不在這個公開倉庫。

管理中心導覽語言是 browser presentation preference,不是 fleet state。右上角是同源 form
`POST /preferences/navigation-language`,只接受 `zh-Hant`/`en`,以 HttpOnly、SameSite=Lax、
Expand Down
13 changes: 7 additions & 6 deletions docs/FEATURE-INVENTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ manifest 位於 `cmd/clawctl-hub/operator_boundary.go`,Web routes 位於

Hub schema 已平台中立:`internal/store/schema.sql` 的 `machine_registry.os`/`arch` 是 TEXT、無 CHECK;`internal/catalog/catalog.go` 的 `Platform{OS,Arch}` 無 enum,同一個檔的 `validPlatform` 只做識別字規則,`"darwin"` 與 `"windows"` 字串都過得了。過得了字串 ≠ 有 adapter 或 installer。

## 358 個 production operations 基線
## 359 個 production operations 基線

此數字按 HTTP method + route 計數:33 個 non-operator operations、325 個 operator
operations;其中 159 個為 operator JSON、166 個為 HTML/BFF/CSV/download。Operator 另按 exact
capability 分成 `view=128`、`operate=34`、`admin=163`。這組數字由 route/API/service/Web/CLI
合約測試固定;358-operation surface 包含受 operator view 保護的 Agent bootstrap download、Tailnet Settings、Maintenance、Ticket usage、資料揭露面、註冊報告、每日早報預覽、註冊上限、軟體清查、每機安裝狀態、發佈與指派對照、Hub 名冊重新命名及名冊備註、管理中心導覽語言偏好,
此數字按 HTTP method + route 計數:33 個 non-operator operations、326 個 operator
operations;其中 160 個為 operator JSON、166 個為 HTML/BFF/CSV/download。Operator 另按 exact
capability 分成 `view=128`、`operate=34`、`admin=164`。這組數字由 route/API/service/Web/CLI
合約測試固定;359-operation surface 包含受 operator view 保護的 Agent bootstrap download、Tailnet Settings、Maintenance、Ticket usage、資料揭露面、註冊報告、每日早報預覽、註冊上限、軟體清查、每機安裝狀態、發佈與指派對照、Hub 名冊重新命名及名冊備註、管理中心導覽語言偏好,
以及只開兩條 route 的 verifier transport;
這是 repo 的 route manifest 基線,live binary 的實機紀錄是私人工作筆記,不在這個公開倉庫,兩者在部署前本來就可能不同。
新增、移除或改 method 時,須同步更新本節與 route manifest 測試。
Expand Down Expand Up @@ -104,7 +104,7 @@ resolver 在 `cmd/clawctl-hub/verifier_api.go`。
| 32 | `GET /api/v1/errors` | `cmd/clawctl-hub/errors_api.go` | No | Scoped errors:read token; filtered error records; opt-in ingest |
| 33 | `GET /api/v1/errors/top` | `cmd/clawctl-hub/errors_api.go` | No | Scoped errors:read token; error aggregates; opt-in ingest |

### Operator HTML/BFF 與 JSON(325)
### Operator HTML/BFF 與 JSON(326)

| # | Method / route | 類型 | 人工 action | 備註 |
|---:|---|---|---|---|
Expand Down Expand Up @@ -458,6 +458,7 @@ domain service 見 `internal/operator/`。
| 356 | `POST /tenant/offline-alerts/apply` | HTML form | Yes | `admin`; Canonical confirmed apply |
| 357 | `POST /machines/{id}/execution-policy/preview` | HTML form | Yes, preview | Human admin; audited profile preview |
| 358 | `POST /machines/{id}/execution-policy/apply` | HTML form | Yes | Human admin + fresh MFA; exact preview, revision, reason, confirmation and request key |
| 359 | `POST /v1/operator/schedule-jobs/{id}/cancel` | JSON | Yes | `admin` (or operator-admin service token); cancels one scheduled job that is still `not_started` and never claimed; required reason and Idempotency-Key, receipt replay and audit |

## Workload / submenu parity matrix

Expand Down
Loading
Loading