2020-12-09 15:31:34 +00:00
|
|
|
package pkg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-16 12:02:02 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/filter"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/middlewares"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/jmespath/go-jmespath"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DriftCTL struct {
|
|
|
|
remoteSupplier resource.Supplier
|
|
|
|
iacSupplier resource.Supplier
|
|
|
|
analyzer analyser.Analyzer
|
|
|
|
filter *jmespath.JMESPath
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:02:02 +00:00
|
|
|
func NewDriftCTL(remoteSupplier resource.Supplier, iacSupplier resource.Supplier, filter *jmespath.JMESPath, alerter *alerter.Alerter) *DriftCTL {
|
|
|
|
return &DriftCTL{remoteSupplier, iacSupplier, analyser.NewAnalyzer(alerter), filter}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d DriftCTL) Run() *analyser.Analysis {
|
|
|
|
remoteResources, resourcesFromState, err := d.scan()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Unable to scan resources: %+v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := middlewares.NewChain(
|
|
|
|
middlewares.NewRoute53DefaultZoneRecordSanitizer(),
|
|
|
|
middlewares.NewS3BucketAcl(),
|
|
|
|
middlewares.NewAwsInstanceBlockDeviceResourceMapper(),
|
|
|
|
middlewares.NewVPCDefaultSecurityGroupSanitizer(),
|
|
|
|
middlewares.NewVPCSecurityGroupRuleSanitizer(),
|
|
|
|
middlewares.NewIamPolicyAttachmentSanitizer(),
|
2020-12-14 11:18:14 +00:00
|
|
|
middlewares.AwsInstanceEIP{},
|
2020-12-15 10:07:03 +00:00
|
|
|
middlewares.NewAwsDefaultVPC(),
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
logrus.Debug("Ready to run middlewares")
|
|
|
|
err = middleware.Execute(&remoteResources, &resourcesFromState)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Unable to run middlewares: %+v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.filter != nil {
|
|
|
|
engine := filter.NewFilterEngine(d.filter)
|
|
|
|
remoteResources, err = engine.Run(remoteResources)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resourcesFromState, err = engine.Run(resourcesFromState)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debug("Checking for driftignore")
|
|
|
|
driftIgnore := filter.NewDriftIgnore()
|
|
|
|
|
2020-12-18 14:28:46 +00:00
|
|
|
analysis, err := d.analyzer.Analyze(remoteResources, resourcesFromState, driftIgnore)
|
2020-12-16 12:02:02 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Unable to analyse resources: %+v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &analysis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DriftCTL) Stop() {
|
|
|
|
stoppableSupplier, ok := d.remoteSupplier.(resource.StoppableSupplier)
|
|
|
|
if ok {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"supplier": fmt.Sprintf("%T", d.remoteSupplier),
|
|
|
|
}).Debug("Stopping remote supplier")
|
|
|
|
stoppableSupplier.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
stoppableSupplier, ok = d.iacSupplier.(resource.StoppableSupplier)
|
|
|
|
if ok {
|
|
|
|
stoppableSupplier.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d DriftCTL) scan() (remoteResources []resource.Resource, resourcesFromState []resource.Resource, err error) {
|
2021-01-08 11:10:25 +00:00
|
|
|
logrus.Info("Start reading terraform state")
|
|
|
|
resourcesFromState, err = d.iacSupplier.Resources()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:10:25 +00:00
|
|
|
logrus.Info("Start scanning cloud provider")
|
|
|
|
remoteResources, err = d.remoteSupplier.Resources()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return remoteResources, resourcesFromState, err
|
|
|
|
}
|