forked from longtao.wu/droboticflow
108 lines
4.3 KiB
Go
108 lines
4.3 KiB
Go
package workspacescope
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Deployment-owned mappings may explicitly delegate prefix-scoped reads.
|
|
// Writes require a second explicit owner grant and disjoint output directories.
|
|
type Mapping struct {
|
|
ConnectionID string `json:"connectionId"`
|
|
ReadConnectionOrganizationID string `json:"readConnectionOrganizationId,omitempty"`
|
|
Bucket string `json:"bucket"`
|
|
ReadPrefixes []string `json:"readPrefixes"`
|
|
WriteConnectionOrganizationID string `json:"writeConnectionOrganizationId,omitempty"`
|
|
DeliveryPrefixes []string `json:"deliveryPrefixes,omitempty"`
|
|
WritePrefixes []string `json:"writePrefixes"`
|
|
}
|
|
|
|
func ForOrganization(org string) (*Mapping, error) {
|
|
raw := os.Getenv("ANNOTATION_WORKSPACE_MAPPINGS")
|
|
if strings.TrimSpace(raw) == "" {
|
|
return nil, nil
|
|
}
|
|
decoder := json.NewDecoder(strings.NewReader(raw))
|
|
decoder.DisallowUnknownFields()
|
|
var mappings map[string]Mapping
|
|
if err := decoder.Decode(&mappings); err != nil {
|
|
return nil, fmt.Errorf("invalid annotation workspace mappings")
|
|
}
|
|
if err := decoder.Decode(new(any)); err != io.EOF || mappings == nil {
|
|
return nil, fmt.Errorf("invalid annotation workspace mappings")
|
|
}
|
|
mapping, ok := mappings[org]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
if strings.TrimSpace(org) == "" || strings.TrimSpace(mapping.ConnectionID) == "" || strings.TrimSpace(mapping.Bucket) == "" || len(mapping.ReadPrefixes) == 0 {
|
|
return nil, fmt.Errorf("annotation organization workspace mapping incomplete")
|
|
}
|
|
if mapping.ReadConnectionOrganizationID != "" && (strings.TrimSpace(mapping.ReadConnectionOrganizationID) != mapping.ReadConnectionOrganizationID || mapping.ReadConnectionOrganizationID == org) {
|
|
return nil, fmt.Errorf("delegated workspace requires an explicit foreign owner")
|
|
}
|
|
if mapping.WriteConnectionOrganizationID != "" && (mapping.WriteConnectionOrganizationID != mapping.ReadConnectionOrganizationID || len(mapping.WritePrefixes) == 0) {
|
|
return nil, fmt.Errorf("write delegation must explicitly match read owner and output prefixes")
|
|
}
|
|
if mapping.ReadConnectionOrganizationID != "" && mapping.WriteConnectionOrganizationID == "" && (len(mapping.WritePrefixes) != 0 || len(mapping.DeliveryPrefixes) != 0) {
|
|
return nil, fmt.Errorf("read-only delegation cannot grant outputs")
|
|
}
|
|
if len(mapping.DeliveryPrefixes) != 0 && mapping.WriteConnectionOrganizationID == "" {
|
|
return nil, fmt.Errorf("delivery requires explicit write delegation")
|
|
}
|
|
if mapping.WriteConnectionOrganizationID != "" {
|
|
for _, input := range mapping.ReadPrefixes {
|
|
for _, output := range append(append([]string{}, mapping.WritePrefixes...), mapping.DeliveryPrefixes...) {
|
|
if PrefixAllows(input, []string{output}) || PrefixAllows(output, []string{input}) {
|
|
return nil, fmt.Errorf("delegated input and output directories must be disjoint")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for _, prefixes := range [][]string{mapping.ReadPrefixes, mapping.WritePrefixes, mapping.DeliveryPrefixes} {
|
|
for _, prefix := range prefixes {
|
|
if prefix == "" || prefix == "." || prefix == ".." || strings.HasPrefix(prefix, "../") || path.IsAbs(prefix) || path.Clean(prefix) != prefix || strings.ContainsAny(prefix, `\*?`) {
|
|
return nil, fmt.Errorf("invalid annotation workspace prefix")
|
|
}
|
|
}
|
|
}
|
|
return &mapping, nil
|
|
}
|
|
|
|
func PrefixAllows(key string, prefixes []string) bool {
|
|
if key == "" || key == "." || key == ".." || path.IsAbs(key) || path.Clean(key) != key || strings.HasPrefix(key, "../") || strings.ContainsAny(key, `\*?`) {
|
|
return false
|
|
}
|
|
for _, prefix := range prefixes {
|
|
if key == prefix || strings.HasPrefix(key, prefix+"/") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// A scoped path must not redirect through a symlink, including an existing
|
|
// parent of a not-yet-created output. The configured workspace root is trusted.
|
|
func ScopedPath(root, relative string) error {
|
|
current := root
|
|
for _, component := range strings.Split(relative, string(filepath.Separator)) {
|
|
current = filepath.Join(current, component)
|
|
info, err := os.Lstat(current)
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return fmt.Errorf("annotation scoped path contains symlink")
|
|
}
|
|
}
|
|
return nil
|
|
}
|