-
Notifications
You must be signed in to change notification settings - Fork 508
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
adds support to scan directory with all iac providers in cli mode #674
adds support to scan directory with all iac providers in cli mode #674
Conversation
Codecov Report
@@ Coverage Diff @@
## master #674 +/- ##
==========================================
+ Coverage 73.28% 73.85% +0.57%
==========================================
Files 110 110
Lines 3099 3175 +76
==========================================
+ Hits 2271 2345 +74
- Misses 650 652 +2
Partials 178 178
|
0425096
to
b5c0465
Compare
resourceConfig := make(output.AllResourceConfigs) | ||
|
||
// when dir path has value, only then it will 'all iac' scan | ||
// when file path has value, we will go with the only iac provider in the list |
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.
Hey @patilpankaj212 , I wondering if we should keep the all iac scan behavior consistent between directory and file scanning?
if e.iacType == "all" { | ||
for _, ip := range iacProvider.SupportedIacProviders() { | ||
// skip tfplan because it doesn't support directory scanning | ||
if ip == "tfplan" { |
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 this list also have kustomize
iac type?
pkg/runtime/executor.go
Outdated
// channel for directory scan response | ||
scanRespChan := make(chan dirScanResp) | ||
|
||
// create results output from Iac provider[s] | ||
for _, iacP := range e.iacProviders { | ||
go func(ip iacProvider.IacProvider) { | ||
rc, err := ip.LoadIacDir(e.dirPath) | ||
scanRespChan <- dirScanResp{err, rc} | ||
}(iacP) | ||
} | ||
|
||
for i := 0; i < len(e.iacProviders); i++ { | ||
sr := <-scanRespChan | ||
merr = multierror.Append(merr, sr.err) | ||
// deduplication of resources | ||
if len(resourceConfig) > 0 { | ||
for key, r := range sr.rc { | ||
updateResourceConfigs(key, r, resourceConfig) | ||
} | ||
} else { | ||
for key := range sr.rc { | ||
resourceConfig[key] = append(resourceConfig[key], sr.rc[key]...) | ||
} | ||
} | ||
} |
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.
I think this piece of code is now eligible to be in it's own function. What do you think @patilpankaj212 ?
pkg/runtime/executor.go
Outdated
return results, err | ||
|
||
// for the iac providers that don't implement sub folder scanning | ||
// return the error (existing behavior) |
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.
May be improve the comment here (existing behavior)
. Elaborate on the behavior rather than comment it as existing behavior
Because once this code is checked-in current behavior becomes the existing behavior.
pkg/runtime/executor.go
Outdated
// channel for engine evaluation result | ||
evalResultChan := make(chan engineEvalResult) | ||
|
||
for _, engine := range e.policyEngines { | ||
output, err := engine.Evaluate(policy.EngineInput{InputData: &results.ResourceConfig}) | ||
if err != nil { | ||
return results, err | ||
go func(eng policy.Engine) { | ||
output, err := eng.Evaluate(policy.EngineInput{InputData: &results.ResourceConfig}) | ||
evalResultChan <- engineEvalResult{err, output} | ||
}(engine) | ||
} | ||
|
||
for i := 0; i < len(e.policyEngines); i++ { | ||
evalR := <-evalResultChan | ||
if evalR.err != nil { | ||
return results, evalR.err | ||
} | ||
violations = violations.Add(output.AsViolationStore()) | ||
violations = violations.Add(evalR.output.AsViolationStore()) | ||
} |
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.
I believe, policy evaluation can also get it's own function.
pkg/runtime/executor.go
Outdated
func updateResourceConfigs(resourceType string, resources []output.ResourceConfig, allResources output.AllResourceConfigs) { | ||
for _, res := range resources { | ||
if !isConfigPresent(allResources[resourceType], res) { | ||
allResources[resourceType] = append(allResources[resourceType], res) | ||
} | ||
} | ||
} | ||
|
||
// isConfigPresent checks whether a resource is already present in the list of configs or not | ||
// the equality of a resource is based on name, source and config of the resource | ||
func isConfigPresent(resources []output.ResourceConfig, resourceConfig output.ResourceConfig) bool { | ||
for _, resource := range resources { | ||
if resource.Name == resourceConfig.Name && resource.Source == resourceConfig.Source { | ||
if reflect.DeepEqual(resource.Config, resourceConfig.Config) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
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.
Can these functions become part of utils
? I see wider applications of the deduplication logic.
bf4f874
to
c8bf725
Compare
pkg/utils/resource.go
Outdated
@@ -61,3 +62,25 @@ func GetResourceCount(resourceMapping map[string][]output.ResourceConfig) (count | |||
} | |||
return | |||
} | |||
|
|||
// UpdateResourceConfigs adds a resource of given type if it is not present in allResources | |||
func UpdateResourceConfigs(resourceType string, resources []output.ResourceConfig, allResources output.AllResourceConfigs) { |
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.
Hey @patilpankaj212 ,
Does it make sense to have this function and other functions in this file to be moved to the output package itself. Come to think of it these are not utils
in real sense but specific use cases related to the AllResourcesConfig
in the output package.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
…nable#674) * initial changes * fix unit tests * run dir scan and engine evaluation concurrently * e2e test fix
scan_errors
, which will include all the errors that occurred while the iac providers did the folder scan.scan_errors
is not supported with default human readable output, also it is not part of--config-only