driftctl/enumeration/remote/aws/iam_role_policy_enumerator.go

55 lines
1.5 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
"github.com/snyk/driftctl/enumeration/resource"
resourceaws "github.com/snyk/driftctl/enumeration/resource/aws"
)
type IamRolePolicyEnumerator struct {
repository repository.IAMRepository
factory resource.ResourceFactory
}
func NewIamRolePolicyEnumerator(repository repository.IAMRepository, factory resource.ResourceFactory) *IamRolePolicyEnumerator {
return &IamRolePolicyEnumerator{
repository,
factory,
}
}
func (e *IamRolePolicyEnumerator) SupportedType() resource.ResourceType {
return resourceaws.AwsIamRolePolicyResourceType
}
2021-08-09 14:03:04 +00:00
func (e *IamRolePolicyEnumerator) Enumerate() ([]*resource.Resource, error) {
roles, err := e.repository.ListAllRoles()
if err != nil {
2021-08-04 09:08:08 +00:00
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), resourceaws.AwsIamRoleResourceType)
}
policies, err := e.repository.ListAllRolePolicies(roles)
if err != nil {
2021-08-04 09:08:08 +00:00
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
2021-10-27 09:30:47 +00:00
results := make([]*resource.Resource, 0, len(policies))
for _, policy := range policies {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
fmt.Sprintf("%s:%s", policy.RoleName, policy.Policy),
map[string]interface{}{
"role": policy.RoleName,
},
),
)
}
return results, nil
}