From 54be74ae8929cb163bc4c4df65fa36131f1ac88b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 15 Aug 2026 10:34:46 +1000 Subject: [PATCH] feat(releaseupdate): stamp install type at build time, fail closed on unknown /update could report success at the correct target version while the archie-agent container running it never actually changed: the update mechanism trusted the configured installer's exit code and reported whatever version number the release claimed, with nothing verifying the two agreed. The first piece of the fix: a new internal/installtype package reports how a binary was built for distribution (Type(), defaulting to "unknown"), stamped once per release artifact via "-ldflags -X .../installtype.buildType=" rather than guessed at runtime by probing the environment -- restic and Tailscale both treat "which install shape is this" as a build-time decision for the same reason: a probed signal is guessable, a compiled-in one is not. releaseupdate.Service.Install now refuses to run the configured Installer at all when InstallType is unset or installtype.Unknown, so an unstamped or misconfigured build can no longer silently attempt an update it has no way to know is even the right kind for how this instance was deployed. Wired through every real archied build path so the gate doesn't just exist in principle: Taskfile.yml's build task (binary), Dockerfile.archied (container), and install.sh (binary) all now stamp it; cmd/archied/telegram_setup.go wires Service.InstallType from installtype.Type(). Configuration-page display of install type/version and post-update verification against the actually-running reference (not just the installer's reported version) remain open -- this lands the correctness gate, not the full observability surface. Refs #522 --- Dockerfile.archied | 2 +- Taskfile.yml | 2 +- cmd/archied/telegram_setup.go | 6 ++-- cmd/archied/telegram_setup_test.go | 28 ++++++++++++++++++ install.sh | 6 +++- internal/installtype/installtype.go | 31 ++++++++++++++++++++ internal/installtype/installtype_test.go | 37 ++++++++++++++++++++++++ internal/releaseupdate/service.go | 23 ++++++++++++++- internal/releaseupdate/service_test.go | 35 +++++++++++++++++++++- 9 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 cmd/archied/telegram_setup_test.go create mode 100644 internal/installtype/installtype.go create mode 100644 internal/installtype/installtype_test.go diff --git a/Dockerfile.archied b/Dockerfile.archied index 7b0447a47..6ca9dba65 100644 --- a/Dockerfile.archied +++ b/Dockerfile.archied @@ -9,7 +9,7 @@ COPY . . ARG GATEWAY_VERSION=dev ARG RUNTIME_VERSION=dev RUN CGO_ENABLED=0 go build \ - -ldflags "-X main.gatewayVersion=${GATEWAY_VERSION} -X main.runtimeVersion=${RUNTIME_VERSION}" \ + -ldflags "-X main.gatewayVersion=${GATEWAY_VERSION} -X main.runtimeVersion=${RUNTIME_VERSION} -X github.com/samcharles93/archie-core/internal/installtype.buildType=container" \ -o /usr/local/bin/archied ./cmd/archied/ # archied is static, but this can't be scratch: in inprocess mode the daemon # runs agent tools itself, and the shell tool invokes bash; codesearch shells diff --git a/Taskfile.yml b/Taskfile.yml index 83728a78e..f762e486a 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -105,7 +105,7 @@ tasks: - "{{.BIN_DIR}}/archied" - "{{.BIN_DIR}}/archie-agent" cmds: - - go build -ldflags "-X main.gatewayVersion={{.GATEWAY_VERSION}} -X main.runtimeVersion={{.RUNTIME_VERSION}}" -o {{.BIN_DIR}}/archied ./cmd/archied + - go build -ldflags "-X main.gatewayVersion={{.GATEWAY_VERSION}} -X main.runtimeVersion={{.RUNTIME_VERSION}} -X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" -o {{.BIN_DIR}}/archied ./cmd/archied - go build -o {{.BIN_DIR}}/archie-agent ./cmd/archie-agent test: diff --git a/cmd/archied/telegram_setup.go b/cmd/archied/telegram_setup.go index 7c699c3b0..6f0e95c08 100644 --- a/cmd/archied/telegram_setup.go +++ b/cmd/archied/telegram_setup.go @@ -18,6 +18,7 @@ import ( "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" + "github.com/samcharles93/archie-core/internal/installtype" "github.com/samcharles93/archie-core/internal/releaseannounce" "github.com/samcharles93/archie-core/internal/releaseupdate" "github.com/samcharles93/archie-core/internal/store" @@ -130,8 +131,9 @@ func makeUpdateService(s telegramSetup) *releaseupdate.Service { return nil } updates := &releaseupdate.Service{ - Catalog: releaseupdate.CommandCatalog{Command: cfg.Chat.Telegram.UpdateCheckCommand}, - StatePath: filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"), + Catalog: releaseupdate.CommandCatalog{Command: cfg.Chat.Telegram.UpdateCheckCommand}, + StatePath: filepath.Join(cfg.WorkDir, "telegram-update-deferrals.json"), + InstallType: installtype.Type(), } if len(cfg.Chat.Telegram.UpdateInstallCommand) != 0 { updates.Installer = releaseupdate.CommandInstaller{Command: cfg.Chat.Telegram.UpdateInstallCommand} diff --git a/cmd/archied/telegram_setup_test.go b/cmd/archied/telegram_setup_test.go new file mode 100644 index 000000000..525ce97d0 --- /dev/null +++ b/cmd/archied/telegram_setup_test.go @@ -0,0 +1,28 @@ +package main + +import ( + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/installtype" +) + +// TestMakeUpdateServiceWiresInstallType is the regression test for +// archie-core-522's fail-closed guarantee actually reaching production: a +// Service built without InstallType always refuses to install (see +// releaseupdate.ErrUnknownInstallType), so wiring it here is what makes +// /update work at all for a correctly-stamped build, not just a detail. +func TestMakeUpdateServiceWiresInstallType(t *testing.T) { + cfg := config.Config{} + cfg.Chat.Telegram.UpdateCheckCommand = []string{"archie-update-check"} + cfg.Chat.Telegram.UpdateInstallCommand = []string{"archie-update-install"} + + service := makeUpdateService(telegramSetup{Cfg: config.NewHolder(cfg)}) + + if service == nil { + t.Fatal("makeUpdateService returned nil despite a configured check command") + } + if service.InstallType != installtype.Type() { + t.Errorf("Service.InstallType = %q, want installtype.Type() = %q", service.InstallType, installtype.Type()) + } +} diff --git a/install.sh b/install.sh index c4ac39b5f..b06cd714a 100755 --- a/install.sh +++ b/install.sh @@ -164,7 +164,11 @@ fi echo "==> Building archied and archie-agent..." ( cd "${SRC_DIR}" - go build -o "${ARCHIE_BIN_DIR}/archied" ./cmd/archied + # installtype.buildType must be stamped here: an unstamped archied + # refuses to self-update (internal/releaseupdate.ErrUnknownInstallType) + # rather than guess whether /update's configured install command is + # even the right kind of update for a script-built native binary. + go build -ldflags "-X github.com/samcharles93/archie-core/internal/installtype.buildType=binary" -o "${ARCHIE_BIN_DIR}/archied" ./cmd/archied go build -o "${ARCHIE_BIN_DIR}/archie-agent" ./cmd/archie-agent ) echo " Installed binaries to ${ARCHIE_BIN_DIR}/" diff --git a/internal/installtype/installtype.go b/internal/installtype/installtype.go new file mode 100644 index 000000000..4aeaf82fe --- /dev/null +++ b/internal/installtype/installtype.go @@ -0,0 +1,31 @@ +// Package installtype identifies how the running binary was distributed, so +// code that must never guess at deployment shape -- most notably the +// self-update dispatcher -- can fail closed instead of assuming. +// +// The install type is decided once, at release-build time, and baked into +// the binary. It is deliberately never detected at runtime by probing the +// environment (checking for a container marker file, a systemd env var, +// and so on): those signals are guessable and can be present for reasons +// that have nothing to do with how the binary would actually be updated. A +// binary that was never stamped by the release pipeline (a local +// `go build`, for instance) reports Unknown, and callers that would +// otherwise silently guess must instead refuse. +package installtype + +// Unknown is what Type reports when a build was never stamped. +const Unknown = "unknown" + +// buildType is stamped per release artifact via +// "-ldflags -X github.com/samcharles93/archie-core/internal/installtype.buildType=", +// one value per build target -- e.g. "binary" for a native build, +// "container" for a Docker image build. Never set anywhere else. +var buildType = Unknown + +// Type reports how this binary was built for distribution: Unknown unless +// stamped by the release pipeline. +func Type() string { + if buildType == "" { + return Unknown + } + return buildType +} diff --git a/internal/installtype/installtype_test.go b/internal/installtype/installtype_test.go new file mode 100644 index 000000000..1f0e9ee60 --- /dev/null +++ b/internal/installtype/installtype_test.go @@ -0,0 +1,37 @@ +package installtype + +import "testing" + +func TestTypeDefaultsToUnknown(t *testing.T) { + if got := Type(); got != Unknown { + t.Errorf("Type() = %q, want %q (a go test binary is never ldflags-stamped)", got, Unknown) + } +} + +// TestTypeReflectsAStampedBuildType proves Type reads the exact variable +// "-ldflags -X .../installtype.buildType=" sets. go test cannot pass +// -ldflags to the package under test, so this is the closest a unit test can +// get to exercising the release pipeline's stamping without actually +// building a release artifact. +func TestTypeReflectsAStampedBuildType(t *testing.T) { + tests := []struct { + name string + stamp string + want string + }{ + {"container stamp", "container", "container"}, + {"binary stamp", "binary", "binary"}, + {"empty stamp is treated as unknown", "", Unknown}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + original := buildType + t.Cleanup(func() { buildType = original }) + + buildType = tt.stamp + if got := Type(); got != tt.want { + t.Errorf("Type() = %q, want %q", got, tt.want) + } + }) + } +} diff --git a/internal/releaseupdate/service.go b/internal/releaseupdate/service.go index cc45d82af..31919e068 100644 --- a/internal/releaseupdate/service.go +++ b/internal/releaseupdate/service.go @@ -11,6 +11,8 @@ import ( "path/filepath" "strconv" "sync" + + "github.com/samcharles93/archie-core/internal/installtype" ) // Catalog discovers the currently installed and newest available releases. @@ -75,7 +77,17 @@ type Service struct { Catalog Catalog Installer Installer StatePath string - mu sync.Mutex + + // InstallType gates Install: it must be a value the release pipeline + // actually stamps (see package installtype), never "" or + // installtype.Unknown, or Install refuses to run the configured + // Installer at all. The composition root wires this from + // installtype.Type() -- Service itself never reads that package + // directly, so a test can exercise every InstallType without touching + // process-wide state. + InstallType string + + mu sync.Mutex } func (s *Service) Check(ctx context.Context, recipient int64) (Snapshot, error) { @@ -138,10 +150,19 @@ func (s *Service) Defer(ctx context.Context, recipient int64, expected Snapshot) return saveDeferrals(s.StatePath, state) } +// ErrUnknownInstallType is returned by Install when InstallType is unset or +// installtype.Unknown -- there is no way to know, without it, whether the +// configured Installer is even the right kind of update for how this +// instance was actually deployed. +var ErrUnknownInstallType = errors.New("refusing to install: install type is unknown") + func (s *Service) Install(ctx context.Context, progress func(string)) error { if s == nil || s.Installer == nil { return errors.New("update installation is not configured") } + if s.InstallType == "" || s.InstallType == installtype.Unknown { + return ErrUnknownInstallType + } return s.Installer.Install(ctx, progress) } diff --git a/internal/releaseupdate/service_test.go b/internal/releaseupdate/service_test.go index eaa12ec9f..629bbb862 100644 --- a/internal/releaseupdate/service_test.go +++ b/internal/releaseupdate/service_test.go @@ -5,6 +5,8 @@ import ( "path/filepath" "strings" "testing" + + "github.com/samcharles93/archie-core/internal/installtype" ) func TestServiceDefersOnlyTheVersionsShownToThatRecipient(t *testing.T) { @@ -48,7 +50,7 @@ func TestServiceDefersOnlyTheVersionsShownToThatRecipient(t *testing.T) { func TestServiceInstallForwardsProgressAndFailure(t *testing.T) { installer := &installerStub{} - service := Service{Installer: installer} + service := Service{Installer: installer, InstallType: "binary"} var progress []string if err := service.Install(context.Background(), func(message string) { progress = append(progress, message) }); err != nil { t.Fatal(err) @@ -61,6 +63,37 @@ func TestServiceInstallForwardsProgressAndFailure(t *testing.T) { } } +// TestServiceInstallRefusesAnUnknownInstallType is the fail-closed guarantee +// that makes InstallType worth having: a binary the release pipeline never +// stamped (or a caller that never wired InstallType at all) must not run +// whatever install command happens to be configured -- there is no way to +// know, at that point, whether it is even the right kind of update for how +// this instance was actually deployed. +func TestServiceInstallRefusesAnUnknownInstallType(t *testing.T) { + tests := []struct { + name string + installType string + }{ + {"unset InstallType", ""}, + {"explicit unknown", installtype.Unknown}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + installer := &installerStub{} + service := Service{Installer: installer, InstallType: tt.installType} + + err := service.Install(context.Background(), func(string) {}) + + if err == nil { + t.Fatal("Install() returned nil error, want a refusal") + } + if installer.called { + t.Error("installer was called despite an unknown install type") + } + }) + } +} + func TestNilServiceReturnsConfigurationErrors(t *testing.T) { var service *Service