hub v0.181.0 (not deployed): root files are 'behind' only when the files differ (R-1392, decision 304); Go 1.27.4 (R-1391)
gates / gates (push) Successful in 1m55s
gates / gates (push) Successful in 1m55s
This commit is contained in:
1 parent
561a4d98e9
commit
83316fc986
15 files changed
+442
-11
No files matched your search
@@ -0,0 +1,142 @@
|
||||
// Package bundlefiles answers one question for the "root files" verdict (R-1392, `09` §3 decision 304): do two config
|
||||
// bundles hold THE SAME FILES under a different label?
|
||||
//
|
||||
// WHY. The agent's config bundle (`felhom-config-bundle.json`, `11` §5.4.2) carries its own `agent_version`, so its
|
||||
// sha256 moves with every agent release — also when `configs/` did not change and the release (rightly) sent no bundle
|
||||
// job. The hub compared that one sha256, so every such release turned every box amber and, 7 days later, mailed
|
||||
// `os_config_bundle_behind` about files that were the vouched ones (measured 2026-11-14: 0.172.1 vs 0.173.0).
|
||||
//
|
||||
// HOW. A bundle is fetched from the package registry by its version, and it counts ONLY when the bytes hash to the
|
||||
// sha256 the caller already trusts (the vouched one, or the one the box reports). Then everything but the label
|
||||
// (`agent_version`) is hashed: the format and the whole `files` list (path, mode, check, policy, sha256, content).
|
||||
//
|
||||
// FAIL DIRECTION. Anything that cannot be read, verified or parsed answers "not the same" — the old verdict (behind,
|
||||
// amber, the alarm after its wait). A registry that is away never makes a box look current.
|
||||
package bundlefiles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Fetcher returns the published bundle file of one agent version.
|
||||
type Fetcher func(ctx context.Context, version string) ([]byte, error)
|
||||
|
||||
var (
|
||||
versionRE = regexp.MustCompile(`^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$`) // the builder's own rule
|
||||
shaRE = regexp.MustCompile(`^[0-9a-f]{64}$`)
|
||||
)
|
||||
|
||||
const (
|
||||
retryAfter = 10 * time.Minute // a failed read is not asked again sooner (the System page calls this too)
|
||||
fetchTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type entry struct {
|
||||
digest string // "" = failed
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// Resolver caches the files digest per bundle sha256. Safe for concurrent use.
|
||||
type Resolver struct {
|
||||
Fetch Fetcher
|
||||
Logger *log.Logger
|
||||
Now func() time.Time
|
||||
|
||||
mu sync.Mutex
|
||||
cache map[string]entry
|
||||
}
|
||||
|
||||
func (r *Resolver) now() time.Time {
|
||||
if r.Now != nil {
|
||||
return r.Now()
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (r *Resolver) logf(format string, a ...any) {
|
||||
if r.Logger != nil {
|
||||
r.Logger.Printf(format, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// FilesDigest hashes a bundle's content without its label. It errors on a bundle with no `files` list.
|
||||
func FilesDigest(bundle []byte) (string, error) {
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(bundle, &body); err != nil {
|
||||
return "", fmt.Errorf("not JSON: %w", err)
|
||||
}
|
||||
files, ok := body["files"].([]any)
|
||||
if !ok || len(files) == 0 {
|
||||
return "", errors.New("no files list")
|
||||
}
|
||||
delete(body, "agent_version")
|
||||
canon, err := json.Marshal(body) // map keys are sorted by encoding/json
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := sha256.Sum256(canon)
|
||||
return hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func (r *Resolver) digest(version, sha string) string {
|
||||
if r == nil || r.Fetch == nil || !versionRE.MatchString(version) || !shaRE.MatchString(sha) {
|
||||
return ""
|
||||
}
|
||||
r.mu.Lock()
|
||||
if e, ok := r.cache[sha]; ok && (e.digest != "" || r.now().Sub(e.at) < retryAfter) {
|
||||
r.mu.Unlock()
|
||||
return e.digest
|
||||
}
|
||||
r.mu.Unlock()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
|
||||
defer cancel()
|
||||
d := ""
|
||||
data, err := r.Fetch(ctx, version)
|
||||
switch {
|
||||
case err != nil:
|
||||
r.logf("[WARN] [bundlefiles] bundle %s: cannot be read (%v) — its files are not compared", version, err)
|
||||
default:
|
||||
sum := sha256.Sum256(data)
|
||||
if got := hex.EncodeToString(sum[:]); got != sha {
|
||||
r.logf("[WARN] [bundlefiles] bundle %s: the published file is sha256 %s, expected %s — its files are not compared", version, got, sha)
|
||||
} else if d, err = FilesDigest(data); err != nil {
|
||||
r.logf("[WARN] [bundlefiles] bundle %s: %v — its files are not compared", version, err)
|
||||
d = ""
|
||||
}
|
||||
}
|
||||
r.mu.Lock()
|
||||
if r.cache == nil {
|
||||
r.cache = map[string]entry{}
|
||||
}
|
||||
r.cache[sha] = entry{digest: d, at: r.now()}
|
||||
r.mu.Unlock()
|
||||
return d
|
||||
}
|
||||
|
||||
// Same reports whether the box's bundle and the vouched bundle hold the same files. Equal sha256s are the caller's
|
||||
// business (nothing is fetched for them); false on anything unread.
|
||||
func (r *Resolver) Same(boxVersion, boxSHA, vouchedVersion, vouchedSHA string) bool {
|
||||
if boxSHA == vouchedSHA {
|
||||
return boxSHA != ""
|
||||
}
|
||||
a := r.digest(vouchedVersion, vouchedSHA)
|
||||
if a == "" {
|
||||
return false
|
||||
}
|
||||
b := r.digest(boxVersion, boxSHA)
|
||||
same := b != "" && a == b
|
||||
if same {
|
||||
r.logf("[DEBUG] [bundlefiles] bundle %s holds the same files as the vouched %s (only the label differs)", boxVersion, vouchedVersion)
|
||||
}
|
||||
return same
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package bundlefiles
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func bundle(version, wrapperSHA string) []byte {
|
||||
return []byte(`{
|
||||
"agent_version": "` + version + `",
|
||||
"files": [
|
||||
{"check": "", "content_b64": "QQ==", "mode": "0o755", "path": "/usr/local/sbin/felhom-os-apply", "policy": "replace", "sha256": "` + wrapperSHA + `"}
|
||||
],
|
||||
"format": 1
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
||||
func sha(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) }
|
||||
|
||||
type reg struct {
|
||||
files map[string][]byte
|
||||
calls int
|
||||
err error
|
||||
}
|
||||
|
||||
func (g *reg) fetch(_ context.Context, v string) ([]byte, error) {
|
||||
g.calls++
|
||||
if g.err != nil {
|
||||
return nil, g.err
|
||||
}
|
||||
b, ok := g.files[v]
|
||||
if !ok {
|
||||
return nil, errors.New("404")
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// R-1392: the three verdicts, and the fail direction.
|
||||
func TestSame(t *testing.T) {
|
||||
old, relabel, changed := bundle("0.172.1", "aa"), bundle("0.173.0", "aa"), bundle("0.174.0", "bb")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel, "0.174.0": changed}}
|
||||
r := &Resolver{Fetch: g.fetch}
|
||||
|
||||
if sha(old) == sha(relabel) {
|
||||
t.Fatal("fixture: the label must move the bundle's sha256")
|
||||
}
|
||||
if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("same files under a new label must be the same")
|
||||
}
|
||||
if r.Same("0.172.1", sha(old), "0.174.0", sha(changed)) {
|
||||
t.Fatal("one file differs: must NOT be the same")
|
||||
}
|
||||
if r.Same("none", "", "0.173.0", sha(relabel)) {
|
||||
t.Fatal("a box that never got a bundle must NOT be the same")
|
||||
}
|
||||
if r.Same("", "", "", "") {
|
||||
t.Fatal("nothing vouched and nothing on the box is not 'the same'")
|
||||
}
|
||||
// The cache: every bundle was read once.
|
||||
n := g.calls
|
||||
r.Same("0.172.1", sha(old), "0.173.0", sha(relabel))
|
||||
if g.calls != n {
|
||||
t.Fatalf("a verified bundle was read again (%d -> %d)", n, g.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// A bundle counts only when its bytes hash to the sha256 the caller trusts.
|
||||
func TestSame_UnverifiedBytesNeverCount(t *testing.T) {
|
||||
old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}}
|
||||
r := &Resolver{Fetch: g.fetch}
|
||||
wrong := sha([]byte("something else"))
|
||||
if r.Same("0.172.1", wrong, "0.173.0", sha(relabel)) {
|
||||
t.Fatal("the box's sha256 does not match the published file: must NOT be the same")
|
||||
}
|
||||
if r.Same("0.172.1", sha(old), "0.173.0", wrong) {
|
||||
t.Fatal("the vouched sha256 does not match the published file: must NOT be the same")
|
||||
}
|
||||
if r.Same("../../x", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("a version that is not a version must never reach the registry")
|
||||
}
|
||||
}
|
||||
|
||||
// A registry that is away answers "not the same", is not hammered, and is asked again later.
|
||||
func TestSame_RegistryAway(t *testing.T) {
|
||||
old, relabel := bundle("0.172.1", "aa"), bundle("0.173.0", "aa")
|
||||
g := ®{files: map[string][]byte{"0.172.1": old, "0.173.0": relabel}, err: errors.New("dial tcp: refused")}
|
||||
now := time.Date(2026, 11, 14, 10, 0, 0, 0, time.UTC)
|
||||
r := &Resolver{Fetch: g.fetch, Now: func() time.Time { return now }}
|
||||
if r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("unreadable must NOT be the same")
|
||||
}
|
||||
n := g.calls
|
||||
r.Same("0.172.1", sha(old), "0.173.0", sha(relabel))
|
||||
if g.calls != n {
|
||||
t.Fatal("a failed read was retried at once")
|
||||
}
|
||||
g.err = nil
|
||||
now = now.Add(11 * time.Minute)
|
||||
if !r.Same("0.172.1", sha(old), "0.173.0", sha(relabel)) {
|
||||
t.Fatal("the registry is back after the retry wait: must be the same")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesDigest_NoFilesIsAnError(t *testing.T) {
|
||||
for _, b := range []string{`{}`, `{"files":[]}`, `{"files":"x"}`, `not json`} {
|
||||
if _, err := FilesDigest([]byte(b)); err == nil {
|
||||
t.Fatalf("%q: no error", b)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user