media: gate refuses restart policies, named users and deadline volumes
Fix round 1 from the Astra and Fable reviews of Task 4. - P1: ContainerDef gains RestartPolicy (restartPolicy.enabled); a new last check, restart_policy, refuses it on either container, since a deadline ECS restarts in place never ends the task. - P2: user now requires an explicit numeric uid:gid on both containers, each in 1..2^31-1; names and a missing group resolve in an image the gate cannot see, and a group-less uid can run as gid 0. - shared_volume refuses any volume on deadline, since two volume names can be backed by the same storage; codec's own volume stays allowed. - A deadline stopTimeout 120 case catches >= on deadline's comparison.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+104
−46
internal/media/gate.go+45 −38internal/media/gate_test.go+59 −8
CI failed — run details
diff --git a/internal/media/gate.go b/internal/media/gate.go| index 79aa55435..9967d7499 100644 |
| --- a/internal/media/gate.go |
| +++ b/internal/media/gate.go |
| @@ -1,7 +1,6 @@ |
| package media |
| import ( |
| - "errors" |
| "math" |
| "slices" |
| "strconv" |
| @@ -47,12 +46,16 @@ type TaskDefinition struct { |
| // linuxParameters.capabilities.drop holds ALL and add is empty, and |
| // Volumes is every volume the container can reach, which is each mount |
| // point's source volume plus those of any container its volumesFrom |
| -// names. StopTimeout is nil when the definition sets none. EntryPoint and |
| -// Command are the definition's entryPoint and command as written, nil |
| -// when unset. |
| +// names. RestartPolicy is true when the definition's |
| +// restartPolicy.enabled is true; the adapter must fill it from the task |
| +// definition, because a deadline container ECS restarts in place never |
| +// ends the task. StopTimeout is nil when the definition sets none. |
| +// EntryPoint and Command are the definition's entryPoint and command as |
| +// written, nil when unset. |
| type ContainerDef struct { |
| Name, Image, User string |
| Essential, ReadOnlyRoot, DropsAll bool |
| + RestartPolicy bool |
| StopTimeout *int |
| DependsOn []Dependency |
| Volumes []string |
| @@ -85,6 +88,7 @@ var gateChecks = []gateCheck{ |
| {"capabilities", gateCapabilities}, |
| {"arm64", gateARM64}, |
| {"command", gateCommand}, |
| + {"restart_policy", gateRestartPolicy}, |
| } |
| // CheckTaskDefinition is the refusal gate (spec §5.5, astra R6-1): nil |
| @@ -96,12 +100,12 @@ var gateChecks = []gateCheck{ |
| // The checks, in order: containers (exactly deadline and codec), |
| // essential (both), depends_on (codec on deadline with START and nothing |
| // else; deadline on nothing), stop_timeout (both exactly 30 s, the stop |
| -// grace), task_role (none), pid_mode (none), shared_volume (no volume |
| -// reachable from both), image (one image for both, pinned by digest), |
| -// user (both set, neither user nor group root), read_only (both), |
| -// capabilities (both drop ALL), arm64, command (each container runs |
| -// exactly its role: EngineEntryPoint with DeadlineCommand or |
| -// CodecCommand). |
| +// grace), task_role (none), pid_mode (none), shared_volume (deadline |
| +// mounts no volume at all), image (one image for both, pinned by digest), |
| +// user (both an explicit numeric uid:gid, each in 1..2^31-1), read_only |
| +// (both), capabilities (both drop ALL), arm64, command (each container |
| +// runs exactly its role: EngineEntryPoint with DeadlineCommand or |
| +// CodecCommand), restart_policy (neither restarts in place). |
| func CheckTaskDefinition(td TaskDefinition) error { |
| deadline, codec, ok := gateContainers(td.Containers) |
| if !ok { |
| @@ -154,15 +158,12 @@ func gateTaskRole(td TaskDefinition, _, _ ContainerDef) bool { return td.TaskRol |
| func gatePIDMode(td TaskDefinition, _, _ ContainerDef) bool { return td.PIDMode == "" } |
| -func gateSharedVolume(_ TaskDefinition, d, c ContainerDef) bool { |
| - for _, dv := range d.Volumes { |
| - for _, cv := range c.Volumes { |
| - if dv == cv { |
| - return false |
| - } |
| - } |
| - } |
| - return true |
| +// gateSharedVolume: deadline only sleeps, so it mounts nothing. Volume |
| +// names prove nothing about storage (two names can be backed by one EFS |
| +// file system), so refusing every volume on deadline is what keeps the |
| +// two containers from sharing one; codec's own volumes are allowed. |
| +func gateSharedVolume(_ TaskDefinition, d, _ ContainerDef) bool { |
| + return len(d.Volumes) == 0 |
| } |
| // gateImage: one image for both, and its reference ends in a full sha256 |
| @@ -187,33 +188,32 @@ func gateImage(_ TaskDefinition, d, c ContainerDef) bool { |
| return true |
| } |
| +// gateUser: both containers name an explicit numeric uid:gid. A name |
| +// resolves in the image's /etc/passwd, which the gate cannot see, and a |
| +// user with no group runs with gid 0 when its uid has no passwd entry, |
| +// so neither is accepted. |
| func gateUser(_ TaskDefinition, d, c ContainerDef) bool { |
| - return notRoot(d.User) && notRoot(c.User) |
| + return numericUser(d.User) && numericUser(c.User) |
| } |
| -// notRoot answers whether a container's user field is set and names |
| -// neither the root user nor the root group: "user" or "user:group", each |
| -// part set, not root and not id 0. |
| -func notRoot(user string) bool { |
| - name, group, hasGroup := strings.Cut(user, ":") |
| - return notRootID(name) && (!hasGroup || notRootID(group) && !strings.Contains(group, ":")) |
| +// numericUser answers whether user is "uid:gid", both decimal digits |
| +// only and each in 1..2^31-1. |
| +func numericUser(user string) bool { |
| + uid, gid, ok := strings.Cut(user, ":") |
| + return ok && numericID(uid) && numericID(gid) |
| } |
| -// notRootID answers whether one part of a user field is set and is not |
| -// root. A numeric id is parsed as the runtime parses it, so 00 and +0 are |
| -// 0 too, and one outside 1..2^31-1 is refused. |
| -func notRootID(name string) bool { |
| - if name == "" || name == "root" { |
| +func numericID(s string) bool { |
| + if s == "" { |
| return false |
| } |
| - n, err := strconv.ParseInt(name, 10, 64) |
| - if errors.Is(err, strconv.ErrRange) { |
| - return false |
| - } |
| - if err == nil && (n < 1 || n > math.MaxInt32) { |
| - return false |
| + for _, r := range s { |
| + if r < '0' || r > '9' { |
| + return false |
| + } |
| } |
| - return true |
| + n, err := strconv.ParseInt(s, 10, 64) |
| + return err == nil && n >= 1 && n <= math.MaxInt32 |
| } |
| func gateReadOnly(_ TaskDefinition, d, c ContainerDef) bool { |
| @@ -234,3 +234,10 @@ func gateCommand(_ TaskDefinition, d, c ContainerDef) bool { |
| return slices.Equal(d.EntryPoint, entry) && slices.Equal(d.Command, []string{DeadlineCommand}) && |
| slices.Equal(c.EntryPoint, entry) && slices.Equal(c.Command, []string{CodecCommand}) |
| } |
| + |
| +// gateRestartPolicy: neither container restarts in place. A restarting |
| +// deadline exits without ending the task, so codec would run past the |
| +// bound; codec restarting would rerun a job inside one execution. |
| +func gateRestartPolicy(_ TaskDefinition, d, c ContainerDef) bool { |
| + return !d.RestartPolicy && !c.RestartPolicy |
| +} |
diff --git a/internal/media/gate_test.go b/internal/media/gate_test.go| index 2208929ee..4adc92926 100644 |
| --- a/internal/media/gate_test.go |
| +++ b/internal/media/gate_test.go |
| @@ -66,10 +66,11 @@ func TestCheckTaskDefinition(t *testing.T) { |
| if err := CheckTaskDefinition(swapped); err != nil { |
| t.Fatalf("the intended definition in the other order is refused: %v", err) |
| } |
| - ownVolume := intendedTaskDefinition() |
| - container(t, &ownVolume, ContainerDeadline).Volumes = []string{"timer"} |
| - if err := CheckTaskDefinition(ownVolume); err != nil { |
| - t.Fatalf("a volume deadline alone mounts is refused: %v", err) |
| + bounds := intendedTaskDefinition() |
| + container(t, &bounds, ContainerDeadline).User = "1:1" |
| + container(t, &bounds, ContainerCodec).User = "2147483647:2147483647" |
| + if err := CheckTaskDefinition(bounds); err != nil { |
| + t.Fatalf("users at the ends of 1..2^31-1 are refused: %v", err) |
| } |
| type mutation struct { |
| @@ -80,7 +81,7 @@ func TestCheckTaskDefinition(t *testing.T) { |
| cases := []mutation{ |
| // containers (pre-flight F21) |
| {"a third container", func(t *testing.T, td *TaskDefinition) { |
| - td.Containers = append(td.Containers, ContainerDef{Name: "sidecar", Image: tdImage, User: "65534", Essential: true, ReadOnlyRoot: true, DropsAll: true, StopTimeout: intPtr(30)}) |
| + td.Containers = append(td.Containers, ContainerDef{Name: "sidecar", Image: tdImage, User: "65534:65534", Essential: true, ReadOnlyRoot: true, DropsAll: true, StopTimeout: intPtr(30)}) |
| }, "containers"}, |
| {"codec renamed", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).Name = "codex" |
| @@ -147,6 +148,9 @@ func TestCheckTaskDefinition(t *testing.T) { |
| {"deadline stop timeout 29", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerDeadline).StopTimeout = intPtr(29) |
| }, "stop_timeout"}, |
| + {"deadline stop timeout 120", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).StopTimeout = intPtr(120) |
| + }, "stop_timeout"}, |
| {"deadline stop timeout nil", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerDeadline).StopTimeout = nil |
| }, "stop_timeout"}, |
| @@ -165,6 +169,9 @@ func TestCheckTaskDefinition(t *testing.T) { |
| {"a volume in both", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerDeadline).Volumes = []string{"timer", "scratch"} |
| }, "shared_volume"}, |
| + {"a volume of deadline's own, which may share codec's storage", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).Volumes = []string{"timer"} |
| + }, "shared_volume"}, |
| // image |
| {":latest instead of a digest", func(t *testing.T, td *TaskDefinition) { |
| @@ -217,16 +224,16 @@ func TestCheckTaskDefinition(t *testing.T) { |
| container(t, td, ContainerCodec).User = "root:root" |
| }, "user"}, |
| {"codec user 00", func(t *testing.T, td *TaskDefinition) { |
| - container(t, td, ContainerCodec).User = "00" |
| + container(t, td, ContainerCodec).User = "00:65534" |
| }, "user"}, |
| {"codec user +0", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).User = "+0:65534" |
| }, "user"}, |
| {"codec user 2^32, which a 32-bit truncation makes 0", func(t *testing.T, td *TaskDefinition) { |
| - container(t, td, ContainerCodec).User = "4294967296" |
| + container(t, td, ContainerCodec).User = "4294967296:65534" |
| }, "user"}, |
| {"codec user beyond int64", func(t *testing.T, td *TaskDefinition) { |
| - container(t, td, ContainerCodec).User = "99999999999999999999" |
| + container(t, td, ContainerCodec).User = "99999999999999999999:65534" |
| }, "user"}, |
| {"codec group 0", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).User = "65534:0" |
| @@ -246,6 +253,39 @@ func TestCheckTaskDefinition(t *testing.T) { |
| {"codec group with a second colon", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).User = "65534:65534:0" |
| }, "user"}, |
| + {"codec group omitted", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "65534" |
| + }, "user"}, |
| + {"deadline group omitted", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "60000" |
| + }, "user"}, |
| + {"codec named user", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "nobody:65534" |
| + }, "user"}, |
| + {"deadline named user", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "nobody:65534" |
| + }, "user"}, |
| + {"codec named group", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "65534:nogroup" |
| + }, "user"}, |
| + {"deadline named group", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "65534:nogroup" |
| + }, "user"}, |
| + {"codec uid 2^31", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "2147483648:65534" |
| + }, "user"}, |
| + {"deadline uid 2^31", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "2147483648:65534" |
| + }, "user"}, |
| + {"codec gid 2^31", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "65534:2147483648" |
| + }, "user"}, |
| + {"deadline gid 2^31", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).User = "65534:2147483648" |
| + }, "user"}, |
| + {"codec uid with a sign", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).User = "+65534:65534" |
| + }, "user"}, |
| {"codec user unset", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).User = "" |
| }, "user"}, |
| @@ -318,6 +358,14 @@ func TestCheckTaskDefinition(t *testing.T) { |
| {"codec with the image's default entry point", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerCodec).EntryPoint = nil |
| }, "command"}, |
| + |
| + // restart_policy |
| + {"deadline restarts in place", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).RestartPolicy = true |
| + }, "restart_policy"}, |
| + {"codec restarts in place", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerCodec).RestartPolicy = true |
| + }, "restart_policy"}, |
| } |
| for _, c := range cases { |
| t.Run(c.name, func(t *testing.T) { |
| @@ -362,6 +410,9 @@ func TestCheckTaskDefinitionOrder(t *testing.T) { |
| {"command", func(t *testing.T, td *TaskDefinition) { |
| container(t, td, ContainerDeadline).Command = []string{"codec"} |
| }}, |
| + {"restart_policy", func(t *testing.T, td *TaskDefinition) { |
| + container(t, td, ContainerDeadline).RestartPolicy = true |
| + }}, |
| } |
| td := intendedTaskDefinition() |
| for i := len(steps) - 1; i >= 0; i-- { |