forked from snyk/driftctl
/
iam_access_key_enumerator.go
52 lines (44 loc) · 1.38 KB
/
iam_access_key_enumerator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package aws
import (
"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 IamAccessKeyEnumerator struct {
repository repository.IAMRepository
factory resource.ResourceFactory
}
func NewIamAccessKeyEnumerator(repository repository.IAMRepository, factory resource.ResourceFactory) *IamAccessKeyEnumerator {
return &IamAccessKeyEnumerator{
repository,
factory,
}
}
func (e *IamAccessKeyEnumerator) SupportedType() resource.ResourceType {
return resourceaws.AwsIamAccessKeyResourceType
}
func (e *IamAccessKeyEnumerator) Enumerate() ([]*resource.Resource, error) {
users, err := e.repository.ListAllUsers()
if err != nil {
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), resourceaws.AwsIamUserResourceType)
}
keys, err := e.repository.ListAllAccessKeys(users)
if err != nil {
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
}
results := make([]*resource.Resource, 0)
for _, key := range keys {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*key.AccessKeyId,
map[string]interface{}{
"user": *key.UserName,
},
),
)
}
return results, nil
}