55
66
77import hashlib
8+ import json
89import os
910import platform
1011import stat
1112import subprocess
1213import sys
13- from typing import List
14+ from typing import List , Dict
1415
1516import requests
1617from azext_confcom .errors import eprint
@@ -91,7 +92,7 @@ def get_policy_image_layers(
9192 platform : str = "linux/amd64" ,
9293 tar_location : str = "" ,
9394 faster_hashing = False
94- ) -> List [str ]:
95+ ) -> Dict [ str , List [str ] ]:
9596 image_name = f"{ image } :{ tag } "
9697 # populate layer info
9798 if self .layer_cache .get (image_name ):
@@ -125,7 +126,7 @@ def get_policy_image_layers(
125126 check = False ,
126127 )
127128
128- output = []
129+ result = {}
129130 if item .returncode != 0 :
130131 if item .stderr .decode ("utf-8" ) != "" and item .stderr .decode ("utf-8" ) is not None :
131132 logger .warning (item .stderr .decode ("utf-8" ))
@@ -137,13 +138,29 @@ def get_policy_image_layers(
137138 )
138139 sys .exit (item .returncode )
139140 elif len (item .stdout ) > 0 :
140- output = item .stdout .decode ("utf8" ).strip ("\n " ).split ("\n " )
141- output = [i .split (": " , 1 )[1 ] for i in output if len (i .split (": " , 1 )) > 1 ]
141+ stdout_str = item .stdout .decode ("utf8" ).strip ()
142+
143+ # Try parsing as JSON (both Linux and Windows now output JSON)
144+ if stdout_str .startswith ("{" ):
145+ try :
146+ json_output = json .loads (stdout_str )
147+ result ["layers" ] = json_output .get ("layers" , [])
148+ # mounted_cim is only present for Windows
149+ if "mounted_cim" in json_output :
150+ result ["mounted_cim" ] = json_output ["mounted_cim" ]
151+ except json .JSONDecodeError as e :
152+ logger .error (f"Failed to parse JSON output: { e } " )
153+ sys .exit (1 )
154+ else :
155+ # Fallback: line-by-line parsing for older dmverity-vhd versions
156+ lines = stdout_str .split ("\n " )
157+ layers = [i .split (": " , 1 )[1 ] for i in lines if len (i .split (": " , 1 )) > 1 ]
158+ result ["layers" ] = layers
142159 else :
143160 eprint (
144161 "Could not get layer hashes"
145162 )
146163
147- # cache output layers
148- self .layer_cache [image_name ] = output
149- return output
164+ # cache output
165+ self .layer_cache [image_name ] = result
166+ return result
0 commit comments