Skip to content

Commit a976b64

Browse files
committed
Dialyze take 1
1 parent 514aae1 commit a976b64

File tree

6 files changed

+25
-82
lines changed

6 files changed

+25
-82
lines changed

deps/rabbitmq_aws/src/rabbitmq_aws.erl

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,9 @@ get_region() ->
4545
[{region, Region}] ->
4646
Region;
4747
[] ->
48-
% Use proper region detection
49-
case rabbitmq_aws_config:region() of
50-
{ok, DetectedRegion} ->
51-
% Cache the detected region
52-
ets:insert(?AWS_CONFIG_TABLE, {region, DetectedRegion}),
53-
DetectedRegion;
54-
_ ->
55-
% Final fallback
56-
ets:insert(?AWS_CONFIG_TABLE, {region, "us-east-1"}),
57-
"us-east-1"
58-
end
48+
{ok, DetectedRegion} = rabbitmq_aws_config:region(),
49+
ets:insert(?AWS_CONFIG_TABLE, {region, DetectedRegion}),
50+
DetectedRegion
5951
end.
6052

6153
-spec set_region(Region :: region()) -> ok.
@@ -65,9 +57,9 @@ set_region(Region) ->
6557

6658
-spec has_credentials() -> boolean().
6759
has_credentials() ->
68-
case ets:lookup(?AWS_CREDENTIALS_TABLE, current) of
69-
[{current, Creds}] when Creds#aws_credentials.access_key =/= undefined ->
70-
not expired_credentials(Creds#aws_credentials.expiration);
60+
case ets:lookup(?AWS_CREDENTIALS_TABLE, aws_credentials) of
61+
[#aws_credentials{access_key = Key, expiration = Expiration}] when Key =/= undefined ->
62+
not expired_credentials(Expiration);
7163
_ ->
7264
false
7365
end.
@@ -76,26 +68,9 @@ has_credentials() ->
7668
%% exported wrapper functions
7769
%%====================================================================
7870

79-
-spec get(
80-
ServiceOrHandle :: string() | connection_handle(),
81-
Path :: path()
82-
) -> result().
83-
%% @doc Perform a HTTP GET request to the AWS API for the specified service. The
84-
%% response will automatically be decoded if it is either in JSON, or XML
85-
%% format.
86-
%% @end
8771
get(ServiceOrHandle, Path) ->
8872
get(ServiceOrHandle, Path, []).
8973

90-
-spec get(
91-
ServiceOrHandle :: string() | connection_handle(),
92-
Path :: path(),
93-
Headers :: headers()
94-
) -> result().
95-
%% @doc Perform a HTTP GET request to the AWS API for the specified service. The
96-
%% response will automatically be decoded if it is either in JSON or XML
97-
%% format.
98-
%% @end
9974
get(ServiceOrHandle, Path, Headers) ->
10075
get(ServiceOrHandle, Path, Headers, []).
10176

@@ -208,7 +183,7 @@ direct_request({GunPid, Service}, Method, Path, Body, Headers, Options) ->
208183
URI :: string(),
209184
Headers :: headers(),
210185
Body :: body(),
211-
BodyHash :: iodata()
186+
BodyHash :: iodata() | undefined
212187
) -> headers().
213188
sign_headers(
214189
AccessKey, SecretKey, SecurityToken, Region, Service, Method, URI, Headers, Body, BodyHash
@@ -264,7 +239,7 @@ request(Service, Method, Path, Body, Headers, HTTPOptions) ->
264239
Body :: body(),
265240
Headers :: headers(),
266241
HTTPOptions :: http_options(),
267-
Endpoint :: host()
242+
Endpoint :: host() | undefined
268243
) -> result().
269244
%% @doc Perform a HTTP request to the AWS API for the specified service, overriding
270245
%% the endpoint URL to use when invoking the API. This is useful for local testing
@@ -291,7 +266,7 @@ set_credentials(AccessKey, SecretAccessKey) ->
291266
security_token = undefined,
292267
expiration = undefined
293268
},
294-
ets:insert(?AWS_CREDENTIALS_TABLE, {current, Creds}),
269+
ets:insert(?AWS_CREDENTIALS_TABLE, Creds),
295270
ok.
296271

297272
-spec ensure_credentials_valid() -> ok.
@@ -366,21 +341,16 @@ endpoint_tld("cn-northwest-1") ->
366341
endpoint_tld(_Other) ->
367342
"amazonaws.com".
368343

369-
-spec get_credentials() ->
370-
{ok, access_key(), secret_access_key(), security_token(), region()} | {error, term()}.
371344
get_credentials() ->
372345
get_credentials(10).
373346

