E5: answer the node-engine seat's composition-diff
The node-engine serves seat verbs and no tools bundle, so composed values read by resource are a read verb beside composition: per resource, the entries that differ between what runs and a revision or digest of its composition key, environment entries by name, a secret's value masked. Builds: design 47 H2 §813 (3); H-A-349 DQ-12b
This commit is contained in:
@@ -2,9 +2,14 @@ package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
||||
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
engcomp "github.com/novox/mesh-host/internal/compositions"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
@@ -335,3 +340,160 @@ func (a *Agent) revisionsOf(ctx context.Context, key string) ([]compNow, error)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// CompositionDiff answers the node-engine seat's read verb composition-diff (design 47 H2 §813 (3), H-A-349 DQ-12b): one
|
||||
// module's composition that runs beside another revision of its composition key, given by its revision or its digest,
|
||||
// as the entries that differ per resource; environment entries by name, a secret's value masked. It changes nothing.
|
||||
func (a *Agent) CompositionDiff(ctx context.Context, module string, revision int64, digest string) (nodeengine.CompositionDiffAnswer, error) {
|
||||
answer := nodeengine.CompositionDiffAnswer{Node: a.env.Node, Module: module, Resources: []nodeengine.CompositionDiffResource{}}
|
||||
switch {
|
||||
case module == "":
|
||||
return answer, errors.New("name the module, or node for the node's own composition")
|
||||
case (revision == 0) == (digest == ""):
|
||||
return answer, errors.New("give the revision or the digest of the composition to compare with, one of the two")
|
||||
}
|
||||
a.mu.Lock()
|
||||
fp := ""
|
||||
if w := a.wants[module]; w.Present {
|
||||
fp = w.Want.Fp
|
||||
}
|
||||
running, known := a.runningComposition(module)
|
||||
a.mu.Unlock()
|
||||
if fp == "" && known {
|
||||
fp = running.FP
|
||||
}
|
||||
if module != NodeModule && fp == "" {
|
||||
return answer, fmt.Errorf("%s has no want and nothing runs of it on %s, so no composition of it is kept here", module, a.env.Node)
|
||||
}
|
||||
key, err := a.compositionKey(module, fp)
|
||||
if err != nil {
|
||||
return answer, err
|
||||
}
|
||||
revs, err := a.revisionsOf(ctx, key)
|
||||
if err != nil {
|
||||
return answer, fmt.Errorf("the revisions of %s cannot be read: %w", key, err)
|
||||
}
|
||||
var other *compNow
|
||||
for i := range revs {
|
||||
if (revision != 0 && revs[i].Rev == uint64(revision)) || (digest != "" && revs[i].Err == nil && revs[i].Dig == digest) {
|
||||
other = &revs[i]
|
||||
}
|
||||
}
|
||||
if other == nil {
|
||||
return answer, fmt.Errorf("no revision of %s the bucket keeps is the one asked; composition lists those it keeps", key)
|
||||
}
|
||||
answer.Other = &nodeengine.Revision{Key: key, Revision: int64(other.Rev), Digest: other.Dig, Resources: int64(len(other.C.Resources)),
|
||||
Latest: other.Rev == revs[len(revs)-1].Rev}
|
||||
if other.Err != nil {
|
||||
answer.Other.Refused = other.Err.Error()
|
||||
return answer, nil
|
||||
}
|
||||
var from []json.RawMessage
|
||||
if known {
|
||||
from = running.Resources
|
||||
if answer.Running, err = engcomp.DigestOf(running); err != nil {
|
||||
return answer, fmt.Errorf("the composition that runs of %s has no digest: %w", module, err)
|
||||
}
|
||||
}
|
||||
answer.Resources = DiffResources(from, other.C.Resources)
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
// DiffResources is, per resource by its id, the entries that differ from running to other (design 47 H2 §813 (3)): each
|
||||
// top-level field by its name, the environment's entries each by `env.<name>`, and a value the declaration keeps secret
|
||||
// (a file's sealed, vault-sealed or secrets, and the content of a file that has any of them) masked. Resources in id
|
||||
// order, entries in name order; none that is the same in both.
|
||||
func DiffResources(running, other []json.RawMessage) []nodeengine.CompositionDiffResource {
|
||||
was, now := entriesByID(running), entriesByID(other)
|
||||
ids := map[string]bool{}
|
||||
for id := range was {
|
||||
ids[id] = true
|
||||
}
|
||||
for id := range now {
|
||||
ids[id] = true
|
||||
}
|
||||
out := []nodeengine.CompositionDiffResource{}
|
||||
for _, id := range slices.Sorted(maps.Keys(ids)) {
|
||||
w, n := was[id], now[id]
|
||||
names := map[string]bool{}
|
||||
for k := range w.values {
|
||||
names[k] = true
|
||||
}
|
||||
for k := range n.values {
|
||||
names[k] = true
|
||||
}
|
||||
var entries []nodeengine.CompositionDiffEntry
|
||||
for _, name := range slices.Sorted(maps.Keys(names)) {
|
||||
rv, rok := w.values[name]
|
||||
ov, ook := n.values[name]
|
||||
if rok == ook && rv == ov {
|
||||
continue
|
||||
}
|
||||
e := nodeengine.CompositionDiffEntry{Name: name}
|
||||
if w.secret[name] || n.secret[name] {
|
||||
e.Masked = true
|
||||
} else {
|
||||
e.Running, e.Other = rv, ov
|
||||
}
|
||||
entries = append(entries, e)
|
||||
}
|
||||
if len(entries) > 0 {
|
||||
out = append(out, nodeengine.CompositionDiffResource{Resource: id, Entries: entries})
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// resourceEntries are one resource's entries by name, as text, and which of them hold a secret.
|
||||
type resourceEntries struct {
|
||||
values map[string]string
|
||||
secret map[string]bool
|
||||
}
|
||||
|
||||
// secretFields are the fields of a resource the declaration keeps secret (declaration.File.Secret, its Secrets).
|
||||
var secretFields = []string{"sealed", "vault-sealed", "secrets"}
|
||||
|
||||
func entriesByID(resources []json.RawMessage) map[string]resourceEntries {
|
||||
out := map[string]resourceEntries{}
|
||||
for _, raw := range resources {
|
||||
var fields map[string]json.RawMessage
|
||||
if json.Unmarshal(raw, &fields) != nil {
|
||||
continue
|
||||
}
|
||||
var id string
|
||||
_ = json.Unmarshal(fields["id"], &id)
|
||||
r := resourceEntries{values: map[string]string{}, secret: map[string]bool{}}
|
||||
hasSecret := false
|
||||
for _, f := range secretFields {
|
||||
if _, ok := fields[f]; ok {
|
||||
hasSecret = true
|
||||
}
|
||||
}
|
||||
for name, value := range fields {
|
||||
if name == "env" {
|
||||
var env map[string]string
|
||||
if json.Unmarshal(value, &env) == nil {
|
||||
for k, v := range env {
|
||||
r.values["env."+k] = v
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
r.values[name] = entryText(value)
|
||||
if slices.Contains(secretFields, name) || (hasSecret && name == "content") {
|
||||
r.secret[name] = true
|
||||
}
|
||||
}
|
||||
out[id] = r
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// entryText is a field's value as the answer says it: a string as itself, anything else as its JSON.
|
||||
func entryText(value json.RawMessage) string {
|
||||
var s string
|
||||
if json.Unmarshal(value, &s) == nil {
|
||||
return s
|
||||
}
|
||||
return string(value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user