Packages
erlcloud
3.8.3
3.8.3
3.8.2
3.8.1
3.7.6
3.7.4
3.7.3
3.7.2
3.7.1
3.7.0
3.6.8
3.6.7
3.6.5
3.6.4
3.6.3
3.6.2
3.6.1
3.6.0
3.5.16
3.5.15
3.5.14
3.5.13
3.5.12
3.5.11
3.5.10
3.5.9
3.5.8
3.5.7
3.5.6
3.5.5
3.5.4
3.5.3
3.5.2
3.5.1
3.5.0
3.4.5
3.4.3
3.4.1
3.4.0
3.3.9
3.3.8
3.3.7
3.3.6
3.3.5
3.3.4
3.3.3
3.3.2
3.3.1
3.3.0
3.2.18
3.2.17
3.2.16
3.2.15
3.2.14
3.2.13
3.2.12
3.2.11
3.2.10
3.2.7
3.2.6
3.2.5
3.2.4
3.2.3
3.2.2
3.2.1
3.2.0
3.1.17
3.1.16
3.1.14
3.1.13
3.1.12
3.1.11
3.1.9
3.1.8
3.1.7
3.1.6
3.1.5
3.1.4
3.1.3
3.1.2
3.1.1
3.1.0
3.0.5
3.0.4
3.0.3
3.0.2
3.0.1
2.2.16
2.2.15
2.2.14
2.2.13
2.2.12
2.2.11
2.2.10
2.2.9
2.2.8
2.2.7
2.2.6
2.2.5
2.2.4
2.2.2
2.2.1
2.2.0
2.1.0
2.0.5
2.0.4
2.0.3
2.0.0
0.13.10
0.13.9
0.13.8
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.0
0.12.0
0.11.0
0.9.2
0.9.2-rc.1
0.9.1
0.9.0
AWS APIs library for Erlang
Current section
Files
Jump to
Current section
Files
src/erlcloud_securityhub.erl
-module(erlcloud_securityhub).
-include("erlcloud.hrl").
-include("erlcloud_aws.hrl").
%% API
-export([
describe_hub/1,
describe_hub/2
]).
-type securityhub() :: proplist().
-type param_name() :: binary() | string() | atom().
-type param_value() :: binary() | string() | atom() | integer().
-type params() :: [param_name() | {param_name(), param_value()}].
-spec describe_hub(AwsConfig) -> Result
when AwsConfig :: aws_config(),
Result :: {ok, securityhub()} | {error, not_found} | {error, term()}.
describe_hub(AwsConfig)
when is_record(AwsConfig, aws_config) ->
describe_hub(AwsConfig, _Params = []);
describe_hub(Params) ->
AwsConfig = erlcloud_aws:default_config(),
describe_hub(AwsConfig, Params).
-spec describe_hub(AwsConfig, Params) -> Result
when AwsConfig :: aws_config(),
Params :: params(),
Result :: {ok, securityhub()}| {error, not_found} | {error, term()}.
describe_hub(AwsConfig, Params) ->
Path = ["accounts"],
case request(AwsConfig, _Method = get, Path, Params) of
{ok, Response} ->
{ok, Response};
{error, {<<"ResourceNotFoundException">>, _Message}} ->
{error, not_found};
{error, Reason} ->
{error, Reason}
end.
request(AwsConfig, Method, Path, Params) ->
request(AwsConfig, Method, Path, Params, _RequestBody = <<>>).
request(AwsConfig0, Method, Path, Params, RequestBody) ->
case erlcloud_aws:update_config(AwsConfig0) of
{ok, AwsConfig1} ->
AwsRequest0 = init_request(AwsConfig1, Method, Path, Params, RequestBody),
AwsRequest1 = erlcloud_retry:request(AwsConfig1, AwsRequest0, fun should_retry/1),
case AwsRequest1#aws_request.response_type of
ok ->
decode_response(AwsRequest1);
error ->
decode_error(AwsRequest1)
end;
{error, Reason} ->
{error, Reason}
end.
init_request(AwsConfig, Method, Path, Params, Payload) ->
Host = AwsConfig#aws_config.securityhub_host,
Service = "securityhub",
NormPath = norm_path(Path),
NormParams = norm_params(Params),
Region = erlcloud_aws:aws_region_from_host(Host),
Headers = [{"host", Host}, {"content-type", "application/json"}],
SignedHeaders = erlcloud_aws:sign_v4(Method, NormPath, AwsConfig, Headers, Payload, Region, Service, Params),
#aws_request{
service = securityhub,
method = Method,
uri = "https://" ++ Host ++ NormPath ++ NormParams,
request_headers = SignedHeaders,
request_body = Payload
}.
norm_path(Path) ->
binary_to_list(iolist_to_binary(["/" | lists:join("/", Path)])).
norm_params([] = _Params) ->
"";
norm_params(Params) ->
"?" ++ erlcloud_aws:canonical_query_string(Params).
should_retry(Request)
when Request#aws_request.response_type == ok ->
Request;
should_retry(Request)
when Request#aws_request.response_type == error,
Request#aws_request.response_status == 429 ->
Request#aws_request{should_retry = true};
should_retry(Request)
when Request#aws_request.response_type == error,
Request#aws_request.response_status >= 500 ->
Request#aws_request{should_retry = true};
should_retry(Request) ->
Request#aws_request{should_retry = false}.
decode_response(AwsRequest) ->
case AwsRequest#aws_request.response_body of
<<>> ->
ok;
ResponseBody ->
Json = jsx:decode(ResponseBody, [{return_maps, false}]),
{ok, Json}
end.
decode_error(AwsRequest) ->
case AwsRequest#aws_request.error_type of
aws ->
Type = extract_error_type(AwsRequest),
Message = extract_error_message(AwsRequest),
{error, {Type, Message}};
_ ->
erlcloud_aws:request_to_return(AwsRequest)
end.
extract_error_type(AwsRequest) ->
Headers = AwsRequest#aws_request.response_headers,
Value = proplists:get_value("x-amzn-errortype", Headers),
iolist_to_binary(Value).
extract_error_message(AwsRequest) ->
ResponseBody = AwsRequest#aws_request.response_body,
Object = jsx:decode(ResponseBody, [{return_maps, false}]),
proplists:get_value(<<"message">>, Object, <<>>).