cantabile_edge, cantabile_modbus, cantabile_eebus 0.1.676: full source

This commit is contained in:
Setti Solutions S.R.L committed 2026-11-04 10:33:14 +02:00
1 parent 3edd015039
commit d48b9aff87
32 files changed
+4633 -133

No files matched your search

+14 -3
View File
@@ -10,7 +10,9 @@ package main
// a command). Fleet
// sends one as a signed command, `tariff` or `model`, with args
// {"bundle": base64 of the bundle's JSON bytes, "sig": base64 of their
// signature with the BUNDLE key}. The command itself is checked with the
// signature with the BUNDLE key}, or, since 2026-11-04 to a link that says
// so in its heartbeat (bundle_text), {"bundle_text": the JSON itself, "sig"}:
// one base64 layer less on the wire (fleet bundlecap.go). The command itself is checked with the
// command key (checkCommand); here the bundle's own signature, its kind, site
// and expiry, and that it is not older than the one the agent has. Then the
// JSON goes into the agent's state directory (tariff.json, model.json),
@@ -37,7 +39,10 @@ var devicesPolicy = func() {
}
}
const bundleMax = 256 << 10 // a tariff is a few kB; a model's data well under this
// bundleMax: the largest bundle taken, reported in the heartbeat
// (bundle_max; fleet sends an older link at most 136 KiB). 512 KiB as text is
// ~0.7 MiB on the wire, under NATS's 1 MiB and any 1 MiB WebSocket proxy.
const bundleMax = 512 << 10
// bundleCert: the bundle key's certificate (CANTABILE_BUNDLE_CERT). Read for
// each bundle: a box may get it after its link started.
@@ -60,7 +65,13 @@ type bundleHead struct {
// expired and not issued in the future.
func checkBundle(key crypto.PublicKey, kind, site string, args map[string]string, now time.Time) ([]byte, bundleHead, error) {
var h bundleHead
data, err1 := base64.StdEncoding.DecodeString(args["bundle"])
var data []byte
var err1 error
if t, ok := args["bundle_text"]; ok {
data = []byte(t)
} else {
data, err1 = base64.StdEncoding.DecodeString(args["bundle"])
}
sig, err2 := base64.StdEncoding.DecodeString(args["sig"])
switch {
case err1 != nil || err2 != nil || len(data) == 0 || len(sig) == 0:
+58
View File
@@ -0,0 +1,58 @@
// Copyright © 2026 Setti Solutions S.R.L, www.setti.md. All rights reserved.
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/base64"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A bundle as text (fleet bundlecap.go, for a link that reports bundle_text)
// is taken like the base64 one, the signature over the same bytes; up to
// bundleMax (reported in the heartbeat), larger refused; a tampered text refused.
func TestBundlesAsTextAndLarge(t *testing.T) {
k, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
site := strings.Repeat("ab", 16)
now := time.Now()
l := &link{id: site, cfg: config{BundleCert: certFile(t, k), AgentState: t.TempDir()}}
model := func(id string, issued int64, pad int) map[string]any {
return map[string]any{"v": 1, "kind": "model", "id": id, "site": site, "issued": issued,
"expires": now.Add(time.Hour).Unix(), "pad": strings.Repeat("x", pad)}
}
text := func(b map[string]any) map[string]string {
a := bundleArgs(t, k, b)
data, _ := base64.StdEncoding.DecodeString(a["bundle"])
return map[string]string{"bundle_text": string(data), "sig": a["sig"]}
}
cmd := func(args map[string]string) command { return command{ID: "c", Cmd: "model", Args: args} }
if ok, msg := l.bundleCmd(cmd(text(model("m1", now.Unix(), 400<<10))), now); !ok { // over the old 256 KiB
t.Fatalf("a 400 KiB model as text: %s", msg)
}
if b, _ := os.ReadFile(filepath.Join(l.cfg.AgentState, "model.json")); len(b) < 400<<10 {
t.Fatalf("model.json: %d bytes", len(b))
}
if ok, msg := l.bundleCmd(cmd(bundleArgs(t, k, model("m2", now.Unix()+1, 300<<10))), now); !ok {
t.Fatalf("a 300 KiB model as base64: %s", msg)
}
if ok, msg := l.bundleCmd(cmd(text(model("m3", now.Unix()+2, bundleMax))), now); ok || !strings.Contains(msg, "larger than") {
t.Errorf("over bundleMax: %v %q", ok, msg)
}
a := text(model("m4", now.Unix()+3, 10))
a["bundle_text"] = strings.Replace(a["bundle_text"], `"m4"`, `"m5"`, 1)
if ok, msg := l.bundleCmd(cmd(a), now); ok || !strings.HasSuffix(msg, "bad bundle signature") {
t.Errorf("tampered text: %v %q", ok, msg)
}
t.Setenv("CANTABILE_HUB", "0")
t.Setenv("CANTABILE_CONSENT_FILE", filepath.Join(t.TempDir(), "none.json"))
hb := (&link{started: now, cfg: config{AgentState: t.TempDir()}, loc: &local{}}).heartbeat()
if hb["bundle_max"] != bundleMax || hb["bundle_text"] != true || bundleMax < 512<<10 {
t.Errorf("heartbeat: %v %v", hb["bundle_max"], hb["bundle_text"])
}
}
+2
View File
@@ -756,6 +756,8 @@ func (l *link) heartbeat() map[string]any {
if p := l.pathInfo(); p != nil {
hb["path"] = p
}
// the bundles this link takes (bundles.go): as large, and as text
hb["bundle_max"], hb["bundle_text"] = bundleMax, true
c := consents(env("CANTABILE_CONSENT_FILE",
map[bool]string{true: "/data/cantabile/edge/consent.json", false: "/data/state/consent.json"}[isHub()]))
hb["consents"] = c