/
account_provider.go
147 lines (124 loc) · 4.46 KB
/
account_provider.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package awslib
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/organizations/types"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/cloudbeat/internal/dataprovider/providers/cloud"
"github.com/elastic/cloudbeat/internal/resources/utils/pointers"
)
type AccountProviderAPI interface {
ListAccounts(ctx context.Context, log *logp.Logger, cfg aws.Config) ([]cloud.Identity, error)
}
type organizationsAPI interface {
organizations.ListAccountsAPIClient
organizations.ListParentsAPIClient
DescribeOrganizationalUnit(context.Context, *organizations.DescribeOrganizationalUnitInput, ...func(*organizations.Options)) (*organizations.DescribeOrganizationalUnitOutput, error)
}
type AccountProvider struct{}
func (a AccountProvider) ListAccounts(ctx context.Context, log *logp.Logger, cfg aws.Config) ([]cloud.Identity, error) {
return listAccounts(ctx, log, organizations.NewFromConfig(cfg))
}
func listAccounts(ctx context.Context, log *logp.Logger, client organizationsAPI) ([]cloud.Identity, error) {
organizationIdToName := make(map[string]string)
input := organizations.ListAccountsInput{}
var accounts []cloud.Identity
for {
o, err := client.ListAccounts(ctx, &input)
if err != nil {
return nil, err
}
for _, account := range o.Accounts {
if account.Status != types.AccountStatusActive || account.Id == nil {
continue
}
organization, err := getOUInfoForAccount(ctx, client, organizationIdToName, account.Id)
if err != nil {
log.Errorf("failed to get organizational unit info for account %s: %v", *account.Id, err)
}
accounts = append(accounts, cloud.Identity{
Provider: "aws",
Account: *account.Id,
AccountAlias: pointers.Deref(account.Name),
OrganizationId: organization.id,
OrganizationName: organization.name,
})
}
if o.NextToken == nil {
break
}
input.NextToken = o.NextToken
}
return accounts, nil
}
type organizationalUnitInfo struct {
id string
name string
}
func getOUInfoForAccount(ctx context.Context, client organizationsAPI, cache map[string]string, accountId *string) (organizationalUnitInfo, error) {
// We need a paginator, according to the AWS docs:
// These operations can occasionally return an empty set of results even when there are more results available.
paginator := organizations.NewListParentsPaginator(client, &organizations.ListParentsInput{ChildId: accountId})
for paginator.HasMorePages() {
o, err := paginator.NextPage(ctx)
if err != nil {
return organizationalUnitInfo{}, err
}
if len(o.Parents) == 0 {
continue
}
// According to AWS, in the current release, a child can have only a single parent.
parent := o.Parents[0]
if parent.Type == types.ParentTypeRoot {
return organizationalUnitInfo{
id: *parent.Id,
name: "Root",
}, nil
}
return describeOU(ctx, client, cache, parent.Id)
}
return organizationalUnitInfo{}, errors.New("empty response")
}
func describeOU(ctx context.Context, client organizationsAPI, cache map[string]string, id *string) (organizationalUnitInfo, error) {
if id == nil {
return organizationalUnitInfo{}, errors.New("nil id")
}
if savedName, ok := cache[*id]; ok {
return organizationalUnitInfo{
id: *id,
name: savedName,
}, nil
}
o, err := client.DescribeOrganizationalUnit(ctx, &organizations.DescribeOrganizationalUnitInput{
OrganizationalUnitId: id,
})
if err != nil {
return organizationalUnitInfo{id: *id}, err
}
name := pointers.Deref(o.OrganizationalUnit.Name)
if cache != nil {
cache[*id] = name
}
return organizationalUnitInfo{
id: *id,
name: name,
}, nil
}