2022-03-28 13:07:02 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2022-06-28 07:23:29 +00:00
|
|
|
"github.com/snyk/driftctl/enumeration/resource"
|
|
|
|
"github.com/snyk/driftctl/enumeration/resource/aws"
|
2022-03-28 13:07:02 +00:00
|
|
|
)
|
|
|
|
|
2022-04-04 06:19:14 +00:00
|
|
|
// AwsEbsEncryptionByDefaultReconciler is a middleware that either creates an 'aws_ebs_encryption_by_default' resource
|
|
|
|
// based on its equivalent state one just for the purpose of getting the Terraform custom Id, or removes the resource
|
|
|
|
// from our list of remote resources if it is not managed and is disabled.
|
2022-03-28 13:07:02 +00:00
|
|
|
type AwsEbsEncryptionByDefaultReconciler struct {
|
|
|
|
resourceFactory resource.ResourceFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAwsEbsEncryptionByDefaultReconciler(resourceFactory resource.ResourceFactory) AwsEbsEncryptionByDefaultReconciler {
|
|
|
|
return AwsEbsEncryptionByDefaultReconciler{
|
|
|
|
resourceFactory: resourceFactory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m AwsEbsEncryptionByDefaultReconciler) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
|
|
|
|
newStateResources := make([]*resource.Resource, 0)
|
|
|
|
newRemoteResources := make([]*resource.Resource, 0)
|
|
|
|
|
2022-03-31 12:34:31 +00:00
|
|
|
var found bool
|
2022-03-28 13:07:02 +00:00
|
|
|
var defaultEbsEncryption *resource.Resource
|
|
|
|
|
|
|
|
for _, res := range *remoteResources {
|
|
|
|
// Ignore all resources other than aws_ebs_encryption_by_default
|
|
|
|
if res.ResourceType() != aws.AwsEbsEncryptionByDefaultResourceType {
|
|
|
|
newRemoteResources = append(newRemoteResources, res)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defaultEbsEncryption = res
|
|
|
|
}
|
|
|
|
|
2022-04-04 13:28:59 +00:00
|
|
|
// We can encounter this case when we don't have permission to get this setting from AWS.
|
|
|
|
if defaultEbsEncryption == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:07:02 +00:00
|
|
|
for _, res := range *resourcesFromState {
|
|
|
|
newStateResources = append(newStateResources, res)
|
|
|
|
|
|
|
|
// Ignore all resources other than aws_ebs_encryption_by_default
|
|
|
|
if res.ResourceType() != aws.AwsEbsEncryptionByDefaultResourceType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-04 06:19:14 +00:00
|
|
|
// Create a new remote resource that will be similar to the state resource but with the 'enabled' attribute of the remote one.
|
|
|
|
// The reason why is that the id is a random string created by Terraform that we need to compare two resources.
|
2022-03-28 13:07:02 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, m.resourceFactory.CreateAbstractResource(
|
|
|
|
res.ResourceType(),
|
|
|
|
res.ResourceId(),
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": res.ResourceId(),
|
|
|
|
"enabled": *defaultEbsEncryption.Attributes().GetBool("enabled"),
|
|
|
|
},
|
|
|
|
))
|
2022-03-31 12:34:31 +00:00
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if defaultEbsEncryption != nil && !found && *defaultEbsEncryption.Attributes().GetBool("enabled") {
|
|
|
|
newRemoteResources = append(newRemoteResources, defaultEbsEncryption)
|
2022-03-28 13:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*resourcesFromState = newStateResources
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
return nil
|
|
|
|
}
|