Merge imported products into local session state
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package artifacts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -15,8 +13,6 @@ const (
|
||||
MaxCurrentCommitPointerBytes int64 = 64 << 10
|
||||
// MaxRemoteCommitManifestBytes bounds an immutable commit manifest.
|
||||
MaxRemoteCommitManifestBytes int64 = 4 << 20
|
||||
// MaxRemoteSessionManifestBytes bounds the selected session manifest.
|
||||
MaxRemoteSessionManifestBytes int64 = 8 << 20
|
||||
)
|
||||
|
||||
func loadCommittedInventory(
|
||||
@@ -64,32 +60,6 @@ func loadCommittedInventory(
|
||||
return &CurrentState{SessionPrefix: sessionPrefix, CurrentPointerKey: pointerKey, RunID: commit.RunID, Commit: commit, Pointer: pointer}, nil
|
||||
}
|
||||
|
||||
func loadCommittedCurrentState(ctx context.Context, store storage.ObjectStore, state *CurrentState, validation CurrentStateValidation) (*CurrentState, error) {
|
||||
sessionManifest, ok := state.Commit.Artifact(RemoteArtifactTypeSessionManifest)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("remote commit must declare exactly one session manifest artifact")
|
||||
}
|
||||
manifestData, err := readVerifiedRemoteObject(ctx, store, sessionManifest.DestinationKey, sessionManifest.SHA256, sessionManifest.Size, sessionManifest.Generation, "committed session manifest", MaxRemoteSessionManifestBytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read committed session manifest %q: %w", sessionManifest.DestinationKey, err)
|
||||
}
|
||||
m, err := decodeCommittedManifest(ctx, manifestData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateCommitManifestIdentity(state.Commit, m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
state.CurrentManifestKey = sessionManifest.DestinationKey
|
||||
state.Manifest = m
|
||||
state.ManifestData = append([]byte(nil), manifestData...)
|
||||
if err := ValidateCurrentStateIdentity(state, validation); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func validatePointerCommitIdentity(pointer *CurrentCommitPointer, commit *RemoteCommitManifest) error {
|
||||
if pointer == nil || commit == nil {
|
||||
return fmt.Errorf("current commit pointer and remote commit are required")
|
||||
@@ -100,18 +70,6 @@ func validatePointerCommitIdentity(pointer *CurrentCommitPointer, commit *Remote
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateCommitManifestIdentity(commit *RemoteCommitManifest, m *manifest.Manifest) error {
|
||||
if commit == nil || m == nil {
|
||||
return fmt.Errorf("remote commit and committed session manifest are required")
|
||||
}
|
||||
if commit.Campaign != strings.TrimSpace(m.Campaign) ||
|
||||
commit.SessionID != strings.TrimSpace(m.SessionID) ||
|
||||
commit.RunID != strings.TrimSpace(m.RunID) {
|
||||
return fmt.Errorf("selected remote commit identity does not match committed session manifest")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readVerifiedRemoteObject(
|
||||
ctx context.Context,
|
||||
store storage.ObjectStore,
|
||||
@@ -148,11 +106,3 @@ func readCurrentStateControlObject(ctx context.Context, store storage.ObjectStor
|
||||
}
|
||||
return info, data, nil
|
||||
}
|
||||
|
||||
func decodeCommittedManifest(ctx context.Context, data []byte) (*manifest.Manifest, error) {
|
||||
m, err := (&manifest.LocalStore{}).LoadReader(ctx, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("committed session manifest decode failed: %w; republish this session to replace unsupported remote state", err)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user