2020-12-09 15:31:34 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-26 08:44:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
2021-01-20 13:01:57 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote"
|
2021-06-11 15:10:06 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
2021-03-26 08:44:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
2021-02-09 18:43:39 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-06-11 15:10:06 +00:00
|
|
|
type ScannerOptions struct {
|
|
|
|
Deep bool
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
type Scanner struct {
|
2021-06-11 15:10:06 +00:00
|
|
|
resourceSuppliers []resource.Supplier
|
|
|
|
enumeratorRunner *parallel.ParallelRunner
|
|
|
|
detailsFetcherRunner *parallel.ParallelRunner
|
|
|
|
remoteLibrary *common.RemoteLibrary
|
|
|
|
alerter *alerter.Alerter
|
|
|
|
options ScannerOptions
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:10:06 +00:00
|
|
|
func NewScanner(resourceSuppliers []resource.Supplier, remoteLibrary *common.RemoteLibrary, alerter *alerter.Alerter, options ScannerOptions) *Scanner {
|
2020-12-09 15:31:34 +00:00
|
|
|
return &Scanner{
|
2021-06-11 15:10:06 +00:00
|
|
|
resourceSuppliers: resourceSuppliers,
|
|
|
|
enumeratorRunner: parallel.NewParallelRunner(context.TODO(), 10),
|
|
|
|
detailsFetcherRunner: parallel.NewParallelRunner(context.TODO(), 10),
|
|
|
|
remoteLibrary: remoteLibrary,
|
|
|
|
alerter: alerter,
|
|
|
|
options: options,
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:10:06 +00:00
|
|
|
func (s *Scanner) retrieveRunnerResults(runner *parallel.ParallelRunner) ([]resource.Resource, error) {
|
|
|
|
results := make([]resource.Resource, 0)
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case resources, ok := <-runner.Read():
|
|
|
|
if !ok || resources == nil {
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range resources.([]resource.Resource) {
|
|
|
|
if res != nil {
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-runner.DoneChan():
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, runner.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scanner) legacyScan() ([]resource.Resource, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
for _, resourceProvider := range s.resourceSuppliers {
|
|
|
|
supplier := resourceProvider
|
2021-06-11 15:10:06 +00:00
|
|
|
s.enumeratorRunner.Run(func() (interface{}, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
res, err := supplier.Resources()
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
err := remote.HandleResourceEnumerationError(err, s.alerter)
|
|
|
|
if err == nil {
|
|
|
|
return []resource.Resource{}, nil
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, resource := range res {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"id": resource.TerraformId(),
|
|
|
|
"type": resource.TerraformType(),
|
2021-06-11 15:10:06 +00:00
|
|
|
}).Debug("[DEPRECATED] Found cloud resource")
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:10:06 +00:00
|
|
|
return s.retrieveRunnerResults(s.enumeratorRunner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scanner) scan() ([]resource.Resource, error) {
|
|
|
|
for _, enumerator := range s.remoteLibrary.Enumerators() {
|
|
|
|
enumerator := enumerator
|
|
|
|
s.enumeratorRunner.Run(func() (interface{}, error) {
|
|
|
|
resources, err := enumerator.Enumerate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-06-11 15:10:06 +00:00
|
|
|
for _, resource := range resources {
|
|
|
|
if resource == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"id": resource.TerraformId(),
|
|
|
|
"type": resource.TerraformType(),
|
|
|
|
}).Debug("Found cloud resource")
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-06-11 15:10:06 +00:00
|
|
|
|
|
|
|
enumerationResult, err := s.retrieveRunnerResults(s.enumeratorRunner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, res := range enumerationResult {
|
|
|
|
res := res
|
|
|
|
s.detailsFetcherRunner.Run(func() (interface{}, error) {
|
|
|
|
fetcher := s.remoteLibrary.GetDetailsFetcher(resource.ResourceType(res.TerraformType()))
|
|
|
|
if fetcher != nil {
|
|
|
|
// If we are in deep mode, retrieve resource details
|
|
|
|
if s.options.Deep {
|
|
|
|
resourceWithDetails, err := fetcher.ReadDetails(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []resource.Resource{resourceWithDetails}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return []resource.Resource{res}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.retrieveRunnerResults(s.detailsFetcherRunner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scanner) Resources() ([]resource.Resource, error) {
|
|
|
|
|
|
|
|
resources, err := s.legacyScan()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.enumeratorRunner = parallel.NewParallelRunner(context.TODO(), 10)
|
|
|
|
|
|
|
|
enumerationResult, err := s.scan()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resources = append(resources, enumerationResult...)
|
|
|
|
|
|
|
|
return resources, err
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Scanner) Stop() {
|
|
|
|
logrus.Debug("Stopping scanner")
|
2021-06-11 15:10:06 +00:00
|
|
|
s.enumeratorRunner.Stop(errors.New("interrupted"))
|
|
|
|
s.detailsFetcherRunner.Stop(errors.New("interrupted"))
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|