44
55import ipaddress
66import re
7+ from pathlib import Path
78
89OWNER = "dev.durable-workflow.fixture-owner"
910RUN = "dev.durable-workflow.fixture-run"
@@ -81,39 +82,139 @@ def verify_network(record, identity, nonce):
8182def expected_command (image , role , bootstrap ):
8283 if role == "consumer" :
8384 return ["/usr/local/bin/python3" ], ["-I" , "-B" , "-c" , bootstrap ], "/"
84- return image ["Config" ][ "Entrypoint" ] , image ["Config" ][ "Cmd" ] , image ["Config" ][ "WorkingDir" ]
85+ return image ["Config" ]. get ( "Entrypoint" ) , image ["Config" ]. get ( "Cmd" ) , image ["Config" ]. get ( "WorkingDir" , "" )
8586
8687
87- def verify_container (record , identity , nonce , image , network , child_digest , role , bootstrap = "" ):
88- require_owner (record , identity , nonce , role )
89- config , host = record ["Config" ], record ["HostConfig" ]
88+ def bind_local_image (index_record , platform_record , index_digest , child_digest , config_digest ):
89+ if platform_record ["Architecture" ] != "amd64" or platform_record ["Os" ] != "linux" :
90+ raise ValueError ("local platform image differs" )
91+ for record in (index_record , platform_record ):
92+ if not any (value .rsplit ("@" , 1 )[- 1 ] == index_digest for value in record ["RepoDigests" ]):
93+ raise ValueError ("local image lacks the selected immutable index" )
94+ descriptor = platform_record .get ("Descriptor" )
95+ if index_record ["Id" ] == config_digest and platform_record ["Id" ] == config_digest :
96+ store = "classic"
97+ elif index_record ["Id" ] == index_digest and platform_record ["Id" ] == child_digest :
98+ store = "containerd"
99+ else :
100+ raise ValueError ("local image IDs do not bind the selected manifest/config" )
101+ if (store == "containerd" or descriptor is not None ) and (
102+ descriptor is None
103+ or descriptor .get ("digest" ) != child_digest
104+ or descriptor .get ("platform" ) != {"architecture" : "amd64" , "os" : "linux" }
105+ ):
106+ raise ValueError ("local platform descriptor differs" )
107+ return {
108+ ** platform_record ,
109+ "CreateReference" : index_record ["Id" ],
110+ "PlatformConfigDigest" : config_digest ,
111+ "ImageStore" : store ,
112+ }
113+
114+
115+ def verify_config (config , identity , nonce , image , role , bootstrap ):
90116 environment = {key : value for key , value in (item .split ("=" , 1 ) for item in image ["Config" ]["Env" ])}
91117 if role == "runtime" :
92118 environment .update (ENVIRONMENT )
93119 received = {key : value for key , value in (item .split ("=" , 1 ) for item in config ["Env" ])}
94120 if len (received ) != len (config ["Env" ]) or received != environment :
95121 raise ValueError ("container environment differs" )
96- entrypoint , command , working_directory = expected_command (image , role , bootstrap )
122+ entrypoint , command , directory = expected_command (image , role , bootstrap )
123+ labels = {** (image ["Config" ].get ("Labels" ) or {}), OWNER : "sdk-python" , RUN : nonce , ROLE : role }
97124 expected = {
98- "Image" : image ["Id" ],
125+ "Hostname" : identity [:12 ],
126+ "Domainname" : "" ,
127+ "Image" : image .get ("CreateReference" , image ["Id" ]),
99128 "User" : "1000:1000" ,
100129 "Entrypoint" : entrypoint ,
101130 "Cmd" : command ,
102- "WorkingDir" : working_directory ,
131+ "WorkingDir" : directory ,
103132 "Tty" : False ,
104133 "OpenStdin" : role == "consumer" ,
134+ "StdinOnce" : role == "consumer" ,
135+ "AttachStdin" : role == "consumer" ,
136+ "AttachStdout" : True ,
137+ "AttachStderr" : True ,
138+ "Labels" : labels ,
139+ "Volumes" : image ["Config" ].get ("Volumes" ),
140+ "ExposedPorts" : image ["Config" ].get ("ExposedPorts" ),
141+ "Healthcheck" : image ["Config" ].get ("Healthcheck" ),
142+ "StopSignal" : image ["Config" ].get ("StopSignal" , "" ),
143+ "StopTimeout" : image ["Config" ].get ("StopTimeout" ),
144+ "Shell" : image ["Config" ].get ("Shell" ),
145+ "OnBuild" : image ["Config" ].get ("OnBuild" ),
146+ "ArgsEscaped" : image ["Config" ].get ("ArgsEscaped" , False ),
147+ "NetworkDisabled" : False ,
148+ "MacAddress" : "" ,
105149 }
106- if record ["Image" ] != image ["Id" ] or any (config .get (key ) != value for key , value in expected .items ()):
107- raise ValueError ("container image or command differs" )
150+ if set (config ) - (set (expected ) | {"Env" }):
151+ raise ValueError ("container has an unreviewed Config field" )
152+ optional = {
153+ "ExposedPorts" : None ,
154+ "Healthcheck" : None ,
155+ "StopSignal" : "" ,
156+ "StopTimeout" : None ,
157+ "Shell" : None ,
158+ "OnBuild" : None ,
159+ "ArgsEscaped" : False ,
160+ "NetworkDisabled" : False ,
161+ "MacAddress" : "" ,
162+ }
163+ if (set (expected ) - set (optional )) - set (config ):
164+ raise ValueError ("container Config lacks a required observation" )
165+ for key , value in expected .items ():
166+ received_value = config .get (key , optional [key ]) if key in optional else config [key ]
167+ if received_value != value :
168+ raise ValueError ("container Config differs from the closed image/role projection" )
169+
170+
171+ def verify_container (record , identity , nonce , image , network , child_digest , role , bootstrap = "" , * , cidfile = "" ):
172+ require_owner (record , identity , nonce , role )
173+ verify_config (record ["Config" ], identity , nonce , image , role , bootstrap )
174+ if record ["Image" ] != image .get ("CreateReference" , image ["Id" ]):
175+ raise ValueError ("container image identity differs" )
108176 descriptor = record .get ("ImageManifestDescriptor" )
109177 if descriptor is not None and descriptor .get ("digest" ) != child_digest :
110178 raise ValueError ("container platform child differs" )
111- verify_host (host , role , network )
179+ if image .get ("ImageStore" ) == "containerd" and (
180+ descriptor is None or descriptor .get ("platform" ) != {"os" : "linux" , "architecture" : "amd64" }
181+ ):
182+ raise ValueError ("containerd platform descriptor is missing or different" )
183+ verify_host (record ["HostConfig" ], role , network , cidfile = cidfile )
112184 if any (mount ["Type" ] != "tmpfs" or mount ["Destination" ] not in TMPFS [role ] for mount in record ["Mounts" ]):
113185 raise ValueError ("container has an undeclared mount" )
114186
115187
116- def verify_host (host , role , network ):
188+ MASKED_PATHS = {
189+ "/proc/acpi" ,
190+ "/proc/asound" ,
191+ "/proc/interrupts" ,
192+ "/proc/kcore" ,
193+ "/proc/keys" ,
194+ "/proc/latency_stats" ,
195+ "/proc/sched_debug" ,
196+ "/proc/scsi" ,
197+ "/proc/timer_list" ,
198+ "/proc/timer_stats" ,
199+ "/sys/devices/virtual/powercap" ,
200+ "/sys/firmware" ,
201+ }
202+ READONLY_PATHS = {"/proc/bus" , "/proc/fs" , "/proc/irq" , "/proc/sys" , "/proc/sysrq-trigger" }
203+
204+
205+ def expected_masked_paths ():
206+ # The fixed local daemon also masks the kernel's CPU thermal-throttle paths.
207+ return MASKED_PATHS | {str (path ) for path in Path ("/sys/devices/system/cpu" ).glob ("cpu[0-9]*/thermal_throttle" )}
208+
209+
210+ def verify_protected_paths (host ):
211+ for field , expected in (("MaskedPaths" , expected_masked_paths ()), ("ReadonlyPaths" , READONLY_PATHS )):
212+ actual = host .get (field )
213+ if not isinstance (actual , list ) or len (actual ) != len (set (actual )) or set (actual ) != expected :
214+ raise ValueError ("container protected system paths differ" )
215+
216+
217+ def verify_host (host , role , network , * , cidfile = "" ):
117218 expected = {
118219 "NetworkMode" : network ,
119220 "ReadonlyRootfs" : True ,
@@ -128,7 +229,8 @@ def verify_host(host, role, network):
128229 "Tmpfs" : TMPFS [role ],
129230 "PublishAllPorts" : False ,
130231 "AutoRemove" : False ,
131- "OomKillDisable" : False ,
232+ "ContainerIDFile" : cidfile ,
233+ "ConsoleSize" : [0 , 0 ],
132234 "MemorySwappiness" : None ,
133235 "ShmSize" : 67_108_864 ,
134236 "Runtime" : "runc" ,
@@ -139,7 +241,12 @@ def verify_host(host, role, network):
139241 }
140242 if any (host .get (key ) != value for key , value in expected .items ()):
141243 raise ValueError ("container resource/network/security configuration differs" )
244+ if "OomKillDisable" not in host or (host ["OomKillDisable" ] is not False and host ["OomKillDisable" ] is not None ):
245+ raise ValueError ("container disables the OOM killer or lacks its lifecycle value" )
246+ verify_protected_paths (host )
142247 zero_fields = (
248+ "IOMaximumIOps" ,
249+ "IOMaximumBandwidth" ,
143250 "CpuShares" ,
144251 "CpuPeriod" ,
145252 "CpuQuota" ,
@@ -156,6 +263,7 @@ def verify_host(host, role, network):
156263 if any (host .get (key , 0 ) != 0 for key in zero_fields ):
157264 raise ValueError ("container has an undeclared resource override" )
158265 forbidden = (
266+ "Annotations" ,
159267 "Binds" ,
160268 "VolumesFrom" ,
161269 "Devices" ,
@@ -188,6 +296,9 @@ def verify_host(host, role, network):
188296 "BlkioDeviceReadIOps" ,
189297 "BlkioDeviceWriteIOps" ,
190298 )
299+ known = set (expected ) | set (zero_fields ) | set (forbidden ) | {"OomKillDisable" , "MaskedPaths" , "ReadonlyPaths" }
300+ if set (host ) - known :
301+ raise ValueError ("container has an unreviewed HostConfig field" )
191302 if any (host .get (key ) for key in forbidden ):
192303 raise ValueError ("container has an undeclared host resource" )
193304
@@ -225,6 +336,8 @@ def create_arguments(image, network, nonce, cidfile, role, bootstrap=""):
225336 "docker" ,
226337 "create" ,
227338 "--pull=never" ,
339+ "--platform" ,
340+ "linux/amd64" ,
228341 "--cidfile" ,
229342 str (cidfile ),
230343 "--name" ,
@@ -278,12 +391,12 @@ def create_arguments(image, network, nonce, cidfile, role, bootstrap=""):
278391 "/" ,
279392 "--entrypoint" ,
280393 "/usr/local/bin/python3" ,
281- image ["Id" ],
394+ image . get ( "CreateReference" , image ["Id" ]) ,
282395 "-I" ,
283396 "-B" ,
284397 "-c" ,
285398 bootstrap ,
286399 ]
287400 for key , value in ENVIRONMENT .items ():
288401 arguments .extend (["-e" , key + "=" + value ])
289- return [* arguments , image ["Id" ]]
402+ return [* arguments , image . get ( "CreateReference" , image ["Id" ]) ]
0 commit comments