Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(*libimage.Image).HasDifferentDigest: add authentication #775

Merged
merged 1 commit into from Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion libimage/image.go
Expand Up @@ -715,10 +715,18 @@ func (i *Image) Size() (int64, error) {
return i.runtime.store.ImageSize(i.ID())
}

// HasDifferentDigestOptions allows for customizing the check if another
// (remote) image has a different digest.
type HasDifferentDigestOptions struct {
// containers-auth.json(5) file to use when authenticating against
// container registries.
AuthFilePath string
}

// HasDifferentDigest returns true if the image specified by `remoteRef` has a
// different digest than the local one. This check can be useful to check for
// updates on remote registries.
func (i *Image) HasDifferentDigest(ctx context.Context, remoteRef types.ImageReference) (bool, error) {
func (i *Image) HasDifferentDigest(ctx context.Context, remoteRef types.ImageReference, options *HasDifferentDigestOptions) (bool, error) {
// We need to account for the arch that the image uses. It seems
// common on ARM to tweak this option to pull the correct image. See
// github.com/containers/podman/issues/6613.
Expand All @@ -738,6 +746,14 @@ func (i *Image) HasDifferentDigest(ctx context.Context, remoteRef types.ImageRef
sys.VariantChoice = inspectInfo.Variant
}

if options != nil && options.AuthFilePath != "" {
sys.AuthFilePath = options.AuthFilePath
}

return i.hasDifferentDigestWithSystemContext(ctx, remoteRef, sys)
}

func (i *Image) hasDifferentDigestWithSystemContext(ctx context.Context, remoteRef types.ImageReference, sys *types.SystemContext) (bool, error) {
remoteImg, err := remoteRef.NewImage(ctx, sys)
if err != nil {
return false, err
Expand Down
4 changes: 2 additions & 2 deletions libimage/image_test.go
Expand Up @@ -134,14 +134,14 @@ func TestImageFunctions(t *testing.T) {
// Same image -> same digest
remoteRef, err := alltransports.ParseImageName("docker://" + busyboxDigest)
require.NoError(t, err)
hasDifferentDigest, err := image.HasDifferentDigest(ctx, remoteRef)
hasDifferentDigest, err := image.HasDifferentDigest(ctx, remoteRef, nil)
require.NoError(t, err)
require.False(t, hasDifferentDigest, "image with same digest should have the same manifest (and hence digest)")

// Different images -> different digests
remoteRef, err = alltransports.ParseImageName("docker://docker.io/library/alpine:latest")
require.NoError(t, err)
hasDifferentDigest, err = image.HasDifferentDigest(ctx, remoteRef)
hasDifferentDigest, err = image.HasDifferentDigest(ctx, remoteRef, nil)
require.NoError(t, err)
require.True(t, hasDifferentDigest, "another image should have a different digest")

Expand Down
2 changes: 1 addition & 1 deletion libimage/pull.go
Expand Up @@ -561,7 +561,7 @@ func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName str
}

if pullPolicy == config.PullPolicyNewer && localImage != nil {
isNewer, err := localImage.HasDifferentDigest(ctx, srcRef)
isNewer, err := localImage.hasDifferentDigestWithSystemContext(ctx, srcRef, c.systemContext)
if err != nil {
pullErrors = append(pullErrors, err)
continue
Expand Down