Move Notarius path and executable resolution into its adapter

This commit is contained in:
2026-12-02 21:37:50 +00:00
parent 106036cf5b
commit d2f8b21c97
10 changed files with 106 additions and 58 deletions

View File

@@ -0,0 +1,37 @@
package notarius
import (
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var unsafeLaneCharacters = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
// LaneRelativePath maps a native lane identifier to its bundle-relative JSON path.
func LaneRelativePath(laneID string) (string, error) {
name := unsafeLaneCharacters.ReplaceAllString(strings.TrimSpace(laneID), "_")
for strings.Contains(name, "..") {
name = strings.ReplaceAll(name, "..", "__")
}
name = strings.Trim(name, "._")
if name == "" {
return "", fmt.Errorf("notarius lane ID %q cannot produce a filename", laneID)
}
return "lanes/" + name + ".json", nil
}
// ResolveExecutable resolves an explicit binary or PATH entry to an absolute path.
func ResolveExecutable(value string) (string, error) {
resolved, err := exec.LookPath(strings.TrimSpace(value))
if err != nil {
return "", err
}
resolved, err = filepath.Abs(strings.TrimSpace(resolved))
if err != nil {
return "", err
}
return filepath.Clean(resolved), nil
}

View File

@@ -0,0 +1,38 @@
package notarius
import (
"os"
"path/filepath"
"testing"
)
func TestLaneRelativePath(t *testing.T) {
for _, test := range []struct{ id, want string }{
{"npc-registry", "lanes/npc-registry.json"},
{" a/b..c ", "lanes/a_b__c.json"},
{"._...", ""},
} {
got, err := LaneRelativePath(test.id)
if got != test.want || (err != nil) != (test.want == "") {
t.Errorf("LaneRelativePath(%q) = %q, %v", test.id, got, err)
}
}
}
func TestResolveExecutableUsesExplicitPathAndSearchPath(t *testing.T) {
dir := t.TempDir()
binary := filepath.Join(dir, "notarius-test")
if err := os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir)
for _, value := range []string{binary, "notarius-test"} {
got, err := ResolveExecutable(value)
if err != nil || got != binary {
t.Fatalf("resolve %q = %q, %v", value, got, err)
}
}
if _, err := ResolveExecutable("missing-notarius"); err == nil {
t.Fatal("missing binary was accepted")
}
}

View File

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
@@ -91,7 +90,7 @@ func (extractStage) Run(ctx context.Context, env *Env, m *manifest.Manifest) (re
if err != nil || timeout <= 0 {
return nil, fmt.Errorf("extract: invalid notarius timeout %q", notariusConfig.Timeout)
}
resolvedBinary, err := resolveExecutable(notariusConfig.Binary)
resolvedBinary, err := notarius.ResolveExecutable(notariusConfig.Binary)
if err != nil {
return nil, fmt.Errorf("extract: resolve notarius binary: %w", err)
}
@@ -310,14 +309,6 @@ func extractionFingerprint(
return hex.EncodeToString(digest[:]), nil
}
func resolveExecutable(value string) (string, error) {
resolved, err := exec.LookPath(strings.TrimSpace(value))
if err != nil {
return "", err
}
return absolutePath(resolved)
}
func absolutePath(value string) (string, error) {
trimmed := strings.TrimSpace(value)
if trimmed == "" {

View File

@@ -291,20 +291,6 @@ func TestNativeExtractionReuseIdentityIgnoresCacheAndDebug(t *testing.T) {
}
}
func TestNativeExtractionRejectsMappedLaneCollisions(t *testing.T) {
staging, bundle := t.TempDir(), t.TempDir()
if err := os.Mkdir(filepath.Join(bundle, "lanes"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(bundle, "lanes", "a_b.json"), []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
required := map[string]config.NotariusOutputConfig{"first": {LaneID: "a/b"}, "second": {LaneID: "a?b"}}
if _, err := stageRequiredNotariusLanes(bundle, staging, required); err == nil || !strings.Contains(err.Error(), "same file") {
t.Fatalf("collision error = %v", err)
}
}
func TestNativeExtractionReportsPartialPromotionFailure(t *testing.T) {
env, m, fake := nativeExtractFixture(t)
env.Config.Pipeline.Notarius.Outputs = map[string]config.NotariusOutputConfig{"alpha": {LaneID: "alpha"}, "zeta": {LaneID: "zeta"}}

View File

@@ -63,7 +63,7 @@ func BuildNotariusReadiness(cfg *config.Config, paths artifacts.SessionPaths, m
if err != nil {
return NotariusReadiness{}, err
}
binary, err := resolveExecutable(n.Binary)
binary, err := notarius.ResolveExecutable(n.Binary)
if err != nil {
return NotariusReadiness{}, fmt.Errorf("resolve Notarius binary: %w", err)
}

View File

@@ -7,31 +7,16 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"gitea.maximumdirect.net/eric/narratio/internal/adapters/notarius"
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
)
var notariusUnsafeLaneCharacters = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
// notariusLanePath follows the documented Notarius lane filename mapping.
func notariusLanePath(laneID string) (string, error) {
name := notariusUnsafeLaneCharacters.ReplaceAllString(strings.TrimSpace(laneID), "_")
for strings.Contains(name, "..") {
name = strings.ReplaceAll(name, "..", "__")
}
name = strings.Trim(name, "._")
if name == "" {
return "", fmt.Errorf("notarius lane ID %q cannot produce a filename", laneID)
}
return "lanes/" + name + ".json", nil
}
type stagedNotariusLane struct {
Key, LaneID, Filename, StagedPath, Checksum string
Size int64
@@ -67,7 +52,7 @@ func stageRequiredNotariusLanes(bundle, staging string, required map[string]conf
laneOwners, filenameOwners := map[string]string{}, map[string]string{}
for _, key := range keys {
output := required[key]
relative, err := notariusLanePath(output.LaneID)
relative, err := notarius.LaneRelativePath(output.LaneID)
if err != nil {
return nil, err
}

View File

@@ -1,16 +1,23 @@
package stage
import "testing"
import (
"gitea.maximumdirect.net/eric/narratio/internal/config"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNotariusLanePath(t *testing.T) {
for _, test := range []struct{ id, want string }{
{"npc-registry", "lanes/npc-registry.json"},
{" a/b..c ", "lanes/a_b__c.json"},
{"._...", ""},
} {
got, err := notariusLanePath(test.id)
if got != test.want || (err != nil) != (test.want == "") {
t.Errorf("notariusLanePath(%q) = %q, %v", test.id, got, err)
}
func TestNativeExtractionRejectsMappedLaneCollisions(t *testing.T) {
staging, bundle := t.TempDir(), t.TempDir()
if err := os.Mkdir(filepath.Join(bundle, "lanes"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(bundle, "lanes", "a_b.json"), []byte(`{}`), 0o644); err != nil {
t.Fatal(err)
}
required := map[string]config.NotariusOutputConfig{"first": {LaneID: "a/b"}, "second": {LaneID: "a?b"}}
if _, err := stageRequiredNotariusLanes(bundle, staging, required); err == nil || !strings.Contains(err.Error(), "same file") {
t.Fatalf("collision error = %v", err)
}
}