@@ -4,17 +4,13 @@ import (
44 "context"
55 "fmt"
66 "log/slog"
7- "math"
8- "strings"
97
108 "github.com/google/uuid"
119 "github.com/l3montree-dev/devguard/database"
1210 "github.com/l3montree-dev/devguard/database/repositories"
1311 "github.com/l3montree-dev/devguard/shared"
1412 "github.com/spf13/cobra"
1513 "go.uber.org/fx"
16-
17- "github.com/l3montree-dev/devguard/database/models"
1814)
1915
2016type CrowdResult struct {
@@ -164,112 +160,3 @@ func assignTrustScore(ctx context.Context, entityType string, entityID string, s
164160
165161 return assignErr
166162}
167-
168- func CalculateConfidenceScoreForPath (ctx context.Context , rules []models.VEXRule , markedAsAffected []models.VEXRule , trustedEntities []models.TrustedEntity ) ([]CrowdResult , error ) {
169- var result []CrowdResult
170- var confidenceValues = make (map [string ]float64 )
171- var assignErr error
172- app := fx .New (
173- fx .NopLogger ,
174- fx .Supply (database .GetPoolConfigFromEnv ()),
175- database .Module ,
176- repositories .Module ,
177- fx .Invoke (func (
178- orgRepo shared.OrganizationRepository ,
179- projectRepo shared.ProjectRepository ,
180- assetRepo shared.AssetRepository ,
181- ) error {
182-
183- for _ , rule := range rules {
184- rulaPath := strings .Join (rule .PathPattern , "->" )
185-
186- asset , err := assetRepo .Read (ctx , nil , rule .AssetID )
187- if err != nil {
188- slog .Error ("failed to read asset for VEX rule" , "assetID" , rule .AssetID , "error" , err )
189- continue
190- }
191- project , err := projectRepo .Read (ctx , nil , asset .ProjectID )
192- if err != nil {
193- slog .Error ("failed to read project for asset" , "projectID" , asset .ProjectID , "error" , err )
194- continue
195- }
196- org , err := orgRepo .Read (ctx , nil , project .OrganizationID )
197- if err != nil {
198- slog .Error ("failed to read organization for project" , "organizationID" , project .OrganizationID , "error" , err )
199- continue
200- }
201- organizationTrustscore := 0.0
202- projectTrustscore := 0.0
203-
204- for _ , te := range trustedEntities {
205- if te .OrganizationID != nil && * te .OrganizationID == org .ID {
206- organizationTrustscore = te .TrustScore
207- } else if te .ProjectID != nil && * te .ProjectID == project .ID {
208- projectTrustscore = te .TrustScore
209- }
210- }
211-
212- ruleConfidence := 1.0 * math .Max (projectTrustscore , organizationTrustscore )
213- confidenceValues [rulaPath ] += ruleConfidence
214- }
215-
216- for _ , rule := range markedAsAffected {
217- rulaPath := strings .Join (rule .PathPattern , "" )
218-
219- asset , err := assetRepo .Read (ctx , nil , rule .AssetID )
220- if err != nil {
221- slog .Error ("failed to read asset for VEX rule" , "assetID" , rule .AssetID , "error" , err )
222- continue
223- }
224- project , err := projectRepo .Read (ctx , nil , asset .ProjectID )
225- if err != nil {
226- slog .Error ("failed to read project for asset" , "projectID" , asset .ProjectID , "error" , err )
227- continue
228- }
229- org , err := orgRepo .Read (ctx , nil , project .OrganizationID )
230- if err != nil {
231- slog .Error ("failed to read organization for project" , "organizationID" , project .OrganizationID , "error" , err )
232- continue
233- }
234- organizationTrustscore := 0.0
235- projectTrustscore := 0.0
236-
237- for _ , te := range trustedEntities {
238- if te .OrganizationID != nil && * te .OrganizationID == org .ID {
239- organizationTrustscore = te .TrustScore
240- } else if te .ProjectID != nil && * te .ProjectID == project .ID {
241- projectTrustscore = te .TrustScore
242- }
243- }
244-
245- ruleConfidence := 1.0 * math .Max (projectTrustscore , organizationTrustscore )
246- confidenceValues [rulaPath ] += ruleConfidence
247- }
248-
249- totalConfidence := 0.0
250- for _ , conf := range confidenceValues {
251- totalConfidence += conf
252- }
253- if totalConfidence == 0 {
254- return nil
255- }
256- for key , value := range confidenceValues {
257- result = append (result , CrowdResult {
258- ConfidenceScore : value / totalConfidence ,
259- Rule : key ,
260- })
261- }
262- return nil
263- }),
264- )
265-
266- if err := app .Start (context .Background ()); err != nil {
267- assignErr = err
268- }
269-
270- if err := app .Stop (context .Background ()); err != nil {
271- assignErr = err
272- }
273-
274- return result , assignErr
275- }
0 commit comments