feat: apply sealed installation plans without interactive setup

This commit is contained in:
Codex
2026-10-06 15:45:18 +02:00
parent c3c9f509bf
commit 8b9a086fd6
31 changed files with 1542 additions and 45 deletions
+10
View File
@@ -29,6 +29,16 @@ func CheckCompose(ctx context.Context, runner Runner, installation config.Instal
args = append(args, "-f", path)
}
args = append(args, "config", "--format", "json")
return checkResolvedCompose(ctx, runner, args, m, platform)
}
// CheckInstalledCompose includes generated projections and any durable image override.
func CheckInstalledCompose(ctx context.Context, runner Runner, installation config.Installation, m Manifest, platform string) Report {
return checkResolvedCompose(ctx, runner, installation.ComposeArgs("config", "--format", "json"), m, platform)
}
func checkResolvedCompose(ctx context.Context, runner Runner, args []string, m Manifest, platform string) Report {
r := NewReport()
result, err := docker(ctx, runner, args...)
var effective struct {
Services map[string]struct {
+27 -10
View File
@@ -28,6 +28,8 @@ type Plan struct {
Platform string `json:"platform"`
WorkspaceDirectory string `json:"workspace_directory"`
WorkspaceRevision string `json:"workspace_revision"`
ManifestPath string `json:"manifest_path,omitempty"`
BootstrapPath string `json:"bootstrap_path,omitempty"`
Inputs []string `json:"inputs"`
AbsentInputs []string `json:"absent_inputs"`
InputSeal string `json:"input_seal"`
@@ -62,10 +64,16 @@ func CaptureInputs(paths []string, workspace string, absent ...string) (func() b
// A separate owner-only random key prevents public/offline guessing of low-entropy secrets.
// Live prerequisites must still be rechecked immediately before any execution or resumption.
func seal(plan Plan, key []byte) (string, error) {
func seal(plan Plan, key []byte, materialized ...bool) (string, error) {
for _, path := range plan.AbsentInputs {
if _, err := os.Lstat(path); !errors.Is(err, os.ErrNotExist) {
return "", invalidPlan()
name := "deploy/" + filepath.Base(path)
allowed := len(materialized) > 0 && materialized[0] && (name == "deploy/compose.git-https.yaml" || name == "deploy/compose.git-ssh.yaml") && path == filepath.Join(plan.Installation.ProjectDirectory, filepath.FromSlash(name))
data, readErr := safeio.ReadCanonicalRegular(path, 32<<20)
sum := sha256.Sum256(data)
if !allowed || readErr != nil || hex.EncodeToString(sum[:]) != plan.Release.Files[name] {
return "", invalidPlan()
}
}
}
mac := hmac.New(sha256.New, key)
@@ -138,35 +146,44 @@ func WritePlan(path string, plan *Plan, guards ...func() bool) error {
return nil
}
func VerifyPlanInputs(path string) error {
_, err := readPlan(path, false)
return err
}
// ReadExecutionPlan permits only exact release-owned overlays installed by a prior attempt.
// The authored documents, credentials, bundle and private seal must otherwise remain unchanged.
func ReadExecutionPlan(path string) (Plan, error) { return readPlan(path, true) }
func readPlan(path string, materialized bool) (Plan, error) {
data, err := safeio.ReadCanonicalPrivateRegular(path, 4<<20)
if err != nil {
return invalidPlan()
return Plan{}, invalidPlan()
}
key, err := safeio.ReadCanonicalPrivateRegular(path+".key", 32)
if err != nil || len(key) != 32 {
return invalidPlan()
return Plan{}, invalidPlan()
}
var plan Plan
if json.Unmarshal(data, &plan) != nil || plan.SchemaVersion != 1 || plan.ValidatorProtocol != Protocol || !plan.Report.OK || len(plan.Inputs) == 0 || len(plan.Inputs) > 10000 {
return invalidPlan()
return Plan{}, invalidPlan()
}
actual, err := seal(plan, key)
actual, err := seal(plan, key, materialized)
if err != nil || !hmac.Equal([]byte(actual), []byte(plan.InputSeal)) {
return invalidPlan()
return Plan{}, invalidPlan()
}
// Detect added or removed workspace files as well as changes to known file bytes.
if plan.WorkspaceDirectory != "" {
files, err := treeFiles(plan.WorkspaceDirectory, true)
if err != nil {
return invalidPlan()
return Plan{}, invalidPlan()
}
for _, file := range files {
if !slices.Contains(plan.Inputs, file) {
return invalidPlan()
return Plan{}, invalidPlan()
}
}
}
return nil
return plan, nil
}
func treeFiles(root string, skipGit bool) ([]string, error) {
paths := []string{}
+29
View File
@@ -1,6 +1,9 @@
package preflight
import (
"crypto/sha256"
"fmt"
"github.com/aritmolab/thothii/tools/tht/internal/config"
"github.com/aritmolab/thothii/tools/tht/internal/safeio"
"os"
"path/filepath"
@@ -8,6 +11,32 @@ import (
"testing"
)
func TestExecutionPlanAcceptsOnlyExactReleaseOwnedMaterialization(t *testing.T) {
root, _ := filepath.EvalSymlinks(t.TempDir())
_ = os.Chmod(root, 0o700)
_ = os.Mkdir(filepath.Join(root, "deploy"), 0o700)
input := filepath.Join(root, "input")
_ = os.WriteFile(input, []byte("prepared"), 0o600)
overlay := filepath.Join(root, "deploy", "compose.git-https.yaml")
data := []byte("services: {}\n")
plan := Plan{SchemaVersion: 1, ValidatorProtocol: Protocol, Installation: config.Installation{ProjectDirectory: root}, Inputs: []string{input}, AbsentInputs: []string{overlay}, Report: NewReport(), Release: Manifest{Files: map[string]string{"deploy/compose.git-https.yaml": fmt.Sprintf("%x", sha256.Sum256(data))}}}
path := filepath.Join(root, "plan.json")
if err := WritePlan(path, &plan); err != nil {
t.Fatal(err)
}
_ = os.WriteFile(overlay, data, 0o600)
if _, err := ReadExecutionPlan(path); err != nil {
t.Fatal(err)
}
if VerifyPlanInputs(path) == nil {
t.Fatal("ordinary validation must still detect new files")
}
_ = os.WriteFile(overlay, []byte("changed"), 0o600)
if _, err := ReadExecutionPlan(path); err == nil {
t.Fatal("modified materialization accepted")
}
}
func TestPlanBindsInputsAndDetectsCredentialRotationWithoutPublicSecretHashes(t *testing.T) {
root, _ := filepath.EvalSymlinks(t.TempDir())
if err := safeio.ProtectPrivateDirectory(root); err != nil {