-
Notifications
You must be signed in to change notification settings - Fork 114
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
Added RunTimeLimit option and runner code to strip profiles metadata #3697
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
ingest_inspec "github.com/chef/automate/api/interservice/compliance/ingest/events/inspec" | ||
"github.com/chef/automate/api/interservice/compliance/ingest/ingest" | ||
"github.com/chef/automate/api/interservice/compliance/jobs" | ||
"github.com/chef/automate/components/compliance-service/ingest/ingestic" | ||
"github.com/chef/automate/components/compliance-service/ingest/ingestic/mappings" | ||
"github.com/chef/automate/components/compliance-service/inspec" | ||
"github.com/chef/automate/components/compliance-service/inspec-agent/remote" | ||
|
@@ -37,6 +38,12 @@ var ListenPort int = 2133 | |
// ControlResultsLimit used for configuring inspec exec command, passed in via config flag | ||
var ControlResultsLimit int = 50 | ||
|
||
// RunTimeLimit used for report post processing to reduce the size of the report. | ||
var RunTimeLimit float32 = 1.0 | ||
|
||
// Set from compliance.go to call the ElasticSearch backend | ||
var ESClient *ingestic.ESClient | ||
|
||
var ( | ||
ScanJobWorkflowName = cereal.NewWorkflowName("scan-job-workflow") | ||
|
||
|
@@ -531,9 +538,22 @@ func (t *InspecJobTask) reportIt(ctx context.Context, job *types.InspecJob, cont | |
logrus.Debugf("hand-over report to ingest service") | ||
|
||
removeResults(reportID, report.Profiles, ControlResultsLimit) | ||
// strip profile meta if missing from the backend | ||
|
||
_, err := t.ingestClient.ProcessComplianceReport(ctx, &report) | ||
allReportProfileIds := make([]string, len(report.Profiles)) | ||
for i, profile := range report.Profiles { | ||
allReportProfileIds[i] = profile.Sha256 | ||
} | ||
esProfilesMissingMeta, err := ESClient.ProfilesMissing(allReportProfileIds) | ||
if err != nil { | ||
return errors.Wrap(err, "Report profiles processing error") | ||
} | ||
profilesMissingMeta := make(map[string]struct{}, 0) | ||
for _, profileId := range esProfilesMissingMeta { | ||
profilesMissingMeta[profileId] = struct{}{} | ||
} | ||
stripProfilesMetadata(&report, profilesMissingMeta, RunTimeLimit) | ||
|
||
_, err = t.ingestClient.ProcessComplianceReport(ctx, &report) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
if err != nil { | ||
return errors.Wrap(err, "Report processing error") | ||
} | ||
|
@@ -756,6 +776,8 @@ func doExec(job *types.InspecJob) (jsonBytes []byte, err *inspec.Error) { | |
return jsonBytes, nil | ||
} | ||
|
||
// The purpose of this function is to remove the results from a control once they | ||
// exceed `limit`. This avoids large reports that cannot be ingested. | ||
func removeResults(reportId string, profiles []*ingest_inspec.Profile, limit int) { | ||
for _, profile := range profiles { | ||
if profile.Controls != nil { | ||
|
@@ -788,6 +810,55 @@ func removeResults(reportId string, profiles []*ingest_inspec.Profile, limit int | |
} | ||
} | ||
|
||
// The purpose of this function is to remove the profiles metadata (control title, desc, code, etc) | ||
// for profiles that already exist in the Automate profiles index. | ||
func stripProfilesMetadata(report *ingest_events_compliance_api.Report, missingProfiles map[string]struct{}, runTimeLimit float32) { | ||
if report.Profiles == nil { | ||
return | ||
} | ||
for _, profile := range report.Profiles { | ||
// If the profile is not in the ones missing in the backend, we can proceed with the metadata removal | ||
if _, ok := missingProfiles[profile.Sha256]; ok { | ||
continue | ||
} | ||
// Profile 'Name' is a required property. By not sending it in the report, we make it clear to the ingestion backend that the profile metadata has been stripped from this profile in the report. | ||
// Profile 'title' and 'version' are still kept for troubleshooting purposes in the backend. | ||
profile.Name = "" | ||
profile.Groups = nil | ||
profile.CopyrightEmail = "" | ||
profile.Copyright = "" | ||
profile.Summary = "" | ||
profile.Supports = nil | ||
profile.License = "" | ||
profile.Maintainer = "" | ||
profile.Depends = nil | ||
if profile.Controls == nil { | ||
continue | ||
} | ||
for _, control := range profile.Controls { | ||
control.Code = "" | ||
control.Desc = "" | ||
// TODO: Add here control.Descriptions when the field makes it into the structs | ||
control.Impact = 0 | ||
control.Refs = nil | ||
control.Tags = nil | ||
control.Title = "" | ||
control.SourceLocation = nil | ||
control.WaiverData = nil | ||
if control.Results == nil { | ||
continue | ||
} | ||
for _, result := range control.Results { | ||
if result.RunTime < runTimeLimit { | ||
result.RunTime = 0 | ||
result.StartTime = "" | ||
} | ||
} | ||
} | ||
report.RunTimeLimit = runTimeLimit | ||
} | ||
} | ||
|
||
func potentialTargetConfigs(job *types.InspecJob) []inspec.TargetConfig { | ||
// GCP profile requires the project_id to be passed in as an attribute. Used the SubscriptionId to get this value from the database | ||
if job.TargetConfig.Backend == "gcp" { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we have a comment on this to document the unit of time? min? seconds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will put a comment and will mention it in the runFlags description.