Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle provider options as in the spec. #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/aws_credentials_ec2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

-export([fetch/1]).

-spec fetch(aws_credentials_provider:options()) ->
-spec fetch(any()) ->
{error, _}
| {ok, aws_credentials:credentials(), aws_credentials_provider:expiration()}.
fetch(_Options) ->
Expand Down
24 changes: 15 additions & 9 deletions src/aws_credentials_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
%%
%% Environment parameters:
%% <ul>
%% <li> &lt;&lt;"credentials_path"&gt;&gt; - this is the base path to the both CLI configuration files.
%% <li> 'credential_path' - this is the base path to the both CLI configuration files.
%% And based on this path, credentials file should exist and config file is optional.
%% By default this is `~/.aws/'</li>
%% <li> &lt;&lt;"profile"&gt;&gt; - this is the desired profile to use in the credentials file.
%% By default this is "~/.aws/"</li>
%% <li> 'profile'; - this is the desired profile to use in the credentials file.
%% The profile can also be provided via the "AWS_PROFILE" os env.
%% By default this is &lt;&lt;"default"&gt;&gt;</li>
%% </ul>
%% @end
Expand All @@ -33,7 +34,12 @@

-export([fetch/1]).

-spec fetch(aws_credentials_provider:options()) ->
-type options() :: #{ credential_path => string()
, profile => binary()
}.
-export_type([options/0]).

-spec fetch(options()) ->
{error, any()} | {ok, aws_credentials:credentials(), 'infinity'}.
fetch(Options) ->
FilePath = get_file_path(Options),
Expand Down Expand Up @@ -62,7 +68,7 @@ maybe_path_from_env(EnvVar, FilePath) ->
{_, Path} -> Path
end.

-spec get_file_path(aws_credentials_provider:options()) -> {error, any()} | string().
-spec get_file_path(options()) -> {error, any()} | string().
get_file_path(Options) ->
case maps:get(credential_path, Options, undefined) of
undefined -> maybe_add_home("/.aws/");
Expand All @@ -79,7 +85,7 @@ maybe_add_home(Path) ->
-spec maybe_add_region(
{error, any()} | {ok, aws_credentials:credentials(), 'infinity'},
{error, any()} | string(),
aws_credentials_provider:options()
options()
) -> {ok, aws_credentials:credentials(), 'infinity'}.
maybe_add_region({error, _} = Error, _Config, _Options) -> Error;
maybe_add_region(Result, {error, _Error}, _Options) -> Result;
Expand All @@ -98,7 +104,7 @@ check_path_exists(Path) ->
true -> Path
end.

-spec parse_credentials_file(string(), aws_credentials_provider:options()) ->
-spec parse_credentials_file(string(), options()) ->
{error, any()} | {ok, aws_credentials:credentials(), 'infinity'}.
parse_credentials_file(Path, Options) ->
{ok, F} = file:read_file(Path),
Expand All @@ -123,7 +129,7 @@ parse_credentials_file(Path, Options) ->
end
end.

-spec parse_config_file(string(), aws_credentials_provider:options()) ->
-spec parse_config_file(string(), options()) ->
{error, any()} | {ok, map()}.
parse_config_file(Path, Options) ->
{ok, F} = file:read_file(Path),
Expand All @@ -138,7 +144,7 @@ read_from_profile(File, Profile) ->
Map -> {ok, Map}
end.

-spec desired_profile(aws_credentials_provider:options()) -> binary().
-spec desired_profile(options()) -> binary().
desired_profile(Options) ->
case {os:getenv("AWS_PROFILE"), maps:get(profile, Options, undefined)} of
{false, undefined} -> <<"default">>;
Expand Down
28 changes: 25 additions & 3 deletions src/aws_credentials_provider.erl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,19 @@

-export([fetch/0, fetch/1]).

-type options() :: #{provider() => map()}.
%% `credential_path' and `profile' are treated as common options,
%% and their values are inherited by `provider_specific_options()'
%% unless the same options exist in `provider_specific_options()'.
%% Note: This behaviour is for compatibility reason only, and
%% do not add any further common options.
-type options() :: #{ credential_path => string()
, profile => binary()
, provider() => provider_specific_options()
}.
-type provider_specific_options() :: #{ credential_path => string()
, profile => binary()
, any() => any()
}.
-type expiration() :: binary() | pos_integer() | infinity.
-type provider() :: aws_credentials_env
| aws_credentials_file
Expand All @@ -40,7 +52,7 @@
-type error_log() :: [{provider(), term()}].
-export_type([ options/0, expiration/0, provider/0 ]).

-callback fetch(options()) ->
-callback fetch(provider_specific_options()) ->
{ok, aws_credentials:credentials(), expiration()} | {error, any()}.

-include_lib("kernel/include/logger.hrl").
Expand Down Expand Up @@ -74,7 +86,8 @@ evaluate_providers([], _Options, []) ->
evaluate_providers([], _Options, Errors) when is_list(Errors) ->
{error, lists:reverse(Errors)};
evaluate_providers([ Provider | Providers ], Options, Errors) ->
case Provider:fetch(Options) of
ProviderOptions = get_provider_specific_options(Provider, Options),
case Provider:fetch(ProviderOptions) of
{error, _} = Error ->
evaluate_providers(Providers, Options, [{Provider, Error} | Errors]);
{ok, Credentials, Expiration} ->
Expand All @@ -87,3 +100,12 @@ get_env(Key, Default) ->
undefined -> Default;
{ok, Value} -> Value
end.

-spec get_provider_specific_options(provider(), options()) -> provider_specific_options().
get_provider_specific_options(Provider, Options) ->
ProviderSpecificOptions = maps:get(Provider, Options, #{}),
IsCommonOptions = fun(profile, _) -> true; (credential_path, _) -> true; (_, _) -> false end,
CommonOptions = maps:filter(IsCommonOptions, Options),
%% If an option exists in both ProviderSpecificOptions and CommonOptions,
%% the value in ProviderSpecificOptions should be adopted.
maps:merge(CommonOptions, ProviderSpecificOptions).
Loading