From a467a9560be5e5614e192109143cfdc7eca3669d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 2 Jul 2022 03:02:14 +0200 Subject: [PATCH] Add internal/imagesource/impl.NoSignatures, use it to reduce boilerplate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Should not change behavior. Signed-off-by: Miloslav Trmač --- docker/internal/tarfile/src.go | 12 +----------- internal/imagesource/impl/signatures.go | 18 ++++++++++++++++++ oci/layout/oci_src.go | 9 +-------- sif/src.go | 12 +----------- tarball/tarball_src.go | 11 +---------- 5 files changed, 22 insertions(+), 40 deletions(-) create mode 100644 internal/imagesource/impl/signatures.go diff --git a/docker/internal/tarfile/src.go b/docker/internal/tarfile/src.go index 7be3999a4..f5cfefe84 100644 --- a/docker/internal/tarfile/src.go +++ b/docker/internal/tarfile/src.go @@ -26,6 +26,7 @@ import ( // Source is a partial implementation of types.ImageSource for reading from tarPath. type Source struct { impl.PropertyMethodsInitialize + impl.NoSignatures impl.DoesNotAffectLayerInfosForCopy stubs.NoGetBlobAtInitialize @@ -314,14 +315,3 @@ func (s *Source) GetBlob(ctx context.Context, info types.BlobInfo, cache types.B return nil, 0, fmt.Errorf("Unknown blob %s", info.Digest) } - -// GetSignatures returns the image's signatures. It may use a remote (= slow) service. -// This source implementation does not support manifest lists, so the passed-in instanceDigest should always be nil, -// as there can be no secondary manifests. -func (s *Source) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) { - if instanceDigest != nil { - // How did we even get here? GetManifest(ctx, nil) has returned a manifest.DockerV2Schema2MediaType. - return nil, errors.New(`Manifest lists are not supported by "docker-daemon:"`) - } - return [][]byte{}, nil -} diff --git a/internal/imagesource/impl/signatures.go b/internal/imagesource/impl/signatures.go new file mode 100644 index 000000000..ee25152b5 --- /dev/null +++ b/internal/imagesource/impl/signatures.go @@ -0,0 +1,18 @@ +package impl + +import ( + "context" + + "github.com/opencontainers/go-digest" +) + +// NoSignatures implements GetSignatures() that returns nothing. +type NoSignatures struct{} + +// GetSignatures returns the image's signatures. It may use a remote (= slow) service. +// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for +// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list +// (e.g. if the source never returns manifest lists). +func (stub NoSignatures) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) { + return nil, nil +} diff --git a/oci/layout/oci_src.go b/oci/layout/oci_src.go index d7ae69e48..db627015c 100644 --- a/oci/layout/oci_src.go +++ b/oci/layout/oci_src.go @@ -23,6 +23,7 @@ import ( type ociImageSource struct { impl.PropertyMethodsInitialize + impl.NoSignatures impl.DoesNotAffectLayerInfosForCopy stubs.NoGetBlobAtInitialize @@ -150,14 +151,6 @@ func (s *ociImageSource) GetBlob(ctx context.Context, info types.BlobInfo, cache return r, fi.Size(), nil } -// GetSignatures returns the image's signatures. It may use a remote (= slow) service. -// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for -// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list -// (e.g. if the source never returns manifest lists). -func (s *ociImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) { - return [][]byte{}, nil -} - // getExternalBlob returns the reader of the first available blob URL from urls, which must not be empty. // This function can return nil reader when no url is supported by this function. In this case, the caller // should fallback to fetch the non-external blob (i.e. pull from the registry). diff --git a/sif/src.go b/sif/src.go index 0aab2ef57..a90d70fcd 100644 --- a/sif/src.go +++ b/sif/src.go @@ -23,6 +23,7 @@ import ( type sifImageSource struct { impl.PropertyMethodsInitialize + impl.NoSignatures impl.DoesNotAffectLayerInfosForCopy stubs.NoGetBlobAtInitialize @@ -198,14 +199,3 @@ func (s *sifImageSource) GetManifest(ctx context.Context, instanceDigest *digest } return s.manifest, imgspecv1.MediaTypeImageManifest, nil } - -// GetSignatures returns the image's signatures. It may use a remote (= slow) service. -// If instanceDigest is not nil, it contains a digest of the specific manifest instance to retrieve signatures for -// (when the primary manifest is a manifest list); this never happens if the primary manifest is not a manifest list -// (e.g. if the source never returns manifest lists). -func (s *sifImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) { - if instanceDigest != nil { - return nil, errors.New("manifest lists are not supported by the sif transport") - } - return nil, nil -} diff --git a/tarball/tarball_src.go b/tarball/tarball_src.go index 832fbee82..039df406c 100644 --- a/tarball/tarball_src.go +++ b/tarball/tarball_src.go @@ -22,6 +22,7 @@ import ( type tarballImageSource struct { impl.PropertyMethodsInitialize + impl.NoSignatures impl.DoesNotAffectLayerInfosForCopy stubs.NoGetBlobAtInitialize @@ -252,16 +253,6 @@ func (is *tarballImageSource) GetManifest(ctx context.Context, instanceDigest *d return is.manifest, imgspecv1.MediaTypeImageManifest, nil } -// GetSignatures returns the image's signatures. It may use a remote (= slow) service. -// This source implementation does not support manifest lists, so the passed-in instanceDigest should always be nil, -// as there can be no secondary manifests. -func (*tarballImageSource) GetSignatures(ctx context.Context, instanceDigest *digest.Digest) ([][]byte, error) { - if instanceDigest != nil { - return nil, fmt.Errorf("manifest lists are not supported by the %q transport", transportName) - } - return nil, nil -} - func (is *tarballImageSource) Reference() types.ImageReference { return &is.reference }