2021-06-28 09:00:59 +00:00
|
|
|
package remote
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-03-26 08:44:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/parallel"
|
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
|
2021-06-28 09:00:59 +00:00
|
|
|
alerter alerter.AlerterInterface
|
2021-06-11 15:10:06 +00:00
|
|
|
options ScannerOptions
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:00:59 +00:00
|
|
|
func NewScanner(resourceSuppliers []resource.Supplier, remoteLibrary *common.RemoteLibrary, alerter alerter.AlerterInterface, 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-06-28 09:00:59 +00:00
|
|
|
err := HandleResourceEnumerationError(err, s.alerter)
|
2021-01-20 13:01:57 +00:00
|
|
|
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 {
|
2021-07-06 10:12:55 +00:00
|
|
|
err := HandleResourceEnumerationError(err, s.alerter)
|
|
|
|
if err == nil {
|
|
|
|
return []resource.Resource{}, nil
|
|
|
|
}
|
2021-06-11 15:10:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:42:52 +00:00
|
|
|
if !s.options.Deep {
|
|
|
|
return enumerationResult, nil
|
|
|
|
}
|
|
|
|
|
2021-06-11 15:10:06 +00:00
|
|
|
for _, res := range enumerationResult {
|
|
|
|
res := res
|
|
|
|
s.detailsFetcherRunner.Run(func() (interface{}, error) {
|
|
|
|
fetcher := s.remoteLibrary.GetDetailsFetcher(resource.ResourceType(res.TerraformType()))
|
|
|
|
if fetcher != nil {
|
2021-07-20 12:42:52 +00:00
|
|
|
resourceWithDetails, err := fetcher.ReadDetails(res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-06-11 15:10:06 +00:00
|
|
|
}
|
2021-07-20 12:42:52 +00:00
|
|
|
return []resource.Resource{resourceWithDetails}, nil
|
2021-06-11 15:10:06 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|