carlos / platform Public

host: the lease identity carries the kernel boot id (#451)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 782003c716273682cd1ffc453dfccf84b7cbb982 parent 3c7cee2
2 files changed, +66 −1
  • internal/host/haself.go +17 −1
  • internal/host/haself_test.go +49 −0
diff --git a/internal/host/haself.go b/internal/host/haself.go
index 71930b2ac..a40ddcede 100644
--- a/internal/host/haself.go
+++ b/internal/host/haself.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
+ "os"
"strings"
"time"
@@ -25,7 +26,22 @@ var haIMDSClient = &http.Client{Timeout: haIdentityRead}
// label is its resolved group reference.
func haSelfNow(getenv func(string) string, client *http.Client) hafresh.Self {
iid, account, region, _ := readIdentity(client, imdsBase)
- return hafresh.Self{Ref: metricsBoxLabel(getenv, readMachineID()), IID: iid, Account: account, Region: region}
+ return hafresh.Self{Ref: metricsBoxLabel(getenv, readMachineID()), IID: iid, Account: account, Region: region, Boot: readBootID(bootIDPath)}
+}
+
+// bootIDPath is where the kernel publishes this boot's id; a test points
+// it elsewhere.
+var bootIDPath = "/proc/sys/kernel/random/boot_id"
+
+// readBootID is the kernel boot id at path, trimmed, "" when it can't be
+// read (off Linux, say). A legacy lock names it, so the next boot can tell
+// its own earlier boot's lock from a live one (#451).
+func readBootID(path string) string {
+ b, err := os.ReadFile(path)
+ if err != nil {
+ return ""
+ }
+ return strings.TrimSpace(string(b))
}
// startHASelfRetry is RunAgent's rule for the retry: a startup identity
diff --git a/internal/host/haself_test.go b/internal/host/haself_test.go
index 771cebf81..d5d8f5807 100644
--- a/internal/host/haself_test.go
+++ b/internal/host/haself_test.go
@@ -3,6 +3,8 @@ package host
import (
"context"
"net/http"
+ "os"
+ "path/filepath"
"slices"
"testing"
"time"
@@ -36,6 +38,7 @@ func TestAgentEntrySetsOwnerBeforeRun(t *testing.T) {
// The box's reference is its metrics label; its instance id, account and
// region come from one identity-document read, "" when that read fails.
func TestHASelfNowNamesTheBox(t *testing.T) {
+ withBootID(t, "") // these assert the IMDS identity; the boot has its own test
f := &fakeIMDS{doc: `{"accountId":"111122223333","instanceId":"i-0a61f580df1acfa65","region":"eu-west-1"}`}
getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"})
want := metricsBoxLabel(getenv, readMachineID())
@@ -70,6 +73,7 @@ func (f *failingFor) RoundTrip(r *http.Request) (*http.Response, error) {
// until it answers, then set once, with the reference; the retry ends
// there, or when its context does.
func TestHASelfRetrySetsALateInstanceID(t *testing.T) {
+ withBootID(t, "") // these assert the IMDS identity; the boot has its own test
f := &failingFor{fakeIMDS: &fakeIMDS{doc: `{"accountId":"111122223333","instanceId":"i-0abc","region":"eu-west-1"}`}, n: 2}
getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"})
var set []hafresh.Self
@@ -110,6 +114,7 @@ func (d *docSwitch) RoundTrip(r *http.Request) (*http.Response, error) {
// A document with an instance id but no region is not an identity yet:
// the retry waits for the tick that brings all of it, and sets it once.
func TestHASelfRetryWaitsForTheWholeIdentity(t *testing.T) {
+ withBootID(t, "") // these assert the IMDS identity; the boot has its own test
d := &docSwitch{fakeIMDS: &fakeIMDS{}, docs: []string{
`{"accountId":"111122223333","instanceId":"i-0abc"}`,
`{"accountId":"111122223333","instanceId":"i-0abc"}`,
@@ -138,6 +143,7 @@ func TestHASelfRetryWaitsForTheWholeIdentity(t *testing.T) {
// starts the retry, and a later complete document reaches the lease
// identity. A complete startup identity starts nothing.
func TestAgentRetriesAPartialIdentity(t *testing.T) {
+ withBootID(t, "") // these assert the IMDS identity; the boot has its own test
t.Cleanup(func() { lease.SetSelf(hafresh.Self{}) })
getenv := envOf(map[string]string{"CARLOS_FLEET": "pi", "CARLOS_FLEET_BOX": "pi-box"})
ref := metricsBoxLabel(getenv, readMachineID())
@@ -161,3 +167,46 @@ func TestAgentRetriesAPartialIdentity(t *testing.T) {
t.Fatal("a complete identity started the retry")
}
}
+
+// withBootID points the boot id read at a file holding id for one test.
+// Tests that use it set a package var, so they never run in parallel.
+func withBootID(t *testing.T, id string) {
+ t.Helper()
+ p := filepath.Join(t.TempDir(), "boot_id")
+ if err := os.WriteFile(p, []byte(id+"\n"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ was := bootIDPath
+ bootIDPath = p
+ t.Cleanup(func() { bootIDPath = was })
+}
+
+// #451: the kernel boot id, trimmed; "" for a missing or empty file.
+func TestReadBootID(t *testing.T) {
+ dir := t.TempDir()
+ p := filepath.Join(dir, "boot_id")
+ os.WriteFile(p, []byte("3f1c0e2a-9b7d-4c1e-8f00-1a2b3c4d5e6f\n"), 0o600)
+ if got := readBootID(p); got != "3f1c0e2a-9b7d-4c1e-8f00-1a2b3c4d5e6f" {
+ t.Fatalf("%q", got)
+ }
+ if got := readBootID(filepath.Join(dir, "missing")); got != "" {
+ t.Fatalf("missing: %q", got)
+ }
+ empty := filepath.Join(dir, "empty")
+ os.WriteFile(empty, nil, 0o600)
+ if got := readBootID(empty); got != "" {
+ t.Fatalf("empty: %q", got)
+ }
+}
+
+// The lease identity carries the boot, with IMDS answering or not, and the
+// boot does not make an identity Complete.
+func TestHASelfNowCarriesTheBoot(t *testing.T) {
+ withBootID(t, "b9")
+ getenv := envOf(nil)
+ f := &fakeIMDS{status: http.StatusInternalServerError}
+ got := haSelfNow(getenv, imdsClient(f))
+ if got.Boot != "b9" || got.IID != "" || got.Complete() {
+ t.Fatalf("IMDS failing: %+v", got)
+ }
+}