Skip to content

Commit 75991cd

Browse files
committed
code simplification
1 parent e21571e commit 75991cd

11 files changed

Lines changed: 802 additions & 384 deletions

controllers/crowdsourced_vexing_controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package controllers
22

33
import (
4+
"errors"
5+
46
"github.com/google/uuid"
7+
"github.com/l3montree-dev/devguard/crowdsourcevexing"
58
"github.com/l3montree-dev/devguard/shared"
69
"github.com/l3montree-dev/devguard/transformer"
710
"github.com/labstack/echo/v4"
@@ -29,11 +32,12 @@ func (c *CrowdsourcedVexingController) Recommend(ctx shared.Context) error {
2932
}
3033

3134
rule, err := c.crowdsourcedVexingService.Recommend(ctx, nil, dependencyVulnIDParsed)
35+
3236
if err != nil {
37+
if errors.Is(err, crowdsourcevexing.NoRecommendationErr) {
38+
return ctx.NoContent(204)
39+
}
3340
return echo.NewHTTPError(500, "Could not calculate recommendation.").WithInternal(err)
3441
}
35-
if rule.ID == "" {
36-
return ctx.NoContent(204)
37-
}
3842
return ctx.JSON(200, transformer.VEXRuleToRecommendationDTO(rule))
3943
}

crowdsourcevexing/crowdsourced_vexing.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func findVexRuleFromPath(vexRulePath string, vexRules []VexRule) (VexRule, bool)
134134
// Some more requirements to consider:
135135
// Application / Creation of vex rules counts as a vote
136136

137+
var NoRecommendationErr = fmt.Errorf("no recommendation")
138+
137139
func CrowdsourcedVexing(dependencyPath []string, cve CVE, vexRules []VexRule, organizations []Organization, projects []Project, assets []Asset) (VexRule, error) {
138140
var adjustedDiminishmentFactor = baseDiminishmentFactor
139141
// If there is only one organization, we don't need a diminishmentfactor and therefore it should be set to 1 (no diminishment, value is worth fully)
@@ -267,7 +269,7 @@ func CrowdsourcedVexing(dependencyPath []string, cve CVE, vexRules []VexRule, or
267269
// [Mitigation 15] Require a minimum number of voters for a decision; disabling the recommendation when too few voters remain
268270
if validVotesCount < minVoterThreshold {
269271
slog.Info("not enough valid votes to create a crowdsourced VEX rule", "validVotesCount", validVotesCount)
270-
return VexRule{}, nil
272+
return VexRule{}, NoRecommendationErr
271273
}
272274

273275
var crowdsourcedVexRule VexRule
@@ -283,14 +285,14 @@ func CrowdsourcedVexing(dependencyPath []string, cve CVE, vexRules []VexRule, or
283285
// [Mitigation 31] Use standardized cutoff; test with extreme values; define deterministictie-breaking rules
284286
// After the sorting, the VexRule with the highest confidence will be at the end of the sortableVotes slice, so we can compare it with the second to last to check for a tie
285287
if len(sortableVotes) == 0 {
286-
return VexRule{}, nil
288+
return VexRule{}, NoRecommendationErr
287289
}
288290
if len(sortableVotes) > 1 {
289291
if votes[sortableVotes[len(sortableVotes)-1]].Value == votes[sortableVotes[len(sortableVotes)-2]].Value {
290292
// Inconclusive result, no clear winner
291293
// In this case we don't recommend any VexRule to the user, to encourage manual assessment by the user
292294
// to generate more data for a better recommendation in the future
293-
return VexRule{}, nil
295+
return VexRule{}, NoRecommendationErr
294296
} else {
295297
// At this point we have a recommendation for a VexRule and want to return the datastructure of the VexRule to the user
296298
// For that take any fitting VexRule from the database, since they should all be the same

database/repositories/asset_version_repository.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -236,47 +236,6 @@ func (repository *assetVersionRepository) GetAssetVersionsByAssetID(ctx context.
236236
return assets, err
237237
}
238238

239-
func (repository *assetVersionRepository) GetAssetVersionsByAssetIDs(ctx context.Context, tx *gorm.DB, assetIDs []uuid.UUID) ([]models.AssetVersion, error) {
240-
var assets []models.AssetVersion
241-
err := repository.GetDB(ctx, tx).Preload("Asset").Where("asset_id IN ?", assetIDs).Find(&assets).Error
242-
return assets, err
243-
}
244-
245-
func (repository *assetVersionRepository) FindByAssetVersionNameAndAssetIDList(
246-
ctx context.Context,
247-
tx *gorm.DB,
248-
assetPairs []shared.AssetVersionPair,
249-
) ([]models.AssetVersion, error) {
250-
251-
var assets []models.AssetVersion
252-
253-
if len(assetPairs) == 0 {
254-
return assets, nil
255-
}
256-
257-
db := repository.GetDB(ctx, tx)
258-
259-
placeholders := make([]string, 0, len(assetPairs))
260-
args := make([]interface{}, 0, len(assetPairs)*2)
261-
262-
for _, p := range assetPairs {
263-
placeholders = append(placeholders, "(?, ?)")
264-
args = append(args, p.AssetID, p.Name)
265-
}
266-
267-
query := fmt.Sprintf(
268-
"(asset_id, name) IN (%s)",
269-
strings.Join(placeholders, ","),
270-
)
271-
272-
err := db.
273-
Preload("Asset").
274-
Where(query, args...).
275-
Find(&assets).Error
276-
277-
return assets, err
278-
}
279-
280239
func (repository *assetVersionRepository) GetAssetVersionsByAssetIDWithArtifacts(ctx context.Context, tx *gorm.DB, assetID uuid.UUID) ([]models.AssetVersion, error) {
281240
var assetVersion []models.AssetVersion
282241
err := repository.GetDB(ctx, tx).Preload("Artifacts").Where("asset_id = ?", assetID).Find(&assetVersion).Error

database/repositories/vex_rule_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (r *vexRuleRepository) All(ctx context.Context, tx *gorm.DB) ([]models.VEXR
5757

5858
func (r *vexRuleRepository) FindByCVE(ctx context.Context, tx *gorm.DB, cveID string) ([]models.VEXRule, error) {
5959
var rules []models.VEXRule
60-
err := r.GetDB(ctx, tx).Where("cve_id = ? AND enabled = ?", cveID, true).Find(&rules).Error
60+
err := r.GetDB(ctx, tx).Preload("Asset").Where("cve_id = ? AND enabled = ?", cveID, true).Find(&rules).Error
6161
return rules, err
6262
}
6363

mocks/mock_AssetVersionRepository.go

Lines changed: 0 additions & 148 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mocks/mock_CrowdSourcedVexingService.go

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)