Skip to content

Commit

Permalink
local image lookup by digest
Browse files Browse the repository at this point in the history
Detect local-image lookups by digest.  Those clearly refer to local
images only, so we must not proceed to remote lookups.

Fixes: containers#2836
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Jan 25, 2021
1 parent 1a04337 commit 20ee3ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
return nil, "", nil, err
}

// If we could resolve the image locally, check if it was referenced by
// ID. In that case, we don't need to bother any further and can
// prevent prompting the user.
if localImage != nil && strings.HasPrefix(localImage.ID, options.FromImage) {
// If we could resolve the image locally, check if it was clearly
// referring to a local image, either by ID or digest. In that case,
// we don't need to perform a remote lookup.
if localImage != nil && (strings.HasPrefix(localImage.ID, options.FromImage) || strings.HasPrefix(options.FromImage, "sha256:")) {
return localImageRef, localImageRef.Transport().Name(), localImage, nil
}

Expand Down
12 changes: 12 additions & 0 deletions tests/from.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ load helpers
check_options_flag_err "--cred=fake fake"
}

@test "from-with-digest" {
run_buildah pull alpine
run_buildah inspect --format "{{.FromImageDigest}}" alpine
digest=$output

run_buildah from $digest
run_buildah rm $output

run_buildah 125 from sha256:1111111111111111111111111111111111111111111111111111111111111111
expect_output --substring "error resolving local image \"sha256:1111111111111111111111111111111111111111111111111111111111111111\": referenced by digest but could not be found in local storage"
}

@test "commit-to-from-elsewhere" {
elsewhere=${TESTDIR}/elsewhere-img
mkdir -p ${elsewhere}
Expand Down
16 changes: 16 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/containers/image/v5/types"
"github.com/containers/storage"
"github.com/docker/distribution/registry/api/errcode"
"github.com/opencontainers/go-digest"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -68,6 +69,21 @@ func ResolveName(name string, firstRegistry string, sc *types.SystemContext, sto
return []string{img.ID}, "", false, nil
}
}
if strings.HasPrefix(name, "sha256:") {
d, err := digest.Parse(name)
if err != nil {
return nil, "", false, err
}
matches, err := store.ImagesByDigest(d)
if err != nil {
return nil, "", false, err
}
if len(matches) == 0 {
// NOTE: this error is wrapped by callers who add `name`.
return nil, "", false, errors.New("referenced by digest but could not be found in local storage")
}
return []string{matches[0].ID}, "", false, nil
}

// Transports are not supported for local image look ups.
srcRef, err := alltransports.ParseImageName(name)
Expand Down

0 comments on commit 20ee3ac

Please sign in to comment.