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: return proper image digest #2310

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions pkg/plugins/trivy/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"
"path/filepath"
"strings"

"github.com/aquasecurity/trivy-operator/pkg/exposedsecretreport"
"github.com/aquasecurity/trivy-operator/pkg/sbomreport"
Expand Down Expand Up @@ -154,7 +155,15 @@ func (p *plugin) ParseReportData(ctx trivyoperator.PluginContext, imageRef strin
return vulnReport, secretReport, nil, err
}

registry, artifact, err := p.parseImageRef(imageRef, reports.Metadata.ImageID)
// Note: every Docker image is associated with 2 different SHAs:
// - image digest (this is what K8s shows in imageID field in kubectl get pod -o yaml) - and, for example,
// kube-prometheus-stack reports this value in kube_pod_container_info metric
// - image ID (this is what is visible in 'docker image ls command')
// Execute docker image ls --digests --no-trunc <image> to see both digest and ID.
// See https://stackoverflow.com/questions/56364643/whats-the-difference-between-a-docker-images-image-id-and-its-digest
var imageDigest = strings.Split(reports.Metadata.RepoDigests[0], "@")[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d-mankowski-synerise any reason to strip the sha256@ prefix?

Copy link
Contributor Author

@d-mankowski-synerise d-mankowski-synerise Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copying from the issue you raised:

    "RepoDigests": [
      "<registry>/<image repo>@sha256:cf3e3b3a98edde46eac58d4745a2467b9da2bc49a11191565a893ffac38034a3"
    ],

so it is not stripping sha256 prefix, but <registry>/<repo>@ part

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh, right! mixed up the delimiters 👍🏼


registry, artifact, err := p.parseImageRef(imageRef, imageDigest)
if err != nil {
return vulnReport, secretReport, nil, err
}
Expand Down Expand Up @@ -212,7 +221,7 @@ func (p *plugin) NewConfigForConfigAudit(ctx trivyoperator.PluginContext) (confi
return getConfig(ctx)
}

func (p *plugin) parseImageRef(imageRef string, imageID string) (v1alpha1.Registry, v1alpha1.Artifact, error) {
func (p *plugin) parseImageRef(imageRef string, imageDigest string) (v1alpha1.Registry, v1alpha1.Artifact, error) {
ref, err := containerimage.ParseReference(imageRef)
if err != nil {
return v1alpha1.Registry{}, v1alpha1.Artifact{}, err
Expand All @@ -230,7 +239,7 @@ func (p *plugin) parseImageRef(imageRef string, imageID string) (v1alpha1.Regist
artifact.Digest = t.DigestStr()
}
if len(artifact.Digest) == 0 {
artifact.Digest = imageID
artifact.Digest = imageDigest
}
return registry, artifact, nil
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/plugins/trivy/testdata/fixture/full_report.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"Family": "alpine",
"Name": "3.10.2",
"EOSL": true
}
},
"RepoDigests": [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really sure why RepoDigests and other parameters are not included in this report (which, I assume, is the output of trivy i --format json alpine:3.10.2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, looks like the fixture is incomplete, might have been generated with an older version of Trivy?

"alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb"
]
},
"Results": [
{
Expand Down Expand Up @@ -90,4 +93,4 @@
]
}
]
}
}
7 changes: 5 additions & 2 deletions pkg/plugins/trivy/testdata/fixture/vulnerability_report.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"Family": "alpine",
"Name": "3.10.2",
"EOSL": true
}
},
"RepoDigests": [
"alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb"
]
},
"Results": [
{
Expand Down Expand Up @@ -42,4 +45,4 @@
]
}
]
}
}
Loading