Skip to content

Commit

Permalink
Add output flag for SBOM
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Sep 30, 2024
1 parent ea68110 commit 2983551
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
9 changes: 9 additions & 0 deletions internal/cmd/report/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ var repositoryId string
var branch string
var vulnerabilities bool
var licenses bool
var output string

const CommitFlag = "commit"
const RepositorylFlag = "repository"
const TokenFlag = "token"
const BranchFlag = "branch"
const VulnerabilitiesFlag = "vulnerabilities"
const LicensesFlag = "licenses"
const OutputFlag = "output"

func NewSBOMCmd(reporter report.IReporter) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -54,6 +56,12 @@ This is an enterprise feature. Please visit https://debricked.com/pricing/ for m
cmd.Flags().BoolVar(&licenses, LicensesFlag, true, "Toggles SBOM license data inclusion")
viper.MustBindEnv(LicensesFlag)

cmd.Flags().StringVarP(&output, OutputFlag, "o", "", `Sets output path for downloaded SBOM json file.
If no output path is set the file is created in the format <repository_id>-<commit_id>.sbom.json`,
)
viper.MustBindEnv(OutputFlag)

return cmd
}

Expand All @@ -65,6 +73,7 @@ func RunE(r report.IReporter) func(_ *cobra.Command, args []string) error {
Branch: viper.GetString(BranchFlag),
Vulnerabilities: viper.GetBool(VulnerabilitiesFlag),
Licenses: viper.GetBool(LicensesFlag),
Output: viper.GetString(OutputFlag),
}

if err := r.Order(orderArgs); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestNewRootCmd(t *testing.T) {
}
}
assert.Truef(t, match, "failed to assert that flag was present: "+AccessTokenFlag)
assert.Len(t, viperKeys, 18)
assert.Len(t, viperKeys, 20)
}

func TestPreRun(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var repositoryUrl string
var verbose bool
var versionHint bool
var sbom bool
var sbomOutput string

const (
BranchFlag = "branch"
Expand All @@ -57,6 +58,7 @@ const (
VerboseFlag = "verbose"
VersionHintFlag = "version-hint"
SBOMFlag = "sbom"
SBOMOutputFlag = "sbom-output"
)

var scanCmdError error
Expand Down Expand Up @@ -153,6 +155,7 @@ For example, if there is a "go.mod" in the target path, its dependencies are goi
}, "\n")
cmd.Flags().BoolP(NpmPreferredFlag, "", npmPreferred, npmPreferredDoc)
cmd.Flags().BoolVar(&sbom, SBOMFlag, false, `Toggles wether to generate and download an SBOM report after scan completion`)
cmd.Flags().StringVar(&sbomOutput, SBOMOutputFlag, "", `Sets output path of downloaded SBOM report (if sbom is toggled)`)

viper.MustBindEnv(RepositoryFlag)
viper.MustBindEnv(CommitFlag)
Expand All @@ -163,6 +166,7 @@ For example, if there is a "go.mod" in the target path, its dependencies are goi
viper.MustBindEnv(PassOnTimeOut)
viper.MustBindEnv(NpmPreferredFlag)
viper.MustBindEnv(SBOMFlag)
viper.MustBindEnv(SBOMOutputFlag)

return cmd
}
Expand All @@ -178,6 +182,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
Resolve: !viper.GetBool(NoResolveFlag),
Fingerprint: !viper.GetBool(NoFingerprintFlag),
SBOM: viper.GetBool(SBOMFlag),
SBOMOutput: viper.GetString(SBOMOutputFlag),
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
Expand Down
13 changes: 10 additions & 3 deletions internal/report/sbom/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type OrderArgs struct {
RepositoryID string
CommitID string
Branch string
Output string
Vulnerabilities bool
Licenses bool
}
Expand All @@ -70,7 +71,7 @@ func (r Reporter) Order(args report.IOrderArgs) error {
return err
}

return r.writeSBOM(orderArgs.RepositoryID, orderArgs.CommitID, sbom)
return r.writeSBOM(orderArgs.Output, orderArgs.RepositoryID, orderArgs.CommitID, sbom)

}

Expand Down Expand Up @@ -151,8 +152,14 @@ func (r Reporter) download(uuid string) ([]byte, error) {
}
}

func (reporter Reporter) writeSBOM(repositoryID, commitID string, sbomBytes []byte) error {
file, err := reporter.FileWriter.Create(fmt.Sprintf("%s-%s.sbom.json", repositoryID, commitID))
func (reporter Reporter) writeSBOM(output, repositoryID, commitID string, sbomBytes []byte) error {
var filename string
if output == "" {
filename = fmt.Sprintf("%s-%s.sbom.json", repositoryID, commitID)
} else {
filename = output
}
file, err := reporter.FileWriter.Create(filename)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/report/sbom/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestWriteSBOM(t *testing.T) {
CreateErr: errors.New(""),
}
reporter := Reporter{DebClient: clientMock, FileWriter: fileWriter}
err := reporter.writeSBOM("", "", nil)
err := reporter.writeSBOM("", "", "", nil)
assert.Error(t, err)
}

Expand All @@ -175,5 +175,6 @@ func orderArgs() OrderArgs {
Branch: "",
CommitID: "",
RepositoryID: "",
Output: "",
}
}
11 changes: 6 additions & 5 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type DebrickedOptions struct {
Fingerprint bool
CallGraph bool
SBOM bool
SBOMOutput string
Exclusions []string
Inclusions []string
Verbose bool
Expand Down Expand Up @@ -144,8 +145,8 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {
return nil
}

func (dScanner *DebrickedScanner) scanReportSBOM(reportSBOM bool, detailsURL, branch string) error {
if !reportSBOM {
func (dScanner *DebrickedScanner) scanReportSBOM(options DebrickedOptions, detailsURL string) error {
if !options.SBOM {
return nil
}
reporter := sbom.Reporter{DebClient: *dScanner.client, FileWriter: io.FileWriter{}}
Expand All @@ -158,9 +159,10 @@ func (dScanner *DebrickedScanner) scanReportSBOM(reportSBOM bool, detailsURL, br
return reporter.Order(sbom.OrderArgs{
RepositoryID: repositoryID,
CommitID: commitID,
Branch: branch,
Branch: options.BranchName,
Vulnerabilities: true,
Licenses: true,
Output: options.SBOMOutput,
})
}

Expand Down Expand Up @@ -272,9 +274,8 @@ func (dScanner *DebrickedScanner) scan(options DebrickedOptions, gitMetaObject g
return nil, err
}
err = dScanner.scanReportSBOM(
options.SBOM,
options,
result.DetailsUrl,
options.BranchName,
)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions internal/scan/testdata/npm/13-37.sbom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 2983551

Please sign in to comment.