From a8918f7df1fdefb02d674bcb90ae34de87e3976c Mon Sep 17 00:00:00 2001 From: Martin Guibert Date: Wed, 15 Sep 2021 21:39:11 +0200 Subject: [PATCH] minor fixes, remove writealert. save reading alert in batch --- pkg/cmd/scan.go | 7 ----- pkg/cmd/scan/output/console.go | 13 ++++----- pkg/iac/errors.go | 27 +++++++++++++++++++ pkg/iac/supplier/IacChainSupplier.go | 24 ++++++----------- pkg/iac/terraform/state/alerts.go | 2 +- pkg/iac/terraform/state/enumerator/file.go | 4 +-- pkg/iac/terraform/state/enumerator/s3.go | 4 +-- .../state/enumerator/state_enumerator.go | 2 +- .../terraform/state/terraform_state_reader.go | 20 ++++++++------ 9 files changed, 58 insertions(+), 45 deletions(-) create mode 100644 pkg/iac/errors.go diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index 86331ef7..a069927a 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -209,7 +209,6 @@ func scanRun(opts *pkg.ScanOptions) error { err := remote.Activate(opts.To, opts.ProviderVersion, alerter, providerLibrary, remoteLibrary, scanProgress, resourceSchemaRepository, resFactory, opts.ConfigDir) if err != nil { - WriteAlert(alerter) return err } @@ -227,7 +226,6 @@ func scanRun(opts *pkg.ScanOptions) error { iacSupplier, err := supplier.GetIACSupplier(opts.From, providerLibrary, opts.BackendOptions, iacProgress, alerter, resFactory, driftIgnore) if err != nil { - WriteAlert(alerter) return err } @@ -252,7 +250,6 @@ func scanRun(opts *pkg.ScanOptions) error { analysis, err := ctl.Run() if err != nil { - WriteAlert(alerter) return err } @@ -292,10 +289,6 @@ func scanRun(opts *pkg.ScanOptions) error { return nil } -func WriteAlert(alerter *alerter.Alerter) { - _ = output.NewConsole().WriteAlerts(alerter.Retrieve()) -} - func parseFromFlag(from []string) ([]config.SupplierConfig, error) { configs := make([]config.SupplierConfig, 0, len(from)) diff --git a/pkg/cmd/scan/output/console.go b/pkg/cmd/scan/output/console.go index 91ac3211..2158f155 100644 --- a/pkg/cmd/scan/output/console.go +++ b/pkg/cmd/scan/output/console.go @@ -9,8 +9,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws/awsutil" - "github.com/cloudskiff/driftctl/pkg/alerter" - remotealerts "github.com/cloudskiff/driftctl/pkg/remote/alerts" + "github.com/cloudskiff/driftctl/pkg/remote/alerts" "github.com/fatih/color" "github.com/mattn/go-isatty" "github.com/r3labs/diff/v2" @@ -161,15 +160,12 @@ func (c *Console) Write(analysis *analyser.Analysis) error { } c.writeSummary(analysis) - return c.WriteAlerts(analysis.Alerts()) -} -func (c *Console) WriteAlerts(alerts alerter.Alerts) error { enumerationErrorMessage := "" - for _, alerts := range alerts { - for _, alert := range alerts { + for _, a := range analysis.Alerts() { + for _, alert := range a { fmt.Println(color.YellowString(alert.Message())) - if alert, ok := alert.(*remotealerts.RemoteAccessDeniedAlert); ok && enumerationErrorMessage == "" { + if alert, ok := alert.(*alerts.RemoteAccessDeniedAlert); ok && enumerationErrorMessage == "" { enumerationErrorMessage = alert.GetProviderMessage() } } @@ -178,6 +174,7 @@ func (c *Console) WriteAlerts(alerts alerter.Alerts) error { if enumerationErrorMessage != "" { _, _ = fmt.Fprintf(os.Stderr, "\n%s\n", color.YellowString(enumerationErrorMessage)) } + return nil } diff --git a/pkg/iac/errors.go b/pkg/iac/errors.go new file mode 100644 index 00000000..f99fc819 --- /dev/null +++ b/pkg/iac/errors.go @@ -0,0 +1,27 @@ +package iac + +import ( + "fmt" + "strings" +) + +type StateReadingError struct { + errors []error +} + +func NewStateReadingError() *StateReadingError { + return &StateReadingError{} +} + +func (s *StateReadingError) Add(err error) { + s.errors = append(s.errors, err) +} + +func (s *StateReadingError) Error() string { + var err strings.Builder + _, _ = fmt.Fprint(&err, "There were errors reading your states files : \n") + for _, e := range s.errors { + _, _ = fmt.Fprintf(&err, " - %s\n", e.Error()) + } + return err.String() +} diff --git a/pkg/iac/supplier/IacChainSupplier.go b/pkg/iac/supplier/IacChainSupplier.go index 8e134c41..23bb4f85 100644 --- a/pkg/iac/supplier/IacChainSupplier.go +++ b/pkg/iac/supplier/IacChainSupplier.go @@ -4,9 +4,9 @@ import ( "context" "runtime" + "github.com/cloudskiff/driftctl/pkg/iac" "github.com/cloudskiff/driftctl/pkg/parallel" "github.com/cloudskiff/driftctl/pkg/resource" - "github.com/pkg/errors" ) type IacChainSupplier struct { @@ -16,8 +16,7 @@ type IacChainSupplier struct { func NewIacChainSupplier() *IacChainSupplier { return &IacChainSupplier{ - suppliers: []resource.Supplier{}, - runner: parallel.NewParallelRunner(context.TODO(), int64(runtime.NumCPU())), + runner: parallel.NewParallelRunner(context.TODO(), int64(runtime.NumCPU())), } } @@ -25,16 +24,8 @@ func (r *IacChainSupplier) AddSupplier(supplier resource.Supplier) { r.suppliers = append(r.suppliers, supplier) } -func (r *IacChainSupplier) CountSuppliers() int { - return len(r.suppliers) -} - func (r *IacChainSupplier) Resources() ([]*resource.Resource, error) { - if len(r.suppliers) <= 0 { - return nil, errors.New("There was an error retrieving your states check alerts for details.") - } - for _, supplier := range r.suppliers { sup := supplier r.runner.Run(func() (interface{}, error) { @@ -44,7 +35,8 @@ func (r *IacChainSupplier) Resources() ([]*resource.Resource, error) { } results := make([]*resource.Resource, 0) - nbErrors := 0 + isSuccess := false + retrieveError := iac.NewStateReadingError() ReadLoop: for { select { @@ -57,10 +49,10 @@ ReadLoop: result, _ := supplierResult.(*result) if result.err != nil { - nbErrors++ + retrieveError.Add(result.err) continue } - + isSuccess = true results = append(results, result.res...) case <-r.runner.DoneChan(): break ReadLoop @@ -71,9 +63,9 @@ ReadLoop: return nil, r.runner.Err() } - if nbErrors == len(r.suppliers) { + if !isSuccess { // only fail if all suppliers failed - return nil, errors.New("There was an error retrieving your states check alerts for details.") + return nil, retrieveError } return results, nil diff --git a/pkg/iac/terraform/state/alerts.go b/pkg/iac/terraform/state/alerts.go index c879ec8a..c23f9135 100644 --- a/pkg/iac/terraform/state/alerts.go +++ b/pkg/iac/terraform/state/alerts.go @@ -12,7 +12,7 @@ func NewStateReadingAlert(key string, err error) *StateReadingAlert { } func (s *StateReadingAlert) Message() string { - return fmt.Sprintf("Your analysis will be incomplete. There was an error reading state file '%s': %s", s.key, s.err) + return fmt.Sprintf("Your analysis may be incomplete. There was an error reading state file '%s': %s", s.key, s.err) } func (s *StateReadingAlert) ShouldIgnoreResource() bool { diff --git a/pkg/iac/terraform/state/enumerator/file.go b/pkg/iac/terraform/state/enumerator/file.go index 202fbc16..fb3aea1e 100644 --- a/pkg/iac/terraform/state/enumerator/file.go +++ b/pkg/iac/terraform/state/enumerator/file.go @@ -23,8 +23,8 @@ func NewFileEnumerator(config config.SupplierConfig) *FileEnumerator { } } -func (s *FileEnumerator) Path() string { - return s.config.Path +func (s *FileEnumerator) Origin() string { + return s.config.String() } func (s *FileEnumerator) Enumerate() ([]string, error) { diff --git a/pkg/iac/terraform/state/enumerator/s3.go b/pkg/iac/terraform/state/enumerator/s3.go index e0d5420a..ed304f4f 100644 --- a/pkg/iac/terraform/state/enumerator/s3.go +++ b/pkg/iac/terraform/state/enumerator/s3.go @@ -33,8 +33,8 @@ func NewS3Enumerator(config config.SupplierConfig) *S3Enumerator { } } -func (s *S3Enumerator) Path() string { - return s.config.Path +func (s *S3Enumerator) Origin() string { + return s.config.String() } func (s *S3Enumerator) Enumerate() ([]string, error) { diff --git a/pkg/iac/terraform/state/enumerator/state_enumerator.go b/pkg/iac/terraform/state/enumerator/state_enumerator.go index 864a169d..f852e6bb 100644 --- a/pkg/iac/terraform/state/enumerator/state_enumerator.go +++ b/pkg/iac/terraform/state/enumerator/state_enumerator.go @@ -7,7 +7,7 @@ import ( ) type StateEnumerator interface { - Path() string + Origin() string Enumerate() ([]string, error) } diff --git a/pkg/iac/terraform/state/terraform_state_reader.go b/pkg/iac/terraform/state/terraform_state_reader.go index 165e1a81..3a3edf85 100644 --- a/pkg/iac/terraform/state/terraform_state_reader.go +++ b/pkg/iac/terraform/state/terraform_state_reader.go @@ -6,6 +6,7 @@ import ( "github.com/cloudskiff/driftctl/pkg/alerter" "github.com/cloudskiff/driftctl/pkg/filter" + "github.com/cloudskiff/driftctl/pkg/iac" "github.com/cloudskiff/driftctl/pkg/output" "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/states" @@ -222,16 +223,17 @@ func (r *TerraformStateReader) retrieveForState(path string) ([]*resource.Resour r.progress.Inc() values, err := r.retrieve() if err != nil { - return nil, err + return nil, errors.Wrap(err, r.config.String()) } - return r.decode(values) + decode, err := r.decode(values) + return decode, errors.Wrap(err, r.config.String()) } func (r *TerraformStateReader) retrieveMultiplesStates() ([]*resource.Resource, error) { keys, err := r.enumerator.Enumerate() if err != nil { - r.alerter.SendAlert("", NewStateReadingAlert(r.enumerator.Path(), err)) - return nil, err + r.alerter.SendAlert("", NewStateReadingAlert(r.enumerator.Origin(), err)) + return nil, errors.Wrap(err, r.config.String()) } logrus.WithFields(logrus.Fields{ @@ -239,21 +241,23 @@ func (r *TerraformStateReader) retrieveMultiplesStates() ([]*resource.Resource, }).Debug("Enumerated keys") results := make([]*resource.Resource, 0) - nbAlert := 0 + isSuccess := false + readingError := iac.NewStateReadingError() for _, key := range keys { resources, err := r.retrieveForState(key) if err != nil { + readingError.Add(err) r.alerter.SendAlert("", NewStateReadingAlert(key, err)) - nbAlert++ continue } + isSuccess = true results = append(results, resources...) } - if nbAlert == len(keys) { + if !isSuccess { // all key failed, throw an error - return results, errors.Errorf("Files were found but none of them could be read as a Terraform state.") + return results, readingError } return results, nil