Skip to content

Commit bf43727

Browse files
committed
using license risk service only in component service
1 parent e261367 commit bf43727

6 files changed

Lines changed: 19 additions & 129 deletions

File tree

internal/core/component/component_service.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func (s *service) GetAndSaveLicenseInformation(assetVersion models.AssetVersion,
172172
slog.Info("getting license information for components", "amount", len(componentsWithoutLicense))
173173
errGroup := utils.ErrGroup[models.Component](10)
174174
for _, component := range componentsWithoutLicense {
175-
component := component
176175
errGroup.Go(func() (models.Component, error) {
177176
return s.GetLicense(component)
178177
})
@@ -189,18 +188,19 @@ func (s *service) GetAndSaveLicenseInformation(assetVersion models.AssetVersion,
189188
return nil, err
190189
}
191190

192-
// find potential license risks for the components which had no prior license
193-
if len(components) > 0 {
194-
err = s.licenseRiskService.FindLicenseRisksInComponents(assetVersion, components, scannerID)
195-
if err != nil {
196-
return nil, err
191+
allComponents := components
192+
// get all the components - with licenses and without
193+
for _, componentDependency := range componentDependencies {
194+
if !seen[componentDependency.DependencyPurl] {
195+
// if the component is not in the seen map, it means it was not processed to get a new license
196+
allComponents = append(allComponents, componentDependency.Dependency)
197197
}
198198
}
199199

200-
// now return all components - each one should have the best license information available
201-
allComponents := components
202-
for _, componentDependency := range componentDependencies {
203-
allComponents = append(allComponents, componentDependency.Dependency)
200+
// find potential license risks
201+
err = s.licenseRiskService.FindLicenseRisksInComponents(assetVersion, allComponents, scannerID)
202+
if err != nil {
203+
return nil, err
204204
}
205205

206206
return allComponents, nil

internal/core/vuln/license_risk_service.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,15 @@ func (service *LicenseRiskService) FindLicenseRisksInComponents(assetVersion mod
3838
}
3939

4040
// get all current valid licenses to compare against
41-
licenses, err := GetOSILicenses()
41+
licenseMap, err := GetOSILicenses()
4242
if err != nil {
4343
return err
4444
}
45-
licenseMap := make(map[string]struct{})
46-
for i := range licenses {
47-
licenseMap[licenses[i]] = struct{}{}
48-
}
4945

5046
//collect all risks before saving to the database, should be more efficient
5147
allLicenseRisks := []models.LicenseRisk{}
5248
allVulnEvents := []models.VulnEvent{}
53-
//go over every component and check if the license if the license is a valid osi license; if not we can create a license risk with the provided information
49+
//go over every component and check if the license is a valid osi license; if not we can create a license risk with the provided information
5450
for _, component := range components {
5551
_, validLicense := licenseMap[*component.License]
5652
_, exists := doesLicenseRiskAlreadyExist[component.Purl]
@@ -86,11 +82,11 @@ func (service *LicenseRiskService) FindLicenseRisksInComponents(assetVersion mod
8682
return nil
8783
}
8884

89-
var validOSILicenses []string = make([]string, 0)
85+
var validOSILicenseMap map[string]struct{} = make(map[string]struct{}) // cache for valid OSI licenses
9086

91-
func GetOSILicenses() ([]string, error) {
92-
if len(validOSILicenses) > 0 {
93-
return validOSILicenses, nil
87+
func GetOSILicenses() (map[string]struct{}, error) {
88+
if len(validOSILicenseMap) > 0 {
89+
return validOSILicenseMap, nil
9490
}
9591

9692
apiURL := os.Getenv("OSI_LICENSES_API")
@@ -129,10 +125,10 @@ func GetOSILicenses() ([]string, error) {
129125

130126
for _, license := range licenses {
131127
if license.ID != "" {
132-
validOSILicenses = append(validOSILicenses, license.ID)
128+
validOSILicenseMap[license.ID] = struct{}{}
133129
}
134130
}
135-
return validOSILicenses, nil
131+
return validOSILicenseMap, nil
136132
}
137133

138134
func (service *LicenseRiskService) UpdateLicenseRiskState(tx core.DB, userID string, licenseRisk *models.LicenseRisk, statusType string, justification string, mechanicalJustification models.MechanicalJustificationType) (models.VulnEvent, error) {

internal/core/vuln/license_risk_test.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

internal/core/vulndb/scan/scan_controller.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ type HTTPController struct {
4141
statisticsService core.StatisticsService
4242

4343
dependencyVulnService core.DependencyVulnService
44-
licenseRiskService core.LicenseRiskService
4544

4645
// mark public to let it be overridden in tests
4746
core.FireAndForgetSynchronizer
4847
}
4948

50-
func NewHTTPController(db core.DB, cveRepository core.CveRepository, componentRepository core.ComponentRepository, assetRepository core.AssetRepository, assetVersionRepository core.AssetVersionRepository, assetVersionService core.AssetVersionService, statisticsService core.StatisticsService, dependencyVulnService core.DependencyVulnService, licenseRiskService core.LicenseRiskService) *HTTPController {
49+
func NewHTTPController(db core.DB, cveRepository core.CveRepository, componentRepository core.ComponentRepository, assetRepository core.AssetRepository, assetVersionRepository core.AssetVersionRepository, assetVersionService core.AssetVersionService, statisticsService core.StatisticsService, dependencyVulnService core.DependencyVulnService) *HTTPController {
5150
cpeComparer := NewCPEComparer(db)
5251
purlComparer := NewPurlComparer(db)
5352

@@ -62,7 +61,6 @@ func NewHTTPController(db core.DB, cveRepository core.CveRepository, componentRe
6261
assetVersionRepository: assetVersionRepository,
6362
statisticsService: statisticsService,
6463
dependencyVulnService: dependencyVulnService,
65-
licenseRiskService: licenseRiskService,
6664
FireAndForgetSynchronizer: utils.NewFireAndForgetSynchronizer(),
6765
}
6866
}
@@ -119,14 +117,6 @@ func (s *HTTPController) DependencyVulnScan(c core.Context, bom normalize.SBOM)
119117
slog.Error("could not update sbom", "err", err)
120118
return scanResults, err
121119
}
122-
123-
// check if we have any license risk in our sbom
124-
sbomComponents := ConvertCDXComponentsToSimpleComponents(*normalizedBom.GetComponents())
125-
err = s.licenseRiskService.FindLicenseRisksInComponents(assetVersion, sbomComponents, scannerID)
126-
if err != nil {
127-
return scanResults, err
128-
}
129-
130120
return s.ScanNormalizedSBOM(org, project, asset, assetVersion, normalizedBom, scannerID, userID)
131121
}
132122

@@ -278,27 +268,3 @@ func (s *HTTPController) ScanSbomFile(c core.Context) error {
278268
return c.JSON(200, scanResults)
279269

280270
}
281-
282-
func ConvertCDXComponentsToSimpleComponents(cdxComponents []cdx.Component) []models.Component {
283-
components := []models.Component{}
284-
// only variables needed for FindLicenseRisksInComponents are converted
285-
for _, cdx := range cdxComponents {
286-
license := ""
287-
// avoid nil pointer dereference
288-
if cdx.Licenses != nil && len(*cdx.Licenses) > 0 {
289-
if (*cdx.Licenses)[0].License != nil {
290-
if (*cdx.Licenses)[0].License.ID != "" {
291-
license = (*cdx.Licenses)[0].License.ID
292-
} else {
293-
license = (*cdx.Licenses)[0].License.Name
294-
}
295-
}
296-
297-
}
298-
components = append(components, models.Component{
299-
Purl: cdx.PackageURL,
300-
License: &license,
301-
})
302-
}
303-
return components
304-
}

internal/database/models/vulnerability_model.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/l3montree-dev/devguard/internal/utils"
99
)
1010

11-
// TO DO move this to common interfaces?
1211
type Vuln interface {
1312
SetState(state VulnState)
1413
GetState() VulnState

internal/inithelper/intialize_modules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,5 @@ func CreateHTTPController(db core.DB, oauth2 map[string]*gitlabint.GitlabOauth2C
9999
CreateAssetVersionService(db, oauth2, rbac, clientFactory, depsDevService),
100100
CreateStatisticsService(db),
101101
CreateDependencyVulnService(db, oauth2, rbac, clientFactory),
102-
CreateLicenseRiskService(db),
103102
)
104103
}

0 commit comments

Comments
 (0)