This commit is contained in:
Simon Unge 2025-07-24 16:22:46 +00:00
parent c5438199e4
commit 2a3c2cfd93
No known key found for this signature in database
2 changed files with 24 additions and 36 deletions

View File

@ -9,7 +9,8 @@
-behavior(gen_server).
%% API exports
-export([get/2, get/3, put/4,
-export([get/2, get/3, get/4,
put/4, put/5,
post/4,
refresh_credentials/0,
request/5, request/6, request/7,
@ -60,7 +61,10 @@ get(Service, Path) ->
%% format.
%% @end
get(Service, Path, Headers) ->
request(Service, get, Path, "", Headers).
request(Service, get, Path, "", Headers, []).
get(Service, Path, Headers, Options) ->
request(Service, get, Path, "", Headers, Options).
-spec post(Service :: string(),
@ -84,7 +88,10 @@ post(Service, Path, Body, Headers) ->
%% format.
%% @end
put(Service, Path, Body, Headers) ->
request(Service, put, Path, Body, Headers).
put(Service, Path, Body, Headers, []).
put(Service, Path, Body, Headers, Options) ->
request(Service, put, Path, Body, Headers, Options).
-spec refresh_credentials() -> ok | error.
@ -657,7 +664,8 @@ get_or_create_gun_connection(State, Host, Port, Path, Options) ->
end.
get_connection_key(Host, Port, Path, Options) ->
case proplists:get_value(connection_per_path, Options, true) of
io:format(">>~p~n",[Options]),
case proplists:get_value(connection_per_path, Options, false) of
true -> Host ++ ":" ++ integer_to_list(Port) ++ Path; % Per-path
false -> Host ++ ":" ++ integer_to_list(Port) % Per-host (default)
end.

View File

@ -746,26 +746,8 @@ region_from_availability_zone(Value) ->
%% @doc Attempt to obtain EC2 IMDSv2 token.
%% @end
load_imdsv2_token() ->
<<<<<<< HEAD
TokenUrl = imdsv2_token_url(),
?LOG_INFO("Attempting to obtain EC2 IMDSv2 token from ~tp ...", [TokenUrl]),
case httpc:request(put, {TokenUrl, [{?METADATA_TOKEN_TTL_HEADER, integer_to_list(?METADATA_TOKEN_TTL_SECONDS)}]},
[{timeout, ?DEFAULT_HTTP_TIMEOUT}], []) of
{ok, {{_, 200, _}, _, Value}} ->
?LOG_DEBUG("Successfully obtained EC2 IMDSv2 token."),
Value;
{error, {{_, 400, _}, _, _}} ->
?LOG_WARNING("Failed to obtain EC2 IMDSv2 token: Missing or Invalid Parameters The PUT request is not valid."),
undefined;
Other ->
?LOG_WARNING(
get_instruction_on_instance_metadata_error("Failed to obtain EC2 IMDSv2 token: ~tp. "
"Falling back to EC2 IMDSv1 for now. It is recommended to use EC2 IMDSv2."), [Other]),
undefined
end.
=======
TokenUrl = imdsv2_token_url(),
rabbit_log:info("Attempting to obtain EC2 IMDSv2 token from ~tp ...", [TokenUrl]),
% Parse metadata service URL
{Host, Port, Path} = rabbitmq_aws:parse_uri(TokenUrl),
% Simple Gun connection for metadata service
@ -779,22 +761,22 @@ load_imdsv2_token() ->
StreamRef = gun:put(ConnPid, Path, Headers, <<>>),
Result = case gun:await(ConnPid, StreamRef, ?DEFAULT_HTTP_TIMEOUT) of
{response, fin, 200, _RespHeaders} ->
rabbit_log:debug("Successfully obtained EC2 IMDSv2 token."),
?LOG_DEBUG("Successfully obtained EC2 IMDSv2 token."),
<<>>; % Empty body for fin response
{response, nofin, 200, _RespHeaders} ->
{ok, Body} = gun:await_body(ConnPid, StreamRef, ?DEFAULT_HTTP_TIMEOUT),
rabbit_log:debug("Successfully obtained EC2 IMDSv2 token."),
?LOG_DEBUG("Successfully obtained EC2 IMDSv2 token."),
binary_to_list(Body);
{response, _, 400, _RespHeaders} ->
rabbit_log:warning("Failed to obtain EC2 IMDSv2 token: Missing or Invalid Parameters The PUT request is not valid."),
?LOG_WARNING("Failed to obtain EC2 IMDSv2 token: Missing or Invalid Parameters The PUT request is not valid."),
undefined;
{error, Reason} ->
rabbit_log:warning(
?LOG_WARNING(
get_instruction_on_instance_metadata_error("Failed to obtain EC2 IMDSv2 token: ~tp. "
"Falling back to EC2 IMDSv1 for now. It is recommended to use EC2 IMDSv2."), [Reason]),
undefined;
Other ->
rabbit_log:warning(
?LOG_WARNING(
get_instruction_on_instance_metadata_error("Failed to obtain EC2 IMDSv2 token: ~tp. "
"Falling back to EC2 IMDSv1 for now. It is recommended to use EC2 IMDSv2."), [Other]),
undefined
@ -803,19 +785,17 @@ load_imdsv2_token() ->
Result;
{error, Reason} ->
gun:close(ConnPid),
rabbit_log:warning(
?LOG_WARNING(
get_instruction_on_instance_metadata_error("Failed to connect for EC2 IMDSv2 token: ~tp. "
"Falling back to EC2 IMDSv1 for now. It is recommended to use EC2 IMDSv2."), [Reason]),
undefined
end;
{error, Reason} ->
rabbit_log:warning(
?LOG_WARNING(
get_instruction_on_instance_metadata_error("Failed to open connection for EC2 IMDSv2 token: ~tp. "
"Falling back to EC2 IMDSv1 for now. It is recommended to use EC2 IMDSv2."), [Reason]),
undefined
end.
>>>>>>> f04e9ce16a (Fully remove httpc)
-spec instance_metadata_request_headers() -> headers().
%% @doc Return headers used for instance metadata service requests.