uploads: dual-write uploads to DB, startup backfill, and audit log
Store now indexes every Save into the upload_files table via SetRepo. DB failures are logged as warnings and do not fail the upload because .meta.json remains the source of truth and startup backfill recovers. Add Store.Backfill to walk Root at startup and insert index rows for any pre-existing .meta.json sidecars, swallowing duplicate-key races. The upload_file MCP Tool now writes an audit_log entry on success. Tests cover dual-write args, repo-error non-failure, and backfill skipping existing rows. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
|
||||
"spark-mcp-go/internal/audit"
|
||||
"spark-mcp-go/internal/cluster"
|
||||
"spark-mcp-go/internal/httpclient"
|
||||
"spark-mcp-go/internal/storage"
|
||||
@@ -24,7 +25,7 @@ import (
|
||||
|
||||
// testDepsWithDataDir returns dependencies backed by an in-memory DB and a
|
||||
// temporary data directory.
|
||||
func testDepsWithDataDir(t *testing.T) (*Deps, *storage.ClusterRepo) {
|
||||
func testDepsWithDataDir(t *testing.T) (*Deps, *storage.ClusterRepo, *audit.Repo) {
|
||||
t.Helper()
|
||||
db, err := storage.Open(":memory:")
|
||||
if err != nil {
|
||||
@@ -43,11 +44,12 @@ func testDepsWithDataDir(t *testing.T) (*Deps, *storage.ClusterRepo) {
|
||||
Timeout: 5 * time.Second,
|
||||
MaxResponseBytes: 1 << 20,
|
||||
}),
|
||||
AuditRepo: audit.NewRepo(db),
|
||||
ClusterRepo: db.Clusters(),
|
||||
MaxResponseBytes: 1 << 20,
|
||||
DataDir: dataDir,
|
||||
UploadStore: uploadStore,
|
||||
}, db.Clusters()
|
||||
}, db.Clusters(), audit.NewRepo(db)
|
||||
}
|
||||
|
||||
// createCluster creates a cluster in the repository with the given fields.
|
||||
@@ -98,7 +100,7 @@ func TestFetchURL(t *testing.T) {
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
deps, repo := testDepsWithDataDir(t)
|
||||
deps, repo, _ := testDepsWithDataDir(t)
|
||||
createCluster(t, repo, &cluster.Cluster{
|
||||
ID: "cluster-a",
|
||||
Name: "Cluster A",
|
||||
@@ -292,7 +294,7 @@ func TestFetchURL_RedirectPreservesAuth(t *testing.T) {
|
||||
}))
|
||||
defer server1.Close()
|
||||
|
||||
deps, repo := testDepsWithDataDir(t)
|
||||
deps, repo, _ := testDepsWithDataDir(t)
|
||||
createCluster(t, repo, &cluster.Cluster{
|
||||
ID: "cluster-redirect",
|
||||
Name: "Redirect",
|
||||
@@ -321,7 +323,7 @@ func TestFetchURL_RedirectPreservesAuth(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUploadFile(t *testing.T) {
|
||||
deps, _ := testDepsWithDataDir(t)
|
||||
deps, _, auditRepo := testDepsWithDataDir(t)
|
||||
|
||||
req := newToolRequest(UploadFileName, map[string]any{
|
||||
"filename": "hello.txt",
|
||||
@@ -367,10 +369,25 @@ func TestUploadFile(t *testing.T) {
|
||||
if info.Mode().Perm() != 0o640 {
|
||||
t.Errorf("mode=%o, want %o", info.Mode().Perm(), 0o640)
|
||||
}
|
||||
|
||||
entries, err := auditRepo.List(context.Background(), 100)
|
||||
if err != nil {
|
||||
t.Fatalf("list audit: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, e := range entries {
|
||||
if e.Action == audit.ActionUploadCreate && e.Actor == "tool:upload_file" && e.ClusterID == fileID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("missing upload.create audit entry for file_id %s", fileID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadFile_PathTraversal(t *testing.T) {
|
||||
deps, _ := testDepsWithDataDir(t)
|
||||
deps, _, _ := testDepsWithDataDir(t)
|
||||
|
||||
cases := []string{
|
||||
"../../../etc/passwd",
|
||||
@@ -401,7 +418,7 @@ func TestUploadFile_PathTraversal(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUploadFile_Base64(t *testing.T) {
|
||||
deps, _ := testDepsWithDataDir(t)
|
||||
deps, _, _ := testDepsWithDataDir(t)
|
||||
|
||||
req := newToolRequest(UploadFileName, map[string]any{
|
||||
"filename": "hello.bin",
|
||||
|
||||
Reference in New Issue
Block a user