2020-12-14 11:18:14 +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-14 11:18:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AwsInstanceEIP struct{}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (a AwsInstanceEIP) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
|
2020-12-14 11:18:14 +00:00
|
|
|
for _, remoteResource := range *remoteResources {
|
|
|
|
// Ignore all resources other than aws_instance
|
2021-08-18 13:58:28 +00:00
|
|
|
if remoteResource.ResourceType() != aws.AwsInstanceResourceType {
|
2020-12-14 11:18:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
if a.hasEIP(remoteResource, resourcesFromState) {
|
2020-12-14 11:18:14 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-08-18 13:58:28 +00:00
|
|
|
"instance": remoteResource.ResourceId(),
|
2020-12-14 11:18:14 +00:00
|
|
|
}).Debug("Ignore instance public ip and dns as it has an eip attached")
|
2021-08-09 14:03:04 +00:00
|
|
|
a.ignorePublicIpAndDns(remoteResource, remoteResources, resourcesFromState)
|
2020-12-14 11:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (a AwsInstanceEIP) hasEIP(instance *resource.Resource, resources *[]*resource.Resource) bool {
|
2021-04-29 10:32:02 +00:00
|
|
|
for _, res := range *resources {
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() == aws.AwsEipResourceType {
|
|
|
|
if (*res.Attrs)["instance"] == instance.ResourceId() {
|
2021-04-29 10:32:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() == aws.AwsEipAssociationResourceType {
|
|
|
|
if (*res.Attrs)["instance_id"] == instance.ResourceId() {
|
2021-04-29 10:32:02 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (a AwsInstanceEIP) ignorePublicIpAndDns(instance *resource.Resource, resourcesSet ...*[]*resource.Resource) {
|
2021-04-29 10:32:02 +00:00
|
|
|
for _, resources := range resourcesSet {
|
|
|
|
for _, res := range *resources {
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() == instance.ResourceType() &&
|
|
|
|
res.ResourceId() == instance.ResourceId() {
|
2021-08-09 14:03:04 +00:00
|
|
|
res.Attrs.SafeDelete([]string{"public_dns"})
|
|
|
|
res.Attrs.SafeDelete([]string{"public_ip"})
|
2021-04-29 10:32:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|