8000 `db list` should render out full URLs for text format by wagoodman · Pull Request #2553 · anchore/grype · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

db list should render out full URLs for text format #2553

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

Merged
merged 2 commits into from
Mar 20, 2025
Merged
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
26 changes: 18 additions & 8 deletions cmd/grype/cli/commands/db_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/url"
"os"
"path"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -62,28 +61,39 @@ func runDBList(opts dbListOptions) error {
return fmt.Errorf("unable to get database listing: %w", err)
}

return presentDBList(opts.Output, opts.DB.UpdateURL, os.Stdout, latest)
u, err := c.ResolveArchiveURL(latest.Archive)
if err != nil {
return fmt.Errorf("unable to resolve database URL: %w", err)
}

return presentDBList(opts.Output, u, opts.DB.UpdateURL, os.Stdout, latest)
}

func presentDBList(format string, u string, writer io.Writer, latest *distribution.LatestDocument) error {
func presentDBList(format string, archiveURL, listingURL string, writer io.Writer, latest *distribution.LatestDocument) error {
if latest == nil {
return fmt.Errorf("no database listing found")
}

parsedURL, err := url.Parse(u)
// remove query params
archiveURLObj, err := url.Parse(archiveURL)
if err != nil {
return fmt.Errorf("failed to parse base URL: %w", err)
return fmt.Errorf("unable to parse db URL %q: %w", archiveURL, err)
}

parsedURL.Path = path.Join(path.Dir(parsedURL.Path), latest.Path)
archiveURLObj.RawQuery = ""

if listingURL == distribution.DefaultConfig().LatestURL {
// append on the schema
listingURL = fmt.Sprintf("%s/v%v/%s", listingURL, latest.SchemaVersion.Model, distribution.LatestFileName)
}

switch format {
case textOutputFormat:
fmt.Fprintf(writer, "Status: %s\n", latest.Status)
fmt.Fprintf(writer, "Schema: %s\n", latest.SchemaVersion.String())
fmt.Fprintf(writer, "Built: %s\n", latest.Built.String())
fmt.Fprintf(writer, "Listing: %s\n", u)
fmt.Fprintf(writer, "DB URL: %s\n", parsedURL.String())
fmt.Fprintf(writer, "Listing: %s\n", listingURL)
fmt.Fprintf(writer, "DB URL: %s\n", archiveURLObj.String())
fmt.Fprintf(writer, "Checksum: %s\n", latest.Checksum)
case jsonOutputFormat, "raw":
enc := json.NewEncoder(writer)
Expand Down
28 changes: 23 additions & 5 deletions cmd/grype/cli/commands/db_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func Test_ListingUserAgent(t *testing.T) {
}

func TestPresentDBList(t *testing.T) {
baseURL := "http://localhost:8000/latest.json"
latestDoc := &distribution.LatestDocument{
Status: "active",
Archive: distribution.Archive{
Expand All @@ -82,20 +81,39 @@ func TestPresentDBList(t *testing.T) {
tests := []struct {
name string
format string
baseURL string
archiveURL string
latest *distribution.LatestDocument
expectedText string
expectedErr require.ErrorAssertionFunc
}{
{
name: "valid text format",
format: textOutputFormat,
latest: latestDoc,
name: "valid text format",
format: textOutputFormat,
latest: latestDoc,
baseURL: "http://localhost:8000/latest.json",
archiveURL: "http://localhost:8000/vulnerability-db_v6.0.0_2024-11-25T01:31:56Z_1732718597.tar.zst",
expectedText: `Status: active
Schema: v6.0.0
Built: 2024-11-27T14:43:17Z
Listing: http://localhost:8000/latest.json
DB URL: http://localhost:8000/vulnerability-db_v6.0.0_2024-11-25T01:31:56Z_1732718597.tar.zst
Checksum: sha256:16bcb6551c748056f752f299fcdb4fa50fe61589d086be3889e670261ff21ca4
`,
expectedErr: require.NoError,
},
{
name: "complete default values",
format: textOutputFormat,
latest: latestDoc,
baseURL: "https://grype.anchore.io/databases",
archiveURL: "https://grype.anchore.io/databases/v6/vulnerability-db_v6.0.0_2024-11-25T01:31:56Z_1732718597.tar.zst",
expectedText: `Status: active
Schema: v6.0.0
Built: 2024-11-27T14:43:17Z
Listing: https://grype.anchore.io/databases/v6/latest.json
DB URL: https://grype.anchore.io/databases/v6/vulnerability-db_v6.0.0_2024-11-25T01:31:56Z_1732718597.tar.zst
Checksum: sha256:16bcb6551c748056f752f299fcdb4fa50fe61589d086be3889e670261ff21ca4
`,
expectedErr: require.NoError,
},
Expand Down Expand Up @@ -133,7 +151,7 @@ Checksum: sha256:16bcb6551c748056f752f299fcdb4fa50fe61589d086be3889e670261ff21ca
t.Run(tt.name, func(t *testing.T) {
writer := &bytes.Buffer{}

err := presentDBList(tt.format, baseURL, writer, tt.latest)
err := presentDBList(tt.format, tt.archiveURL, tt.baseURL, writer, tt.latest)
if tt.expectedErr == nil {
tt.expectedErr = require.NoError
}
Expand Down
Loading
0