374-
-spec get_credentials(Retries :: non_neg_integer()) ->
375-
{ok, access_key(), secret_access_key(), security_token(), region()} | {error, term()}.
376347
get_credentials(Retries) ->
377-
case ets:lookup(?AWS_CREDENTIALS_TABLE, current) of
378-
[{current, Creds}] ->
379-
case expired_credentials(Creds#aws_credentials.expiration) of
348+
case ets:lookup(?AWS_CREDENTIALS_TABLE, aws_credentials) of
349+
[#aws_credentials{access_key = Key, expiration = Expiration, secret_key = SecretKey, security_token = SecurityToken}] ->
350+
case expired_credentials(Expiration) of
380351
false ->
381352
Region = get_region(),
382-
{ok, Creds#aws_credentials.access_key, Creds#aws_credentials.secret_key,
383-
Creds#aws_credentials.security_token, Region};
353+
{ok, Key, SecretKey, SecurityToken, Region};
384354
true ->
385355
refresh_credentials_with_lock(Retries)
386356
end;
@@ -398,14 +368,12 @@ refresh_credentials_with_lock(Retries) ->
398368
true ->
399369
try
400370
% Double-check if someone else already refreshed
401-
case ets:lookup(?AWS_CREDENTIALS_TABLE, current) of
402-
[{current, Creds}] ->
403-
case expired_credentials(Creds#aws_credentials.expiration) of
371+
case ets:lookup(?AWS_CREDENTIALS_TABLE, aws_credentials) of
372+
[#aws_credentials{access_key = Key, expiration = Expiration, secret_key = SecretKey, security_token = SecurityToken}] ->
373+
case expired_credentials(Expiration) of
404374
false ->
405375
Region = get_region(),
406-
{ok, Creds#aws_credentials.access_key,
407-
Creds#aws_credentials.secret_key,
408-
Creds#aws_credentials.security_token, Region};
376+
{ok, Key, SecretKey, SecurityToken, Region};
409377
true ->
410378
do_refresh_credentials()
411379
end;
@@ -433,7 +401,7 @@ do_refresh_credentials() ->
433401
security_token = SecurityToken,
434402
expiration = Expiration
435403
},
436-
ets:insert(?AWS_CREDENTIALS_TABLE, {current, Creds}),
404+
ets:insert(?AWS_CREDENTIALS_TABLE, Creds),
437405
{ok, AccessKey, SecretAccessKey, SecurityToken, Region};
438406
{error, Reason} ->
439407
{error, Reason}

deps/rabbitmq_aws/src/rabbitmq_aws_app.erl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
-export([start/2, stop/1]).
55

66
start(_Type, _Args) ->
7-
ets:new(aws_credentials, [named_table, public, {read_concurrency, true}]),
8-
ets:new(aws_config, [named_table, public, {read_concurrency, true}]),
97
rabbitmq_aws_sup:start_link().
108

119
stop(_State) ->

deps/rabbitmq_aws/src/rabbitmq_aws_config.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ lookup_credentials_from_proplist(_, undefined) ->
535535
lookup_credentials_from_proplist(AccessKey, SecretKey) ->
536536
{ok, AccessKey, SecretKey, undefined, undefined}.
537537

538-
-spec with_metadata_connection(fun((gun:conn_ref()) -> Result)) -> Result.
538+
-spec with_metadata_connection(fun((pid()) -> Result)) -> Result.
539539
%% @doc Execute a function with a shared metadata service connection
540540
%% @end
541541
with_metadata_connection(Fun) ->
@@ -619,15 +619,15 @@ maybe_convert_number(Value) ->
619619
end.
620620

621621
-spec maybe_get_credentials_from_instance_metadata_with_conn(
622-
ConnPid :: gun:conn_ref(),
622+
ConnPid :: pid(),
623623
{ok, Role :: string()}
624624
| {error, undefined}
625625
) ->
626626
{'ok', security_credentials()} | {'error', term()}.
627627
%% @doc Try to query the EC2 local instance metadata service to get temporary
628628
%% authentication credentials using an existing connection.
629629
%% @end
630-
maybe_get_credentials_from_instance_metadata_with_conn(_, {error, undefined}) ->
630+
maybe_get_credentials_from_instance_metadata_with_conn(_, {error, _}) ->
631631
{error, undefined};
632632
maybe_get_credentials_from_instance_metadata_with_conn(ConnPid, {ok, Role}) ->
633633
URL = instance_credentials_url(Role),
@@ -642,7 +642,7 @@ maybe_get_region_from_instance_metadata() ->
642642
URL = instance_availability_zone_url(),
643643
parse_az_response(perform_http_get_instance_metadata(URL)).
644644

645-
-spec perform_http_get_with_conn(gun:conn_ref(), string()) -> httpc_result().
645+
-spec perform_http_get_with_conn(pid(), string()) -> httpc_result().
646646
%% @doc Make HTTP GET request using existing Gun connection
647647
%% @end
648648
perform_http_get_with_conn(ConnPid, Path) ->

deps/rabbitmq_aws/src/rabbitmq_aws_sup.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ start_link() ->
1818

1919
init([]) ->
2020
% No children needed - just return empty supervisor
21+
ets:new(aws_credentials, [named_table, public, {read_concurrency, true}]),
22+
ets:new(aws_config, [named_table, public, {read_concurrency, true}]),
2123
{ok, {{one_for_one, 5, 10}, []}}.

deps/rabbitmq_aws/test/rabbitmq_aws_app_tests.erl

Lines changed: 0 additions & 25 deletions
This file was deleted.

deps/rabbitmq_aws/test/rabbitmq_aws_tests.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ set_test_credentials(AccessKey, SecretKey, SecurityToken, Expiration) ->
2424
security_token = SecurityToken,
2525
expiration = Expiration
2626
},
27-
ets:insert(?AWS_CREDENTIALS_TABLE, {current, Creds}).
27+
ets:insert(?AWS_CREDENTIALS_TABLE, Creds).
2828

2929
set_test_region(Region) ->
3030
ets:insert(?AWS_CONFIG_TABLE, {region, Region}).

0 commit comments

Comments
 (0)