2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/aws"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Remote NS and SAO records from remote state if not managed by IAC
|
|
|
|
type Route53DefaultZoneRecordSanitizer struct{}
|
|
|
|
|
|
|
|
func NewRoute53DefaultZoneRecordSanitizer() Route53DefaultZoneRecordSanitizer {
|
|
|
|
return Route53DefaultZoneRecordSanitizer{}
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (m Route53DefaultZoneRecordSanitizer) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
newRemoteResources := make([]*resource.Resource, 0)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
// We iterate on remote resource and adding them to a new slice except for default records
|
|
|
|
// added by aws in the zone at creation
|
|
|
|
for _, remoteResource := range *remoteResources {
|
|
|
|
existInState := false
|
|
|
|
|
|
|
|
// Ignore all resources other than route53 records
|
2021-08-18 13:58:28 +00:00
|
|
|
if remoteResource.ResourceType() != aws.AwsRoute53RecordResourceType {
|
2020-12-09 15:31:34 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
if !isDefaultRecord(remoteResource) {
|
2020-12-09 15:31:34 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stateResource := range *resourcesFromState {
|
2021-08-09 14:03:04 +00:00
|
|
|
if remoteResource.Equal(stateResource) {
|
2020-12-09 15:31:34 +00:00
|
|
|
existInState = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if existInState {
|
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !existInState {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-08-18 13:58:28 +00:00
|
|
|
"id": remoteResource.ResourceId(),
|
|
|
|
"type": remoteResource.ResourceType(),
|
2020-12-09 15:31:34 +00:00
|
|
|
}).Debug("Ignoring default unmanaged record")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the record is considered as default one added by aws
|
2021-08-09 14:03:04 +00:00
|
|
|
func isDefaultRecord(record *resource.Resource) bool {
|
2021-05-17 15:03:10 +00:00
|
|
|
ty, _ := record.Attrs.Get("type")
|
|
|
|
return ty == "NS" || ty == "SOA"
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|