forked from longtao.wu/droboticflow
157 lines
8.3 KiB
Go
157 lines
8.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestAnnotationTenantWorkspaceVersion(t *testing.T) {
|
|
var calls atomic.Int32
|
|
endpoint := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
calls.Add(1)
|
|
if r.Method != http.MethodHead {
|
|
t.Error("expected HEAD")
|
|
}
|
|
w.Header().Set("ETag", "test-version")
|
|
w.Header().Set("Content-Length", "4")
|
|
}))
|
|
defer endpoint.Close()
|
|
db := store.NewMemoryStore()
|
|
ctx := context.Background()
|
|
for _, row := range []struct{ id, org string }{{"production", "customer"}, {"test-storage", "test-org"}} {
|
|
if err := db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: row.id, OrganizationID: row.org, Provider: domain.StorageProviderBOS, Endpoint: endpoint.URL, AccessKeyID: "test", SecretAccessKey: "test"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
t.Setenv("ANNOTATION_WORKSPACE_CONNECTION_ID", "production")
|
|
t.Setenv("ANNOTATION_WORKSPACE_BUCKET", "bucket")
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", `{"test-org":{"connectionId":"test-storage","bucket":"bucket","readPrefixes":["internal-test/samples"],"writePrefixes":["system/runs/test-run"]}}`)
|
|
s := &server{store: db, workspaceMountPath: t.TempDir()}
|
|
if _, err := s.annotationFileVersion(ctx, "test-org", filepath.Join(s.workspaceMountPath, "internal-test/samples/left.csv")); err != nil {
|
|
t.Fatalf("test organization cannot read its copied source: %v", err)
|
|
}
|
|
if _, err := s.annotationFileVersion(ctx, "customer", filepath.Join(s.workspaceMountPath, "customer/source.csv")); err != nil {
|
|
t.Fatalf("legacy customer mapping changed: %v", err)
|
|
}
|
|
for _, key := range []string{"customer/source.csv", "internal-test/samples-other/source.csv", "system/runs/customer-run/left.csv"} {
|
|
if _, err := s.annotationFileVersion(ctx, "test-org", filepath.Join(s.workspaceMountPath, key)); err == nil {
|
|
t.Errorf("cross-prefix read allowed: %s", key)
|
|
}
|
|
}
|
|
if calls.Load() != 2 {
|
|
t.Fatalf("denied paths reached object store: %d calls", calls.Load())
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTenantWorkspaceWriteAndCleanupBoundaries(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := store.NewMemoryStore()
|
|
if err := db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "test-storage", OrganizationID: "test-org", Provider: domain.StorageProviderBOS}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", `{"test-org":{"connectionId":"test-storage","bucket":"bucket","readPrefixes":["internal-test/samples"],"writePrefixes":["system/runs/test-run"]}}`)
|
|
s := &server{store: db, workspaceMountPath: t.TempDir()}
|
|
for _, key := range []string{"internal-test/samples/left.mp4", "system/runs/other-run/file", "system/runs/test-run-other/file", "../outside"} {
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "test-org", filepath.Join(s.workspaceMountPath, key)); err == nil {
|
|
t.Errorf("write allowed outside output scope: %s", key)
|
|
}
|
|
}
|
|
output := filepath.Join(s.workspaceMountPath, "system/runs/test-run/human-materializations/item/task/revision-000001/left.mp4")
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "test-org", output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", output, false); err != nil {
|
|
t.Fatal("output must also be readable", err)
|
|
}
|
|
if err := s.removeAnnotationGeneration(ctx, "test-org", filepath.Join(s.workspaceMountPath, "system/runs/customer-run/human-materializations/item/task/revision-000001")); err == nil {
|
|
t.Fatal("customer cleanup was allowed")
|
|
}
|
|
// Never turn a configured mapping into local filesystem access on errors.
|
|
for _, raw := range []string{`null`, `{"test-org":{"connectionId":"missing","bucket":"b","readPrefixes":["input"],"writePrefixes":["output"]}}`, `{"test-org":{"connectionId":"test-storage","bucket":"b","readPrefixes":["../escape"],"writePrefixes":["output"]}}`, `{"test-org":{"connectionId":"test-storage","bucket":"b","readPrefixes":["."],"writePrefixes":["output"]}}`, `{"test-org":{"unknown":true}}`} {
|
|
t.Setenv("ANNOTATION_WORKSPACE_CONNECTION_ID", "test-storage")
|
|
t.Setenv("ANNOTATION_WORKSPACE_BUCKET", "bucket")
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", raw)
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "test-org", output); err == nil {
|
|
t.Errorf("invalid mapping accepted: %s", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTenantWorkspaceRejectsForeignConnectionAndSymlink(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := store.NewMemoryStore()
|
|
db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "foreign", OrganizationID: "customer", Provider: domain.StorageProviderBOS})
|
|
s := &server{store: db, workspaceMountPath: t.TempDir()}
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", `{"test-org":{"connectionId":"foreign","bucket":"bucket","readPrefixes":["input"],"writePrefixes":["output"]}}`)
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", filepath.Join(s.workspaceMountPath, "input/file"), false); err == nil {
|
|
t.Fatal("cross-organization connection accepted")
|
|
}
|
|
db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "foreign", OrganizationID: "test-org", Provider: domain.StorageProviderBOS})
|
|
if err := os.Symlink(t.TempDir(), filepath.Join(s.workspaceMountPath, "output")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "test-org", filepath.Join(s.workspaceMountPath, "output/new/file")); err == nil {
|
|
t.Fatal("symlink parent accepted")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTenantWorkspaceDelegatedReadOnly(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := store.NewMemoryStore()
|
|
if err := db.UpsertStorageConnection(ctx, domain.StorageConnectionRecord{ID: "production", OrganizationID: "customer", Provider: domain.StorageProviderBOS}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{store: db, workspaceMountPath: t.TempDir()}
|
|
t.Setenv("ANNOTATION_WORKSPACE_CONNECTION_ID", "production")
|
|
t.Setenv("ANNOTATION_WORKSPACE_BUCKET", "bucket")
|
|
config := `{"test-org":{"connectionId":"production","readConnectionOrganizationId":"customer","bucket":"bucket","readPrefixes":["internal-test/samples"]}}`
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", config)
|
|
input := filepath.Join(s.workspaceMountPath, "internal-test/samples/left.csv")
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", input, false); err != nil {
|
|
t.Fatal("explicit delegated read failed", err)
|
|
}
|
|
for _, key := range []string{"internal-test/samples/left.csv", "system/runs/test-run/file", "customer/source.csv"} {
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "test-org", filepath.Join(s.workspaceMountPath, key)); err == nil {
|
|
t.Fatal("delegated write allowed", key)
|
|
}
|
|
}
|
|
for _, key := range []string{"customer/source.csv", "internal-test/samples-other/file", "../outside"} {
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", filepath.Join(s.workspaceMountPath, key), false); err == nil {
|
|
t.Fatal("delegated read escaped prefix", key)
|
|
}
|
|
}
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "other-org", input, false); err == nil {
|
|
t.Fatal("unconfigured organization borrowed connection")
|
|
}
|
|
if _, _, _, err := s.annotationWorkspaceObject(ctx, "customer", filepath.Join(s.workspaceMountPath, "customer/output")); err != nil {
|
|
t.Fatal("customer legacy access changed", err)
|
|
}
|
|
for _, raw := range []string{
|
|
`{"test-org":{"connectionId":"production","readConnectionOrganizationId":"wrong-owner","bucket":"bucket","readPrefixes":["internal-test/samples"]}}`,
|
|
`{"test-org":{"connectionId":"production","readConnectionOrganizationId":"customer","bucket":"bucket","readPrefixes":["internal-test/samples"],"writePrefixes":["output"]}}`,
|
|
} {
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", raw)
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", input, false); err == nil {
|
|
t.Fatal("unsafe delegation accepted")
|
|
}
|
|
}
|
|
t.Setenv("ANNOTATION_WORKSPACE_MAPPINGS", config)
|
|
if err := os.MkdirAll(filepath.Dir(input), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(t.TempDir(), filepath.Join(filepath.Dir(input), "redirect")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, _, err := s.annotationWorkspaceObjectAccess(ctx, "test-org", filepath.Join(filepath.Dir(input), "redirect/file"), false); err == nil {
|
|
t.Fatal("delegated symlink read allowed")
|
|
}
|
|
}
|