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

fix : support use image id arg scan vulnerability for grype #179

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pkg/image/docker/daemon_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,31 @@ import (

// DaemonImageProvider is a image.Provider capable of fetching and representing a docker image from the docker daemon API.
type DaemonImageProvider struct {
imageStr string
tmpDirGen *file.TempDirGenerator
client client.APIClient
platform *image.Platform
imageStr string
originalImageRef string
tmpDirGen *file.TempDirGenerator
client client.APIClient
platform *image.Platform
}

// NewProviderFromDaemon creates a new provider instance for a specific image that will later be cached to the given directory.
func NewProviderFromDaemon(imgStr string, tmpDirGen *file.TempDirGenerator, c client.APIClient, platform *image.Platform) (*DaemonImageProvider, error) {
var originalRef string
ref, err := name.ParseReference(imgStr, name.WithDefaultRegistry(""))
if err != nil {
return nil, err
}
tag, ok := ref.(name.Tag)
if ok {
imgStr = tag.Name()
originalRef = tag.String() // blindly takes the original input passed into Tag
}
return &DaemonImageProvider{
imageStr: imgStr,
tmpDirGen: tmpDirGen,
client: c,
platform: platform,
imageStr: imgStr,
originalImageRef: originalRef,
tmpDirGen: tmpDirGen,
client: c,
platform: platform,
}, nil
}

Expand Down Expand Up @@ -314,6 +318,12 @@ func (p *DaemonImageProvider) saveImage(ctx context.Context) (string, error) {
func (p *DaemonImageProvider) pullImageIfMissing(ctx context.Context) error {
// check if the image exists locally
inspectResult, _, err := p.client.ImageInspectWithRaw(ctx, p.imageStr)
if err != nil {
inspectResult, _, err = p.client.ImageInspectWithRaw(ctx, p.originalImageRef)
if err == nil {
p.imageStr = strings.TrimSuffix(p.imageStr, ":latest")
}
}
if err != nil {
if client.IsErrNotFound(err) {
if err = p.pull(ctx); err != nil {
Expand All @@ -324,7 +334,7 @@ func (p *DaemonImageProvider) pullImageIfMissing(ctx context.Context) error {
}
} else {
// looks like the image exists, but if the platform doesn't match what the user specified, we may need to
// pull the image again with the correct platofmr specifier, which will override the local tag.
// pull the image again with the correct platform specifier, which will override the local tag.
if err := p.validatePlatform(inspectResult); err != nil {
if err = p.pull(ctx); err != nil {
return err
Expand Down
Loading