chore: include complete dc refactor implementation and audits
Release / Build & Release (push) Canceled after 0s

This commit is contained in:
kory
2026-10-02 13:23:30 +08:00
parent 192969d7f6
commit 38382d2486
22 changed files with 3022 additions and 8 deletions
+48 -2
View File
@@ -1,7 +1,10 @@
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
@@ -59,8 +62,8 @@ func TestSecurityEffectiveModeAndExecGate(t *testing.T) {
}
defer store.DB.Close()
s := &Server{Exec: executor.Local{MaxOutput: 1024 * 1024}, Store: store, LocalName: "local"}
if got := s.GetEffectiveSecurityMode("node-a"); got != "medium" {
t.Fatalf("default mode=%s", got)
if got := s.GetEffectiveSecurityMode("node-a"); got != "unknown" {
t.Fatalf("unknown mode=%s", got)
}
if err := store.SetNodeSecurityMode("node-a", "high"); err != nil {
t.Fatal(err)
@@ -88,3 +91,46 @@ func TestSecurityEffectiveModeAndExecGate(t *testing.T) {
t.Fatalf("missing denial audit: %+v", logs)
}
}
func TestEditBlockSecurityAndReplacement(t *testing.T) {
dir := t.TempDir()
store, err := storage.Open(filepath.Join(dir, "db.sqlite"))
if err != nil {
t.Fatal(err)
}
defer store.DB.Close()
if err := store.SetGlobalSecurityMode("medium"); err != nil {
t.Fatal(err)
}
s := &Server{Exec: executor.Local{MaxOutput: 1024 * 1024}, Store: store, LocalName: "local"}
if err := s.LoadSecurityPolicySnapshot(); err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "config.txt")
if err := os.WriteFile(path, []byte("alpha\nTARGET\nomega\n"), 0644); err != nil {
t.Fatal(err)
}
r := httptest.NewRequest(http.MethodPost, "/api/v1/devices/file/edit_block", strings.NewReader(fmt.Sprintf(`{"path":%q,"old_text":"TARGET","new_text":"CHANGED"}`, path)))
w := httptest.NewRecorder()
s.editBlock(w, r)
if w.Code != 200 {
t.Fatalf("edit status=%d body=%s", w.Code, w.Body.String())
}
b, _ := os.ReadFile(path)
if string(b) != "alpha\nCHANGED\nomega\n" {
t.Fatalf("unexpected content: %q", b)
}
if err := store.SetGlobalSecurityMode("low"); err != nil {
t.Fatal(err)
}
if err := s.LoadSecurityPolicySnapshot(); err != nil {
t.Fatal(err)
}
r = httptest.NewRequest(http.MethodPost, "/api/v1/devices/file/edit_block", strings.NewReader(fmt.Sprintf(`{"path":%q,"old_text":"CHANGED","new_text":"NO"}`, path)))
w = httptest.NewRecorder()
s.editBlock(w, r)
if w.Code != 403 {
t.Fatalf("low mode status=%d body=%s", w.Code, w.Body.String())
}
}
+54
View File
@@ -0,0 +1,54 @@
package api
import (
"context"
"path/filepath"
"testing"
"github.com/wjyhk/vps-commander/internal/executor"
"github.com/wjyhk/vps-commander/internal/storage"
)
func TestMCPBackendHonorsSecuritySnapshot(t *testing.T) {
store, err := storage.Open(filepath.Join(t.TempDir(), "commander.db"))
if err != nil {
t.Fatal(err)
}
defer store.DB.Close()
if err := store.SetGlobalSecurityMode("low"); err != nil {
t.Fatal(err)
}
s := &Server{Store: store, LocalName: "local", Exec: executor.Local{MaxOutput: 1024 * 1024}}
if err := s.LoadSecurityPolicySnapshot(); err != nil {
t.Fatal(err)
}
b := s.NewMCPBackend()
if _, err := b.ExecCommand(context.Background(), "test", "local", "true", "", 5); err == nil {
t.Fatal("MCP exec must be denied in low mode")
}
if _, err := b.ReadFile(context.Background(), "test", "local", "/etc/hosts", 0, 128); err != nil {
t.Fatalf("MCP read should remain available in low mode: %v", err)
}
}
func TestUnknownDeviceFailsClosed(t *testing.T) {
store, err := storage.Open(filepath.Join(t.TempDir(), "commander.db"))
if err != nil {
t.Fatal(err)
}
defer store.DB.Close()
if err := store.SetGlobalSecurityMode("high"); err != nil {
t.Fatal(err)
}
s := &Server{Store: store, LocalName: "local", Exec: executor.Local{MaxOutput: 1024 * 1024}}
if err := s.LoadSecurityPolicySnapshot(); err != nil {
t.Fatal(err)
}
if got := s.GetEffectiveSecurityMode("not-registered"); got != "unknown" {
t.Fatalf("unknown device mode=%q", got)
}
b := s.NewMCPBackend()
if _, err := b.ExecCommand(context.Background(), "test", "not-registered", "true", "", 5); err == nil {
t.Fatal("unknown device must not inherit high risk")
}
}
+130
View File
@@ -616,6 +616,136 @@
}
}
}
},
"/api/v1/devices/file/edit_block": {
"post": {
"summary": "Atomically replace a unique text block on a device",
"security": [
{
"bearerAuth": []
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"path",
"old_text",
"new_text"
],
"properties": {
"device": {
"type": "string"
},
"path": {
"type": "string"
},
"old_text": {
"type": "string"
},
"new_text": {
"type": "string"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Edited"
},
"403": {
"description": "Security policy denied"
},
"409": {
"description": "Device offline or edit conflict"
},
"413": {
"description": "Edit block too large"
}
}
}
},
"/api/v1/process/session": {
"post": {
"summary": "Manage long-running process sessions",
"security": [
{
"bearerAuth": []
}
],
"parameters": [
{
"name": "action",
"in": "query",
"required": true,
"schema": {
"type": "string",
"enum": [
"start",
"read",
"input",
"stop",
"list"
]
}
}
],
"responses": {
"200": {
"description": "Session operation result"
}
}
}
},
"/api/v1/file/list": {
"post": {
"summary": "List directory",
"security": [
{
"bearerAuth": []
}
],
"responses": {
"200": {
"description": "Successful result"
}
}
}
},
"/api/v1/file/info": {
"post": {
"summary": "Get file metadata",
"security": [
{
"bearerAuth": []
}
],
"responses": {
"200": {
"description": "Successful result"
}
}
}
},
"/api/v1/file/search": {
"post": {
"summary": "Search files",
"security": [
{
"bearerAuth": []
}
],
"responses": {
"200": {
"description": "Successful result"
}
}
}
}
}
}