2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2020-12-09 15:31:34 +00:00
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam/iamiface"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
|
|
|
"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
|
|
|
|
deserializer deserializer.CTYDeserializer
|
|
|
|
client iamiface.IAMAPI
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:56:11 +00:00
|
|
|
func NewIamRolePolicySupplier(provider *AWSTerraformProvider) *IamRolePolicySupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &IamRolePolicySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewIamRolePolicyDeserializer(),
|
2021-01-22 17:06:17 +00:00
|
|
|
iam.New(provider.session),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s IamRolePolicySupplier) Resources() ([]resource.Resource, error) {
|
2021-01-20 13:01:57 +00:00
|
|
|
policies, err := listIamRolePolicies(s.client, resourceaws.AwsIamRolePolicyResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, policyName := range policies {
|
|
|
|
name := policyName
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readRes(name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
results, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.deserializer.Deserialize(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s IamRolePolicySupplier) readRes(name string) (cty.Value, error) {
|
|
|
|
res, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: resourceaws.AwsIamRolePolicyResourceType,
|
|
|
|
ID: name,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error reading iam role policy %s[%s]: %+v", name, resourceaws.AwsIamRolePolicyResourceType, err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return *res, nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
func listIamRolePolicies(client iamiface.IAMAPI, supplierType string) ([]string, error) {
|
|
|
|
roles, err := listIamRoles(client, supplierType)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resources []string
|
|
|
|
for _, role := range roles {
|
2021-02-04 21:42:57 +00:00
|
|
|
role := role
|
2020-12-09 15:31:34 +00:00
|
|
|
input := &iam.ListRolePoliciesInput{
|
|
|
|
RoleName: role.RoleName,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ListRolePoliciesPages(input, func(res *iam.ListRolePoliciesOutput, lastPage bool) bool {
|
|
|
|
for _, policy := range res.PolicyNames {
|
2021-02-04 21:42:57 +00:00
|
|
|
policy := policy
|
2020-12-09 15:31:34 +00:00
|
|
|
resources = append(
|
|
|
|
resources,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"%s:%s",
|
|
|
|
*role.RoleName,
|
|
|
|
*policy,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return !lastPage
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, supplierType, resourceaws.AwsIamRoleResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|