Packages
erlcloud
3.5.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_guardduty.erl
-module(erlcloud_guardduty).
-include_lib("erlcloud/include/erlcloud.hrl").
-include_lib("erlcloud/include/erlcloud_aws.hrl").
%%------------------------------------------------------------------------------
%% Library initialization.
%%------------------------------------------------------------------------------
-export([configure/2, configure/3, new/2, new/3]).
%%------------------------------------------------------------------------------
%% GuardDuty API Functions
%%------------------------------------------------------------------------------
-export([
get_detector/1, get_detector/2,
list_detectors/0, list_detectors/1, list_detectors/2, list_detectors/3
]).
-import(erlcloud_util, [filter_undef/1]).
-type gd_return() :: {ok, proplist()} | {error, term()}.
-spec new(AccessKeyID ::string(), SecretAccessKey :: string()) -> aws_config().
new(AccessKeyID, SecretAccessKey) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey}.
-spec new(AccessKeyID :: string(), SecretAccessKey :: string(), Host :: string()) -> aws_config().
new(AccessKeyID, SecretAccessKey, Host) ->
#aws_config{access_key_id=AccessKeyID,
secret_access_key=SecretAccessKey,
guardduty_host=Host}.
-spec configure(AccessKeyID :: string(), SecretAccessKey :: string()) -> ok.
configure(AccessKeyID, SecretAccessKey) ->
put(aws_config, new(AccessKeyID, SecretAccessKey)),
ok.
-spec configure(AccessKeyID :: string(), SecretAccessKey :: string(), Host :: string()) -> ok.
configure(AccessKeyID, SecretAccessKey, Host) ->
put(aws_config, new(AccessKeyID, SecretAccessKey, Host)),
ok.
%%------------------------------------------------------------------------------
%% API
%%------------------------------------------------------------------------------
%%------------------------------------------------------------------------------
%% @doc
%% GuardDuty API:
%% [https://docs.aws.amazon.com/guardduty/latest/ug/get-detector.html]
%%
%%------------------------------------------------------------------------------
-spec get_detector(DetectorId :: binary()) -> gd_return().
get_detector(DetectorId) ->
get_detector(DetectorId, default_config()).
-spec get_detector(DetectorId :: binary(),
Config :: aws_config()) -> gd_return().
get_detector(DetectorId, Config) ->
Path = "/detector/" ++ binary_to_list(DetectorId),
guardduty_request(Config, get, Path, undefined).
%%------------------------------------------------------------------------------
%% @doc
%% GuardDuty API:
%% [https://docs.aws.amazon.com/guardduty/latest/ug/list-detectors.html]
%%
%%------------------------------------------------------------------------------
-spec list_detectors() -> gd_return().
list_detectors() -> list_detectors(default_config()).
-spec list_detectors(Config :: aws_config()) -> gd_return().
list_detectors(Config) ->
list_detectors(undefined, undefined, Config).
-spec list_detectors(Marker :: binary(),
MaxItems :: integer()) -> gd_return().
list_detectors(Marker, MaxItems) ->
list_detectors(Marker, MaxItems, default_config()).
-spec list_detectors(Marker :: undefined | binary(),
MaxItems :: undefined | integer(),
Config :: aws_config()) -> gd_return().
list_detectors(Marker, MaxItems, Config) ->
Path = "/detector",
QParams = filter_undef([{"Marker", Marker},
{"MaxItems", MaxItems}]),
guardduty_request(Config, get, Path, undefined, QParams).
%%%------------------------------------------------------------------------------
%%% Internal Functions
%%%------------------------------------------------------------------------------
guardduty_request(Config, Method, Path, Body) ->
guardduty_request(Config, Method, Path, Body, []).
guardduty_request(Config, Method, Path, Body, QParam) ->
case erlcloud_aws:update_config(Config) of
{ok, Config1} ->
guardduty_request_no_update(Config1, Method, Path, Body, QParam);
{error, Reason} ->
{error, Reason}
end.
guardduty_request_no_update(Config, Method, Path, Body, QParam) ->
Form = case encode_body(Body) of
<<>> -> erlcloud_http:make_query_string(QParam);
Value -> Value
end,
Headers = headers(Method, Path, Config, encode_body(Body), QParam),
case erlcloud_aws:aws_request_form_raw(
Method, Config#aws_config.guardduty_scheme, Config#aws_config.guardduty_host,
Config#aws_config.guardduty_port, Path, Form, Headers, [], Config) of
{ok, Data} ->
{ok, jsx:decode(Data, [{return_maps, false}])};
E ->
E
end.
encode_body(undefined) ->
<<>>.
headers(Method, Uri, Config, Body, QParam) ->
Headers = [{"host", Config#aws_config.guardduty_host},
{"content-type", "application/json"}],
Region = erlcloud_aws:aws_region_from_host(Config#aws_config.guardduty_host),
erlcloud_aws:sign_v4(Method, Uri, Config,
Headers, Body, Region, "guardduty", QParam).
default_config() -> erlcloud_aws:default_config().