2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2021-05-19 15:09:11 +00:00
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IamPolicySupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-06-28 16:21:12 +00:00
|
|
|
repository repository.IAMRepository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:21:12 +00:00
|
|
|
func NewIamPolicySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamPolicySupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &IamPolicySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-06-28 16:21:12 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *IamPolicySupplier) Resources() ([]resource.Resource, error) {
|
2021-06-28 16:21:12 +00:00
|
|
|
policies, err := s.repository.ListAllPolicies()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamPolicyResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
if len(policies) > 0 {
|
|
|
|
for _, policy := range policies {
|
|
|
|
u := *policy
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
2021-05-19 15:09:11 +00:00
|
|
|
return s.readPolicy(&u)
|
2020-12-09 15:31:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err = s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(resourceaws.AwsIamPolicyResourceType, results)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:09:11 +00:00
|
|
|
func (s *IamPolicySupplier) readPolicy(resource *iam.Policy) (cty.Value, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
res, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsIamPolicyResourceType,
|
|
|
|
ID: *resource.Arn,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading iam policy %s[%s]: %+v", *resource.Arn, resourceaws.AwsIamPolicyResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *res, nil
|
|
|
|
}
|