-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathaws_accessanalyzer.erl
More file actions
471 lines (386 loc) · 15.8 KB
/
aws_accessanalyzer.erl
File metadata and controls
471 lines (386 loc) · 15.8 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
%% WARNING: DO NOT EDIT, AUTO-GENERATED CODE!
%% See https://github.com/aws-beam/aws-codegen for more details.
%% @doc AWS IAM Access Analyzer helps identify potential resource-access
%% risks by enabling you to identify any policies that grant access to an
%% external principal.
%%
%% It does this by using logic-based reasoning to analyze resource-based
%% policies in your AWS environment. An external principal can be another AWS
%% account, a root user, an IAM user or role, a federated user, an AWS
%% service, or an anonymous user. This guide describes the AWS IAM Access
%% Analyzer operations that you can call programmatically. For general
%% information about Access Analyzer, see AWS IAM Access Analyzer in the IAM
%% User Guide.
%%
%% To start using Access Analyzer, you first need to create an analyzer.
-module(aws_accessanalyzer).
-export([apply_archive_rule/2,
apply_archive_rule/3,
create_analyzer/2,
create_analyzer/3,
create_archive_rule/3,
create_archive_rule/4,
delete_analyzer/3,
delete_analyzer/4,
delete_archive_rule/4,
delete_archive_rule/5,
get_analyzed_resource/3,
get_analyzed_resource/4,
get_analyzer/2,
get_analyzer/3,
get_archive_rule/3,
get_archive_rule/4,
get_finding/3,
get_finding/4,
list_analyzed_resources/2,
list_analyzed_resources/3,
list_analyzers/4,
list_analyzers/5,
list_archive_rules/4,
list_archive_rules/5,
list_findings/2,
list_findings/3,
list_tags_for_resource/2,
list_tags_for_resource/3,
start_resource_scan/2,
start_resource_scan/3,
tag_resource/3,
tag_resource/4,
untag_resource/3,
untag_resource/4,
update_archive_rule/4,
update_archive_rule/5,
update_findings/2,
update_findings/3]).
-include_lib("hackney/include/hackney_lib.hrl").
%%====================================================================
%% API
%%====================================================================
%% @doc Retroactively applies the archive rule to existing findings that meet
%% the archive rule criteria.
apply_archive_rule(Client, Input) ->
apply_archive_rule(Client, Input, []).
apply_archive_rule(Client, Input0, Options) ->
Method = put,
Path = ["/archive-rule"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Creates an analyzer for your account.
create_analyzer(Client, Input) ->
create_analyzer(Client, Input, []).
create_analyzer(Client, Input0, Options) ->
Method = put,
Path = ["/analyzer"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Creates an archive rule for the specified analyzer.
%%
%% Archive rules automatically archive new findings that meet the criteria
%% you define when you create the rule.
create_archive_rule(Client, AnalyzerName, Input) ->
create_archive_rule(Client, AnalyzerName, Input, []).
create_archive_rule(Client, AnalyzerName, Input0, Options) ->
Method = put,
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), "/archive-rule"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Deletes the specified analyzer.
%%
%% When you delete an analyzer, Access Analyzer is disabled for the account
%% in the current or specific Region. All findings that were generated by the
%% analyzer are deleted. You cannot undo this action.
delete_analyzer(Client, AnalyzerName, Input) ->
delete_analyzer(Client, AnalyzerName, Input, []).
delete_analyzer(Client, AnalyzerName, Input0, Options) ->
Method = delete,
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), ""],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
QueryMapping = [
{<<"clientToken">>, <<"clientToken">>}
],
{Query_, Input} = aws_request:build_headers(QueryMapping, Input1),
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Deletes the specified archive rule.
delete_archive_rule(Client, AnalyzerName, RuleName, Input) ->
delete_archive_rule(Client, AnalyzerName, RuleName, Input, []).
delete_archive_rule(Client, AnalyzerName, RuleName, Input0, Options) ->
Method = delete,
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), "/archive-rule/", aws_util:encode_uri(RuleName), ""],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
QueryMapping = [
{<<"clientToken">>, <<"clientToken">>}
],
{Query_, Input} = aws_request:build_headers(QueryMapping, Input1),
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Retrieves information about a resource that was analyzed.
get_analyzed_resource(Client, AnalyzerArn, ResourceArn)
when is_map(Client) ->
get_analyzed_resource(Client, AnalyzerArn, ResourceArn, []).
get_analyzed_resource(Client, AnalyzerArn, ResourceArn, Options)
when is_map(Client), is_list(Options) ->
Path = ["/analyzed-resource"],
SuccessStatusCode = 200,
Headers = [],
Query0_ =
[
{<<"analyzerArn">>, AnalyzerArn},
{<<"resourceArn">>, ResourceArn}
],
Query_ = [H || {_, V} = H <- Query0_, V =/= undefined],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves information about the specified analyzer.
get_analyzer(Client, AnalyzerName)
when is_map(Client) ->
get_analyzer(Client, AnalyzerName, []).
get_analyzer(Client, AnalyzerName, Options)
when is_map(Client), is_list(Options) ->
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), ""],
SuccessStatusCode = 200,
Headers = [],
Query_ = [],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves information about an archive rule.
%%
%% To learn about filter keys that you can use to create an archive rule, see
%% Access Analyzer filter keys in the IAM User Guide.
get_archive_rule(Client, AnalyzerName, RuleName)
when is_map(Client) ->
get_archive_rule(Client, AnalyzerName, RuleName, []).
get_archive_rule(Client, AnalyzerName, RuleName, Options)
when is_map(Client), is_list(Options) ->
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), "/archive-rule/", aws_util:encode_uri(RuleName), ""],
SuccessStatusCode = 200,
Headers = [],
Query_ = [],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves information about the specified finding.
get_finding(Client, Id, AnalyzerArn)
when is_map(Client) ->
get_finding(Client, Id, AnalyzerArn, []).
get_finding(Client, Id, AnalyzerArn, Options)
when is_map(Client), is_list(Options) ->
Path = ["/finding/", aws_util:encode_uri(Id), ""],
SuccessStatusCode = 200,
Headers = [],
Query0_ =
[
{<<"analyzerArn">>, AnalyzerArn}
],
Query_ = [H || {_, V} = H <- Query0_, V =/= undefined],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves a list of resources of the specified type that have been
%% analyzed by the specified analyzer..
list_analyzed_resources(Client, Input) ->
list_analyzed_resources(Client, Input, []).
list_analyzed_resources(Client, Input0, Options) ->
Method = post,
Path = ["/analyzed-resource"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Retrieves a list of analyzers.
list_analyzers(Client, MaxResults, NextToken, Type)
when is_map(Client) ->
list_analyzers(Client, MaxResults, NextToken, Type, []).
list_analyzers(Client, MaxResults, NextToken, Type, Options)
when is_map(Client), is_list(Options) ->
Path = ["/analyzer"],
SuccessStatusCode = 200,
Headers = [],
Query0_ =
[
{<<"maxResults">>, MaxResults},
{<<"nextToken">>, NextToken},
{<<"type">>, Type}
],
Query_ = [H || {_, V} = H <- Query0_, V =/= undefined],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves a list of archive rules created for the specified analyzer.
list_archive_rules(Client, AnalyzerName, MaxResults, NextToken)
when is_map(Client) ->
list_archive_rules(Client, AnalyzerName, MaxResults, NextToken, []).
list_archive_rules(Client, AnalyzerName, MaxResults, NextToken, Options)
when is_map(Client), is_list(Options) ->
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), "/archive-rule"],
SuccessStatusCode = 200,
Headers = [],
Query0_ =
[
{<<"maxResults">>, MaxResults},
{<<"nextToken">>, NextToken}
],
Query_ = [H || {_, V} = H <- Query0_, V =/= undefined],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Retrieves a list of findings generated by the specified analyzer.
%%
%% To learn about filter keys that you can use to create an archive rule, see
%% Access Analyzer filter keys in the IAM User Guide.
list_findings(Client, Input) ->
list_findings(Client, Input, []).
list_findings(Client, Input0, Options) ->
Method = post,
Path = ["/finding"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Retrieves a list of tags applied to the specified resource.
list_tags_for_resource(Client, ResourceArn)
when is_map(Client) ->
list_tags_for_resource(Client, ResourceArn, []).
list_tags_for_resource(Client, ResourceArn, Options)
when is_map(Client), is_list(Options) ->
Path = ["/tags/", aws_util:encode_uri(ResourceArn), ""],
SuccessStatusCode = 200,
Headers = [],
Query_ = [],
request(Client, get, Path, Query_, Headers, undefined, Options, SuccessStatusCode).
%% @doc Immediately starts a scan of the policies applied to the specified
%% resource.
start_resource_scan(Client, Input) ->
start_resource_scan(Client, Input, []).
start_resource_scan(Client, Input0, Options) ->
Method = post,
Path = ["/resource/scan"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Adds a tag to the specified resource.
tag_resource(Client, ResourceArn, Input) ->
tag_resource(Client, ResourceArn, Input, []).
tag_resource(Client, ResourceArn, Input0, Options) ->
Method = post,
Path = ["/tags/", aws_util:encode_uri(ResourceArn), ""],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Removes a tag from the specified resource.
untag_resource(Client, ResourceArn, Input) ->
untag_resource(Client, ResourceArn, Input, []).
untag_resource(Client, ResourceArn, Input0, Options) ->
Method = delete,
Path = ["/tags/", aws_util:encode_uri(ResourceArn), ""],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
QueryMapping = [
{<<"tagKeys">>, <<"tagKeys">>}
],
{Query_, Input} = aws_request:build_headers(QueryMapping, Input1),
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Updates the criteria and values for the specified archive rule.
update_archive_rule(Client, AnalyzerName, RuleName, Input) ->
update_archive_rule(Client, AnalyzerName, RuleName, Input, []).
update_archive_rule(Client, AnalyzerName, RuleName, Input0, Options) ->
Method = put,
Path = ["/analyzer/", aws_util:encode_uri(AnalyzerName), "/archive-rule/", aws_util:encode_uri(RuleName), ""],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%% @doc Updates the status for the specified findings.
update_findings(Client, Input) ->
update_findings(Client, Input, []).
update_findings(Client, Input0, Options) ->
Method = put,
Path = ["/finding"],
SuccessStatusCode = 200,
Headers = [],
Input1 = Input0,
Query_ = [],
Input = Input1,
request(Client, Method, Path, Query_, Headers, Input, Options, SuccessStatusCode).
%%====================================================================
%% Internal functions
%%====================================================================
-spec request(aws_client:aws_client(), atom(), iolist(), list(),
list(), map() | undefined, list(), pos_integer() | undefined) ->
{ok, Result, {integer(), list(), hackney:client()}} |
{error, Error, {integer(), list(), hackney:client()}} |
{error, term()} when
Result :: map(),
Error :: map().
request(Client, Method, Path, Query, Headers0, Input, Options, SuccessStatusCode) ->
Client1 = Client#{service => <<"access-analyzer">>},
Host = build_host(<<"access-analyzer">>, Client1),
URL0 = build_url(Host, Path, Client1),
URL = aws_request:add_query(URL0, Query),
AdditionalHeaders = [ {<<"Host">>, Host}
, {<<"Content-Type">>, <<"application/x-amz-json-1.1">>}
],
Headers1 = aws_request:add_headers(AdditionalHeaders, Headers0),
Payload =
case proplists:get_value(should_send_body_as_binary, Options) of
true ->
maps:get(<<"Body">>, Input, <<"">>);
undefined ->
encode_payload(Input)
end,
MethodBin = aws_request:method_to_binary(Method),
SignedHeaders = aws_request:sign_request(Client1, MethodBin, URL, Headers1, Payload),
Response = hackney:request(Method, URL, SignedHeaders, Payload, Options),
handle_response(Response, SuccessStatusCode).
handle_response({ok, StatusCode, ResponseHeaders, Client}, SuccessStatusCode)
when StatusCode =:= 200;
StatusCode =:= 202;
StatusCode =:= 204;
StatusCode =:= SuccessStatusCode ->
case hackney:body(Client) of
{ok, <<>>} when StatusCode =:= 200;
StatusCode =:= SuccessStatusCode ->
{ok, #{}, {StatusCode, ResponseHeaders, Client}};
{ok, Body} ->
Result = jsx:decode(Body),
{ok, Result, {StatusCode, ResponseHeaders, Client}}
end;
handle_response({ok, StatusCode, ResponseHeaders, Client}, _) ->
{ok, Body} = hackney:body(Client),
Error = jsx:decode(Body),
{error, Error, {StatusCode, ResponseHeaders, Client}};
handle_response({error, Reason}, _) ->
{error, Reason}.
build_host(_EndpointPrefix, #{region := <<"local">>, endpoint := Endpoint}) ->
Endpoint;
build_host(_EndpointPrefix, #{region := <<"local">>}) ->
<<"localhost">>;
build_host(EndpointPrefix, #{region := Region, endpoint := Endpoint}) ->
aws_util:binary_join([EndpointPrefix, Region, Endpoint], <<".">>).
build_url(Host, Path0, Client) ->
Proto = maps:get(proto, Client),
Path = erlang:iolist_to_binary(Path0),
Port = maps:get(port, Client),
aws_util:binary_join([Proto, <<"://">>, Host, <<":">>, Port, Path], <<"">>).
-spec encode_payload(undefined | map()) -> binary().
encode_payload(undefined) ->
<<>>;
encode_payload(Input) ->
jsx:encode(Input).