Refuse a declaration for the other mode, or older than the mesh's last, and say what an apply would change first
An operator ran `mesh-host reconcile` on an adopted control-node with twelve modules assigned. It applied the bundle the host carries — the genesis declaration, foundation only, converged: recreated the store, failed on the broker's held port, wrote the converged base filter and started its service, and stopped at the first failing action. The filter closed the machine for forty-five minutes. The host reported the node adopted in every report, the declaration said converged, and nothing compared the two; nothing was printed before acting (hq issue 104). The host now records the node's mode — from every declaration the mesh sends, and at genesis from what the operator said — and refuses, at the point of application, a declaration that says the other mode, naming both and the act that changes it. Only a declaration the link delivers, signed, changes the mode: that is how `converge` and `adopt` arrive, so the flip still works and nothing else can do it. Genesis marks the bundle consumed, with the digest of what it applied, so `reconcile` holds a node the mesh has spoken to against what the mesh last said and never the bundle, and refuses the carried bytes when they are not what genesis applied. A file is refused when it is not what the mesh last said: a declaration carries no sequence and no issued-at, so the host cannot tell older from newer, and says so. Both commands print what they would change — a hold, a removal, an action named as one — before touching anything, and --dry-run is that list and nothing more.
This commit is contained in:
@@ -55,11 +55,13 @@ connects to nothing and listens on nothing — what it applies comes from a file
|
|||||||
mesh-host profile what this machine can be asked to do
|
mesh-host profile what this machine can be asked to do
|
||||||
mesh-host inventory what this machine is, and what it holds
|
mesh-host inventory what this machine is, and what it holds
|
||||||
mesh-host apply FILE make this machine match a declaration from a file
|
mesh-host apply FILE make this machine match a declaration from a file
|
||||||
mesh-host reconcile make this machine match the declaration this host carries
|
mesh-host reconcile make this machine match what the mesh last told it — or, before
|
||||||
|
any mesh has, the bundle this host carries
|
||||||
mesh-host bundle show what this host carries
|
mesh-host bundle show what this host carries
|
||||||
mesh-host owned what this host has applied and still owns
|
mesh-host owned what this host has applied and still owns
|
||||||
--json machine-readable
|
--json machine-readable
|
||||||
--state where this node keeps what it knows
|
--state where this node keeps what it knows
|
||||||
|
--dry-run say what applying would change, and change nothing
|
||||||
--dry-run read and check the declaration, change nothing
|
--dry-run read and check the declaration, change nothing
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -114,8 +116,12 @@ A host built for a machine carries its declaration **inside the binary**:
|
|||||||
make host BUNDLE=path/to/foundation.lock
|
make host BUNDLE=path/to/foundation.lock
|
||||||
```
|
```
|
||||||
|
|
||||||
`mesh-host reconcile` then applies it. That is the first node's path — no mesh present, nothing
|
`mesh-host reconcile` then applies it, on a machine the mesh has told nothing yet. That is the
|
||||||
fetched, nothing else copied onto the machine. `copy it and run it` stops being true the moment
|
first node's path — no mesh present, nothing fetched, nothing else copied onto the machine. Once
|
||||||
|
the mesh has spoken, `reconcile` holds the machine to what it last said and never to the bundle,
|
||||||
|
which genesis consumed; and any declaration — bundle, file or kept — is refused when it says the
|
||||||
|
other mode than the node is in, or is not what the mesh last said. Both commands say what they
|
||||||
|
would change before changing anything, and `--dry-run` is that alone. `copy it and run it` stops being true the moment
|
||||||
a second file has to arrive with it, which is why the bundle is embedded rather than beside it.
|
a second file has to arrive with it, which is why the bundle is embedded rather than beside it.
|
||||||
|
|
||||||
**A default build carries nothing and refuses to reconcile**, saying so. A host that applied
|
**A default build carries nothing and refuses to reconcile**, saying so. A host that applied
|
||||||
|
|||||||
+231
-27
@@ -15,6 +15,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -53,7 +54,8 @@ const usage = `mesh-host — the node host
|
|||||||
profile what this machine can be asked to do
|
profile what this machine can be asked to do
|
||||||
inventory what this machine is, and what it holds
|
inventory what this machine is, and what it holds
|
||||||
apply FILE make this machine match a declaration from a file
|
apply FILE make this machine match a declaration from a file
|
||||||
reconcile make this machine match the declaration this host carries
|
reconcile make this machine match what the mesh last told it — or, before any
|
||||||
|
mesh has, the bundle this host carries
|
||||||
bundle show what this host carries
|
bundle show what this host carries
|
||||||
owned what this host has applied and still owns
|
owned what this host has applied and still owns
|
||||||
version
|
version
|
||||||
@@ -61,7 +63,10 @@ const usage = `mesh-host — the node host
|
|||||||
--json machine-readable output
|
--json machine-readable output
|
||||||
--timeout how long any single probe may take (default 10s)
|
--timeout how long any single probe may take (default 10s)
|
||||||
--state where this node keeps what it knows (default /var/lib/mesh-host/state.json)
|
--state where this node keeps what it knows (default /var/lib/mesh-host/state.json)
|
||||||
--dry-run read the declaration and refuse it if wrong, but change nothing
|
--dry-run say what applying would change, and change nothing
|
||||||
|
|
||||||
|
Both apply and reconcile say what they would change before changing anything, and refuse a
|
||||||
|
declaration for the other mode than this node is in, or one older than what the mesh last said.
|
||||||
|
|
||||||
It connects to nothing and listens on nothing. What it applies comes from a file.
|
It connects to nothing and listens on nothing. What it applies comes from a file.
|
||||||
`
|
`
|
||||||
@@ -90,6 +95,8 @@ type options struct {
|
|||||||
nodeName string
|
nodeName string
|
||||||
dryRun bool
|
dryRun bool
|
||||||
file string
|
file string
|
||||||
|
// out is where what a command says goes. Stdout, and a buffer under test.
|
||||||
|
out io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseArgs takes the subcommand first, then its flags.
|
// parseArgs takes the subcommand first, then its flags.
|
||||||
@@ -99,7 +106,7 @@ type options struct {
|
|||||||
// passed, silently ignored, with a successful exit. That is the fault this whole project keeps
|
// passed, silently ignored, with a successful exit. That is the fault this whole project keeps
|
||||||
// naming, so the parser takes the subcommand off the front and parses what follows.
|
// naming, so the parser takes the subcommand off the front and parses what follows.
|
||||||
func parseArgs(args []string) (string, options, error) {
|
func parseArgs(args []string) (string, options, error) {
|
||||||
opts := options{timeout: 10 * time.Second, state: store.DefaultPath}
|
opts := options{timeout: 10 * time.Second, state: store.DefaultPath, out: os.Stdout}
|
||||||
|
|
||||||
command := ""
|
command := ""
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
@@ -184,18 +191,14 @@ func run(ctx context.Context, command string, opts options) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return runApply(ctx, opts, d, opts.file)
|
return runApply(ctx, opts, d, raw, fromFile)
|
||||||
|
|
||||||
case "reconcile":
|
case "reconcile":
|
||||||
// The first node's path. novox/hq ADR 0004: no mesh reachable means the declaration
|
d, raw, from, err := reconcileSource(opts)
|
||||||
// comes from the bundle the host carries. There is no link yet, so this is currently
|
|
||||||
// the only source — which is a stage, not a design, and saying so beats implying the
|
|
||||||
// other source exists.
|
|
||||||
d, err := bundle.Load(builtFor)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return runApply(ctx, opts, d, "the carried bundle")
|
return runApply(ctx, opts, d, raw, from)
|
||||||
|
|
||||||
case "bundle":
|
case "bundle":
|
||||||
if bundle.IsEmpty(builtFor) {
|
if bundle.IsEmpty(builtFor) {
|
||||||
@@ -247,8 +250,10 @@ func run(ctx context.Context, command string, opts options) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeJSON(v any) error {
|
func writeJSON(v any) error { return writeJSONTo(os.Stdout, v) }
|
||||||
enc := json.NewEncoder(os.Stdout)
|
|
||||||
|
func writeJSONTo(w io.Writer, v any) error {
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
return enc.Encode(v)
|
return enc.Encode(v)
|
||||||
}
|
}
|
||||||
@@ -305,20 +310,199 @@ func writeInventory(inv inventory.Inventory) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// runApply reads a declaration and makes the machine match it.
|
// provenance is where a declaration a command applies came from. It decides the origin its
|
||||||
|
// resources are recorded under, what it is held against, and what is recorded once it applied.
|
||||||
|
type provenance int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// fromFile is `apply FILE`: handed to the host by someone already running it as root.
|
||||||
|
fromFile provenance = iota
|
||||||
|
// fromBundle is `reconcile` on a machine the mesh has told nothing yet: the bundle carried in
|
||||||
|
// the binary, the first node's path before its mesh is up (novox/hq ADR 0004).
|
||||||
|
fromBundle
|
||||||
|
// fromDeclared is `reconcile` on a node the mesh has spoken to: what it last said, kept
|
||||||
|
// signed beside the state (declared.json), verified again before it is applied.
|
||||||
|
fromDeclared
|
||||||
|
)
|
||||||
|
|
||||||
|
// reconcileSource is which declaration `reconcile` holds this machine to.
|
||||||
|
//
|
||||||
|
// **What the mesh last said, never the bundle, once the mesh has said anything** (novox/hq issue
|
||||||
|
// 104). The bundle is right at genesis and stale a minute later — genesis rewrites it for the
|
||||||
|
// machine before applying it, and every declaration since came from the controller — and on an
|
||||||
|
// adopted node it is a converged declaration for a machine that is not converged. A node that
|
||||||
|
// has been told something and cannot prove it is the mesh's is refused, with the reason; the
|
||||||
|
// bundle is not applied in its place.
|
||||||
|
func reconcileSource(opts options) (*declaration.Declaration, []byte, provenance, error) {
|
||||||
|
path := store.DeclaredPath(opts.state)
|
||||||
|
_, err := store.ReadDeclared(path)
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
mine, err := identity.Load(identity.Path(opts.state))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0, fmt.Errorf("this node was told a declaration by the mesh, kept at %s, "+
|
||||||
|
"and cannot prove it is the mesh's: %w\n\nIt is not applied unproven, and the bundle "+
|
||||||
|
"this host carries is not applied in its place: that was consumed at genesis", path, err)
|
||||||
|
}
|
||||||
|
raw, err := store.LoadDeclared(path, mine.Membership.Signer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0, fmt.Errorf("%w\n\nThe bundle this host carries is not applied in its "+
|
||||||
|
"place: that was consumed at genesis", err)
|
||||||
|
}
|
||||||
|
d, err := declaration.Parse(raw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0, err
|
||||||
|
}
|
||||||
|
return d, raw, fromDeclared, nil
|
||||||
|
case errors.Is(err, store.ErrNothingDeclared):
|
||||||
|
d, err := bundle.Load(builtFor)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, 0, err
|
||||||
|
}
|
||||||
|
return d, bundle.Raw(builtFor), fromBundle, nil
|
||||||
|
default:
|
||||||
|
return nil, nil, 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p provenance) origin() string {
|
||||||
|
if p == fromDeclared {
|
||||||
|
return store.OriginDeclared
|
||||||
|
}
|
||||||
|
// A file handed to the host is applied as the bundle is: what it puts here is invisible to
|
||||||
|
// the removal pass of a declaration that later arrives from the mesh (novox/hq issue 010).
|
||||||
|
return store.OriginCarried
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p provenance) name(opts options) string {
|
||||||
|
switch p {
|
||||||
|
case fromBundle:
|
||||||
|
return "the carried bundle"
|
||||||
|
case fromDeclared:
|
||||||
|
return "what the mesh last told this node (" + store.DeclaredName + ")"
|
||||||
|
}
|
||||||
|
return opts.file
|
||||||
|
}
|
||||||
|
|
||||||
|
// short is a digest as a person reads one aloud.
|
||||||
|
func short(digest string) string {
|
||||||
|
if len(digest) > 12 {
|
||||||
|
return digest[:12]
|
||||||
|
}
|
||||||
|
return digest
|
||||||
|
}
|
||||||
|
|
||||||
|
// refuseStale refuses a declaration that is not the one this node should be held to (novox/hq
|
||||||
|
// issue 104), naming both.
|
||||||
|
//
|
||||||
|
// **A declaration carries no order.** The controller signs a version — the vocabulary — the
|
||||||
|
// node it is for, the mode, and the resources: no sequence, no issued-at. What exists is
|
||||||
|
// identity: the mesh names what it sends by the digest of the bytes, the node keeps the last one
|
||||||
|
// it was sent, and genesis records the digest of what it consumed. So "older" can be said of
|
||||||
|
// exactly two things — the bundle genesis consumed, and a bundle other than the one it consumed —
|
||||||
|
// and of anything else only that it is not what the mesh last said, which is refused too: a
|
||||||
|
// host that cannot tell older from newer and applies anyway is the fault this issue names.
|
||||||
|
func refuseStale(known store.State, kept store.Declared, keptErr error, digest string, from provenance) error {
|
||||||
|
switch from {
|
||||||
|
case fromDeclared:
|
||||||
|
return nil
|
||||||
|
case fromBundle:
|
||||||
|
if known.Genesis == nil || known.Genesis.Digest == digest {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
how := ""
|
||||||
|
if known.Genesis.Rewritten {
|
||||||
|
how = ", rewritten for this machine — its foundation ports, root credentials and mode"
|
||||||
|
}
|
||||||
|
return fmt.Errorf("the bundle this host carries (%s) is not the one genesis applied here on %s "+
|
||||||
|
"(%s%s). The bundle was consumed then, and nothing the mesh has said is kept on this node "+
|
||||||
|
"yet, so there is nothing to reconcile against: enrol this node, or push to it from the "+
|
||||||
|
"controller", short(digest), known.Genesis.At.Format(time.RFC3339), short(known.Genesis.Digest), how)
|
||||||
|
}
|
||||||
|
// A file.
|
||||||
|
switch {
|
||||||
|
case errors.Is(keptErr, store.ErrNothingDeclared):
|
||||||
|
// The mesh has said nothing here. Nothing to be older than.
|
||||||
|
return nil
|
||||||
|
case keptErr != nil:
|
||||||
|
return fmt.Errorf("%w\n\nNothing is applied over what this node cannot read back", keptErr)
|
||||||
|
}
|
||||||
|
last := apply.DigestOf(kept.Declaration)
|
||||||
|
if digest == last {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if known.Genesis != nil && digest == known.Genesis.Digest {
|
||||||
|
return fmt.Errorf("this file is the bundle genesis consumed on %s (%s), and the mesh has since told "+
|
||||||
|
"this node declaration %s, kept as %s. An older declaration is not applied over a newer one: "+
|
||||||
|
"`reconcile` applies what the mesh last said", known.Genesis.At.Format(time.RFC3339), short(digest),
|
||||||
|
short(last), store.DeclaredName)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("this file (%s) is not the declaration the mesh last told this node (%s, kept as %s). "+
|
||||||
|
"A declaration carries no sequence and no issued-at, so this host cannot tell an older one from a "+
|
||||||
|
"newer, and it applies only what the mesh last said: `reconcile` applies that, and `push` from the "+
|
||||||
|
"controller changes it", short(digest), short(last), store.DeclaredName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// applied is what an apply says in machine-readable form: what it planned, then what it did.
|
||||||
|
type applied struct {
|
||||||
|
Plan []apply.Step `json:"plan"`
|
||||||
|
Report apply.Report `json:"report"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// runApply makes the machine match a declaration — after refusing one this node must not apply,
|
||||||
|
// and after saying what it would change.
|
||||||
|
//
|
||||||
|
// Refused first, and before anything is read from the machine: a declaration for the other
|
||||||
|
// mode than this node is in, or one older than what the mesh last said (novox/hq issue 104).
|
||||||
|
// Then the plan, printed whole before a single resource is touched; `--dry-run` is that and
|
||||||
|
// nothing more.
|
||||||
//
|
//
|
||||||
// The state is loaded before anything is touched and saved after, including when the apply
|
// The state is loaded before anything is touched and saved after, including when the apply
|
||||||
// fails part-way: what was applied before the failure is on the machine, and a host that did
|
// fails part-way: what was applied before the failure is on the machine, and a host that did
|
||||||
// not record it would believe it owns less than it does and leave that behind forever.
|
// not record it would believe it owns less than it does and leave that behind forever.
|
||||||
func runApply(ctx context.Context, opts options, d *declaration.Declaration, source string) error {
|
func runApply(ctx context.Context, opts options, d *declaration.Declaration, raw []byte, from provenance) error {
|
||||||
|
out := opts.out
|
||||||
|
if out == nil {
|
||||||
|
out = os.Stdout
|
||||||
|
}
|
||||||
known, err := store.Load(opts.state)
|
known, err := store.Load(opts.state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
source, origin, digest := from.name(opts), from.origin(), apply.DigestOf(raw)
|
||||||
|
|
||||||
|
// The mode this node is in. Recorded in the state since this check existed; a state written
|
||||||
|
// before then has it in what the mesh last said, which this node kept.
|
||||||
|
kept, keptErr := store.ReadDeclared(store.DeclaredPath(opts.state))
|
||||||
|
if known.Mode == "" && keptErr == nil {
|
||||||
|
if last, err := declaration.Parse(kept.Declaration); err == nil {
|
||||||
|
known.Mode = apply.ModeOf(last)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := apply.CheckMode(known, d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := refuseStale(known, kept, keptErr, digest, from); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Said before anything is done.
|
||||||
|
steps := apply.Plan(d, known, origin)
|
||||||
|
if opts.json && opts.dryRun {
|
||||||
|
return writeJSONTo(out, steps)
|
||||||
|
}
|
||||||
|
mode := known.Mode
|
||||||
|
if mode == "" {
|
||||||
|
mode = "in no mode yet — nothing has said one"
|
||||||
|
}
|
||||||
|
fmt.Fprintf(out, "%s (%s): %d resource(s), version %d\n", source, short(digest), len(d.Resources), d.Version)
|
||||||
|
fmt.Fprintf(out, "this node is %s; the declaration says %s\n", mode, apply.ModeOf(d))
|
||||||
|
fmt.Fprintf(out, "\nwould change, in this order:\n")
|
||||||
|
for _, step := range steps {
|
||||||
|
fmt.Fprintln(out, " "+step.String())
|
||||||
|
}
|
||||||
if opts.dryRun {
|
if opts.dryRun {
|
||||||
fmt.Printf("%s: %d resource(s), version %d — accepted, nothing applied\n",
|
fmt.Fprintf(out, "\n--dry-run: nothing applied\n")
|
||||||
source, len(d.Resources), d.Version)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,12 +522,20 @@ func runApply(ctx context.Context, opts options, d *declaration.Declaration, sou
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
report, updated, applyErr := apply.Apply(ctx, sys, d, known, store.OriginCarried,
|
fmt.Fprintf(out, "\napplying:\n")
|
||||||
|
report, updated, applyErr := apply.ApplyKeeping(ctx, sys, d, known, origin,
|
||||||
apply.ExecRunner, func(line string) {
|
apply.ExecRunner, func(line string) {
|
||||||
if !opts.json {
|
if !opts.json {
|
||||||
fmt.Println(line)
|
fmt.Fprintln(out, line)
|
||||||
}
|
}
|
||||||
}, sealOpener(opts.state))
|
}, sealOpener(opts.state), apply.KeepIn(filepath.Dir(opts.state)))
|
||||||
|
|
||||||
|
// What this apply settles about the node, whichever way it went. The mode is what the
|
||||||
|
// declaration said and the check above agreed with; the bundle, once applied, is consumed.
|
||||||
|
updated.Mode = apply.ModeOf(d)
|
||||||
|
if from == fromBundle {
|
||||||
|
updated.Genesis = &store.Genesis{Digest: digest, At: time.Now().UTC()}
|
||||||
|
}
|
||||||
|
|
||||||
// Saved whichever way it went. Recording only on success would lose the footprint of a
|
// Saved whichever way it went. Recording only on success would lose the footprint of a
|
||||||
// failed apply, and that footprint is on the machine either way.
|
// failed apply, and that footprint is on the machine either way.
|
||||||
@@ -380,13 +572,13 @@ func runApply(ctx context.Context, opts options, d *declaration.Declaration, sou
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opts.json {
|
if opts.json {
|
||||||
return writeJSON(report)
|
return writeJSONTo(out, applied{Plan: steps, Report: report})
|
||||||
}
|
}
|
||||||
if !report.Changed() {
|
if !report.Changed() {
|
||||||
fmt.Printf("%s: already matches — %d resource(s) checked\n", source, len(report.Outcomes))
|
fmt.Fprintf(out, "%s: already matches — %d resource(s) checked\n", source, len(report.Outcomes))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fmt.Printf("%s: applied — %d resource(s)\n", source, len(report.Outcomes))
|
fmt.Fprintf(out, "%s: applied — %d resource(s)\n", source, len(report.Outcomes))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -838,6 +1030,19 @@ func applyAndKeep(ctx context.Context, opts options, raw []byte, signed *store.D
|
|||||||
return link.Report{Refused: err.Error()}
|
return link.Report{Refused: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
known, err := store.Load(opts.state)
|
||||||
|
if err != nil {
|
||||||
|
return link.Report{Refused: err.Error()}
|
||||||
|
}
|
||||||
|
// A declaration the link delivered is the controller's word on the node's mode — the flip
|
||||||
|
// arrives as exactly that, the first converged declaration after adopted ones — and becomes
|
||||||
|
// the record. What this node re-applies on its own is held to the record (novox/hq issue 104).
|
||||||
|
if signed == nil {
|
||||||
|
if err := apply.CheckMode(known, declared); err != nil {
|
||||||
|
return link.Report{Refused: err.Error()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
built, err := system.For(builtFor)
|
built, err := system.For(builtFor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return link.Report{Refused: err.Error()}
|
return link.Report{Refused: err.Error()}
|
||||||
@@ -846,11 +1051,6 @@ func applyAndKeep(ctx context.Context, opts options, raw []byte, signed *store.D
|
|||||||
return link.Report{Refused: err.Error()}
|
return link.Report{Refused: err.Error()}
|
||||||
}
|
}
|
||||||
|
|
||||||
known, err := store.Load(opts.state)
|
|
||||||
if err != nil {
|
|
||||||
return link.Report{Refused: err.Error()}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := built.Confirm(ctx, apply.ExecRunner); err != nil {
|
if err := built.Confirm(ctx, apply.ExecRunner); err != nil {
|
||||||
return link.Report{Refused: err.Error()}
|
return link.Report{Refused: err.Error()}
|
||||||
}
|
}
|
||||||
@@ -860,6 +1060,10 @@ func applyAndKeep(ctx context.Context, opts options, raw []byte, signed *store.D
|
|||||||
outcome, updated, applyErr := apply.ApplyKeeping(ctx, built, declared, known, store.OriginDeclared,
|
outcome, updated, applyErr := apply.ApplyKeeping(ctx, built, declared, known, store.OriginDeclared,
|
||||||
apply.ExecRunner, nil, sealOpener(opts.state), apply.KeepIn(filepath.Dir(opts.state)))
|
apply.ExecRunner, nil, sealOpener(opts.state), apply.KeepIn(filepath.Dir(opts.state)))
|
||||||
|
|
||||||
|
// The mode the mesh said, recorded whichever way the apply went: the declaration is kept
|
||||||
|
// either way, and the node is held to it from the next reconcile (novox/hq ADR 0100).
|
||||||
|
updated.Mode = apply.ModeOf(declared)
|
||||||
|
|
||||||
// Saved whichever way it went. Recording only on success would lose the footprint of a
|
// Saved whichever way it went. Recording only on success would lose the footprint of a
|
||||||
// failed apply, and that footprint is on the machine either way.
|
// failed apply, and that footprint is on the machine either way.
|
||||||
if saveErr := store.Save(opts.state, updated); saveErr != nil {
|
if saveErr := store.Save(opts.state, updated); saveErr != nil {
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/ed25519"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/novox/mesh-host/internal/apply"
|
"github.com/novox/mesh-host/internal/apply"
|
||||||
|
"github.com/novox/mesh-host/internal/bundle"
|
||||||
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
|
"github.com/novox/mesh-host/internal/identity"
|
||||||
"github.com/novox/mesh-host/internal/link"
|
"github.com/novox/mesh-host/internal/link"
|
||||||
"github.com/novox/mesh-host/internal/store"
|
"github.com/novox/mesh-host/internal/store"
|
||||||
"github.com/novox/mesh-host/internal/system"
|
"github.com/novox/mesh-host/internal/system"
|
||||||
@@ -262,3 +268,204 @@ func TestAChangeThatNeverReachedTheMeshIsSaidAgain(t *testing.T) {
|
|||||||
t.Error("a change the mesh was told was said again")
|
t.Error("a change the mesh was told was said again")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: a declaration for the other mode than this node is in is refused at
|
||||||
|
// the point of application, whichever command delivered it, naming both — an adopted control-node
|
||||||
|
// once applied its converged genesis bundle and closed itself for forty-five minutes.
|
||||||
|
|
||||||
|
func stateWithMode(t *testing.T, mode string) options {
|
||||||
|
t.Helper()
|
||||||
|
dir := t.TempDir()
|
||||||
|
opts := options{state: filepath.Join(dir, "state.json"), out: &bytes.Buffer{}}
|
||||||
|
if err := store.Save(opts.state, store.State{Mode: mode}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAConvergedDeclarationIsRefusedOnAnAdoptedNode(t *testing.T) {
|
||||||
|
opts := stateWithMode(t, store.ModeAdopted)
|
||||||
|
path := filepath.Join(filepath.Dir(opts.state), "filter.conf")
|
||||||
|
raw := []byte(`{"declaration":1,"resources":[{"id":"filter","type":"file","path":"` + path +
|
||||||
|
`","content":"table inet filter { chain input { policy drop; } }\n"}]}`)
|
||||||
|
d, err := declaration.ParseFileTrusted(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, from := range []provenance{fromFile, fromBundle} {
|
||||||
|
err := runApply(context.Background(), opts, d, raw, from)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("a converged declaration was applied to an adopted node (from %d)", from)
|
||||||
|
}
|
||||||
|
want := "this node is adopted; the declaration says converged"
|
||||||
|
if !strings.Contains(err.Error(), want) || !strings.Contains(err.Error(), "`converge`") {
|
||||||
|
t.Errorf("the refusal does not name both modes and the act that changes it: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Error("the refused declaration touched the machine")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAnAdoptedDeclarationIsRefusedOnAConvergedNode(t *testing.T) {
|
||||||
|
// Only the mesh can say a node is adopted, so this one arrives the way a disconnected node
|
||||||
|
// re-applies what it kept: through the reconcile loop, unsigned.
|
||||||
|
opts := stateWithMode(t, store.ModeConverged)
|
||||||
|
raw := []byte(`{"declaration":1,"adoption":{"taken":[]},"resources":[{"id":"a","type":"file","path":"` +
|
||||||
|
filepath.Join(filepath.Dir(opts.state), "a.conf") + `","content":"x\n"}]}`)
|
||||||
|
report := applyAndKeep(context.Background(), opts, raw, nil, nil)
|
||||||
|
want := "this node is converged; the declaration says adopted"
|
||||||
|
if !strings.Contains(report.Refused, want) || !strings.Contains(report.Refused, "`adopt`") {
|
||||||
|
t.Errorf("refused = %q, want it to name both modes and the act that changes it", report.Refused)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTheMeshItselfMayChangeTheMode(t *testing.T) {
|
||||||
|
// The flip is a declaration: `converge` on the controller records the mode and sends the
|
||||||
|
// first converged declaration. Delivered by the link, signed, it is not held to the record —
|
||||||
|
// it becomes it. (With no system linked in, the apply is refused later for that; what this
|
||||||
|
// checks is that the refusal is not the mode's.)
|
||||||
|
opts := stateWithMode(t, store.ModeAdopted)
|
||||||
|
raw := []byte(`{"declaration":1,"resources":[{"id":"a","type":"file","path":"` +
|
||||||
|
filepath.Join(filepath.Dir(opts.state), "a.conf") + `","content":"x\n"}]}`)
|
||||||
|
report := applyAndKeep(context.Background(), opts, raw, &store.Declared{Declaration: raw}, nil)
|
||||||
|
if strings.Contains(report.Refused, "the declaration says") {
|
||||||
|
t.Errorf("the mesh's own flip was refused for its mode: %s", report.Refused)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: a declaration older than what the mesh last said is refused, naming
|
||||||
|
// both — and a declaration carries no order, so one that is merely not the last is refused too,
|
||||||
|
// saying what is missing.
|
||||||
|
func TestAnOlderDeclarationIsRefused(t *testing.T) {
|
||||||
|
opts := stateWithMode(t, "")
|
||||||
|
genesis := []byte(`{"declaration":1,"resources":[{"id":"g","type":"file","path":"/tmp/g","content":"genesis\n"}]}`)
|
||||||
|
since := []byte(`{"declaration":1,"resources":[{"id":"g","type":"file","path":"/tmp/g","content":"since\n"}]}`)
|
||||||
|
other := []byte(`{"declaration":1,"resources":[{"id":"g","type":"file","path":"/tmp/g","content":"other\n"}]}`)
|
||||||
|
if err := store.Save(opts.state, store.State{Genesis: &store.Genesis{Digest: apply.DigestOf(genesis),
|
||||||
|
At: time.Now(), Rewritten: true}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := store.SaveDeclared(store.DeclaredPath(opts.state), store.Declared{Declaration: since,
|
||||||
|
Signature: []byte("unverified here")}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
parsed := func(raw []byte) *declaration.Declaration {
|
||||||
|
d, err := declaration.ParseFileTrusted(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
err := runApply(context.Background(), opts, parsed(genesis), genesis, fromFile)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("the bundle genesis consumed was applied over what the mesh said since")
|
||||||
|
}
|
||||||
|
for _, want := range []string{"older", short(apply.DigestOf(genesis)), short(apply.DigestOf(since))} {
|
||||||
|
if !strings.Contains(err.Error(), want) {
|
||||||
|
t.Errorf("the refusal does not say %q: %v", want, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = runApply(context.Background(), opts, parsed(other), other, fromFile)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("a declaration that is not what the mesh last said was applied")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "no sequence and no issued-at") ||
|
||||||
|
!strings.Contains(err.Error(), short(apply.DigestOf(since))) {
|
||||||
|
t.Errorf("the refusal does not say what is missing and what was last said: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A bundle other than the one genesis consumed: what genesis applied was rewritten for this
|
||||||
|
// machine, so the carried bytes never are.
|
||||||
|
err = runApply(context.Background(), opts, parsed(other), other, fromBundle)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "consumed") ||
|
||||||
|
!strings.Contains(err.Error(), short(apply.DigestOf(genesis))) {
|
||||||
|
t.Errorf("a bundle other than the consumed one was not refused naming it: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: what an apply would change is said before anything is, and
|
||||||
|
// `--dry-run` is that and nothing else — an action listed as the action it is.
|
||||||
|
func TestADryRunChangesNothingAndListsTheActions(t *testing.T) {
|
||||||
|
opts := stateWithMode(t, "")
|
||||||
|
opts.dryRun = true
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
opts.out = out
|
||||||
|
path := filepath.Join(filepath.Dir(opts.state), "a.conf")
|
||||||
|
raw := []byte(`{"declaration":1,"resources":[
|
||||||
|
{"id":"a","type":"file","path":"` + path + `","content":"x\n"},
|
||||||
|
{"id":"init","type":"action","command":["createdb","mesh"],"verify":["psql","-c","select 1"]}]}`)
|
||||||
|
d, err := declaration.ParseFileTrusted(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := runApply(context.Background(), opts, d, raw, fromFile); err != nil {
|
||||||
|
t.Fatalf("a dry run failed: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Error("a dry run wrote the file")
|
||||||
|
}
|
||||||
|
for _, want := range []string{"would change", "create file a", "run action init",
|
||||||
|
"`createdb mesh`", "--dry-run: nothing applied"} {
|
||||||
|
if !strings.Contains(out.String(), want) {
|
||||||
|
t.Errorf("the preview does not say %q:\n%s", want, out.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.Contains(out.String(), "applying:") {
|
||||||
|
t.Errorf("a dry run went on to apply:\n%s", out.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: once the mesh has told this node anything, `reconcile` holds it to
|
||||||
|
// that — never to the bundle the host carries, which genesis consumed.
|
||||||
|
func TestReconcileAfterAControllerDeclarationDoesNotReapplyTheBundle(t *testing.T) {
|
||||||
|
was := builtFor
|
||||||
|
builtFor = "arch"
|
||||||
|
t.Cleanup(func() { builtFor = was })
|
||||||
|
opts := stateWithMode(t, store.ModeAdopted)
|
||||||
|
|
||||||
|
controllerPublic, controllerPrivate, err := ed25519.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
said := []byte(`{"declaration":1,"adoption":{"taken":[]},"resources":[{"id":"a","type":"file","path":"/tmp/a","content":"x\n"}]}`)
|
||||||
|
if err := store.SaveDeclared(store.DeclaredPath(opts.state), store.Declared{Declaration: said,
|
||||||
|
Signature: ed25519.Sign(controllerPrivate, said)}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Told, and unable to prove by whom: refused, and the bundle is not applied in its place.
|
||||||
|
_, _, _, err = reconcileSource(opts)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "not applied in its place") {
|
||||||
|
t.Errorf("a node that cannot prove what it was told fell back to something: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mine, err := identity.Generate("workstation")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mine.Membership = identity.Membership{Broker: "198.51.100.10:5671", Fingerprint: "sha256:0",
|
||||||
|
Signer: controllerPublic, Password: "issued"}
|
||||||
|
if err := identity.Save(identity.Path(opts.state), mine); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
d, raw, from, err := reconcileSource(opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if from != fromDeclared || !bytes.Equal(raw, said) || d.Adoption == nil {
|
||||||
|
t.Errorf("reconcile chose %d with %d bytes, not what the mesh last said", from, len(raw))
|
||||||
|
}
|
||||||
|
|
||||||
|
// And before the mesh has said anything: the bundle, as it always was. A test binary carries
|
||||||
|
// only the placeholder, and asking for it is what proves the path.
|
||||||
|
if err := os.Remove(store.DeclaredPath(opts.state)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, _, _, err = reconcileSource(opts)
|
||||||
|
if !errors.Is(err, bundle.ErrEmpty) {
|
||||||
|
t.Errorf("with nothing said, reconcile did not reach for the carried bundle: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1611,6 +1611,13 @@ func digestOf(content string) string {
|
|||||||
return hex.EncodeToString(sum[:])
|
return hex.EncodeToString(sum[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DigestOf names a declaration by its bytes, exactly as the mesh names what it sends: sha256 of
|
||||||
|
// the raw bytes, hex. The two sides never digest different things.
|
||||||
|
func DigestOf(raw []byte) string {
|
||||||
|
sum := sha256.Sum256(raw)
|
||||||
|
return hex.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|
||||||
// holds is the machine's own ports a resource occupies.
|
// holds is the machine's own ports a resource occupies.
|
||||||
//
|
//
|
||||||
// **What the declaration binds, not what is open.** A machine's open ports are a moving target —
|
// **What the declaration binds, not what is open.** A machine's open ports are a moving target —
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package apply
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
|
"github.com/novox/mesh-host/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A node is adopted or converged, and a declaration says which it is for (novox/hq ADR 0100).
|
||||||
|
//
|
||||||
|
// **The two are not interchangeable, and the host knows which it is.** A converged declaration
|
||||||
|
// carries the mesh's drop-by-default filter and retires the firewall the node was found with; on
|
||||||
|
// an adopted node that closes the machine to everything the predecessor still serves — which is
|
||||||
|
// what happened when an operator ran `reconcile` on an adopted control-node and it applied the
|
||||||
|
// converged genesis bundle (novox/hq issue 104). The host reported "adopted" in every report and
|
||||||
|
// compared nothing. Now it compares, at the point of application, whichever command delivered
|
||||||
|
// the declaration.
|
||||||
|
//
|
||||||
|
// Only the mesh changes a node's mode, and it does so by sending a declaration: the flip arrives
|
||||||
|
// over the link as the first converged declaration after adopted ones (`converge` on the
|
||||||
|
// controller), and the way back as the first adopted one (`adopt`). So a declaration the link
|
||||||
|
// delivers, signed and verified, is the mode's authority and is not checked against the record —
|
||||||
|
// it becomes the record. Everything else — the carried bundle, a file, the kept declaration a
|
||||||
|
// disconnected node re-applies — is held to the mode already recorded.
|
||||||
|
|
||||||
|
// ModeOf says which mode a declaration is for.
|
||||||
|
func ModeOf(d *declaration.Declaration) string {
|
||||||
|
if d.Adoption != nil {
|
||||||
|
return store.ModeAdopted
|
||||||
|
}
|
||||||
|
return store.ModeConverged
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckMode refuses a declaration whose mode is not the one this node has recorded. A node with
|
||||||
|
// no recorded mode — a machine nothing has said a mode to yet — takes either.
|
||||||
|
func CheckMode(known store.State, d *declaration.Declaration) error {
|
||||||
|
if known.Mode == "" || known.Mode == ModeOf(d) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
act := "`converge`"
|
||||||
|
if ModeOf(d) == store.ModeAdopted {
|
||||||
|
act = "`adopt`"
|
||||||
|
}
|
||||||
|
return fmt.Errorf("this node is %s; the declaration says %s — %s declaration is not applied "+
|
||||||
|
"to %s node; %s on the controller is the act that changes it, and it sends the "+
|
||||||
|
"declaration that does", known.Mode, ModeOf(d), article(ModeOf(d)), article(known.Mode), act)
|
||||||
|
}
|
||||||
|
|
||||||
|
func article(mode string) string {
|
||||||
|
if mode == store.ModeAdopted {
|
||||||
|
return "an adopted"
|
||||||
|
}
|
||||||
|
return "a converged"
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package apply
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-host/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: a declaration is refused for the other mode than the node is in,
|
||||||
|
// naming both and the act that changes it; a node no mode has been said to takes either.
|
||||||
|
func TestADeclarationForTheOtherModeIsRefused(t *testing.T) {
|
||||||
|
converged := parse(t, `{"declaration":1,"resources":[{"id":"a","type":"file","path":"/tmp/a","content":"x\n"}]}`)
|
||||||
|
adopted := parse(t, `{"declaration":1,"adoption":{"taken":[]},"resources":[{"id":"a","type":"file","path":"/tmp/a","content":"x\n"}]}`)
|
||||||
|
|
||||||
|
err := CheckMode(store.State{Mode: store.ModeAdopted}, converged)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "this node is adopted; the declaration says converged") ||
|
||||||
|
!strings.Contains(err.Error(), "`converge`") {
|
||||||
|
t.Errorf("a converged declaration on an adopted node: %v", err)
|
||||||
|
}
|
||||||
|
err = CheckMode(store.State{Mode: store.ModeConverged}, adopted)
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "this node is converged; the declaration says adopted") ||
|
||||||
|
!strings.Contains(err.Error(), "`adopt`") {
|
||||||
|
t.Errorf("an adopted declaration on a converged node: %v", err)
|
||||||
|
}
|
||||||
|
if err := CheckMode(store.State{Mode: store.ModeAdopted}, adopted); err != nil {
|
||||||
|
t.Errorf("the node's own mode was refused: %v", err)
|
||||||
|
}
|
||||||
|
if err := CheckMode(store.State{}, converged); err != nil {
|
||||||
|
t.Errorf("a node in no mode yet refused a declaration: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,170 @@
|
|||||||
|
package apply
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
|
"github.com/novox/mesh-host/internal/firewall"
|
||||||
|
"github.com/novox/mesh-host/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A declaration is said before it is done.
|
||||||
|
//
|
||||||
|
// An apply that prints what it did after it did it is a report; what an operator reaching for
|
||||||
|
// `reconcile` under pressure needs is a preview — the base filter that closed an adopted
|
||||||
|
// control-node for forty-five minutes was listed nowhere until it was on disk (novox/hq issue
|
||||||
|
// 104). Converging already previews on the controller; the host's own commands now do too, and
|
||||||
|
// `--dry-run` is the preview and nothing else.
|
||||||
|
|
||||||
|
// Step is one thing an apply would do to this machine.
|
||||||
|
type Step struct {
|
||||||
|
// Verb is create · update · check · hold · run · remove · forget · disable · enable.
|
||||||
|
Verb string `json:"verb"`
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
ID string `json:"id,omitempty"`
|
||||||
|
Target string `json:"target,omitempty"`
|
||||||
|
Why string `json:"why,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Step) String() string {
|
||||||
|
line := fmt.Sprintf("%-8s %-10s %s", s.Verb, s.Type, s.ID)
|
||||||
|
if s.Target != "" && s.Target != s.ID {
|
||||||
|
line += " (" + s.Target + ")"
|
||||||
|
}
|
||||||
|
if s.Why != "" {
|
||||||
|
line += " — " + s.Why
|
||||||
|
}
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plan says what applying a declaration would change, in the order ApplyKeeping would do it,
|
||||||
|
// before anything on the machine is touched.
|
||||||
|
//
|
||||||
|
// **Read from the declaration and the node's own record, not from the machine.** What the
|
||||||
|
// record cannot settle — whether a file recorded here has since drifted, whether a container
|
||||||
|
// runs the spec it was made from — is said as a check, because that is what the apply does: it
|
||||||
|
// reads the machine and corrects it. What the record does settle is said as it is: a resource
|
||||||
|
// with no record is created; a plain file whose declared content differs from what this host
|
||||||
|
// last wrote is updated; a hold is kept; an action is run — an action is a command, and a
|
||||||
|
// preview that folded it into "check" would hide the one kind of step that is not read back
|
||||||
|
// from state.
|
||||||
|
func Plan(d *declaration.Declaration, known store.State, origin string) []Step {
|
||||||
|
var steps []Step
|
||||||
|
|
||||||
|
declared := map[string]bool{}
|
||||||
|
for _, r := range d.Resources {
|
||||||
|
declared[r.Identity()] = true
|
||||||
|
}
|
||||||
|
rec := known.Firewall
|
||||||
|
ufw := rec != nil && rec.Kind == string(firewall.UFW)
|
||||||
|
|
||||||
|
if d.Adoption != nil && ufw && rec.DisabledByMesh {
|
||||||
|
steps = append(steps, Step{Verb: "enable", Type: "firewall", ID: "ufw",
|
||||||
|
Why: "this node is adopted again, so the firewall found on it is put back in force"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// What is held and no longer declared is let go of on paper only (ApplyKeeping does this
|
||||||
|
// before the resources); the file or container itself is left as found.
|
||||||
|
if origin == store.OriginDeclared {
|
||||||
|
for _, h := range known.Held {
|
||||||
|
if declared[h.ID] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
steps = append(steps, Step{Verb: "forget", Type: h.Kind, ID: h.ID, Target: h.Target,
|
||||||
|
Why: "held for " + h.Module + " and no longer declared; left as found"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var protecting, orphans []Step
|
||||||
|
for _, orphan := range known.Orphans(declared, origin) {
|
||||||
|
step := Step{Verb: "remove", Type: orphan.Type, ID: orphan.ID, Target: orphan.Target,
|
||||||
|
Why: "recorded here and no longer declared"}
|
||||||
|
if d.Adoption == nil && strings.HasPrefix(orphan.ID, declaration.AdoptionPrefix) {
|
||||||
|
step.Why = "what protected this node while adopted; removed last, once everything else applied"
|
||||||
|
protecting = append(protecting, step)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
orphans = append(orphans, step)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The guard goes up before anything is removed on an adopted node; on a converged one the
|
||||||
|
// removals go first (novox/hq ADR 0103).
|
||||||
|
var guard, rest []declaration.Resource
|
||||||
|
for _, r := range d.Resources {
|
||||||
|
if d.Adoption != nil && strings.HasPrefix(r.Identity(), guardPrefix) {
|
||||||
|
guard = append(guard, r)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
rest = append(rest, r)
|
||||||
|
}
|
||||||
|
for _, r := range guard {
|
||||||
|
steps = append(steps, planned(r, d, known))
|
||||||
|
}
|
||||||
|
steps = append(steps, orphans...)
|
||||||
|
for _, r := range rest {
|
||||||
|
steps = append(steps, planned(r, d, known))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only a declaration from the mesh converges a node; a bundle or a file never retires the
|
||||||
|
// firewall found here, and neither says so in a plan.
|
||||||
|
if d.Adoption == nil && origin == store.OriginDeclared && ufw && rec.WasActive && !rec.DisabledByMesh {
|
||||||
|
steps = append(steps, Step{Verb: "disable", Type: "firewall", ID: "ufw",
|
||||||
|
Why: "this node converges: retired once the mesh's own filter is loaded, never before; " +
|
||||||
|
"its configuration stays on disk"})
|
||||||
|
}
|
||||||
|
steps = append(steps, protecting...)
|
||||||
|
return steps
|
||||||
|
}
|
||||||
|
|
||||||
|
// planned is what one declared resource would come to.
|
||||||
|
func planned(r declaration.Resource, d *declaration.Declaration, known store.State) Step {
|
||||||
|
step := Step{Type: string(r.Kind()), ID: r.Identity(), Target: r.Target()}
|
||||||
|
|
||||||
|
if d.Adoption != nil {
|
||||||
|
if h, held := known.HeldAt(r.Identity()); held {
|
||||||
|
step.Verb, step.Why = "hold", "found on this machine and kept as it is until "+h.Module+" is taken"
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
if module, untaken := d.Adoption.UntakenModuleOf(r.Identity()); untaken {
|
||||||
|
step.Verb = "create"
|
||||||
|
step.Why = "unless it is found on this machine — then held as it is until " + module + " is taken"
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if a, ok := r.(*declaration.Action); ok {
|
||||||
|
// Named as what it is. Its verify decides whether it runs, and that is read from the
|
||||||
|
// machine, not the record.
|
||||||
|
step.Verb = "run"
|
||||||
|
step.Target = ""
|
||||||
|
step.Why = fmt.Sprintf("an action: `%s`, unless its verify `%s` already passes; if it fails, "+
|
||||||
|
"nothing after it is attempted", strings.Join(a.Command, " "), strings.Join(a.Verify, " "))
|
||||||
|
if a.In != "" {
|
||||||
|
step.Why = "in " + a.In + ", " + step.Why
|
||||||
|
}
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
|
||||||
|
was, recorded := known.Find(r.Identity())
|
||||||
|
if !recorded {
|
||||||
|
step.Verb, step.Why = "create", "no record of it on this node"
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
if want := wouldWrite(r); want != "" && was.Wrote != "" && want != was.Wrote {
|
||||||
|
step.Verb, step.Why = "update", "the declaration changed since this host applied it"
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
step.Verb, step.Why = "check", "recorded here; corrected if this machine drifted from it"
|
||||||
|
return step
|
||||||
|
}
|
||||||
|
|
||||||
|
// wouldWrite is the digest a plain file would be recorded under, or empty where only the apply
|
||||||
|
// can know: a sealed file, one with secrets in it, one written into, one carrying bytes.
|
||||||
|
func wouldWrite(r declaration.Resource) string {
|
||||||
|
f, ok := r.(*declaration.File)
|
||||||
|
if !ok || f.Into != "" || f.Bytes != "" || f.Secret() || len(f.Secrets) > 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return digestOf(f.Content)
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package apply
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
|
"github.com/novox/mesh-host/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: what an apply would change is said before anything is, from the
|
||||||
|
// declaration and the node's record — and an action is named as the action it is.
|
||||||
|
|
||||||
|
func trusted(t *testing.T, raw string) *declaration.Declaration {
|
||||||
|
t.Helper()
|
||||||
|
d, err := declaration.ParseFileTrusted([]byte(raw))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("fixture is not a valid declaration: %v", err)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func verbs(steps []Step) string {
|
||||||
|
var out []string
|
||||||
|
for _, s := range steps {
|
||||||
|
out = append(out, s.Verb+" "+s.ID)
|
||||||
|
}
|
||||||
|
return strings.Join(out, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPlanNamesAnActionAsAnAction(t *testing.T) {
|
||||||
|
d := trusted(t, `{"declaration":1,"resources":[
|
||||||
|
{"id":"init","type":"action","command":["createdb","mesh"],"verify":["psql","-c","select 1"]}]}`)
|
||||||
|
steps := Plan(d, store.State{}, store.OriginCarried)
|
||||||
|
if len(steps) != 1 || steps[0].Verb != "run" {
|
||||||
|
t.Fatalf("an action was planned as %s", verbs(steps))
|
||||||
|
}
|
||||||
|
if !strings.Contains(steps[0].Why, "createdb mesh") || !strings.Contains(steps[0].Why, "nothing after it") {
|
||||||
|
t.Errorf("the plan does not say what the action runs and what failing it means: %q", steps[0].Why)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPlanSaysWhatIsRecordedAndWhatIsNot(t *testing.T) {
|
||||||
|
d := parse(t, `{"declaration":1,"resources":[
|
||||||
|
{"id":"new","type":"file","path":"/tmp/new","content":"a\n"},
|
||||||
|
{"id":"same","type":"file","path":"/tmp/same","content":"b\n"},
|
||||||
|
{"id":"moved","type":"file","path":"/tmp/moved","content":"c\n"},
|
||||||
|
{"id":"sealed","type":"file","path":"/tmp/sealed","sealed":"AAAA","mode":"0600"}]}`)
|
||||||
|
known := store.State{}
|
||||||
|
known.Record(store.Applied{ID: "same", Type: "file", Target: "/tmp/same", Wrote: digestOf("b\n")})
|
||||||
|
known.Record(store.Applied{ID: "moved", Type: "file", Target: "/tmp/moved", Wrote: digestOf("old\n")})
|
||||||
|
known.Record(store.Applied{ID: "sealed", Type: "file", Target: "/tmp/sealed", Wrote: digestOf("secret")})
|
||||||
|
known.Record(store.Applied{ID: "gone", Type: "file", Target: "/tmp/gone"})
|
||||||
|
|
||||||
|
got := verbs(Plan(d, known, store.OriginCarried))
|
||||||
|
want := "remove gone, create new, check same, update moved, check sealed"
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("planned %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPlanSaysTheFirewallAConvergingNodeRetires(t *testing.T) {
|
||||||
|
d := parse(t, `{"declaration":1,"resources":[{"id":"a","type":"file","path":"/tmp/a","content":"x\n"}]}`)
|
||||||
|
known := store.State{Firewall: &store.FoundFirewall{Kind: "ufw", WasActive: true, FoundAt: time.Now()}}
|
||||||
|
known.Record(store.Applied{ID: "adoption.guard.table", Type: "file", Target: "/etc/guard", Origin: store.OriginDeclared})
|
||||||
|
|
||||||
|
got := verbs(Plan(d, known, store.OriginDeclared))
|
||||||
|
// The firewall goes last but for what protected the node, which goes after it.
|
||||||
|
if got != "create a, disable ufw, remove adoption.guard.table" {
|
||||||
|
t.Errorf("a converging node planned %q", got)
|
||||||
|
}
|
||||||
|
// A file or the bundle never retires the firewall found here, and says nothing about it.
|
||||||
|
if got := verbs(Plan(d, known, store.OriginCarried)); strings.Contains(got, "ufw") {
|
||||||
|
t.Errorf("a carried declaration planned to touch the firewall: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAPlanHoldsWhatAnAdoptedNodeFound(t *testing.T) {
|
||||||
|
d := parse(t, `{"declaration":1,"adoption":{"taken":[],"untaken":{"hello-web":["hello-web.page","hello-web.server"]}},
|
||||||
|
"resources":[
|
||||||
|
{"id":"hello-web.page","type":"file","path":"/srv/index.html","content":"x\n"},
|
||||||
|
{"id":"hello-web.server","type":"container","name":"hello-web","image":"example/web@sha256:0000000000000000000000000000000000000000000000000000000000000000"}]}`)
|
||||||
|
known := store.State{}
|
||||||
|
known.RecordHeld(store.Held{ID: "hello-web.page", Module: "hello-web", Kind: "file", Target: "/srv/index.html"})
|
||||||
|
|
||||||
|
steps := Plan(d, known, store.OriginDeclared)
|
||||||
|
if len(steps) != 2 || steps[0].Verb != "hold" || steps[1].Verb != "create" {
|
||||||
|
t.Fatalf("an adopted node planned %s", verbs(steps))
|
||||||
|
}
|
||||||
|
if !strings.Contains(steps[1].Why, "held as it is until hello-web is taken") {
|
||||||
|
t.Errorf("the plan does not say an untaken module's resource is held if found: %q", steps[1].Why)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/novox/mesh-host/internal/apply"
|
"github.com/novox/mesh-host/internal/apply"
|
||||||
"github.com/novox/mesh-host/internal/declaration"
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
@@ -33,8 +34,15 @@ type Runner = apply.Runner
|
|||||||
// What it does not do is the host's own lifecycle bookkeeping — recording a known-good version,
|
// What it does not do is the host's own lifecycle bookkeeping — recording a known-good version,
|
||||||
// clearing the launcher's start counter. Those are facts about a running `mesh-host`, and this is
|
// clearing the launcher's start counter. Those are facts about a running `mesh-host`, and this is
|
||||||
// not one.
|
// not one.
|
||||||
|
//
|
||||||
|
// What it does record is that the bundle was consumed, and in which mode the operator raised
|
||||||
|
// the machine. `raw` is the exact bytes of what is applied — the bundle as rewritten for this
|
||||||
|
// machine, not as carried — and its digest is what `mesh-host reconcile` later holds the carried
|
||||||
|
// bundle against: what genesis applied had its ports, root credentials and adoption rewritten,
|
||||||
|
// so the carried bytes are never it, and applying them on a raised node recreated the store and
|
||||||
|
// loaded the converged filter on an adopted one (novox/hq issue 104).
|
||||||
func ApplyBundle(ctx context.Context, o Options, sys system.System, d *declaration.Declaration,
|
func ApplyBundle(ctx context.Context, o Options, sys system.System, d *declaration.Declaration,
|
||||||
run Runner, say func(string)) (apply.Report, error) {
|
raw []byte, run Runner, say func(string)) (apply.Report, error) {
|
||||||
|
|
||||||
// Refuse a shape this host cannot apply before anything is applied, exactly as `mesh-host`
|
// Refuse a shape this host cannot apply before anything is applied, exactly as `mesh-host`
|
||||||
// does: finding out half way through is the half-configured machine tier 0 exists to prevent.
|
// does: finding out half way through is the half-configured machine tier 0 exists to prevent.
|
||||||
@@ -55,6 +63,16 @@ func ApplyBundle(ctx context.Context, o Options, sys system.System, d *declarati
|
|||||||
func(line string) { say(" " + strings.TrimPrefix(line, " ")) }, refuseSealed,
|
func(line string) { say(" " + strings.TrimPrefix(line, " ")) }, refuseSealed,
|
||||||
apply.KeepIn(filepath.Dir(o.State)))
|
apply.KeepIn(filepath.Dir(o.State)))
|
||||||
|
|
||||||
|
// The bundle is consumed, whichever way the apply went: what is on the machine came from these
|
||||||
|
// bytes, and the carried ones must not be applied over it. The mode is the operator's word at
|
||||||
|
// genesis; the controller records the same and says it in every declaration from then on
|
||||||
|
// (novox/hq ADR 0100).
|
||||||
|
updated.Genesis = &store.Genesis{Digest: apply.DigestOf(raw), At: time.Now().UTC(), Rewritten: true}
|
||||||
|
updated.Mode = store.ModeConverged
|
||||||
|
if o.Adopted {
|
||||||
|
updated.Mode = store.ModeAdopted
|
||||||
|
}
|
||||||
|
|
||||||
// Saved whichever way it went, for the reason `mesh-host` gives: what was applied before a
|
// Saved whichever way it went, for the reason `mesh-host` gives: what was applied before a
|
||||||
// failure is on the machine either way, and a host that did not record it would believe it
|
// failure is on the machine either way, and a host that did not record it would believe it
|
||||||
// owns less than it does and leave that behind for ever.
|
// owns less than it does and leave that behind for ever.
|
||||||
|
|||||||
@@ -3,12 +3,15 @@ package bootstrap
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/novox/mesh-host/internal/apply"
|
||||||
"github.com/novox/mesh-host/internal/declaration"
|
"github.com/novox/mesh-host/internal/declaration"
|
||||||
|
"github.com/novox/mesh-host/internal/store"
|
||||||
"github.com/novox/mesh-host/internal/system"
|
"github.com/novox/mesh-host/internal/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -113,7 +116,7 @@ func TestTheBundleKeepsTheOriginalOfWhatItWritesOver(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
o := Options{State: filepath.Join(dir, "state.json")}
|
o := Options{State: filepath.Join(dir, "state.json")}
|
||||||
report, err := ApplyBundle(context.Background(), o, sys, d, nil, quietly)
|
report, err := ApplyBundle(context.Background(), o, sys, d, nil, nil, quietly)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -127,3 +130,40 @@ func TestTheBundleKeepsTheOriginalOfWhatItWritesOver(t *testing.T) {
|
|||||||
t.Errorf("the kept original is %q (%v)", got, err)
|
t.Errorf("the kept original is %q (%v)", got, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Defends novox/hq issue 104: genesis consumes the bundle, recording the digest of what it applied
|
||||||
|
// and the mode the operator raised the machine in, so the host's own `reconcile` never applies the
|
||||||
|
// carried bytes over it.
|
||||||
|
func TestGenesisConsumesTheBundleAndRecordsTheMode(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
raw := []byte(`{"declaration":1,"resources":[
|
||||||
|
{"id":"a","type":"file","path":"` + filepath.Join(dir, "a.conf") + `","content":"x\n"}]}`)
|
||||||
|
d, err := declaration.ParseFileTrusted(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sys, err := system.For("arch")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, adopted := range []bool{false, true} {
|
||||||
|
o := Options{State: filepath.Join(dir, fmt.Sprintf("state-%v.json", adopted)), Adopted: adopted}
|
||||||
|
if _, err := ApplyBundle(context.Background(), o, sys, d, raw, nil, quietly); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
known, err := store.Load(o.State)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if known.Genesis == nil || known.Genesis.Digest != apply.DigestOf(raw) || !known.Genesis.Rewritten {
|
||||||
|
t.Errorf("adopted=%v: genesis did not record the bundle it consumed: %+v", adopted, known.Genesis)
|
||||||
|
}
|
||||||
|
want := store.ModeConverged
|
||||||
|
if adopted {
|
||||||
|
want = store.ModeAdopted
|
||||||
|
}
|
||||||
|
if known.Mode != want {
|
||||||
|
t.Errorf("adopted=%v: genesis recorded the mode as %q, want %q", adopted, known.Mode, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -566,7 +566,7 @@ func Run(ctx context.Context, o Options, d Deps, say func(string)) (Result, erro
|
|||||||
|
|
||||||
// ---- 4. apply -----------------------------------------------------------------------
|
// ---- 4. apply -----------------------------------------------------------------------
|
||||||
say("apply — raising the foundation")
|
say("apply — raising the foundation")
|
||||||
report, err := ApplyBundle(ctx, o, sys, rewritten.Declaration, d.Run, say)
|
report, err := ApplyBundle(ctx, o, sys, rewritten.Declaration, rewritten.Bundle, d.Run, say)
|
||||||
result.Applied, result.Changed = len(report.Outcomes), report.Changed()
|
result.Applied, result.Changed = len(report.Outcomes), report.Changed()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return result, failed(StepApply, err)
|
return result, failed(StepApply, err)
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ func RetireTheTemporaryControlPlane(ctx context.Context, o Options, sys system.S
|
|||||||
// same state file. What makes this a removal rather than a no-op is that the state file
|
// same state file. What makes this a removal rather than a no-op is that the state file
|
||||||
// records the container as something this installer applied, and the declaration no longer
|
// records the container as something this installer applied, and the declaration no longer
|
||||||
// asks for it.
|
// asks for it.
|
||||||
report, err := ApplyBundle(ctx, o, sys, without, run, say)
|
report, err := ApplyBundle(ctx, o, sys, without, bundle, run, say)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return out, fmt.Errorf(
|
return out, fmt.Errorf(
|
||||||
"%w\n\nThe permanent control plane is running and the temporary one is still here. "+
|
"%w\n\nThe permanent control plane is running and the temporary one is still here. "+
|
||||||
|
|||||||
@@ -80,6 +80,25 @@ func SaveDeclared(path string, d Declared) error {
|
|||||||
return os.Rename(tmp.Name(), path)
|
return os.Rename(tmp.Name(), path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadDeclared reads what was kept without proving it is the mesh's.
|
||||||
|
//
|
||||||
|
// For naming and comparing only — which declaration this node was last told, and what mode it
|
||||||
|
// said — never for applying. A declaration to apply goes through LoadDeclared, which verifies.
|
||||||
|
func ReadDeclared(path string) (Declared, error) {
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
return Declared{}, ErrNothingDeclared
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return Declared{}, fmt.Errorf("this node was told something and cannot read it back: %w", err)
|
||||||
|
}
|
||||||
|
var d Declared
|
||||||
|
if err := json.Unmarshal(raw, &d); err != nil {
|
||||||
|
return Declared{}, fmt.Errorf("what this node was told is unreadable at %s: %w", path, err)
|
||||||
|
}
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
// LoadDeclared reads it back and proves it is still the mesh's.
|
// LoadDeclared reads it back and proves it is still the mesh's.
|
||||||
//
|
//
|
||||||
// Verified against the signing key this node holds, which came from its token. A declaration on
|
// Verified against the signing key this node holds, which came from its token. A declaration on
|
||||||
|
|||||||
@@ -104,6 +104,34 @@ type State struct {
|
|||||||
// Firewall is the firewall found on this machine when it was first adopted, and whether the
|
// Firewall is the firewall found on this machine when it was first adopted, and whether the
|
||||||
// mesh has since retired it (novox/hq ADR 0100). Nil on a node that was never adopted.
|
// mesh has since retired it (novox/hq ADR 0100). Nil on a node that was never adopted.
|
||||||
Firewall *FoundFirewall `json:"firewall,omitempty"`
|
Firewall *FoundFirewall `json:"firewall,omitempty"`
|
||||||
|
|
||||||
|
// Mode is the node's mode as this host last recorded it: adopted or converged (novox/hq ADR
|
||||||
|
// 0100). Empty on a machine nothing has said a mode to yet. Recorded from every declaration
|
||||||
|
// the mesh sends and at genesis from what the operator said, so a declaration that says the
|
||||||
|
// other mode can be refused before it is applied (novox/hq issue 104).
|
||||||
|
Mode string `json:"mode,omitempty"`
|
||||||
|
|
||||||
|
// Genesis is the bundle this host consumed raising the foundation, if it has. Once recorded,
|
||||||
|
// the bundle carried in the binary is not applied again: what genesis applied was rewritten
|
||||||
|
// for this machine, and the mesh has said more since (novox/hq issue 104).
|
||||||
|
Genesis *Genesis `json:"genesis,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Modes a node can be in (novox/hq ADR 0100).
|
||||||
|
const (
|
||||||
|
ModeAdopted = "adopted"
|
||||||
|
ModeConverged = "converged"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Genesis is the bundle a host consumed raising this machine's foundation.
|
||||||
|
type Genesis struct {
|
||||||
|
// Digest is sha256 of the exact bytes applied.
|
||||||
|
Digest string `json:"digest"`
|
||||||
|
At time.Time `json:"at"`
|
||||||
|
// Rewritten is true when the bytes applied were the carried bundle rewritten for this
|
||||||
|
// machine — its foundation ports, root credentials, and adoption — so the bundle the binary
|
||||||
|
// carries is not what was applied.
|
||||||
|
Rewritten bool `json:"rewritten,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FoundFirewall is what the host found filtering this machine, and what it did about it.
|
// FoundFirewall is what the host found filtering this machine, and what it did about it.
|
||||||
|
|||||||
Reference in New Issue
Block a user