@@ -41,15 +41,15 @@ func (r *ExtractedImage) Cleanup() {
4141}
4242
4343// UnpackImage pulls the image, extracts it to disk, and opens it as an OCI store.
44- func UnpackImage (ctx context.Context , imageRef , name string , sysCtx * types.SystemContext ) (res * ExtractedImage , err error ) {
44+ func UnpackImage (ctx context.Context , imageRef , name string , sysCtx * types.SystemContext ) (* ExtractedImage , error ) {
4545 tmpDir , err := os .MkdirTemp ("" , fmt .Sprintf ("oci-%s-" , name ))
4646 if err != nil {
4747 return nil , fmt .Errorf ("create temp dir: %w" , err )
4848 }
4949
5050 var digestTag string
5151
52- res , err = func () (* ExtractedImage , error ) {
52+ extracted , err : = func () (* ExtractedImage , error ) {
5353 srcRef , err := docker .ParseReference ("//" + imageRef )
5454 if err != nil {
5555 return nil , fmt .Errorf ("parse image ref: %w" , err )
@@ -59,7 +59,12 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
5959 if err != nil {
6060 return nil , fmt .Errorf ("create policy context: %w" , err )
6161 }
62- defer policyCtx .Destroy ()
62+ // Ensure policy context is cleaned up properly
63+ defer func () {
64+ if err := policyCtx .Destroy (); err != nil {
65+ fmt .Printf ("unable to destroy policy context: %s" , err )
66+ }
67+ }()
6368
6469 canonicalRef , err := resolveCanonicalRef (ctx , srcRef , sysCtx )
6570 if err != nil {
@@ -115,11 +120,13 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
115120 }()
116121
117122 if err != nil {
118- os .RemoveAll (tmpDir )
123+ if err := os .RemoveAll (tmpDir ); err != nil {
124+ fmt .Printf ("failed to remove temp dir: %v\n " , err )
125+ }
119126 return nil , err
120127 }
121128
122- return res , nil
129+ return extracted , nil
123130}
124131
125132// extractLayers extracts the filesystem layers from the OCI image layout under the given digest tag.
@@ -166,8 +173,9 @@ func extractLayers(ctx context.Context, layoutPath, fsPath, tag string) error {
166173
167174 _ , err := archive .Apply (ctx , fsPath , decompress , archive .WithFilter (func (hdr * tar.Header ) (bool , error ) {
168175 // Clean up extended headers and enforce safe permissions
176+ // This configuration allow to extract the image layers
177+ // without the need of root permissions in CI environments
169178 hdr .PAXRecords = nil
170- hdr .Xattrs = nil
171179 hdr .Uid = os .Getuid ()
172180 hdr .Gid = os .Getgid ()
173181 if hdr .FileInfo ().IsDir () {
@@ -183,10 +191,9 @@ func extractLayers(ctx context.Context, layoutPath, fsPath, tag string) error {
183191 return nil
184192 }()
185193 if err != nil {
186- return fmt . Errorf ( "decompress layer %d: %w" , i , err )
194+ return err
187195 }
188196 }
189-
190197 return nil
191198}
192199
@@ -229,7 +236,7 @@ func loadPolicyContext(sourceContext *types.SystemContext, imageRef string) (*si
229236 // if we need to validate the image signature then we will need to
230237 // change it.
231238 if err != nil {
232- fmt .Println ( fmt . Sprintf ( "no default policy found for (%s), using insecure policy" , imageRef ) )
239+ fmt .Printf ( "no default policy found for (%s), using insecure policy \n " , imageRef )
233240 insecurePolicy := []byte (`{
234241 "default": [{"type": "insecureAcceptAnything"}]
235242 }` )
0 commit comments