2026-08-09 23:47:38 +00:00
|
|
|
package notarius
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess"
|
2026-08-10 18:30:28 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
2026-08-29 15:16:06 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/notariusref"
|
2026-08-09 23:47:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2026-08-29 14:41:22 +00:00
|
|
|
maxReceiptBytes = 1 << 20
|
|
|
|
|
maxIndexBytes = 4 << 20
|
|
|
|
|
maxSummaryBytes = 4 << 20
|
|
|
|
|
canonicalIndexFile = "index.json"
|
|
|
|
|
canonicalManifestFile = "manifest.json"
|
|
|
|
|
canonicalRejectedFile = "rejected.json"
|
|
|
|
|
canonicalWarningsFile = "warnings.json"
|
|
|
|
|
canonicalDiagnosticsFile = "diagnostics.json"
|
|
|
|
|
warningsSchemaVersion = "notarius.warnings.v2"
|
|
|
|
|
diagnosticsSchemaVersion = "notarius.diagnostics.v1"
|
|
|
|
|
maxWarningGroups = 128
|
|
|
|
|
maxDiagnosticGroups = 256
|
|
|
|
|
maxFindingSamples = 3
|
2026-08-09 23:47:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type subprocessRun func(context.Context, subprocess.RunRequest) (subprocess.RunResult, error)
|
|
|
|
|
|
|
|
|
|
// SubprocessRunner invokes Notarius through its public CLI.
|
|
|
|
|
type SubprocessRunner struct {
|
|
|
|
|
run subprocessRun
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSubprocessRunner constructs a production Notarius subprocess runner.
|
|
|
|
|
func NewSubprocessRunner() *SubprocessRunner {
|
|
|
|
|
return &SubprocessRunner{run: subprocess.Run}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run executes a complete Notarius pipeline and discovers its published bundle.
|
|
|
|
|
func (r *SubprocessRunner) Run(ctx context.Context, req RunRequest) (RunResult, error) {
|
|
|
|
|
if r == nil || r.run == nil {
|
|
|
|
|
return RunResult{}, fmt.Errorf("notarius subprocess runner is nil")
|
|
|
|
|
}
|
2026-08-29 15:16:06 +00:00
|
|
|
references, err := validateRunRequest(req)
|
|
|
|
|
if err != nil {
|
2026-08-09 23:47:38 +00:00
|
|
|
return RunResult{}, err
|
|
|
|
|
}
|
2026-09-25 15:33:46 +00:00
|
|
|
envOverrides, sensitiveEnvNames, err := subprocess.CredentialEnvironment(req.Credentials)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return RunResult{}, fmt.Errorf("notarius credentials: %w", err)
|
2026-09-16 14:27:06 +00:00
|
|
|
}
|
2026-08-09 23:47:38 +00:00
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
|
"run", req.PipelineID,
|
|
|
|
|
"--config", req.ConfigPath,
|
2026-11-30 00:48:14 +00:00
|
|
|
"--input-file", req.InputPath,
|
2026-08-09 23:47:38 +00:00
|
|
|
"--output-dir", req.OutputRoot,
|
|
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if req.Cache != nil {
|
|
|
|
|
args = append(args, "--cache", *req.Cache)
|
2026-09-16 17:44:28 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if req.Debug {
|
|
|
|
|
args = append(args, "--debug")
|
2026-09-16 17:44:28 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
args = append(args, "--debug-dir", req.DebugDirectory)
|
2026-08-29 15:16:06 +00:00
|
|
|
for _, reference := range references {
|
|
|
|
|
args = append(args, "--reference", reference.Selector+"="+reference.Path)
|
|
|
|
|
}
|
2026-09-28 03:09:55 +00:00
|
|
|
if req.ConfigOverlay != nil {
|
2026-11-30 00:48:14 +00:00
|
|
|
args = append(args, "--overlay", *req.ConfigOverlay)
|
2026-09-21 15:03:29 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
args = append(args, "--format", "json")
|
2026-08-09 23:47:38 +00:00
|
|
|
processResult, err := r.run(ctx, subprocess.RunRequest{
|
2026-09-16 14:27:06 +00:00
|
|
|
Executable: req.Binary,
|
|
|
|
|
Args: args,
|
|
|
|
|
WorkingDir: req.WorkingDirectory,
|
|
|
|
|
Timeout: req.Timeout,
|
|
|
|
|
DiagnosticOwner: "notarius",
|
|
|
|
|
StdoutLogPath: req.ReceiptPath,
|
|
|
|
|
StderrLogPath: req.LogPath,
|
|
|
|
|
EnvOverrides: envOverrides,
|
|
|
|
|
SensitiveEnvNames: sensitiveEnvNames,
|
2026-08-09 23:47:38 +00:00
|
|
|
})
|
|
|
|
|
baseResult := RunResult{
|
|
|
|
|
ReceiptPath: req.ReceiptPath,
|
|
|
|
|
LogPath: req.LogPath,
|
|
|
|
|
ExitCode: processResult.ExitCode,
|
|
|
|
|
Duration: processResult.Duration,
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return baseResult, fmt.Errorf("run notarius pipeline %q: %w", req.PipelineID, err)
|
|
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if processResult.ExitCode != 0 {
|
|
|
|
|
return baseResult, fmt.Errorf("run notarius pipeline %q: exit code %d", req.PipelineID, processResult.ExitCode)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
report, err := loadCompletionReport(req.ReceiptPath, req.PipelineID)
|
2026-08-09 23:47:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return baseResult, err
|
|
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
bundleRoot, err := validateBundleRoot(req.OutputRoot, report.OutputDirectory)
|
2026-08-09 23:47:38 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return baseResult, err
|
|
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
baseResult.Report = report
|
|
|
|
|
baseResult.BundleRoot = bundleRoot
|
|
|
|
|
return baseResult, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadCompletionReport(path, pipelineID string) (CompletionReport, error) {
|
|
|
|
|
var document struct {
|
|
|
|
|
Pipeline string `json:"pipeline"`
|
|
|
|
|
ArtifactCount *int `json:"artifact_count"`
|
|
|
|
|
OutputDirectory string `json:"output_directory"`
|
|
|
|
|
DebugDirectory string `json:"debug_directory"`
|
|
|
|
|
FallbackCount *int `json:"fallback_count"`
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if err := decodeBoundedJSON(path, maxReceiptBytes, &document); err != nil {
|
|
|
|
|
return CompletionReport{}, fmt.Errorf("decode notarius completion report: %w", err)
|
2026-08-29 14:41:22 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if document.Pipeline != pipelineID || document.ArtifactCount == nil || *document.ArtifactCount < 0 || !filepath.IsAbs(document.OutputDirectory) {
|
|
|
|
|
return CompletionReport{}, fmt.Errorf("notarius completion report is missing required fields or has an incompatible pipeline")
|
2026-08-29 14:41:22 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if document.DebugDirectory != "" && !filepath.IsAbs(document.DebugDirectory) {
|
|
|
|
|
return CompletionReport{}, fmt.Errorf("notarius debug directory must be absolute")
|
2026-08-29 14:41:22 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
fallback := 0
|
|
|
|
|
if document.FallbackCount != nil {
|
|
|
|
|
if *document.FallbackCount <= 0 {
|
|
|
|
|
return CompletionReport{}, fmt.Errorf("notarius fallback count must be positive when provided")
|
|
|
|
|
}
|
|
|
|
|
fallback = *document.FallbackCount
|
2026-08-29 14:41:22 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
return CompletionReport{Pipeline: document.Pipeline, ArtifactCount: *document.ArtifactCount, OutputDirectory: filepath.Clean(document.OutputDirectory), DebugDirectory: document.DebugDirectory, FallbackCount: fallback}, nil
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-29 15:16:06 +00:00
|
|
|
func validateRunRequest(req RunRequest) ([]ReferenceBinding, error) {
|
2026-08-09 23:47:38 +00:00
|
|
|
if strings.TrimSpace(req.Binary) == "" {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius binary is required")
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if strings.TrimSpace(req.PipelineID) == "" {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius pipeline id is required")
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if req.Timeout <= 0 {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius timeout must be positive")
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
2026-11-30 00:48:14 +00:00
|
|
|
if req.Cache != nil && (*req.Cache == "" || !validChunkCacheMode(*req.Cache)) {
|
|
|
|
|
return nil, fmt.Errorf("notarius cache mode must be auto, bypass, or refresh")
|
2026-09-16 17:44:28 +00:00
|
|
|
}
|
2026-08-09 23:47:38 +00:00
|
|
|
for label, path := range map[string]string{
|
|
|
|
|
"config": req.ConfigPath,
|
|
|
|
|
"input": req.InputPath,
|
|
|
|
|
"output root": req.OutputRoot,
|
|
|
|
|
"working directory": req.WorkingDirectory,
|
|
|
|
|
"receipt": req.ReceiptPath,
|
|
|
|
|
"log": req.LogPath,
|
2026-11-30 00:48:14 +00:00
|
|
|
"debug directory": req.DebugDirectory,
|
2026-08-09 23:47:38 +00:00
|
|
|
} {
|
|
|
|
|
if strings.TrimSpace(path) == "" {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius %s path is required", label)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if !filepath.IsAbs(path) {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius %s path must be absolute", label)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if filepath.Clean(req.ReceiptPath) == filepath.Clean(req.LogPath) {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("notarius receipt and log paths must be different")
|
|
|
|
|
}
|
|
|
|
|
references := make([]ReferenceBinding, 0, len(req.References))
|
|
|
|
|
selectors := make(map[string]struct{}, len(req.References))
|
|
|
|
|
for index, binding := range req.References {
|
|
|
|
|
selector, err := notariusref.NormalizeSelector(binding.Selector)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("notarius reference %d selector: %w", index, err)
|
|
|
|
|
}
|
|
|
|
|
if _, duplicate := selectors[selector]; duplicate {
|
|
|
|
|
return nil, fmt.Errorf("notarius reference selector %q is duplicated", selector)
|
|
|
|
|
}
|
|
|
|
|
selectors[selector] = struct{}{}
|
|
|
|
|
if strings.TrimSpace(binding.Path) == "" {
|
|
|
|
|
return nil, fmt.Errorf("notarius reference %q path is required", selector)
|
|
|
|
|
}
|
|
|
|
|
if !filepath.IsAbs(binding.Path) {
|
|
|
|
|
return nil, fmt.Errorf("notarius reference %q path must be absolute", selector)
|
|
|
|
|
}
|
|
|
|
|
references = append(references, ReferenceBinding{Selector: selector, Path: binding.Path})
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := requireRegularFile(req.ConfigPath); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius config path: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := requireRegularFile(req.InputPath); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius input path: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := requireDirectory(req.OutputRoot); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius output root: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := requireDirectory(req.WorkingDirectory); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius working directory: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := validateLogDestination(req.ReceiptPath); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius receipt path: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := validateLogDestination(req.LogPath); err != nil {
|
2026-08-29 15:16:06 +00:00
|
|
|
return nil, fmt.Errorf("validate notarius log path: %w", err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
2026-08-29 15:16:06 +00:00
|
|
|
return references, nil
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-16 17:44:28 +00:00
|
|
|
func validChunkCacheMode(mode string) bool {
|
|
|
|
|
switch mode {
|
|
|
|
|
case "", "auto", "bypass", "refresh":
|
|
|
|
|
return true
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-09 23:47:38 +00:00
|
|
|
func decodeBoundedJSON(path string, limit int64, destination any) error {
|
2026-09-01 17:51:37 +00:00
|
|
|
data, err := fileops.ReadFileBounded(path, limit)
|
2026-08-09 23:47:38 +00:00
|
|
|
if err != nil {
|
2026-08-10 18:30:28 +00:00
|
|
|
return fmt.Errorf("notarius JSON result exceeds or cannot be read within %d-byte limit: %w", limit, err)
|
2026-08-09 23:47:38 +00:00
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(data, destination); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateBundleRoot(outputRoot, bundleRoot string) (string, error) {
|
|
|
|
|
root := filepath.Clean(outputRoot)
|
|
|
|
|
bundle := filepath.Clean(bundleRoot)
|
|
|
|
|
relative, err := filepath.Rel(root, bundle)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("compare notarius output paths: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if relative == "." || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
|
|
|
|
return "", fmt.Errorf("notarius output directory %q is not beneath output root %q", bundleRoot, outputRoot)
|
|
|
|
|
}
|
|
|
|
|
if err := requireDirectoryTree(root, relative); err != nil {
|
|
|
|
|
return "", fmt.Errorf("validate notarius output directory: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return bundle, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveRegularFile(root, logicalPath string) (string, error) {
|
|
|
|
|
resolved, err := pathsafe.JoinSlashRelativeUnderRoot(root, logicalPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
relative, err := filepath.Rel(root, resolved)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
if err := requireRegularFileTree(root, relative); err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func requireDirectoryTree(root, relative string) error {
|
|
|
|
|
if err := requireDirectory(root); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current := root
|
|
|
|
|
for _, component := range strings.Split(relative, string(filepath.Separator)) {
|
|
|
|
|
current = filepath.Join(current, component)
|
|
|
|
|
if err := requireDirectory(current); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func requireRegularFileTree(root, relative string) error {
|
|
|
|
|
components := strings.Split(relative, string(filepath.Separator))
|
|
|
|
|
if len(components) == 0 {
|
|
|
|
|
return fmt.Errorf("regular file path is required")
|
|
|
|
|
}
|
|
|
|
|
if err := requireDirectory(root); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
current := root
|
|
|
|
|
for _, component := range components[:len(components)-1] {
|
|
|
|
|
current = filepath.Join(current, component)
|
|
|
|
|
if err := requireDirectory(current); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return requireRegularFile(filepath.Join(current, components[len(components)-1]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func requireDirectory(path string) error {
|
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
|
|
|
|
return fmt.Errorf("path %q must be a directory without symlinks", path)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func requireRegularFile(path string) error {
|
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
|
|
|
|
return fmt.Errorf("path %q must be a regular file without symlinks", path)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateLogDestination(path string) error {
|
|
|
|
|
if err := requireDirectory(filepath.Dir(path)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
info, err := os.Lstat(path)
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
|
|
|
|
|
return fmt.Errorf("path %q must be absent or a regular file without symlinks", path)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|