Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix rejected exceptions execution #973

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions policy/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,13 @@ func (s *LocalServices) policyToJobs(ctx context.Context, policyMrn string, owne
}
for i := range group.Checks {
check := group.Checks[i]
if check.Action == explorer.Action_DEACTIVATE || group.Type == GroupType_DISABLE {
if check.Action == explorer.Action_DEACTIVATE || (group.Type == GroupType_DISABLE && group.ReviewStatus != ReviewStatus_REJECTED) {
cache.removedQueries[check.Mrn] = struct{}{}
}
}
for i := range group.Queries {
query := group.Queries[i]
if query.Action == explorer.Action_DEACTIVATE || group.Type == GroupType_DISABLE {
if query.Action == explorer.Action_DEACTIVATE || (group.Type == GroupType_DISABLE && group.ReviewStatus != ReviewStatus_REJECTED) {
cache.removedQueries[query.Mrn] = struct{}{}
}
}
Expand Down Expand Up @@ -891,7 +891,9 @@ func (s *LocalServices) policyGroupToJobs(ctx context.Context, group *PolicyGrou
continue
}

if check.Action == explorer.Action_IGNORE || group.Type == GroupType_IGNORED {
// The type is IGNORED and the review status is not REJECTED
ignoreGroup := group.Type == GroupType_IGNORED && group.ReviewStatus != ReviewStatus_REJECTED
if check.Action == explorer.Action_IGNORE || ignoreGroup {
stillValid := CheckValidUntil(group.EndDate, check.Mrn)
if !stillValid {
// the exception is no longer valid => score the check
Expand All @@ -904,7 +906,7 @@ func (s *LocalServices) policyGroupToJobs(ctx context.Context, group *PolicyGrou
if impact == nil {
impact = &explorer.Impact{}
}
if check.Action == explorer.Action_IGNORE || group.Type == GroupType_IGNORED {
if check.Action == explorer.Action_IGNORE || ignoreGroup {
impact.Scoring = explorer.ScoringSystem_IGNORE_SCORE
impact.Action = explorer.Action_IGNORE
}
Expand All @@ -917,7 +919,7 @@ func (s *LocalServices) policyGroupToJobs(ctx context.Context, group *PolicyGrou
}

// TODO: can we simplify this to simply IGNORE?
if check.Action == explorer.Action_MODIFY || check.Action == explorer.Action_IGNORE || group.Type == GroupType_IGNORED {
if check.Action == explorer.Action_MODIFY || check.Action == explorer.Action_IGNORE || ignoreGroup {
cache.modifyCheckJob(check, impact)
}
}
Expand Down
65 changes: 65 additions & 0 deletions policy/scan/local_scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,71 @@ func (s *LocalScannerSuite) TestRunIncognito_ExceptionGroups() {
}
}

func (s *LocalScannerSuite) TestRunIncognito_ExceptionGroups_RejectedReview() {
bundle, err := policy.BundleFromPaths("./testdata/exception-groups.mql.yaml")
s.Require().NoError(err)

bundle.Policies[1].Groups[0].ReviewStatus = policy.ReviewStatus_REJECTED
bundle.Policies[1].Groups[1].ReviewStatus = policy.ReviewStatus_REJECTED

_, err = bundle.CompileExt(context.Background(), policy.BundleCompileConf{
Schema: s.schema,
RemoveFailing: true,
})
s.Require().NoError(err)

s.job.Bundle = bundle
s.job.PolicyFilters = []string{"asset-policy"}
bundleMap := bundle.ToMap()

ctx := context.Background()
scanner := NewLocalScanner()
res, err := scanner.RunIncognito(ctx, s.job)
s.Require().NoError(err)
s.Require().NotNil(res)

full := res.GetFull()
s.Require().NotNil(full)

s.Equal(1, len(full.Reports))

for k, r := range full.Reports {
// Verify the score is 16
s.Equal(uint32(16), r.GetScore().Value)

p := full.ResolvedPolicies[k]

// Get the code id for all the executed queries
executedQueries := []string{}
for qCodeId := range p.ExecutionJob.Queries {
executedQueries = append(executedQueries, qCodeId)
}

expectedQueries := []string{
bundleMap.Queries["//local.cnspec.io/run/local-execution/queries/ignored-query"].CodeId,
bundleMap.Queries["//local.cnspec.io/run/local-execution/queries/deactivate-query"].CodeId,
bundleMap.Queries["//local.cnspec.io/run/local-execution/queries/sshd-score-01"].CodeId,
}
s.ElementsMatch(expectedQueries, executedQueries)

queryIdToReportingJob := map[string]*policy.ReportingJob{}
for _, rj := range p.CollectorJob.ReportingJobs {
_, ok := queryIdToReportingJob[rj.QrId]
s.Require().False(ok)
queryIdToReportingJob[rj.QrId] = rj
}

// Make sure the ignored query is ignored
queryRj := queryIdToReportingJob[bundleMap.Queries["//local.cnspec.io/run/local-execution/queries/ignored-query"].CodeId]
s.Require().NotNil(queryRj)

parent := queryRj.Notify[0]
parentJob := p.CollectorJob.ReportingJobs[parent]
s.Require().NotNil(parentJob)
s.Equal(explorer.ScoringSystem_SCORING_UNSPECIFIED, parentJob.ChildJobs[queryRj.Uuid].Scoring)
}
}

func (s *LocalScannerSuite) TestRunIncognito_QueryExceptions() {
bundle, err := policy.BundleFromPaths("./testdata/exceptions.mql.yaml")
s.Require().NoError(err)
Expand Down