Skip to content

Commit

Permalink
fix(core): Fixed getLibraries to execute once, instead for every query (
Browse files Browse the repository at this point in the history
Checkmarx#4155)

Signed-off-by: Felipe Avelar <[email protected]>
  • Loading branch information
felipe-avelar authored Sep 6, 2021
1 parent 59ed1a1 commit 6562471
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions pkg/engine/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,16 @@ func NewInspector(
if err != nil {
sentry.CaptureException(err)
log.Err(err).
Msgf("Inspector failed to get general query, query=%s", "common")
Msgf("Inspector failed to get library for %s platform", "common")
return nil, errors.Wrap(err, "failed to get library")
}
platformLibraries := getPlatformLibraries(queriesSource, queries)
opaQueries := make([]*preparedQuery, 0, len(queries))
for _, metadata := range queries {
platformGeneralQuery, err := queriesSource.GetQueryLibrary(metadata.Platform)
if err != nil {
sentry.CaptureException(err)
platformGeneralQuery, ok := platformLibraries[metadata.Platform]
if !ok {
log.Err(err).
Msgf("Inspector failed to get generic query, query=%s", metadata.Query)

continue
}

Expand Down Expand Up @@ -202,6 +201,25 @@ func NewInspector(
}, nil
}

func getPlatformLibraries(queriesSource source.QueriesSource, queries []model.QueryMetadata) map[string]string {
supportedPlatforms := make(map[string]string)
for _, query := range queries {
supportedPlatforms[query.Platform] = ""
}
platformLibraries := make(map[string]string)
for platform := range supportedPlatforms {
platformLibrary, errLoadingPlatformLib := queriesSource.GetQueryLibrary(platform)
if errLoadingPlatformLib != nil {
sentry.CaptureException(errLoadingPlatformLib)
log.Err(errLoadingPlatformLib).
Msgf("Inspector failed to get library for %s platform", platform)
continue
}
platformLibraries[platform] = platformLibrary
}
return platformLibraries
}

func sumAllAggregatedQueries(opaQueries []*preparedQuery) int {
sum := 0
for _, query := range opaQueries {
Expand Down

0 comments on commit 6562471

Please sign in to comment.