2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-05-21 14:09:45 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-06-07 09:04:46 +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 IamUserPolicyAttachmentSupplier 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 NewIamUserPolicyAttachmentSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.IAMRepository) *IamUserPolicyAttachmentSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &IamUserPolicyAttachmentSupplier{
|
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 *IamUserPolicyAttachmentSupplier) Resources() ([]resource.Resource, error) {
|
2021-06-28 16:21:12 +00:00
|
|
|
users, err := s.repository.ListAllUsers()
|
2021-05-19 15:05:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamUserPolicyAttachmentResourceType, resourceaws.AwsIamUserResourceType)
|
|
|
|
}
|
2021-06-28 16:21:12 +00:00
|
|
|
policyAttachments, err := s.repository.ListAllUserPolicyAttachments(users)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-05-19 15:05:41 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamUserPolicyAttachmentResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
results := make([]cty.Value, 0)
|
2021-05-19 15:05:41 +00:00
|
|
|
if len(policyAttachments) > 0 {
|
|
|
|
for _, attachedPolicy := range policyAttachments {
|
2020-12-09 15:31:34 +00:00
|
|
|
attached := *attachedPolicy
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
2021-05-19 15:05:41 +00:00
|
|
|
return s.readUserPolicyAttachment(&attached)
|
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.AwsIamUserPolicyAttachmentResourceType, results)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:05:41 +00:00
|
|
|
func (s *IamUserPolicyAttachmentSupplier) readUserPolicyAttachment(attachedPol *repository.AttachedUserPolicy) (cty.Value, error) {
|
2020-12-09 15:31:34 +00:00
|
|
|
res, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsIamUserPolicyAttachmentResourceType,
|
2021-06-07 09:04:46 +00:00
|
|
|
ID: fmt.Sprintf("%s-%s", *attachedPol.PolicyName, attachedPol.UserName),
|
2020-12-09 15:31:34 +00:00
|
|
|
Attributes: map[string]string{
|
2021-05-19 15:05:41 +00:00
|
|
|
"user": attachedPol.UserName,
|
2020-12-09 15:31:34 +00:00
|
|
|
"policy_arn": *attachedPol.PolicyArn,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading iam user policy attachment %s[%s]: %+v", attachedPol, resourceaws.AwsIamUserPolicyAttachmentResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *res, nil
|
|
|
|
}
|