@@ -300,11 +300,31 @@ def load(self):
300300 n_images = len (self .config .get ('images' , []))
301301 self .data += struct .pack (e + 'I' , n_images )
302302
303+ # Sort images, based on the image index
304+ images = self .config .get ('images' , [])
305+ images .sort (key = lambda img : img .get ('index' , 0 ))
306+
307+ # Verify that all indexes are present
308+ last_index = - 1
309+ if 'manifest_index' in self .config :
310+ manifest_index = int (self .config ['manifest_index' ])
311+ else :
312+ manifest_index = - 1
313+
303314 # Encode each image hash
304315 exp_hash_len = None
305- for image in self .config .get ('images' , []):
316+ for image in images :
317+ if 'index' in image :
318+ # Skip manifest index
319+ if last_index + 1 == manifest_index :
320+ last_index += 1
321+
322+ index = int (image ['index' ])
323+ if index != last_index + 1 :
324+ raise click .BadParameter (f"Manifest image indexes must be consecutive. Missing index: { last_index + 1 } " )
325+ last_index = index
306326 if 'path' not in image and 'hash' not in image :
307- raise click .UsageError (
327+ raise click .BadParameter (
308328 "Manifest image entry must contain either 'path' or 'hash'" )
309329
310330 # Encode hash, based on the signed image path
@@ -316,17 +336,23 @@ def load(self):
316336 if exp_hash_len is None :
317337 exp_hash_len = len (digest )
318338 elif exp_hash_len != len (digest ):
319- raise click .UsageError ("All image hashes must have the same length" )
339+ raise click .BadParameter ("All image hashes must have the same length" )
320340 self .data += struct .pack (e + f'{ exp_hash_len } s' , digest )
321341
322342 # Encode RAW image hash
323343 if 'hash' in image :
324344 if exp_hash_len is None :
325345 exp_hash_len = len (bytes .fromhex (image ['hash' ]))
326346 elif exp_hash_len != len (bytes .fromhex (image ['hash' ])):
327- raise click .UsageError ("All image hashes must have the same length" )
347+ raise click .BadParameter ("All image hashes must have the same length" )
328348 self .data += struct .pack (e + f'{ exp_hash_len } s' , bytes .fromhex (image ['hash' ]))
329349
350+ # Add manifest index if needed
351+ if last_index + 1 == manifest_index :
352+ last_index += 1
353+ if last_index != - 1 and last_index != n_images :
354+ raise click .BadParameter (f"Manifest image indexes must be consecutive. Missing index: { last_index + 1 } " )
355+
330356 except FileNotFoundError :
331357 raise click .UsageError (f"Manifest file { self .path } not found" ) from None
332358
0 commit comments