Skip to content

Commit

Permalink
🐛 fix risk factors missing in inmemory w/ upstream (#1224)
Browse files Browse the repository at this point in the history
When running against upstream policies, we don't get risk factors from
the policy bundle, since we generally don't need the bundle to run the
scan. However, we do require basic risk factor info to help score
everything before we send data up.

In this change we pull risk info from the resolved policy and inject it
into the inmemory datastore before the scan is started (unless the risk
factor exists for any reason, like prior policy bundles). It has enough
information for scoring.

For any later reporting step we may still inject all risk factor
metadata into the inmemory store before printing the output.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Apr 4, 2024
1 parent bef1cb3 commit 8d5b7c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/datalakes/inmemory/policyhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ func (db *Db) invalidateFrameworkAndBundleAncestors(ctx context.Context, wrap *w
return nil
}

func (db *Db) getRiskFactor(ctx context.Context, mrn string) (*policy.RiskFactor, error) {
found, ok := db.cache.Get(dbIDRiskFactor + mrn)
if !ok {
return nil, errors.New("risk factor " + mrn + " not found")
}
return found.(*policy.RiskFactor), nil
}

func (db *Db) SetRiskFactor(ctx context.Context, riskFactor *policy.RiskFactor) error {
db.cache.Set(dbIDRiskFactor+riskFactor.Mrn, riskFactor, 1)
return nil
Expand Down
13 changes: 13 additions & 0 deletions internal/datalakes/inmemory/policyresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,19 @@ func (db *Db) SetResolvedPolicy(ctx context.Context, mrn string, resolvedPolicy
return errors.New("failed to save resolved policy '" + mrn + "'")
}

// We need the risk factors for initial reporting, but don't require all
// their metadata. The risk factors in the resolved policy provides everything
// we need for scoring. If we fetch a full risk factor we can replace it.
for mrn, rf := range resolvedPolicy.CollectorJob.RiskFactors {
rf.Mrn = mrn
if _, err := db.getRiskFactor(ctx, rf.Mrn); err == nil {
continue
}
if err := db.SetRiskFactor(ctx, rf); err != nil {
return err
}
}

if cached {
x, ok := db.cache.Get(dbIDPolicy + mrn)
if !ok {
Expand Down

0 comments on commit 8d5b7c5

Please sign in to comment.