Skip to content

Commit bec5010

Browse files
added-sbom-flag-feature
1 parent fccb94f commit bec5010

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

internal/commands/scan.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,9 @@ func scanCreateSubCommand(
805805
createScanCmd.PersistentFlags().Bool(commonParams.ContainersExcludeNonFinalStagesFlag, false, "Scan only the final deployable image")
806806
createScanCmd.PersistentFlags().String(commonParams.ContainersImageTagFilterFlag, "", "Exclude images by image name and/or tag, ex: \"*dev\"")
807807

808+
//reading sbom-only flag
809+
createScanCmd.PersistentFlags().Bool(commonParams.SbomFlag, false, "Execute SBOM scan exclusively on the provided XML/JSON file.")
810+
808811
return createScanCmd
809812
}
810813

@@ -958,6 +961,7 @@ func getResubmitConfiguration(scansWrapper wrappers.ScansWrapper, projectID, use
958961
scanModelResponse := allScansModel.Scans[0]
959962
config = scanModelResponse.Metadata.Configs
960963
engines := scanModelResponse.Engines
964+
fmt.Println("engines::", engines)
961965
// Check if there are no scan types sent using the flags, and use the latest scan engine types
962966
if userScanTypes == "" {
963967
actualScanTypes = strings.Join(engines, ",")
@@ -1086,6 +1090,7 @@ func addScaScan(cmd *cobra.Command, resubmitConfig []wrappers.Config, hasContain
10861090
scaMapConfig := make(map[string]interface{})
10871091
scaConfig := wrappers.ScaConfig{}
10881092
scaMapConfig[resultsMapType] = commonParams.ScaType
1093+
isSbom, _ := cmd.PersistentFlags().GetBool(commonParams.SbomFlag)
10891094
scaConfig.Filter, _ = cmd.Flags().GetString(commonParams.ScaFilterFlag)
10901095
scaConfig.LastSastScanTime, _ = cmd.Flags().GetString(commonParams.LastSastScanTime)
10911096
scaConfig.PrivatePackageVersion, _ = cmd.Flags().GetString(commonParams.ScaPrivatePackageVersionFlag)
@@ -1104,6 +1109,7 @@ func addScaScan(cmd *cobra.Command, resubmitConfig []wrappers.Config, hasContain
11041109
}
11051110
}
11061111
}
1112+
scaConfig.SBom = strconv.FormatBool(isSbom)
11071113
scaMapConfig[resultsMapValue] = &scaConfig
11081114
return scaMapConfig
11091115
}
@@ -1318,6 +1324,8 @@ func validateScanTypes(cmd *cobra.Command, jwtWrapper wrappers.JWTWrapper, featu
13181324
var scanTypes []string
13191325
var SCSScanTypes []string
13201326

1327+
isSbomScan, _ := cmd.PersistentFlags().GetBool(commonParams.SbomFlag)
1328+
13211329
allowedEngines, err := jwtWrapper.GetAllowedEngines(featureFlagsWrapper)
13221330
if err != nil {
13231331
err = errors.Errorf("Error validating scan types: %v", err)
@@ -1333,6 +1341,21 @@ func validateScanTypes(cmd *cobra.Command, jwtWrapper wrappers.JWTWrapper, featu
13331341
userSCSScanTypes = strings.Replace(strings.ToLower(userSCSScanTypes), commonParams.SCSEnginesFlag, commonParams.ScsType, 1)
13341342

13351343
scanTypes = strings.Split(userScanTypes, ",")
1344+
1345+
//sbom check
1346+
if isSbomScan {
1347+
if len(scanTypes) > 1 {
1348+
err = errors.Errorf("while using sbom-only flag only the sca scan type is allowed.")
1349+
return err
1350+
}
1351+
1352+
if scanTypes[0] != "sca" {
1353+
err = errors.Errorf("while using sbom-only flag only the sca scan type is allowed.")
1354+
return err
1355+
}
1356+
1357+
}
1358+
13361359
for _, scanType := range scanTypes {
13371360
if !allowedEngines[scanType] {
13381361
keys := reflect.ValueOf(allowedEngines).MapKeys()
@@ -1348,9 +1371,21 @@ func validateScanTypes(cmd *cobra.Command, jwtWrapper wrappers.JWTWrapper, featu
13481371
return err
13491372
}
13501373
} else {
1351-
for k := range allowedEngines {
1352-
scanTypes = append(scanTypes, k)
1374+
if isSbomScan {
1375+
if allowedEngines["sca"] {
1376+
//set
1377+
fmt.Println("setting sca as scan-type")
1378+
scanTypes = append(scanTypes, "sca")
1379+
1380+
} else {
1381+
return errors.Errorf("sbom needs sca engine to be allowed")
1382+
}
1383+
} else {
1384+
for k := range allowedEngines {
1385+
scanTypes = append(scanTypes, k)
1386+
}
13531387
}
1388+
13541389
}
13551390

13561391
actualScanTypes = strings.Join(scanTypes, ",")
@@ -1651,8 +1686,14 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
16511686
scaResolverPath, _ := cmd.Flags().GetString(commonParams.ScaResolverFlag)
16521687

16531688
scaResolverParams, scaResolver := getScaResolverFlags(cmd)
1654-
1655-
zipFilePath, directoryPath, err := definePathForZipFileOrDirectory(cmd)
1689+
isSbom, _ := cmd.PersistentFlags().GetBool(commonParams.SbomFlag)
1690+
var directoryPath string
1691+
if isSbom {
1692+
sbomFile, _ := cmd.Flags().GetString(commonParams.SourcesFlag)
1693+
zipFilePath, err = util.CompressFile(sbomFile, "sbomFileCompress", directoryCreationPrefix)
1694+
} else {
1695+
zipFilePath, directoryPath, err = definePathForZipFileOrDirectory(cmd)
1696+
}
16561697

16571698
if zipFilePath != "" && scaResolverPath != "" {
16581699
return "", "", errors.New("Scanning Zip files is not supported by ScaResolver.Please use non-zip source")
@@ -1712,7 +1753,9 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
17121753
}
17131754
}
17141755
} else {
1715-
zipFilePath, dirPathErr = compressFolder(directoryPath, sourceDirFilter, userIncludeFilter, scaResolver)
1756+
if !isSbom {
1757+
zipFilePath, dirPathErr = compressFolder(directoryPath, sourceDirFilter, userIncludeFilter, scaResolver)
1758+
}
17161759
}
17171760
if dirPathErr != nil {
17181761
return "", "", dirPathErr

internal/params/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ const (
230230
ContainersImageTagFilterFlag = "containers-image-tag-filter"
231231
ContainersPackageFilterFlag = "containers-package-filter"
232232
ContainersExcludeNonFinalStagesFlag = "containers-exclude-non-final-stages"
233+
234+
//SBOM - flag
235+
SbomFlag = "sbom-only"
233236
)
234237

235238
// Parameter values

internal/wrappers/scans.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ type ScaConfig struct {
143143
LastSastScanTime string `json:"LastSastScanTime,omitempty"`
144144
PrivatePackageVersion string `json:"privatePackageVersion,omitempty"`
145145
EnableContainersScan bool `json:"enableContainersScan,omitempty"`
146+
SBom string `json:"sbom,omitempty"`
146147
}
147148
type ContainerConfig struct {
148149
FilesFilter string `json:"filesFilter,omitempty"`

0 commit comments

Comments
 (0)