2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-05-19 15:02:45 +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"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IamRolePolicySupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-05-27 14:15:55 +00:00
|
|
|
repo repository.IAMRepository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-06-07 13:02:12 +00:00
|
|
|
func NewIamRolePolicySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.IAMRepository) *IamRolePolicySupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &IamRolePolicySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-06-07 13:02:12 +00:00
|
|
|
repo,
|
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 *IamRolePolicySupplier) Resources() ([]resource.Resource, error) {
|
2021-05-27 14:15:55 +00:00
|
|
|
roles, err := s.repo.ListAllRoles()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-05-19 15:02:45 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamRolePolicyResourceType, resourceaws.AwsIamRoleResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-05-27 14:15:55 +00:00
|
|
|
policies, err := s.repo.ListAllRolePolicies(roles)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-05-19 15:02:45 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamRolePolicyResourceType)
|
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
|
|
|
if len(policies) > 0 {
|
|
|
|
for _, policy := range policies {
|
|
|
|
p := policy
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readRolePolicy(p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err = s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(resourceaws.AwsIamRolePolicyResourceType, results)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:02:45 +00:00
|
|
|
func (s *IamRolePolicySupplier) readRolePolicy(policyName string) (cty.Value, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
res, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsIamRolePolicyResourceType,
|
2021-05-19 15:02:45 +00:00
|
|
|
ID: policyName,
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-19 15:02:45 +00:00
|
|
|
logrus.Warnf("Error reading iam role policy %s[%s]: %+v", policyName, resourceaws.AwsIamRolePolicyResourceType, err)
|
2020-12-09 15:31:34 +00:00
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *res, nil
|
|
|
|
}
|