-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
owenrumney
wants to merge
6
commits into
main
Choose a base branch
from
feat/add-update-checking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fac6271
feat(cli): Add version check logic
owenrumney dc27ddd
docs(flags): Regenerate the docs to get the new CLI flag
owenrumney f0b9322
chore: rename the updated atomic to make more sense
owenrumney be383c3
rearrange structure and make scan only
owenrumney d170b43
test: fix tests
owenrumney fe7b0ac
Merge branch 'main' into feat/add-update-checking
owenrumney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package notification | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func getMachineIdentifier() (string, error) { | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
return "", err | ||
} | ||
interfaces, err := net.Interfaces() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var macAddr string | ||
for _, iface := range interfaces { | ||
if iface.HardwareAddr.String() != "" { | ||
macAddr = iface.HardwareAddr.String() | ||
break | ||
} | ||
} | ||
identifier := fmt.Sprintf("%s-%s-%s", hostname, macAddr, strings.ToLower(hostname)) | ||
|
||
return identifier, nil | ||
} | ||
|
||
func generateMachineHash(identifier string) string { | ||
hash := sha256.New() | ||
hash.Write([]byte(identifier)) | ||
return fmt.Sprintf("%x", hash.Sum(nil)) | ||
} | ||
|
||
func uniqueIdentifier() string { | ||
identifier, err := getMachineIdentifier() | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return generateMachineHash(fmt.Sprintf("trivy-%s", identifier)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package notification | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenerateMachineHash(t *testing.T) { | ||
// Test with known input | ||
identifier := "test-identifier" | ||
hash := generateMachineHash(identifier) | ||
|
||
// Known hash for "test-identifier" | ||
expectedHash := "115ae872eb1d3e23f9de03f7ab344193b21068812ee52eb37e8169e6d093c7ae" | ||
assert.Equal(t, expectedHash, hash) | ||
} | ||
|
||
// This test requires some modification to the original code to make it testable | ||
// by injecting mocked network interfaces | ||
func TestGetMachineIdentifier(t *testing.T) { | ||
// This is a basic test that at least ensures the function returns without error | ||
// A more complete test would mock os.Hostname and net.Interfaces | ||
identifier, err := getMachineIdentifier() | ||
require.NoError(t, err) | ||
require.NotEmpty(t, identifier) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.