feat(resource): add stable resource registry with LLM-context alias compaction
Introduce resource://<handle> as the stable application identity for stored objects, decoupling API responses, persisted rich text, and LLM prompts from physical storage paths. Adds a resource catalog service (register/resolve/bind, revocable expiring access grants), a request-local alias registry that compacts resource references while in the LLM context and restores them (including across streaming chunks), plus router, IM, chat-pipeline, and multimodal integration. Includes schema migrations (resources, resource_bindings, resource_access_grants) for sqlite, and hardens URL/output security handling.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -22,6 +23,46 @@ type stubFileService struct {
|
||||
getFile func(ctx context.Context, filePath string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
type stubResourceCatalog struct {
|
||||
resource *types.StoredResource
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) Register(
|
||||
context.Context,
|
||||
uint64,
|
||||
string,
|
||||
interfaces.ResourceRegistration,
|
||||
) (string, error) {
|
||||
panic("unexpected Register")
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) Resolve(context.Context, string) (*types.StoredResource, error) {
|
||||
return s.resource, nil
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) ResolvePath(_ context.Context, value string) (string, *types.StoredResource, error) {
|
||||
if _, ok := types.ParseResourcePath(value); ok && s.resource != nil {
|
||||
return s.resource.PhysicalPath, s.resource, nil
|
||||
}
|
||||
return value, nil, nil
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) Bind(context.Context, string, string, string, string) error {
|
||||
panic("unexpected Bind")
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) MarkDeleted(context.Context, string) error {
|
||||
panic("unexpected MarkDeleted")
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) CreateAccessGrant(context.Context, string, time.Duration) (string, error) {
|
||||
panic("unexpected CreateAccessGrant")
|
||||
}
|
||||
|
||||
func (s *stubResourceCatalog) ResolveAccessGrant(context.Context, string) (*types.StoredResource, error) {
|
||||
return s.resource, nil
|
||||
}
|
||||
|
||||
func (s *stubFileService) CheckConnectivity(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
@@ -84,6 +125,87 @@ func TestServeFilesFallsBackToGlobalFileService(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeFilesResolvesShortResourceReference(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Setenv("STORAGE_TYPE", "local")
|
||||
const ref = "resource://AbCdEfGhIjKlMnOpQrStUv"
|
||||
const physical = "local://42/exports/a.png"
|
||||
|
||||
engine := gin.New()
|
||||
var requestedPath string
|
||||
serveFilesWithResources(engine, &stubFileService{getFile: func(_ context.Context, path string) (io.ReadCloser, error) {
|
||||
requestedPath = path
|
||||
return io.NopCloser(strings.NewReader("image")), nil
|
||||
}}, nil, &stubResourceCatalog{resource: &types.StoredResource{TenantID: 42, PhysicalPath: physical}})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape(ref), nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
||||
recorder := httptest.NewRecorder()
|
||||
engine.ServeHTTP(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if requestedPath != physical {
|
||||
t.Fatalf("requested path = %q, want %q", requestedPath, physical)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeFilesRejectsCrossTenantResourceReference(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
const ref = "resource://AbCdEfGhIjKlMnOpQrStUv"
|
||||
engine := gin.New()
|
||||
serveFilesWithResources(engine, &stubFileService{getFile: func(context.Context, string) (io.ReadCloser, error) {
|
||||
t.Fatal("GetFile should not be called")
|
||||
return nil, nil
|
||||
}}, nil, &stubResourceCatalog{resource: &types.StoredResource{TenantID: 7, PhysicalPath: "local://7/exports/a.png"}})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape(ref), nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
|
||||
recorder := httptest.NewRecorder()
|
||||
engine.ServeHTTP(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceGrantServesShortPublicURL(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
physical := "local://42/exports/a.png"
|
||||
engine := gin.New()
|
||||
serveResourceGrants(
|
||||
engine,
|
||||
&stubResourceCatalog{resource: &types.StoredResource{
|
||||
ID: "resource-1",
|
||||
TenantID: 42,
|
||||
PhysicalPath: physical,
|
||||
OriginalName: "a.png",
|
||||
MimeType: "image/png",
|
||||
}},
|
||||
&stubTenantService{get: func(_ context.Context, id uint64) (*types.Tenant, error) {
|
||||
return &types.Tenant{ID: id}, nil
|
||||
}},
|
||||
&stubFileService{getFile: func(_ context.Context, path string) (io.ReadCloser, error) {
|
||||
if path != physical {
|
||||
t.Fatalf("path = %q, want %q", path, physical)
|
||||
}
|
||||
return io.NopCloser(strings.NewReader("image")), nil
|
||||
}},
|
||||
nil,
|
||||
)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/r/GrantTokenAbCdEfGhIjKlM", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
engine.ServeHTTP(recorder, req)
|
||||
if recorder.Code != http.StatusOK || recorder.Body.String() != "image" {
|
||||
t.Fatalf("status=%d body=%q", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if got := recorder.Header().Get("X-Content-Type-Options"); got != "nosniff" {
|
||||
t.Fatalf("X-Content-Type-Options = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeFilesDoesNotFallbackWhenProviderDoesNotMatchGlobalStorage(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
t.Setenv("STORAGE_TYPE", "minio")
|
||||
|
||||
Reference in New Issue
Block a user