/
table_aws_organizations_policy_target.go
178 lines (160 loc) · 5.57 KB
/
table_aws_organizations_policy_target.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/organizations/types"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
func tableAwsOrganizationsPolicyTarget(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_organizations_policy_target",
Description: "AWS Organizations Policy Target",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("id"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"PolicyNotFoundException", "InvalidInputException"}),
},
Hydrate: getOrganizationsPolicy,
Tags: map[string]string{"service": "organizations", "action": "DescribePolicy"},
},
List: &plugin.ListConfig{
Hydrate: listOrganizationsPolicyTragets,
Tags: map[string]string{"service": "organizations", "action": "ListPoliciesForTarget"},
KeyColumns: plugin.AllColumns([]string{"type", "target_id"}),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"InvalidInputException", "TargetNotFoundException"}),
},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getOrganizationsPolicy,
Tags: map[string]string{"service": "organizations", "action": "DescribePolicy"},
},
},
Columns: awsGlobalRegionColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name of the policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Name"),
},
{
Name: "id",
Description: "The unique identifier (ID) of the policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Id"),
},
{
Name: "target_id",
Description: "The unique identifier (ID) of the root, organizational unit, or account whose policies you want to list.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("target_id"),
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Arn"),
},
{
Name: "type",
Description: "The type of policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Type"),
},
{
Name: "aws_managed",
Description: "A boolean value that indicates whether the specified policy is an Amazon Web Services managed policy. If true, then you can attach the policy to roots, OUs, or accounts, but you cannot edit it.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("PolicySummary.AwsManaged"),
},
{
Name: "description",
Description: "The description of the policy.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Description"),
},
{
Name: "content",
Description: "The text content of the policy.",
Type: proto.ColumnType_JSON,
Hydrate: getOrganizationsPolicy,
Transform: transform.FromField("Content"),
},
// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("PolicySummary.Name"),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("PolicySummary.Arn").Transform(transform.EnsureStringArray),
},
}),
}
}
//// LIST FUNCTION
func listOrganizationsPolicyTragets(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Get Client
svc, err := OrganizationClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_organizations_policy_target.listOrganizationsPolicyTragets", "client_error", err)
return nil, err
}
policyType := d.EqualsQualString("type")
targetId := d.EqualsQualString("target_id")
// Empty Check
if policyType == "" || targetId == "" {
return nil, nil
}
// Limiting the result
maxItems := int32(20)
// Reduce the basic request limit down if the user has only requested a small number of rows
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxItems {
maxItems = int32(limit)
}
}
params := &organizations.ListPoliciesForTargetInput{
Filter: types.PolicyType(policyType),
TargetId: &targetId,
MaxResults: &maxItems,
}
paginator := organizations.NewListPoliciesForTargetPaginator(svc, params, func(o *organizations.ListPoliciesForTargetPaginatorOptions) {
o.Limit = maxItems
o.StopOnDuplicateToken = true
})
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_organizations_policy_target.listOrganizationsPolicies", "api_error", err)
return nil, err
}
for _, policy := range output.Policies {
d.StreamListItem(ctx, types.Policy{
PolicySummary: &types.PolicySummary{
Arn: policy.Arn,
AwsManaged: policy.AwsManaged,
Description: policy.Description,
Id: policy.Id,
Name: policy.Name,
Type: policy.Type,
},
})
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, nil
}