carlos / platform Public

media: refuse an engine task definition that loosens the deadline

The refusal gate (spec 5.5, plan 2 Task 4): CheckTaskDefinition reads a
task definition revision and answers *Misconfigured naming the first
check it fails, in order: containers, essential, depends_on,
stop_timeout, task_role, pid_mode, shared_volume, image, user,
read_only, capabilities, arm64. It answers no other error, so callers
can keep transport failures apart from refusals (pre-flight F13).

TestCheckTaskDefinition covers every check with its own mutations,
including the containers and essential cases of pre-flight F21;
TestCheckTaskDefinitionOrder pins the order. Plan 3's adapter fills
TaskDefinition from DescribeTaskDefinition.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 74641f97fb7411fdbc0f47c422114657efcbc691 parent c741f48
2 files changed, +499 −0
  • internal/media/gate.go +193 −0
  • internal/media/gate_test.go +306 −0
diff --git a/internal/media/gate.go b/internal/media/gate.go
new file mode 100644
index 000000000..c993cb108
--- /dev/null
+++ b/internal/media/gate.go
@@ -0,0 +1,193 @@
+package media
+
+import (
+ "errors"
+ "math"
+ "strconv"
+ "strings"
+ "time"
+)
+
+// TaskDefinition is what the refusal gate reads of one ECS task
+// definition revision (spec §5.5). Plan 3's adapter fills it from
+// DescribeTaskDefinition and caches the verdict per revision ARN.
+type TaskDefinition struct {
+ ARN string // the revision; Misconfigured.Revision
+ TaskRoleARN string // taskRoleArn; must be empty
+ PIDMode string // pidMode; must be empty
+ CPUArchitecture string // runtimePlatform.cpuArchitecture; must be ARM64
+ Containers []ContainerDef
+}
+
+// ContainerDef is one container definition of a TaskDefinition.
+//
+// The adapter must fill the booleans conservatively, because the gate
+// cannot see what they summarise: DropsAll is true only when
+// 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.
+type ContainerDef struct {
+ Name, Image, User string
+ Essential, ReadOnlyRoot, DropsAll bool
+ StopTimeout *int
+ DependsOn []Dependency
+ Volumes []string
+}
+
+// Dependency is one dependsOn entry: the container waited for and the
+// condition (START, COMPLETE, SUCCESS or HEALTHY).
+type Dependency struct{ Container, Condition string }
+
+// gateCheck is one check after containers, in the order the gate runs
+// them. ok answers whether the revision passes it.
+type gateCheck struct {
+ check string
+ ok func(td TaskDefinition, deadline, codec ContainerDef) bool
+}
+
+// gateChecks are the refusal gate's checks after containers, in order;
+// the first one a revision fails names its refusal.
+var gateChecks = []gateCheck{
+ {"essential", gateEssential},
+ {"depends_on", gateDependsOn},
+ {"stop_timeout", gateStopTimeout},
+ {"task_role", gateTaskRole},
+ {"pid_mode", gatePIDMode},
+ {"shared_volume", gateSharedVolume},
+ {"image", gateImage},
+ {"user", gateUser},
+ {"read_only", gateReadOnly},
+ {"capabilities", gateCapabilities},
+ {"arm64", gateARM64},
+}
+
+// CheckTaskDefinition is the refusal gate (spec §5.5, astra R6-1): nil
+// when the revision keeps the deadline's bound, else *Misconfigured
+// naming the first check it fails. It never answers any other error, so
+// a caller can map *Misconfigured to engine_misconfigured and anything
+// else to a transport failure (pre-flight F13).
+//
+// 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 and not root), read_only (both), capabilities (both
+// drop ALL), arm64.
+func CheckTaskDefinition(td TaskDefinition) error {
+ deadline, codec, ok := gateContainers(td.Containers)
+ if !ok {
+ return &Misconfigured{Revision: td.ARN, Check: "containers"}
+ }
+ for _, g := range gateChecks {
+ if !g.ok(td, deadline, codec) {
+ return &Misconfigured{Revision: td.ARN, Check: g.check}
+ }
+ }
+ return nil
+}
+
+// gateContainers answers the deadline and codec containers when the
+// definition holds exactly those two and nothing else.
+func gateContainers(cs []ContainerDef) (deadline, codec ContainerDef, ok bool) {
+ var seenDeadline, seenCodec bool
+ for _, c := range cs {
+ switch {
+ case c.Name == ContainerDeadline && !seenDeadline:
+ deadline, seenDeadline = c, true
+ case c.Name == ContainerCodec && !seenCodec:
+ codec, seenCodec = c, true
+ default:
+ return deadline, codec, false
+ }
+ }
+ return deadline, codec, seenDeadline && seenCodec
+}
+
+func gateEssential(_ TaskDefinition, d, c ContainerDef) bool {
+ return d.Essential && c.Essential
+}
+
+// gateDependsOn: codec cannot start before the timer runs, and nothing
+// holds the timer back.
+func gateDependsOn(_ TaskDefinition, d, c ContainerDef) bool {
+ return len(d.DependsOn) == 0 &&
+ len(c.DependsOn) == 1 &&
+ c.DependsOn[0] == Dependency{Container: ContainerDeadline, Condition: "START"}
+}
+
+func gateStopTimeout(_ TaskDefinition, d, c ContainerDef) bool {
+ want := int(StopGrace / time.Second)
+ return d.StopTimeout != nil && *d.StopTimeout == want &&
+ c.StopTimeout != nil && *c.StopTimeout == want
+}
+
+func gateTaskRole(td TaskDefinition, _, _ ContainerDef) bool { return td.TaskRoleARN == "" }
+
+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
+}
+
+// gateImage: one image for both, and its reference ends in a full sha256
+// digest, so no tag can move under the revision.
+func gateImage(_ TaskDefinition, d, c ContainerDef) bool {
+ if d.Image != c.Image {
+ return false
+ }
+ i := strings.Index(c.Image, "@sha256:")
+ if i <= 0 {
+ return false
+ }
+ digest := c.Image[i+len("@sha256:"):]
+ if len(digest) != 64 {
+ return false
+ }
+ for _, r := range digest {
+ if !(r >= '0' && r <= '9' || r >= 'a' && r <= 'f') {
+ return false
+ }
+ }
+ return true
+}
+
+func gateUser(_ TaskDefinition, d, c ContainerDef) bool {
+ return notRoot(d.User) && notRoot(c.User)
+}
+
+// notRoot answers whether a container's user field is set and names a
+// user other than root. A numeric user is parsed as the runtime parses
+// it, so 00 and +0 are root too, and one outside 1..2^31-1 is refused.
+func notRoot(user string) bool {
+ name, _, _ := strings.Cut(user, ":")
+ if name == "" || name == "root" {
+ 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
+ }
+ return true
+}
+
+func gateReadOnly(_ TaskDefinition, d, c ContainerDef) bool {
+ return d.ReadOnlyRoot && c.ReadOnlyRoot
+}
+
+func gateCapabilities(_ TaskDefinition, d, c ContainerDef) bool {
+ return d.DropsAll && c.DropsAll
+}
+
+func gateARM64(td TaskDefinition, _, _ ContainerDef) bool { return td.CPUArchitecture == "ARM64" }
diff --git a/internal/media/gate_test.go b/internal/media/gate_test.go
new file mode 100644
index 000000000..6b998f53a
--- /dev/null
+++ b/internal/media/gate_test.go
@@ -0,0 +1,306 @@
+package media
+
+import (
+ "errors"
+ "testing"
+)
+
+const (
+ tdImage = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ tdOtherImage = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
+ tdARN = "arn:aws:ecs:eu-west-1:123456789012:task-definition/media-video-720p-short:7"
+)
+
+// intendedTaskDefinition is the media-engine module's task definition
+// (spec §5.5, plan 3's Global Constraints): built fresh on every call so
+// a mutation never leaks into the next case.
+func intendedTaskDefinition() TaskDefinition {
+ thirty := func() *int { v := 30; return &v }
+ return TaskDefinition{
+ ARN: tdARN,
+ CPUArchitecture: "ARM64",
+ Containers: []ContainerDef{
+ {
+ Name: ContainerDeadline, Image: tdImage, User: "65534:65534",
+ Essential: true, ReadOnlyRoot: true, DropsAll: true,
+ StopTimeout: thirty(),
+ },
+ {
+ Name: ContainerCodec, Image: tdImage, User: "65534:65534",
+ Essential: true, ReadOnlyRoot: true, DropsAll: true,
+ StopTimeout: thirty(),
+ DependsOn: []Dependency{{Container: ContainerDeadline, Condition: "START"}},
+ Volumes: []string{"scratch"},
+ },
+ },
+ }
+}
+
+// container answers the named container of td for a mutation to edit.
+func container(t *testing.T, td *TaskDefinition, name string) *ContainerDef {
+ t.Helper()
+ for i := range td.Containers {
+ if td.Containers[i].Name == name {
+ return &td.Containers[i]
+ }
+ }
+ t.Fatalf("the intended definition has no %s container", name)
+ return nil
+}
+
+func intPtr(v int) *int { return &v }
+
+func TestCheckTaskDefinition(t *testing.T) {
+ if err := CheckTaskDefinition(intendedTaskDefinition()); err != nil {
+ t.Fatalf("the module's intended definition is refused: %v", err)
+ }
+ // The intended definition also passes with the containers listed the
+ // other way round, and with deadline mounting a volume of its own.
+ swapped := intendedTaskDefinition()
+ swapped.Containers[0], swapped.Containers[1] = swapped.Containers[1], swapped.Containers[0]
+ 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)
+ }
+
+ type mutation struct {
+ name string
+ mutate func(t *testing.T, td *TaskDefinition)
+ check string
+ }
+ 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)})
+ }, "containers"},
+ {"codec renamed", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).Name = "codex"
+ }, "containers"},
+ {"deadline renamed", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).Name = "timer"
+ }, "containers"},
+ {"codec only", func(t *testing.T, td *TaskDefinition) {
+ td.Containers = []ContainerDef{*container(t, td, ContainerCodec)}
+ }, "containers"},
+ {"two codecs", func(t *testing.T, td *TaskDefinition) {
+ c := *container(t, td, ContainerCodec)
+ td.Containers = []ContainerDef{c, c}
+ }, "containers"},
+ {"a second deadline beside the pair", func(t *testing.T, td *TaskDefinition) {
+ td.Containers = append(td.Containers, *container(t, td, ContainerDeadline))
+ }, "containers"},
+ {"a second codec beside the pair", func(t *testing.T, td *TaskDefinition) {
+ td.Containers = append(td.Containers, *container(t, td, ContainerCodec))
+ }, "containers"},
+ {"no containers", func(t *testing.T, td *TaskDefinition) {
+ td.Containers = nil
+ }, "containers"},
+
+ // essential (pre-flight F21)
+ {"deadline not essential", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).Essential = false
+ }, "essential"},
+ {"codec not essential", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).Essential = false
+ }, "essential"},
+
+ // depends_on
+ {"no dependsOn", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).DependsOn = nil
+ }, "depends_on"},
+ {"condition COMPLETE", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).DependsOn[0].Condition = "COMPLETE"
+ }, "depends_on"},
+ {"condition start in lower case", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).DependsOn[0].Condition = "start"
+ }, "depends_on"},
+ {"codec depends on itself", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).DependsOn[0].Container = ContainerCodec
+ }, "depends_on"},
+ {"codec depends on deadline twice", func(t *testing.T, td *TaskDefinition) {
+ c := container(t, td, ContainerCodec)
+ c.DependsOn = append(c.DependsOn, Dependency{Container: ContainerDeadline, Condition: "SUCCESS"})
+ }, "depends_on"},
+ {"deadline also depending on codec", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).DependsOn = []Dependency{{Container: ContainerCodec, Condition: "START"}}
+ }, "depends_on"},
+
+ // stop_timeout
+ {"codec stop timeout 29", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).StopTimeout = intPtr(29)
+ }, "stop_timeout"},
+ {"codec stop timeout 120", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).StopTimeout = intPtr(120)
+ }, "stop_timeout"},
+ {"codec stop timeout nil", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).StopTimeout = nil
+ }, "stop_timeout"},
+ {"deadline stop timeout 29", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).StopTimeout = intPtr(29)
+ }, "stop_timeout"},
+ {"deadline stop timeout nil", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).StopTimeout = nil
+ }, "stop_timeout"},
+
+ // task_role
+ {"a task role", func(t *testing.T, td *TaskDefinition) {
+ td.TaskRoleARN = "arn:aws:iam::123456789012:role/media-task"
+ }, "task_role"},
+
+ // pid_mode
+ {"pidMode task", func(t *testing.T, td *TaskDefinition) {
+ td.PIDMode = "task"
+ }, "pid_mode"},
+
+ // shared_volume
+ {"a volume in both", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).Volumes = []string{"timer", "scratch"}
+ }, "shared_volume"},
+
+ // image
+ {":latest instead of a digest", func(t *testing.T, td *TaskDefinition) {
+ const latest = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media:latest"
+ container(t, td, ContainerDeadline).Image = latest
+ container(t, td, ContainerCodec).Image = latest
+ }, "image"},
+ {"two different digests", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).Image = tdOtherImage
+ }, "image"},
+ {"a truncated digest", func(t *testing.T, td *TaskDefinition) {
+ const short = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123"
+ container(t, td, ContainerDeadline).Image = short
+ container(t, td, ContainerCodec).Image = short
+ }, "image"},
+ {"an empty digest", func(t *testing.T, td *TaskDefinition) {
+ const empty = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:"
+ container(t, td, ContainerDeadline).Image = empty
+ container(t, td, ContainerCodec).Image = empty
+ }, "image"},
+ {"a digest with characters that are not hex", func(t *testing.T, td *TaskDefinition) {
+ const bad = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/carlos-media@sha256:0123456789ABCDEF0123456789abcdef0123456789abcdef0123456789abcdef"
+ container(t, td, ContainerDeadline).Image = bad
+ container(t, td, ContainerCodec).Image = bad
+ }, "image"},
+ {"a digest with no repository", func(t *testing.T, td *TaskDefinition) {
+ const bare = "@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ container(t, td, ContainerDeadline).Image = bare
+ container(t, td, ContainerCodec).Image = bare
+ }, "image"},
+ {"no image", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).Image = ""
+ container(t, td, ContainerCodec).Image = ""
+ }, "image"},
+
+ // user
+ {"codec user 0", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "0"
+ }, "user"},
+ {"deadline user 0", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).User = "0"
+ }, "user"},
+ {"codec user root", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "root"
+ }, "user"},
+ {"codec user 0:65534", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "0:65534"
+ }, "user"},
+ {"codec user root:root", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "root:root"
+ }, "user"},
+ {"codec user 00", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "00"
+ }, "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"
+ }, "user"},
+ {"codec user beyond int64", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = "99999999999999999999"
+ }, "user"},
+ {"codec user unset", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = ""
+ }, "user"},
+ {"codec user :65534", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).User = ":65534"
+ }, "user"},
+
+ // read_only
+ {"codec writable root", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).ReadOnlyRoot = false
+ }, "read_only"},
+ {"deadline writable root", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).ReadOnlyRoot = false
+ }, "read_only"},
+
+ // capabilities
+ {"codec capabilities not dropped", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerCodec).DropsAll = false
+ }, "capabilities"},
+ {"deadline capabilities not dropped", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).DropsAll = false
+ }, "capabilities"},
+
+ // arm64
+ {"X86_64", func(t *testing.T, td *TaskDefinition) {
+ td.CPUArchitecture = "X86_64"
+ }, "arm64"},
+ {"no architecture", func(t *testing.T, td *TaskDefinition) {
+ td.CPUArchitecture = ""
+ }, "arm64"},
+ }
+ for _, c := range cases {
+ t.Run(c.name, func(t *testing.T) {
+ td := intendedTaskDefinition()
+ c.mutate(t, &td)
+ err := CheckTaskDefinition(td)
+ var mis *Misconfigured
+ if !errors.As(err, &mis) {
+ t.Fatalf("CheckTaskDefinition = %v, want *Misconfigured{Check: %q}", err, c.check)
+ }
+ if mis.Check != c.check || mis.Revision != tdARN {
+ t.Fatalf("CheckTaskDefinition = %+v, want Revision %q, Check %q", *mis, tdARN, c.check)
+ }
+ })
+ }
+}
+
+// TestCheckTaskDefinitionOrder: a definition failing several checks is
+// refused for the first of them in the brief's order. Each step applies
+// one more check's mutation on top of the later ones'.
+func TestCheckTaskDefinitionOrder(t *testing.T) {
+ steps := []struct {
+ check string
+ mutate func(t *testing.T, td *TaskDefinition)
+ }{
+ {"containers", func(t *testing.T, td *TaskDefinition) {
+ td.Containers = append(td.Containers, ContainerDef{Name: "sidecar"})
+ }},
+ {"essential", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerDeadline).Essential = false }},
+ {"depends_on", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).DependsOn = nil }},
+ {"stop_timeout", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).StopTimeout = nil }},
+ {"task_role", func(t *testing.T, td *TaskDefinition) { td.TaskRoleARN = "arn:aws:iam::123456789012:role/x" }},
+ {"pid_mode", func(t *testing.T, td *TaskDefinition) { td.PIDMode = "task" }},
+ {"shared_volume", func(t *testing.T, td *TaskDefinition) {
+ container(t, td, ContainerDeadline).Volumes = []string{"scratch"}
+ }},
+ {"image", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).Image = tdOtherImage }},
+ {"user", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).User = "0" }},
+ {"read_only", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).ReadOnlyRoot = false }},
+ {"capabilities", func(t *testing.T, td *TaskDefinition) { container(t, td, ContainerCodec).DropsAll = false }},
+ {"arm64", func(t *testing.T, td *TaskDefinition) { td.CPUArchitecture = "X86_64" }},
+ }
+ td := intendedTaskDefinition()
+ for i := len(steps) - 1; i >= 0; i-- {
+ steps[i].mutate(t, &td)
+ var mis *Misconfigured
+ if err := CheckTaskDefinition(td); !errors.As(err, &mis) || mis.Check != steps[i].check {
+ t.Fatalf("with checks %s onward failing, CheckTaskDefinition = %v, want %q", steps[i].check, err, steps[i].check)
+ }
+ }
+}