driftctl/pkg/middlewares/aws_default_igw_route.go

73 lines
2.6 KiB
Go
Raw Normal View History

2021-01-22 10:32:56 +00:00
package middlewares
import (
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration/resource"
2022-07-21 08:37:03 +00:00
"github.com/snyk/driftctl/pkg/resource/aws"
2021-01-22 10:32:56 +00:00
)
// Each region has a default vpc which has an internet gateway attached and thus the route table of this
// same vpc has a default route (0.0.0.0/0) that should not be seen as unmanaged if not managed by IaC
2021-01-22 10:32:56 +00:00
// This middleware ignores the above route from unmanaged resources if not managed by IaC
type AwsDefaultInternetGatewayRoute struct{}
func NewAwsDefaultInternetGatewayRoute() AwsDefaultInternetGatewayRoute {
return AwsDefaultInternetGatewayRoute{}
}
2021-08-09 14:03:04 +00:00
func (m AwsDefaultInternetGatewayRoute) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
newRemoteResources := make([]*resource.Resource, 0)
2021-01-22 10:32:56 +00:00
for _, remoteResource := range *remoteResources {
// Ignore all resources other than routes
if remoteResource.ResourceType() != aws.AwsRouteResourceType {
2021-01-22 10:32:56 +00:00
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Ignore all routes except the one that came from the default internet gateway
2021-08-09 14:03:04 +00:00
if !isDefaultInternetGatewayRoute(remoteResource, remoteResources) {
2021-01-22 10:32:56 +00:00
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Check if route is managed by IaC
existInState := false
for _, stateResource := range *resourcesFromState {
2021-08-09 14:03:04 +00:00
if remoteResource.Equal(stateResource) {
2021-01-22 10:32:56 +00:00
existInState = true
break
}
}
// Include resource if it's managed in IaC
if existInState {
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
// Else, resource is not added to newRemoteResources slice so it will be ignored
logrus.WithFields(logrus.Fields{
"id": remoteResource.ResourceId(),
"type": remoteResource.ResourceType(),
2021-01-22 10:32:56 +00:00
}).Debug("Ignoring default internet gateway route as it is not managed by IaC")
}
*remoteResources = newRemoteResources
return nil
}
// Return true if the route's target is the default internet gateway (e.g. attached to the default vpc)
2021-08-09 14:03:04 +00:00
func isDefaultInternetGatewayRoute(route *resource.Resource, remoteResources *[]*resource.Resource) bool {
2021-01-22 10:32:56 +00:00
for _, remoteResource := range *remoteResources {
if remoteResource.ResourceType() == aws.AwsInternetGatewayResourceType &&
2021-08-09 14:03:04 +00:00
isDefaultInternetGateway(remoteResource, remoteResources) {
gtwId, gtwIdExist := route.Attrs.Get("gateway_id")
destCIDRBlock, destCIDRBlockExist := route.Attrs.Get("destination_cidr_block")
return gtwIdExist && destCIDRBlockExist && gtwId == remoteResource.ResourceId() && destCIDRBlock == "0.0.0.0/0"
2021-01-22 10:32:56 +00:00
}
}
return false
}