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

feat(cli): Add available version checking #8553

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

owenrumney
Copy link
Member

@owenrumney owenrumney commented Mar 14, 2025

Description

Adds a background check to https://api.trivy.cloud/check to see if there is new version or any relevant notices available.

The check will be suppressed if the user uses the --no-notices or --quiet envvars or flags. The docs have been updated with the new notices flag

Example output

image

Although the image shows dummy versions, the api has been updated to reflect the correct information and has no announcements at this time... just the latest version (0.60.0)

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@owenrumney owenrumney self-assigned this Mar 14, 2025
@owenrumney owenrumney marked this pull request as ready for review March 14, 2025 11:44
@owenrumney owenrumney requested a review from knqyf263 as a code owner March 14, 2025 11:44
@knqyf263 knqyf263 requested a review from DmitriyLewen March 17, 2025 02:44
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

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

@owenrumney left comments.
Take a look, please.

Also I think update is not a very good name for this logic.
Maybe something like notification?

UPD:
and I would also think about how we can inform users about the ---no-notises flag

Comment on lines 59 to 62
req.Header.Set("-x-trivy-identifier", uniqueIdentifier())
req.Header.Set("-x-trivy-command", strings.Join(args, " "))
req.Header.Set("-x-trivy-os", runtime.GOOS)
req.Header.Set("-x-trivy-arch", runtime.GOARCH)
Copy link
Contributor

Choose a reason for hiding this comment

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

We take information about PC components.
Do we need it?

OS and architecture are not a big problem.
But I am worried that users will ask why we need information about network interfaces, mac address, etc.

Copy link
Member Author

@owenrumney owenrumney Mar 17, 2025

Choose a reason for hiding this comment

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

We're not taking the nic or mac details and forwarding them on, we're using them to generate a unique identifier for the user - it's a one way hash purely as an ID

Copy link
Contributor

Choose a reason for hiding this comment

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

i didn't see hash - identifier is MacBook-Pro-Dmitrij.local-xx:xx:xx:xx:xx:db-macbook-pro-dmitrij.local

Copy link
Contributor

Choose a reason for hiding this comment

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

we're using them to generate a unique identifier for the user

why does API need this ID?

Copy link
Member Author

Choose a reason for hiding this comment

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

i didn't see hash - identifier is MacBook-Pro-Dmitrij.local-xx:xx:xx:xx:xx:db-macbook-pro-dmitrij.local

do you mean you saw this in the TestGetMachineIdentifier test? I'm not sure where that line is output.

Copy link
Contributor

Choose a reason for hiding this comment

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

@owenrumney owenrumney marked this pull request as draft March 17, 2025 11:54
- Update the preRun and postRun to check and print the latest version
  - Run in a go routine so as not to interfere or slow the normal flow
  - Provide new `--no-notices` flag to prevent notice checking, same if
    they `-q/--quiet` flag is used
- Add tests for the identifier and the check logic
@owenrumney owenrumney force-pushed the feat/add-update-checking branch from 38dc33e to 4a269dd Compare March 17, 2025 13:44
- move the package from update to notifications
- use a version-check prefix on the debug logs
- move the `--no-notices` flag to the scan flags
  - update the tests to include a flag test
- regenerate the docs
@owenrumney owenrumney force-pushed the feat/add-update-checking branch from 4a269dd to be383c3 Compare March 17, 2025 13:46
@@ -135,6 +137,13 @@ func NewRunner(ctx context.Context, cliOptions flag.Options, opts ...RunnerOptio
m.Register()
r.module = m

// Make a silent attempt to check for updates in the background
Copy link
Member Author

Choose a reason for hiding this comment

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

@DmitriyLewen - I'm not sure if this is the optimal place to hook into Scan runs to trigger the best efforts check for notices. Happy to move if somewhere more appropriate can be suggested

Copy link
Contributor

Choose a reason for hiding this comment

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

I think your previous place was better.
You run the check before rootCommand (PersistentPreRunE) and show notifications after rootCommand (PersistentPostRun).

Both commands are on the same level

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm trying to sasify you suggestion that it only run on scan commands. If its in the root command there will need to be some inspection of the called sub command to ensure its a scan.

As it's a best efforts request, I think I should go back to it being run as part of the preRun and all commands get it so --no-notices goes back to being a global flag. wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

I looked at the current implementation again. Let's leave it like this. Given that we only need notifications when scanning, this would be the optimal place.
Thanks for clarifying this nuance.

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.

feat(cli): Add new version checking
2 participants