Stream verified prepared inputs into publication archives
This commit is contained in:
49
internal/stage/publish_prepared_test.go
Normal file
49
internal/stage/publish_prepared_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublishPreparedInputsRetainVerifiedBytesAndRejectTampering(t *testing.T) {
|
||||
env, m, _ := prepareResumeLocalFixture(t)
|
||||
paths := sessionPathsForEnv(env, m.SessionID)
|
||||
runRoot := filepath.Join(paths.Root, "runs", "20260519T010203Z-a1b2c3d4")
|
||||
files, err := collectPublishPreparedInputs(env.ArtifactStore, paths, runRoot, m.Inputs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(files) != 3 {
|
||||
t.Fatalf("prepared archive files = %d", len(files))
|
||||
}
|
||||
for _, file := range files {
|
||||
path := filepath.Join(runRoot, filepath.FromSlash(file.RelativePath))
|
||||
checksum, err := env.ArtifactStore.Checksum(path)
|
||||
if err != nil || checksum != file.Checksum {
|
||||
t.Fatalf("archive checksum = %q, %v", checksum, err)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || info.Mode().Perm() != 0o664 {
|
||||
t.Fatalf("archive mode: %v, %v", info, err)
|
||||
}
|
||||
}
|
||||
archivedParty := filepath.Join(runRoot, "prepare", "inputs", "party.yml")
|
||||
before, err := os.ReadFile(archivedParty)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := collectPublishPreparedInputs(env.ArtifactStore, paths, runRoot, m.Inputs); err != nil {
|
||||
t.Fatalf("reuse archive: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(paths.InputsDir, "party.yml"), []byte("changed"), 0o664); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := collectPublishPreparedInputs(env.ArtifactStore, paths, runRoot, m.Inputs); err == nil {
|
||||
t.Fatal("accepted changed prepared input")
|
||||
}
|
||||
after, err := os.ReadFile(archivedParty)
|
||||
if err != nil || string(after) != string(before) {
|
||||
t.Fatalf("changed archive after failed validation: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"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/manifest"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
)
|
||||
@@ -46,17 +47,9 @@ func collectPublishPreparedInputs(store artifacts.Store, paths artifacts.Session
|
||||
_ = source.Close()
|
||||
return nil, err
|
||||
}
|
||||
data, err := io.ReadAll(source)
|
||||
closeErr := source.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if closeErr != nil {
|
||||
return nil, closeErr
|
||||
}
|
||||
archive := filepath.ToSlash(filepath.Join("prepare", "inputs", name))
|
||||
destination := filepath.Join(runRoot, filepath.FromSlash(archive))
|
||||
if _, err := writeBytesIfChanged(store, destination, data); err != nil {
|
||||
if err := archivePreparedPublishInput(store, source, destination, record.Checksum); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, publishUploadFile{RelativePath: archive, SourceRelativePath: archive, Checksum: record.Checksum})
|
||||
@@ -64,6 +57,26 @@ func collectPublishPreparedInputs(store artifacts.Store, paths artifacts.Session
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// archivePreparedPublishInput consumes and closes the verified source descriptor.
|
||||
func archivePreparedPublishInput(store artifacts.Store, source *os.File, destination, checksum string) (resultErr error) {
|
||||
defer func() { resultErr = errors.Join(resultErr, source.Close()) }()
|
||||
exists, err := store.Exists(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
current, err := store.Checksum(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(current, checksum) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
_, err = fileops.WriteReaderAtomicWithChecksum(destination, source, fileops.WorkspaceFileMode)
|
||||
return err
|
||||
}
|
||||
|
||||
func collectPublishRunFiles(runRoot string, sessionManifest *manifest.Manifest) ([]publishUploadFile, error) {
|
||||
if sessionManifest == nil {
|
||||
return nil, fmt.Errorf("session manifest is required")
|
||||
|
||||
Reference in New Issue
Block a user