Centralize extraction bundle evidence
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifactmodel"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
@@ -22,11 +20,6 @@ const (
|
||||
extractionMetadataRoot = "bundle_root"
|
||||
)
|
||||
|
||||
type hydratedExtraction struct {
|
||||
sourceID string
|
||||
path string
|
||||
}
|
||||
|
||||
// HydrateExtractionArtifacts marks extraction sources available only when the current
|
||||
// manifest contains one complete, internally consistent, succeeded extraction bundle.
|
||||
// Invalid, stale, incomplete, or unsafe records leave every extraction source unavailable.
|
||||
@@ -38,71 +31,12 @@ func (c *ArtifactCatalog) HydrateExtractionArtifacts(
|
||||
if c == nil || m == nil || len(configured) == 0 {
|
||||
return
|
||||
}
|
||||
record := m.Stages[extractStageName]
|
||||
if record == nil || record.Name != extractStageName || record.Status != manifest.StatusSucceeded {
|
||||
proof := InspectExtractionEvidence(paths, m, configured)
|
||||
if proof.State != ExtractionEvidenceValid {
|
||||
return
|
||||
}
|
||||
producerRunID := extractionMetadataString(record.Metadata, extractionMetadataRun)
|
||||
if !safeExtractionPathSegment(producerRunID) {
|
||||
return
|
||||
}
|
||||
bundleRoot := filepath.Clean(filepath.Join(paths.ArtifactsDir, "notarius", producerRunID))
|
||||
if !filepath.IsAbs(bundleRoot) || extractionMetadataString(record.Metadata, extractionMetadataRoot) != bundleRoot {
|
||||
return
|
||||
}
|
||||
if !safeExistingExtractionDirectory(paths.Root, bundleRoot) {
|
||||
return
|
||||
}
|
||||
receiptRunID, receiptPipelineID := extractionReceiptIdentity(record.Metadata)
|
||||
if receiptRunID == "" || receiptPipelineID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
expected := make(map[string]ExtractionArtifactDefinition, len(configured))
|
||||
for key, definition := range configured {
|
||||
sourceID, ok := c.SourceIDForExtractionKey(key)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
expected[sourceID] = definition
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(expected))
|
||||
hydrated := make([]hydratedExtraction, 0, len(expected))
|
||||
indexSeen := false
|
||||
for _, output := range record.Outputs {
|
||||
if strings.TrimSpace(output.ProducerRunID) != producerRunID {
|
||||
return
|
||||
}
|
||||
if output.SourceID == "" {
|
||||
if indexSeen || output.Kind != extractionIndexKind || filepath.Clean(output.LocalPath) != filepath.Join(bundleRoot, "index.json") ||
|
||||
!validExtractionPayload(bundleRoot, output.LocalPath, output.Checksum) {
|
||||
return
|
||||
}
|
||||
indexSeen = true
|
||||
continue
|
||||
}
|
||||
|
||||
definition, ok := expected[output.SourceID]
|
||||
if !ok || output.Kind != extractionLaneKind {
|
||||
return
|
||||
}
|
||||
if _, duplicate := seen[output.SourceID]; duplicate {
|
||||
return
|
||||
}
|
||||
if !compatibleCatalogExtractionContract(output.Contract, definition) ||
|
||||
!compatibleCatalogExtractionProvenance(output.ExternalProvenance, receiptRunID, receiptPipelineID, definition) ||
|
||||
!validExtractionPayload(bundleRoot, output.LocalPath, output.Checksum) {
|
||||
return
|
||||
}
|
||||
seen[output.SourceID] = struct{}{}
|
||||
hydrated = append(hydrated, hydratedExtraction{sourceID: output.SourceID, path: output.LocalPath})
|
||||
}
|
||||
if !indexSeen || len(seen) != len(expected) || len(record.Outputs) != len(expected)+1 {
|
||||
return
|
||||
}
|
||||
for _, item := range hydrated {
|
||||
_ = c.markAvailableFromExtractManifest(item.sourceID, item.path, producerRunID)
|
||||
for sourceID, path := range proof.Outputs {
|
||||
_ = c.markAvailableFromExtractManifest(sourceID, path, proof.ProducerRunID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,25 +54,6 @@ func compatibleCatalogExtractionProvenance(
|
||||
pipelineID == strings.TrimSpace(want.PipelineID) && got.ArtifactID == want.LaneID
|
||||
}
|
||||
|
||||
func validExtractionPayload(bundleRoot, path, checksum string) bool {
|
||||
if !filepath.IsAbs(path) || !pathWithinExtractionRoot(bundleRoot, path) || strings.TrimSpace(checksum) == "" {
|
||||
return false
|
||||
}
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil || !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 {
|
||||
return false
|
||||
}
|
||||
if !safeExtractionComponents(bundleRoot, path) {
|
||||
return false
|
||||
}
|
||||
actual, err := SHA256File(path)
|
||||
if err != nil || actual != checksum {
|
||||
return false
|
||||
}
|
||||
body, err := fileops.ReadRegularFile(path, MaxExtractionPayloadBytes)
|
||||
return err == nil && json.Valid(body)
|
||||
}
|
||||
|
||||
func safeExistingExtractionDirectory(sessionRoot, bundleRoot string) bool {
|
||||
if !pathWithinExtractionRoot(sessionRoot, bundleRoot) || !safeExtractionComponents(sessionRoot, bundleRoot) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user