instancectl: accept hashed instance IDs in the start identity check
A host over 40 bytes is keyed by a hashed instance ID that starts with "_" (provision.InstanceID). UnitFamily checked the unit's instance ID with execsandbox.ValidID, the OCI container-ID rule, which refuses a leading "_". So since v0.34.34 ValidateStartIdentity answered EXEC-SANDBOX-MISMATCH for every unit of such a host, and the activator SIGKILLed the unit it had just started, logging no reason (#854; Tito staging, live on flagship). UnitFamily now checks the ID as an instance ID (validInstanceIDPart), and holds only a gVisor unit's container ID, "run-"+iid, to ValidID. This also fixes UnitIdentity and the size-status reader for long hosts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+85
−1
internal/instancectl/identity.go+12 −1internal/instancectl/identity_test.go+73 −0
CI failed — run details
diff --git a/internal/instancectl/identity.go b/internal/instancectl/identity.go| index 88ae1a189..1cdb620f6 100644 |
| --- a/internal/instancectl/identity.go |
| +++ b/internal/instancectl/identity.go |
| @@ -6,15 +6,26 @@ import ( |
| "github.com/carlosframework/platform/internal/execsandbox" |
| ) |
| +// UnitFamily reports which sandbox family runs unit, and whether unit is a |
| +// well-formed instance unit at all. The name's instance ID is checked as an |
| +// instance ID (validInstanceIDPart), not as an OCI container ID: a host |
| +// over provision.MaxInstanceID bytes is keyed by a hashed ID that starts |
| +// with "_", which execsandbox.ValidID refuses (#854). Only a gVisor unit's |
| +// ID becomes a container ID, as "run-"+iid (GVisorID), so only that is |
| +// held to execsandbox.ValidID. |
| func UnitFamily(unit string) (execsandbox.Family, bool) { |
| _, esc, ok := ParseUnitName(unit) |
| if !ok { |
| return "", false |
| } |
| - if id, ok := Unescape(esc); !ok || !execsandbox.ValidID(id) { |
| + id, ok := Unescape(esc) |
| + if !ok || !validInstanceIDPart(id) { |
| return "", false |
| } |
| if strings.HasPrefix(unit, UnitPrefixGVisor) { |
| + if !execsandbox.ValidID("run-" + id) { |
| + return "", false |
| + } |
| return execsandbox.FamilyGVisor, true |
| } |
| return execsandbox.FamilySystemd, true |
diff --git a/internal/instancectl/identity_test.go b/internal/instancectl/identity_test.go| new file mode 100644 |
| index 000000000..18d7c12da |
| --- /dev/null |
| +++ b/internal/instancectl/identity_test.go |
| @@ -0,0 +1,73 @@ |
| +package instancectl |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/execsandbox" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// A host over provision.MaxInstanceID bytes is keyed by a hashed instance |
| +// ID that starts with "_" (provision.InstanceID). Every unit root starts |
| +// for it carries that ID, so the agent's check of root's answer must |
| +// accept it (#854: v0.34.34..v0.34.42 refused it and SIGKILLed the unit |
| +// it had just started). |
| +func TestValidateStartIdentityAcceptsHashedInstanceIDs(t *testing.T) { |
| + const long = "imageproxy.shop.staging.platform.example.com" |
| + if id := provision.InstanceID(long); !strings.HasPrefix(id, "_") { |
| + t.Fatalf("test host %q is not hashed (InstanceID %q); make it longer", long, id) |
| + } |
| + sys := execsandbox.EffectiveSystemd |
| + for _, tc := range []struct { |
| + name string |
| + unit string |
| + mode execsandbox.Effective |
| + role Role |
| + gen uint64 |
| + rootfs bool |
| + worker string |
| + }{ |
| + {"run", UnitName(RoleRun, long), sys, RoleRun, 0, false, ""}, |
| + {"seed", UnitName(RoleSeed, long), sys, RoleSeed, 0, false, ""}, |
| + {"cmd", UnitName(RoleCmd, long), sys, RoleCmd, 0, false, ""}, |
| + {"generation", GenUnitName(long, 7), sys, RoleRun, 7, false, ""}, |
| + {"rootfs run", RootfsUnitName(RoleRun, long), sys, RoleRun, 0, true, ""}, |
| + {"rootfs generation", RootfsGenUnitName(long, 7), sys, RoleRun, 7, true, ""}, |
| + {"rootfs worker", WorkerUnitName(long, "sidekiq"), sys, RoleWorker, 0, true, "sidekiq"}, |
| + {"gvisor run", GVisorUnitName(long), execsandbox.EffectiveGVisor, RoleRun, 0, false, ""}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + if tc.unit == "" { |
| + t.Fatal("no unit name for the long host") |
| + } |
| + if _, ok := UnitFamily(tc.unit); !ok { |
| + t.Errorf("UnitFamily(%q) refused a hashed instance ID", tc.unit) |
| + } |
| + got := StartResponse{Unit: tc.unit, Sandbox: tc.mode, InvocationID: "inv"} |
| + if err := ValidateStartIdentity(got, tc.mode, long, tc.role, tc.gen, tc.rootfs, tc.worker); err != nil { |
| + t.Errorf("ValidateStartIdentity(%q) = %v, want nil", tc.unit, err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// The check still refuses what it exists to refuse: another host's unit, |
| +// and an instance ID no host could have. |
| +func TestValidateStartIdentityStillRefusesMismatches(t *testing.T) { |
| + const long = "imageproxy.shop.staging.platform.example.com" |
| + sys := execsandbox.EffectiveSystemd |
| + other := RootfsGenUnitName("billing.shop.staging.platform.example.com", 7) |
| + if err := ValidateStartIdentity(StartResponse{Unit: other, Sandbox: sys, InvocationID: "inv"}, sys, long, RoleRun, 7, true, ""); err == nil { |
| + t.Error("another host's generation unit was accepted") |
| + } |
| + for _, unit := range []string{ |
| + UnitPrefixRun + Escape("a/b") + ".service", |
| + UnitPrefixRun + Escape("..") + ".service", |
| + UnitPrefixGVisor + Escape("x\x00y") + ".service", |
| + } { |
| + if _, ok := UnitFamily(unit); ok { |
| + t.Errorf("UnitFamily(%q) accepted an invalid instance ID", unit) |
| + } |
| + } |
| +} |