2021-07-12 10:26:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-07-22 10:26:05 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-07-12 10:26:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
resourceaws "github.com/cloudskiff/driftctl/pkg/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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *IamRolePolicyEnumerator) Enumerate() ([]resource.Resource, error) {
|
|
|
|
roles, err := e.repository.ListAllRoles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsIamRolePolicyResourceType, resourceaws.AwsIamRoleResourceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
policies, err := e.repository.ListAllRolePolicies(roles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsIamRolePolicyResourceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make([]resource.Resource, len(policies))
|
|
|
|
for _, policy := range policies {
|
|
|
|
results = append(
|
|
|
|
results,
|
|
|
|
e.factory.CreateAbstractResource(
|
|
|
|
string(e.SupportedType()),
|
2021-07-22 10:26:05 +00:00
|
|
|
fmt.Sprintf("%s:%s", policy.RoleName, policy.Policy),
|
|
|
|
map[string]interface{}{
|
|
|
|
"role": policy.RoleName,
|
|
|
|
},
|
2021-07-12 10:26:34 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|