forked from longtao.wu/droboticflow
70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/workspacescope"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type annotationWorkspaceMapping = workspacescope.Mapping
|
|
|
|
var annotationMappingForOrganization = workspacescope.ForOrganization
|
|
var annotationPrefixAllows = workspacescope.PrefixAllows
|
|
var annotationScopedPath = workspacescope.ScopedPath
|
|
|
|
func (s *server) annotationWorkspaceObjectAccess(ctx context.Context, org, filename string, write bool) (*domain.StorageConnectionRecord, string, string, error) {
|
|
mapping, err := annotationMappingForOrganization(org)
|
|
if err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
id, bucket := os.Getenv("ANNOTATION_WORKSPACE_CONNECTION_ID"), os.Getenv("ANNOTATION_WORKSPACE_BUCKET")
|
|
if id == "" && bucket == "" {
|
|
id, bucket = os.Getenv("PROJECT_DELETION_WORKSPACE_CONNECTION_ID"), os.Getenv("PROJECT_DELETION_WORKSPACE_BUCKET")
|
|
}
|
|
if mapping != nil {
|
|
id, bucket = mapping.ConnectionID, mapping.Bucket
|
|
}
|
|
if id == "" && bucket == "" {
|
|
return nil, "", "", nil
|
|
}
|
|
if id == "" || bucket == "" {
|
|
return nil, "", "", fmt.Errorf("annotation workspace mapping incomplete")
|
|
}
|
|
c, err := s.store.GetStorageConnection(ctx, id)
|
|
if err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
owner := org
|
|
if mapping != nil && mapping.ReadConnectionOrganizationID != "" {
|
|
if write && mapping.WriteConnectionOrganizationID == "" {
|
|
return nil, "", "", fmt.Errorf("delegated annotation workspace is read-only")
|
|
}
|
|
owner = mapping.ReadConnectionOrganizationID
|
|
}
|
|
if c == nil || c.OrganizationID != owner || c.Provider != domain.StorageProviderBOS {
|
|
return nil, "", "", fmt.Errorf("annotation workspace scope mismatch")
|
|
}
|
|
relative, err := filepath.Rel(s.workspaceMountPath, filename)
|
|
if err != nil || relative == "." || strings.HasPrefix(relative, "..") {
|
|
return nil, "", "", fmt.Errorf("annotation path outside workspace")
|
|
}
|
|
key := filepath.ToSlash(relative)
|
|
if mapping != nil {
|
|
allowed := annotationPrefixAllows(key, mapping.WritePrefixes)
|
|
if !write {
|
|
allowed = allowed || annotationPrefixAllows(key, mapping.ReadPrefixes) || annotationPrefixAllows(key, mapping.DeliveryPrefixes)
|
|
}
|
|
if !allowed {
|
|
return nil, "", "", fmt.Errorf("annotation path outside organization workspace scope")
|
|
}
|
|
if err := annotationScopedPath(s.workspaceMountPath, relative); err != nil {
|
|
return nil, "", "", err
|
|
}
|
|
}
|
|
return c, bucket, key, nil
|
|
}
|