@@ -30,7 +30,6 @@ import (
3030
3131 "github.com/containerd/containerd/content"
3232 "github.com/containerd/containerd/errdefs"
33- "github.com/containerd/containerd/leases"
3433 "github.com/containerd/containerd/platforms"
3534 digest "github.com/opencontainers/go-digest"
3635 specs "github.com/opencontainers/image-spec/specs-go"
@@ -149,7 +148,7 @@ func decryptLayer(cc *encconfig.CryptoConfig, dataReader content.ReaderAt, desc
149148}
150149
151150// cryptLayer handles the changes due to encryption or decryption of a layer
152- func cryptLayer (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , cryptoOp cryptoOp ) (ocispec.Descriptor , error ) {
151+ func cryptLayer (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , cryptoOp cryptoOp ) (ocispec.Descriptor , error ) {
153152 var (
154153 resultReader io.Reader
155154 newDesc ocispec.Descriptor
@@ -171,33 +170,13 @@ func cryptLayer(ctx context.Context, cs content.Store, ls leases.Manager, l leas
171170 }
172171 // some operations, such as changing recipients, may not touch the layer at all
173172 if resultReader != nil {
174- if ls == nil {
175- return ocispec.Descriptor {}, errors .New ("Unexpected write to object without lease" )
176- }
177-
178- var rsrc leases.Resource
179173 var ref string
180-
181174 // If we have the digest, write blob with checks
182175 haveDigest := newDesc .Digest .String () != ""
183176 if haveDigest {
184177 ref = fmt .Sprintf ("layer-%s" , newDesc .Digest .String ())
185- rsrc = leases.Resource {
186- ID : newDesc .Digest .String (),
187- Type : "content" ,
188- }
189178 } else {
190179 ref = fmt .Sprintf ("blob-%d-%d" , rand .Int (), rand .Int ())
191- rsrc = leases.Resource {
192- ID : ref ,
193- Type : "ingests" ,
194- }
195-
196- }
197-
198- // Add resource to lease and write blob
199- if err := ls .AddResource (ctx , l , rsrc ); err != nil {
200- return ocispec.Descriptor {}, errors .Wrap (err , "Unable to add resource to lease" )
201180 }
202181
203182 if haveDigest {
@@ -240,7 +219,7 @@ func ingestReader(ctx context.Context, cs content.Ingester, ref string, r io.Rea
240219}
241220
242221// Encrypt or decrypt all the Children of a given descriptor
243- func cryptChildren (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp , thisPlatform * ocispec.Platform ) (ocispec.Descriptor , bool , error ) {
222+ func cryptChildren (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp , thisPlatform * ocispec.Platform ) (ocispec.Descriptor , bool , error ) {
244223 children , err := images .Children (ctx , cs , desc )
245224 if err != nil {
246225 if errdefs .IsNotFound (err ) {
@@ -261,7 +240,7 @@ func cryptChildren(ctx context.Context, cs content.Store, ls leases.Manager, l l
261240 case images .MediaTypeDockerSchema2LayerGzip , images .MediaTypeDockerSchema2Layer ,
262241 ocispec .MediaTypeImageLayerGzip , ocispec .MediaTypeImageLayer :
263242 if cryptoOp == cryptoOpEncrypt && lf (child ) {
264- nl , err := cryptLayer (ctx , cs , ls , l , child , cc , cryptoOp )
243+ nl , err := cryptLayer (ctx , cs , child , cc , cryptoOp )
265244 if err != nil {
266245 return ocispec.Descriptor {}, false , err
267246 }
@@ -273,7 +252,7 @@ func cryptChildren(ctx context.Context, cs content.Store, ls leases.Manager, l l
273252 case images .MediaTypeDockerSchema2LayerGzipEnc , images .MediaTypeDockerSchema2LayerEnc :
274253 // this one can be decrypted but also its recipients list changed
275254 if lf (child ) {
276- nl , err := cryptLayer (ctx , cs , ls , l , child , cc , cryptoOp )
255+ nl , err := cryptLayer (ctx , cs , child , cc , cryptoOp )
277256 if err != nil || cryptoOp == cryptoOpUnwrapOnly {
278257 return ocispec.Descriptor {}, false , err
279258 }
@@ -319,19 +298,6 @@ func cryptChildren(ctx context.Context, cs content.Store, ls leases.Manager, l l
319298
320299 ref := fmt .Sprintf ("manifest-%s" , newDesc .Digest .String ())
321300
322- if ls == nil {
323- return ocispec.Descriptor {}, false , errors .New ("Unexpected write to object without lease" )
324- }
325-
326- rsrc := leases.Resource {
327- ID : desc .Digest .String (),
328- Type : "content" ,
329- }
330-
331- if err := ls .AddResource (ctx , l , rsrc ); err != nil {
332- return ocispec.Descriptor {}, false , errors .Wrap (err , "Unable to add resource to lease" )
333- }
334-
335301 if err := content .WriteBlob (ctx , cs , ref , bytes .NewReader (mb ), newDesc , content .WithLabels (labels )); err != nil {
336302 return ocispec.Descriptor {}, false , errors .Wrap (err , "failed to write config" )
337303 }
@@ -342,7 +308,7 @@ func cryptChildren(ctx context.Context, cs content.Store, ls leases.Manager, l l
342308}
343309
344310// cryptManifest encrypts or decrypts the children of a top level manifest
345- func cryptManifest (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
311+ func cryptManifest (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
346312 p , err := content .ReadBlob (ctx , cs , desc )
347313 if err != nil {
348314 return ocispec.Descriptor {}, false , err
@@ -352,15 +318,15 @@ func cryptManifest(ctx context.Context, cs content.Store, ls leases.Manager, l l
352318 return ocispec.Descriptor {}, false , err
353319 }
354320 platform := platforms .DefaultSpec ()
355- newDesc , modified , err := cryptChildren (ctx , cs , ls , l , desc , cc , lf , cryptoOp , & platform )
321+ newDesc , modified , err := cryptChildren (ctx , cs , desc , cc , lf , cryptoOp , & platform )
356322 if err != nil || cryptoOp == cryptoOpUnwrapOnly {
357323 return ocispec.Descriptor {}, false , err
358324 }
359325 return newDesc , modified , nil
360326}
361327
362328// cryptManifestList encrypts or decrypts the children of a top level manifest list
363- func cryptManifestList (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
329+ func cryptManifestList (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
364330 // read the index; if any layer is encrypted and any manifests change we will need to rewrite it
365331 b , err := content .ReadBlob (ctx , cs , desc )
366332 if err != nil {
@@ -375,7 +341,7 @@ func cryptManifestList(ctx context.Context, cs content.Store, ls leases.Manager,
375341 var newManifests []ocispec.Descriptor
376342 modified := false
377343 for _ , manifest := range index .Manifests {
378- newManifest , m , err := cryptChildren (ctx , cs , ls , l , manifest , cc , lf , cryptoOp , manifest .Platform )
344+ newManifest , m , err := cryptChildren (ctx , cs , manifest , cc , lf , cryptoOp , manifest .Platform )
379345 if err != nil || cryptoOp == cryptoOpUnwrapOnly {
380346 return ocispec.Descriptor {}, false , err
381347 }
@@ -410,19 +376,6 @@ func cryptManifestList(ctx context.Context, cs content.Store, ls leases.Manager,
410376
411377 ref := fmt .Sprintf ("index-%s" , newDesc .Digest .String ())
412378
413- if ls == nil {
414- return ocispec.Descriptor {}, false , errors .New ("Unexpected write to object without lease" )
415- }
416-
417- rsrc := leases.Resource {
418- ID : desc .Digest .String (),
419- Type : "content" ,
420- }
421-
422- if err := ls .AddResource (ctx , l , rsrc ); err != nil {
423- return ocispec.Descriptor {}, false , errors .Wrap (err , "Unable to add resource to lease" )
424- }
425-
426379 if err = content .WriteBlob (ctx , cs , ref , bytes .NewReader (mb ), newDesc , content .WithLabels (labels )); err != nil {
427380 return ocispec.Descriptor {}, false , errors .Wrap (err , "failed to write index" )
428381 }
@@ -434,28 +387,28 @@ func cryptManifestList(ctx context.Context, cs content.Store, ls leases.Manager,
434387
435388// cryptImage is the dispatcher to encrypt/decrypt an image; it accepts either an OCI descriptor
436389// representing a manifest list or a single manifest
437- func cryptImage (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
390+ func cryptImage (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter , cryptoOp cryptoOp ) (ocispec.Descriptor , bool , error ) {
438391 if cc == nil {
439392 return ocispec.Descriptor {}, false , errors .Wrapf (errdefs .ErrInvalidArgument , "CryptoConfig must not be nil" )
440393 }
441394 switch desc .MediaType {
442395 case ocispec .MediaTypeImageIndex , images .MediaTypeDockerSchema2ManifestList :
443- return cryptManifestList (ctx , cs , ls , l , desc , cc , lf , cryptoOp )
396+ return cryptManifestList (ctx , cs , desc , cc , lf , cryptoOp )
444397 case ocispec .MediaTypeImageManifest , images .MediaTypeDockerSchema2Manifest :
445- return cryptManifest (ctx , cs , ls , l , desc , cc , lf , cryptoOp )
398+ return cryptManifest (ctx , cs , desc , cc , lf , cryptoOp )
446399 default :
447400 return ocispec.Descriptor {}, false , errors .Errorf ("CryptImage: Unhandled media type: %s" , desc .MediaType )
448401 }
449402}
450403
451404// EncryptImage encrypts an image; it accepts either an OCI descriptor representing a manifest list or a single manifest
452- func EncryptImage (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter ) (ocispec.Descriptor , bool , error ) {
453- return cryptImage (ctx , cs , ls , l , desc , cc , lf , cryptoOpEncrypt )
405+ func EncryptImage (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter ) (ocispec.Descriptor , bool , error ) {
406+ return cryptImage (ctx , cs , desc , cc , lf , cryptoOpEncrypt )
454407}
455408
456409// DecryptImage decrypts an image; it accepts either an OCI descriptor representing a manifest list or a single manifest
457- func DecryptImage (ctx context.Context , cs content.Store , ls leases. Manager , l leases. Lease , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter ) (ocispec.Descriptor , bool , error ) {
458- return cryptImage (ctx , cs , ls , l , desc , cc , lf , cryptoOpDecrypt )
410+ func DecryptImage (ctx context.Context , cs content.Store , desc ocispec.Descriptor , cc * encconfig.CryptoConfig , lf LayerFilter ) (ocispec.Descriptor , bool , error ) {
411+ return cryptImage (ctx , cs , desc , cc , lf , cryptoOpDecrypt )
459412}
460413
461414// CheckAuthorization checks whether a user has the right keys to be allowed to access an image (every layer)
@@ -465,11 +418,12 @@ func CheckAuthorization(ctx context.Context, cs content.Store, desc ocispec.Desc
465418 cc := encconfig.CryptoConfig {
466419 DecryptConfig : dc ,
467420 }
421+
468422 lf := func (desc ocispec.Descriptor ) bool {
469423 return true
470424 }
471- // We shouldn't need to create any objects in CheckAuthorization, so no lease required.
472- _ , _ , err := cryptImage (ctx , cs , nil , leases. Lease {}, desc , & cc , lf , cryptoOpUnwrapOnly )
425+
426+ _ , _ , err := cryptImage (ctx , cs , desc , & cc , lf , cryptoOpUnwrapOnly )
473427 if err != nil {
474428 return errors .Wrapf (err , "you are not authorized to use this image" )
475429 }
0 commit comments