@@ -41,15 +41,15 @@ func (r *ExtractedImage) Cleanup() {
41
41
}
42
42
43
43
// 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 ) {
45
45
tmpDir , err := os .MkdirTemp ("" , fmt .Sprintf ("oci-%s-" , name ))
46
46
if err != nil {
47
47
return nil , fmt .Errorf ("create temp dir: %w" , err )
48
48
}
49
49
50
50
var digestTag string
51
51
52
- res , err = func () (* ExtractedImage , error ) {
52
+ extracted , err : = func () (* ExtractedImage , error ) {
53
53
srcRef , err := docker .ParseReference ("//" + imageRef )
54
54
if err != nil {
55
55
return nil , fmt .Errorf ("parse image ref: %w" , err )
@@ -59,7 +59,12 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
59
59
if err != nil {
60
60
return nil , fmt .Errorf ("create policy context: %w" , err )
61
61
}
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
+ }()
63
68
64
69
canonicalRef , err := resolveCanonicalRef (ctx , srcRef , sysCtx )
65
70
if err != nil {
@@ -115,11 +120,13 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
115
120
}()
116
121
117
122
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
+ }
119
126
return nil , err
120
127
}
121
128
122
- return res , nil
129
+ return extracted , nil
123
130
}
124
131
125
132
// 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 {
166
173
167
174
_ , err := archive .Apply (ctx , fsPath , decompress , archive .WithFilter (func (hdr * tar.Header ) (bool , error ) {
168
175
// 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
169
178
hdr .PAXRecords = nil
170
- hdr .Xattrs = nil
171
179
hdr .Uid = os .Getuid ()
172
180
hdr .Gid = os .Getgid ()
173
181
if hdr .FileInfo ().IsDir () {
@@ -183,10 +191,9 @@ func extractLayers(ctx context.Context, layoutPath, fsPath, tag string) error {
183
191
return nil
184
192
}()
185
193
if err != nil {
186
- return fmt . Errorf ( "decompress layer %d: %w" , i , err )
194
+ return err
187
195
}
188
196
}
189
-
190
197
return nil
191
198
}
192
199
@@ -229,7 +236,7 @@ func loadPolicyContext(sourceContext *types.SystemContext, imageRef string) (*si
229
236
// if we need to validate the image signature then we will need to
230
237
// change it.
231
238
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 )
233
240
insecurePolicy := []byte (`{
234
241
"default": [{"type": "insecureAcceptAnything"}]
235
242
}` )
0 commit comments