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

cli: add command to convert Vulnerability Reports #194

Merged
merged 1 commit into from
Nov 25, 2024
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
86 changes: 86 additions & 0 deletions cmd/clair-action/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/quay/clair-action/output"
"github.com/quay/claircore"
"github.com/urfave/cli/v2"
)

var convertCmd = &cli.Command{
RTann marked this conversation as resolved.
Show resolved Hide resolved
Name: "convert",
Aliases: []string{"c"},
Usage: "Convert a Clair Vulnerability report to the Quay secscan or Sarif format",
Action: convert,
Flags: []cli.Flag{
&cli.PathFlag{
Name: "file-path",
Value: "",
Usage: "where to look for the Clair Vulnerability report to convert",
EnvVars: []string{"FILE_PATH"},
},
&cli.GenericFlag{
Name: "format",
Aliases: []string{"f"},
Value: &EnumValue{
Enum: []string{sarifFmt, quayFmt},
Default: quayFmt,
},
Usage: "what output format the results should be in",
EnvVars: []string{"FORMAT"},
},
},
}

func convert(c *cli.Context) error {
var (
// All arguments
filePath = c.String("file-path")
format = c.String("format")
)
vulnReportFile, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}

defer vulnReportFile.Close()

bs, err := io.ReadAll(vulnReportFile)
if err != nil {
return fmt.Errorf("error reading file data: %v", err)
}

var vr claircore.VulnerabilityReport
err = json.Unmarshal(bs, &vr)
if err != nil {
return fmt.Errorf("error unmarshaling Vulnerability report: %v", err)
}
switch format {
case sarifFmt:
tw, err := output.NewSarifWriter(os.Stdout)
if err != nil {
return fmt.Errorf("error creating sarif report writer: %v", err)
}
err = tw.Write(&vr)
if err != nil {
return fmt.Errorf("error writing sarif report: %v", err)
}
case quayFmt:
quayReport, err := output.ReportToSecScan(&vr)
if err != nil {
return fmt.Errorf("error creating quay format report: %v", err)
}
b, err := json.MarshalIndent(quayReport, "", " ")
if err != nil {
return fmt.Errorf("could not marshal quay report: %v", err)
}
fmt.Println(string(b))
default:
return fmt.Errorf("unrecognized format: %q", format)
}
return nil
}
1 change: 1 addition & 0 deletions cmd/clair-action/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func main() {
Commands: []*cli.Command{
reportCmd,
updateCmd,
convertCmd,
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down