Complete the host's vocabulary: package, container, action
The three shapes the substrate bootstrap needs and the host did not have. Until now tier 1 could not be raised at all -- step 0 is a package, step 1 a container, steps 2 and 3 actions -- so every line of the tier 1 and 2 designs was unbuildable. package -- present, never upgraded, never uninstalled. Removal is "forgotten", not "removed": the host cannot know what else needs the package, uninstalling a container runtime because a declaration changed would stop every container on the node, and the machine may have had it before the mesh saw it. Reporting it removed would claim an effect the host declined to have. container -- identified by a label carrying a digest of the declaration that made it. Comparing every field the runtime reports cannot be done reliably: a runtime normalises, defaults and reorders what it is given, and that is indistinguishable from real drift. There is no in-place update; a container's configuration is fixed at creation, so any change is a replacement, and saying so beats a partial update that leaves the running thing half-declared. This is the one shape the host removes, because it is the one the host created. action -- bundle-only, per ADR 0047. Verify is mandatory and does double duty: it is the idempotency check as well as the read-back. The host does not know what a database is, so "is it already there" is a question only the declaration can ask. `in` runs the action inside a named container, which steps 2 and 3 need. Parse now refuses actions; ParseTrusted permits them. The safe path is the default and the permissive one has to be named. The bundle and a local file handed to a root process use ParseTrusted; the link will use Parse. Also replaced the per-type "fields this type ignores" check with a field-set diff stated as what each type USES. The negative form needs every type revisited whenever a field is added, and the one nobody revisits silently accepts a field it will never read. Images must be pinned by digest (ADR 0046). A bundle naming a tag pins nothing. Verified against a real machine, not only fakes: an action ran and was idempotent on the second apply; an action that exits zero and satisfies nothing fails the apply; a real container was created, labelled, replaced when its declaration changed, exec'd into, and removed; a real package query round- tripped. Each new test was also confirmed to fail on an injected fault -- five injections, each breaking exactly its own test. One existing test changed: a vanished unit is now reported "forgotten" rather than "removed", which is what actually happened.
This commit is contained in:
@@ -26,13 +26,25 @@ const (
|
||||
TypeDirectory Type = "directory"
|
||||
TypeFile Type = "file"
|
||||
TypeService Type = "service"
|
||||
TypePackage Type = "package"
|
||||
TypeContainer Type = "container"
|
||||
TypeAction Type = "action"
|
||||
)
|
||||
|
||||
// known is the whole vocabulary. Anything else is refused.
|
||||
var known = map[Type]bool{
|
||||
TypeDirectory: true,
|
||||
TypeFile: true,
|
||||
TypeService: true,
|
||||
// uses names the fields each type consumes. A field set on a type that is not listed here as
|
||||
// using it is refused.
|
||||
//
|
||||
// Stated as what each type USES rather than as what it ignores. The negative form needs every
|
||||
// type revisited whenever a field is added, and the one nobody revisits is the one that
|
||||
// silently accepts a field it will never read — which is the whole fault this package exists
|
||||
// to prevent.
|
||||
var uses = map[Type]map[string]bool{
|
||||
TypeDirectory: {"path": true, "mode": true},
|
||||
TypeFile: {"path": true, "content": true, "mode": true},
|
||||
TypeService: {"unit": true, "state": true},
|
||||
TypePackage: {"package": true},
|
||||
TypeContainer: {"image": true, "name": true, "env": true, "ports": true, "volumes": true, "args": true},
|
||||
TypeAction: {"command": true, "verify": true, "in": true},
|
||||
}
|
||||
|
||||
// Resource is one thing that should be true of the machine.
|
||||
@@ -54,6 +66,30 @@ type Resource struct {
|
||||
// Unit and State, for a service. State is "running" or "stopped".
|
||||
Unit string `json:"unit,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
|
||||
// Package, for a package: the name this machine's own package manager knows it by.
|
||||
Package string `json:"package,omitempty"`
|
||||
|
||||
// Image and Name, for a container. Image is pinned by digest (novox/hq ADR 0046) — a tag
|
||||
// moves and a digest does not, and a bundle that pinned a tag would not be pinned.
|
||||
Image string `json:"image,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
// Env, Ports, Volumes and Args, for a container. Literal; the host renders nothing.
|
||||
Env map[string]string `json:"env,omitempty"`
|
||||
Ports []string `json:"ports,omitempty"`
|
||||
Volumes []string `json:"volumes,omitempty"`
|
||||
Args []string `json:"args,omitempty"`
|
||||
|
||||
// Command, Verify and In, for an action.
|
||||
//
|
||||
// Verify is not optional and is not a courtesy. An action that runs and reports success
|
||||
// without reading anything back is the fault this repository exists to name, and an action
|
||||
// is the easiest place in the vocabulary to reintroduce it (novox/hq ADR 0047).
|
||||
Command []string `json:"command,omitempty"`
|
||||
Verify []string `json:"verify,omitempty"`
|
||||
// In names a container to run the action inside, when the thing being acted on lives
|
||||
// there. Empty means the machine itself.
|
||||
In string `json:"in,omitempty"`
|
||||
}
|
||||
|
||||
// Declaration is what a machine should be, in the order it should be made so.
|
||||
@@ -85,8 +121,20 @@ func (e *RefusalError) Error() string {
|
||||
strings.Join(e.Problems, "\n - "))
|
||||
}
|
||||
|
||||
// Parse reads a declaration and refuses anything it does not fully understand.
|
||||
func Parse(raw []byte) (*Declaration, error) {
|
||||
// Parse reads a declaration that arrived over the link, and refuses anything it does not fully
|
||||
// understand — including any action, which the link may not carry (novox/hq ADR 0047).
|
||||
func Parse(raw []byte) (*Declaration, error) { return parse(raw, false) }
|
||||
|
||||
// ParseTrusted reads a declaration from a source already as privileged as the host itself: the
|
||||
// bundle it carries, or a file handed to it by someone who is running it as root.
|
||||
//
|
||||
// Actions are permitted here and nowhere else. The asymmetry is deliberate and is the entire
|
||||
// content of ADR 0047: refusing actions from the bundle buys nothing, because whoever built the
|
||||
// bundle built the binary; refusing them from the link buys the bound on what a compromised
|
||||
// control plane can express.
|
||||
func ParseTrusted(raw []byte) (*Declaration, error) { return parse(raw, true) }
|
||||
|
||||
func parse(raw []byte, allowActions bool) (*Declaration, error) {
|
||||
// DisallowUnknownFields is the whole point rather than strictness for its own sake: a
|
||||
// field the host does not know is a thing the control plane believes it asked for.
|
||||
dec := json.NewDecoder(bytes.NewReader(raw))
|
||||
@@ -97,13 +145,13 @@ func Parse(raw []byte) (*Declaration, error) {
|
||||
return nil, &RefusalError{Problems: []string{"not a declaration: " + err.Error()}}
|
||||
}
|
||||
|
||||
if problems := validate(&d); len(problems) > 0 {
|
||||
if problems := validate(&d, allowActions); len(problems) > 0 {
|
||||
return nil, &RefusalError{Problems: problems}
|
||||
}
|
||||
return &d, nil
|
||||
}
|
||||
|
||||
func validate(d *Declaration) []string {
|
||||
func validate(d *Declaration, allowActions bool) []string {
|
||||
var problems []string
|
||||
|
||||
if d.Version != Version {
|
||||
@@ -138,32 +186,31 @@ func validate(d *Declaration) []string {
|
||||
seen[r.ID] = i
|
||||
}
|
||||
|
||||
if !known[r.Type] {
|
||||
if _, ok := uses[r.Type]; !ok {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s: unknown type %q. This host understands %s", where, r.Type, vocabulary()))
|
||||
continue
|
||||
}
|
||||
problems = append(problems, validateResource(where, r)...)
|
||||
problems = append(problems, validateResource(where, r, allowActions)...)
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
func validateResource(where string, r Resource) []string {
|
||||
var problems []string
|
||||
func validateResource(where string, r Resource, allowActions bool) []string {
|
||||
problems := unusedBy(where, r)
|
||||
|
||||
switch r.Type {
|
||||
case TypeDirectory:
|
||||
if r.Path == "" {
|
||||
problems = append(problems, where+": a directory needs a path")
|
||||
}
|
||||
problems = append(problems, checkMode(where, r.Mode)...)
|
||||
problems = append(problems, unusedBy(where, r, "unit", r.Unit, "state", r.State, "content", r.Content)...)
|
||||
|
||||
case TypeFile:
|
||||
if r.Path == "" {
|
||||
problems = append(problems, where+": a file needs a path")
|
||||
}
|
||||
problems = append(problems, checkMode(where, r.Mode)...)
|
||||
problems = append(problems, unusedBy(where, r, "unit", r.Unit, "state", r.State)...)
|
||||
|
||||
case TypeService:
|
||||
if r.Unit == "" {
|
||||
@@ -173,22 +220,101 @@ func validateResource(where string, r Resource) []string {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s: state %q; a service is \"running\" or \"stopped\"", where, r.State))
|
||||
}
|
||||
problems = append(problems, unusedBy(where, r, "path", r.Path, "content", r.Content, "mode", r.Mode)...)
|
||||
|
||||
case TypePackage:
|
||||
if r.Package == "" {
|
||||
problems = append(problems, where+": a package needs a package name")
|
||||
}
|
||||
|
||||
case TypeContainer:
|
||||
if r.Name == "" {
|
||||
problems = append(problems, where+": a container needs a name")
|
||||
}
|
||||
problems = append(problems, checkImage(where, r.Image)...)
|
||||
|
||||
case TypeAction:
|
||||
// The whole reason an action is bounded rather than forbidden (novox/hq ADR 0047).
|
||||
if !allowActions {
|
||||
problems = append(problems, where+
|
||||
": an action arrived over the link, and the link may not carry one. The host "+
|
||||
"applies declarations of known shape; a command to run is not one. A bundle "+
|
||||
"may carry an action because it arrives with the binary — anyone able to put "+
|
||||
"a hostile action there could have put it in the host itself")
|
||||
break
|
||||
}
|
||||
if len(r.Command) == 0 {
|
||||
problems = append(problems, where+": an action needs a command")
|
||||
}
|
||||
if len(r.Verify) == 0 {
|
||||
problems = append(problems, where+
|
||||
": an action needs a verify. An action that runs and reports success without "+
|
||||
"reading anything back is the fault this host exists to prevent, and verify is "+
|
||||
"also how the host knows whether the action is already done")
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// checkImage insists on a digest.
|
||||
//
|
||||
// A tag moves and a digest does not. The bundle's whole claim is that what it names is exact
|
||||
// (novox/hq ADR 0046), and a bundle pinning `postgres:17` pins nothing — it names whatever
|
||||
// that tag points at on the day the host happens to run.
|
||||
func checkImage(where, image string) []string {
|
||||
if image == "" {
|
||||
return []string{where + ": a container needs an image"}
|
||||
}
|
||||
name, digest, found := strings.Cut(image, "@")
|
||||
if !found || name == "" {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s: image %q is not pinned. Write it as name@sha256:... — a tag moves, and a "+
|
||||
"bundle that pinned a tag would not be pinned", where, image)}
|
||||
}
|
||||
if !strings.HasPrefix(digest, "sha256:") || len(digest) != len("sha256:")+64 {
|
||||
return []string{fmt.Sprintf(
|
||||
"%s: image digest %q is not a sha256 digest", where, digest)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// setFields names every field carried on this resource, other than its identity and type.
|
||||
func setFields(r Resource) []string {
|
||||
var set []string
|
||||
add := func(name string, populated bool) {
|
||||
if populated {
|
||||
set = append(set, name)
|
||||
}
|
||||
}
|
||||
add("path", r.Path != "")
|
||||
add("content", r.Content != "")
|
||||
add("mode", r.Mode != "")
|
||||
add("unit", r.Unit != "")
|
||||
add("state", r.State != "")
|
||||
add("package", r.Package != "")
|
||||
add("image", r.Image != "")
|
||||
add("name", r.Name != "")
|
||||
add("env", len(r.Env) > 0)
|
||||
add("ports", len(r.Ports) > 0)
|
||||
add("volumes", len(r.Volumes) > 0)
|
||||
add("args", len(r.Args) > 0)
|
||||
add("command", len(r.Command) > 0)
|
||||
add("verify", len(r.Verify) > 0)
|
||||
add("in", r.In != "")
|
||||
sort.Strings(set)
|
||||
return set
|
||||
}
|
||||
|
||||
// unusedBy refuses a field this type does not use.
|
||||
//
|
||||
// A field set and ignored is the fault this package exists to prevent, in miniature: the
|
||||
// control plane believes it asked for something the host will never do.
|
||||
func unusedBy(where string, r Resource, pairs ...string) []string {
|
||||
func unusedBy(where string, r Resource) []string {
|
||||
var problems []string
|
||||
for i := 0; i+1 < len(pairs); i += 2 {
|
||||
if pairs[i+1] != "" {
|
||||
for _, name := range setFields(r) {
|
||||
if !uses[r.Type][name] {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s: a %s does not use %q, and it is set. Refused rather than ignored",
|
||||
where, r.Type, pairs[i]))
|
||||
where, r.Type, name))
|
||||
}
|
||||
}
|
||||
return problems
|
||||
@@ -213,7 +339,7 @@ func checkMode(where, mode string) []string {
|
||||
|
||||
func vocabulary() string {
|
||||
var names []string
|
||||
for t := range known {
|
||||
for t := range uses {
|
||||
names = append(names, string(t))
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
@@ -156,3 +156,103 @@ func TestARefusalSaysNothingWasApplied(t *testing.T) {
|
||||
func TestAnEmptyDeclarationIsAMistake(t *testing.T) {
|
||||
refusalFor(t, `{"declaration":1,"resources":[]}`)
|
||||
}
|
||||
|
||||
// --- the vocabulary the substrate bootstrap needs (novox/hq 07-the-substrate.md) ---
|
||||
|
||||
func TestAnActionOverTheLinkIsRefused(t *testing.T) {
|
||||
// novox/hq ADR 0047. The link may push declarations of known shape and never a command to
|
||||
// run. This is the boundary the whole security argument rests on, so it is asserted
|
||||
// directly rather than inferred from the type list.
|
||||
raw := []byte(`{"declaration":1,"resources":[
|
||||
{"id":"schema","type":"action","command":["psql","-f","x.sql"],"verify":["psql","-c","select 1"]}
|
||||
]}`)
|
||||
|
||||
if _, err := Parse(raw); err == nil {
|
||||
t.Fatal("an action arriving over the link was accepted")
|
||||
} else if !strings.Contains(err.Error(), "the link may not carry one") {
|
||||
t.Errorf("refused for the wrong reason: %v", err)
|
||||
}
|
||||
|
||||
// And the same bytes from the bundle are fine — the asymmetry IS the decision.
|
||||
if _, err := ParseTrusted(raw); err != nil {
|
||||
t.Errorf("the bundle may carry an action, and this one was refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnActionWithoutVerifyIsRefused(t *testing.T) {
|
||||
// An action that runs and reports success without reading anything back is the fault this
|
||||
// host exists to prevent. Verify is also the idempotency check, so an action without one
|
||||
// cannot be applied twice safely either.
|
||||
_, err := ParseTrusted([]byte(`{"declaration":1,"resources":[
|
||||
{"id":"schema","type":"action","command":["psql","-f","x.sql"]}
|
||||
]}`))
|
||||
if err == nil {
|
||||
t.Fatal("an action with no verify was accepted")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "needs a verify") {
|
||||
t.Errorf("refused for the wrong reason: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnImageMustBePinnedByDigest(t *testing.T) {
|
||||
// novox/hq ADR 0046: reproducibility comes from pinning the identity of a thing. A bundle
|
||||
// naming a tag pins nothing — it names whatever that tag points at on the day it runs.
|
||||
for _, image := range []string{
|
||||
"postgres:17",
|
||||
"postgres",
|
||||
"postgres@sha256:short",
|
||||
"@sha256:0000000000000000000000000000000000000000000000000000000000000000",
|
||||
} {
|
||||
_, err := ParseTrusted([]byte(`{"declaration":1,"resources":[
|
||||
{"id":"store","type":"container","name":"store","image":"` + image + `"}
|
||||
]}`))
|
||||
if err == nil {
|
||||
t.Errorf("image %q was accepted and is not pinned", image)
|
||||
}
|
||||
}
|
||||
|
||||
good := "postgres@sha256:" + strings.Repeat("a", 64)
|
||||
if _, err := ParseTrusted([]byte(`{"declaration":1,"resources":[
|
||||
{"id":"store","type":"container","name":"store","image":"` + good + `"}
|
||||
]}`)); err != nil {
|
||||
t.Errorf("a properly pinned image was refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAFieldTheNewTypesDoNotUseIsRefused(t *testing.T) {
|
||||
// The field-set check must cover the types added last, not only the three it was written
|
||||
// for. A package that carries a `content` is a control plane believing it asked for
|
||||
// something that will never happen.
|
||||
for _, body := range []string{
|
||||
`{"id":"p","type":"package","package":"docker","content":"x"}`,
|
||||
`{"id":"p","type":"package","package":"docker","image":"x"}`,
|
||||
`{"id":"c","type":"container","name":"n","image":"i@sha256:` + strings.Repeat("a", 64) + `","unit":"x.service"}`,
|
||||
`{"id":"a","type":"action","command":["x"],"verify":["y"],"path":"/tmp/x"}`,
|
||||
} {
|
||||
_, err := ParseTrusted([]byte(`{"declaration":1,"resources":[` + body + `]}`))
|
||||
if err == nil {
|
||||
t.Errorf("a resource carrying a field its type does not use was accepted: %s", body)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Refused rather than ignored") {
|
||||
t.Errorf("refused for the wrong reason: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheVocabularyIsTheSixShapesTheBootstrapNeeds(t *testing.T) {
|
||||
// novox/hq 07-the-substrate.md names six shapes and the bootstrap uses all of them.
|
||||
// Asserted so that removing one is a failing test rather than a discovery during a
|
||||
// first-node install.
|
||||
for _, want := range []Type{
|
||||
TypeDirectory, TypeFile, TypeService, TypePackage, TypeContainer, TypeAction,
|
||||
} {
|
||||
if _, ok := uses[want]; !ok {
|
||||
t.Errorf("the host no longer speaks %q", want)
|
||||
}
|
||||
}
|
||||
if len(uses) != 6 {
|
||||
t.Errorf("the vocabulary is %d shapes; every addition widens what a compromised "+
|
||||
"control plane can express, so a change here is a decision: %s", len(uses), vocabulary())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user