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

Enrich discover --verbose command tree output with additional data (Layers, data from Descriptor) #1501

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bedla
Copy link

@bedla bedla commented Sep 17, 2024

WIP

Hi,
when I was investigating why Trivy does not see SBOMs attached to Harbor Images (it was because old Harbor v2.10.2 was malformes ArtifactType of attached SBOM JSON. It always override to application/vnd.oci.empty.v1+json. In latest version of Harbor it is fixed), I found out that it is good to see (at that time only in debugger) some additional data about Images.
What helped me, was to see MediaType from Descriptor, and MediaType from Layers.

What do you think about those additions to --verbose mode of discover command?

Thank you

Ivos

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

Output looks like this:

localhost:8888/my-project/demo@sha256:901cf8eb9b8b4be961247fdfdcc87471b06461357a44fb05e60401b9f78670b9
└── application/vnd.cyclonedx+json
    └── sha256:23dbde23c33a226959ea7733abeed67922069d3c0a93a8d88589e6c478539817
        ├── Descriptor
        │   ├── MediaType: application/vnd.oci.image.manifest.v1+json
        │   └── Size: 821
        ├── Annotations
        │   ├── created-by: Ivos
        │   └── org.opencontainers.image.created: "2024-09-16T07:03:43Z"
        └── Layers
            └── Layer[0]
                ├── annotations
                │   └── org.opencontainers.image.title: C:\Users\bedla.czech\GolandProjects\trivy\result.json
                ├── digest: sha256:95340261766214250b2238f4a8055bbd8b9f5b6203a30ca6e0e59c9348d5c674
                ├── mediaType: application/vnd.cyclonedx+json
                └── size: 36449

@FeynmanZhou
Copy link
Member

FeynmanZhou commented Sep 19, 2024

Hi @bedla , thanks for your contribution. Would you mind raising an issue to discuss your idea? We are also working on the verbose output improvement and defining a proposal doc in #1483

Copy link
Member

@TerryHowe TerryHowe left a comment

Choose a reason for hiding this comment

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

I'm not sure if this feature is wanted, but some general comments

Comment on lines +124 to +137
fetchOpts := oras.DefaultFetchBytesOptions
fetchOpts.TargetPlatform = platform.Platform
_, content, err := oras.FetchBytes(ctx, target, digest.String(), fetchOpts)
if err != nil {
return false, fmt.Errorf("failed to fetch the content of %q: %w", "", err)
}

var jsonMap map[string]any
if err = json.Unmarshal(content, &jsonMap); err != nil {
return false, err
}

if layers, ok := jsonMap["layers"].([]interface{}); ok {
if len(layers) != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

This whole thing seems to me like a method fetchLayers or something which returns layers or error

Comment on lines +90 to +101
func addDescriptorInfo(referrer ocispec.Descriptor, referrerNode *tree.Node) {
descriptorNode := referrerNode.AddPath("Descriptor")
descriptorNode.AddPath(strings.TrimSpace("MediaType: " + referrer.MediaType))
descriptorNode.AddPath(strings.TrimSpace("Size: " + strconv.FormatInt(referrer.Size, 10)))
}

func addAnnotationsInfo(referrer ocispec.Descriptor, referrerNode *tree.Node) error {
if len(referrer.Annotations) != 0 {
annotationsNode := referrerNode.AddPath("Annotations")
annotationsKeys := sortedKeys(referrer.Annotations)
for _, k := range annotationsKeys {
v := referrer.Annotations[k]
Copy link
Member

Choose a reason for hiding this comment

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

Seems like it would be easier to unit test this stuff if it was a wrapper for referrerNode and these were addDescriptor, addAnnotations, etc.

v := annotationsMap[k]
bytes, err := yaml.Marshal(map[string]interface{}{k: v})
if err != nil {
continue
Copy link
Member

Choose a reason for hiding this comment

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

This is too deep, something needs to be broken up here

results, err := registry.Referrers(ctx, repo, desc, artifactType)
if err != nil {
return err
}

for _, r := range results {
if err := handler.OnDiscovered(r, desc); err != nil {
if err := handler.OnDiscovered(r, desc, ctx, repo, platform); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Not loving passing ctx in the middle here. I haven't looked everything over, but seems like this should be passing down a fetch method around here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants