Skip to content

Commit

Permalink
Merge pull request #6628 from SimonUnge/6016-fetch_app_environment_api
Browse files Browse the repository at this point in the history
See #6016. Add HTTP to fetch app environment config
  • Loading branch information
michaelklishin committed Dec 12, 2022
2 parents cf9a9bf + 3209703 commit a2533a6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions deps/rabbitmq_ct_helpers/src/rabbit_mgmt_test_util.erl
Expand Up @@ -49,6 +49,12 @@ http_get_no_auth(Config, Path, CodeExp) ->
assert_code(CodeExp, CodeAct, "GET", Path, ResBody),
decode(CodeExp, Headers, ResBody).

http_get_no_decode(Config, Path, User, Pass, CodeExp) ->
{ok, {{_HTTP, CodeAct, _}, _Headers, ResBody}} =
req(Config, 0, get, Path, [auth_header(User, Pass)]),
assert_code(CodeExp, CodeAct, "GET", Path, ResBody),
ResBody.

http_put(Config, Path, List, CodeExp) ->
http_put_raw(Config, Path, format_for_upload(List), CodeExp).

Expand Down
3 changes: 2 additions & 1 deletion deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl
Expand Up @@ -182,5 +182,6 @@ dispatcher() ->
{"/auth", rabbit_mgmt_wm_auth, []},
{"/auth/attempts/:node", rabbit_mgmt_wm_auth_attempts, [all]},
{"/auth/attempts/:node/source", rabbit_mgmt_wm_auth_attempts, [by_source]},
{"/login", rabbit_mgmt_wm_login, []}
{"/login", rabbit_mgmt_wm_login, []},
{"/config/effective", rabbit_mgmt_wm_environment, []}
].
33 changes: 33 additions & 0 deletions deps/rabbitmq_management/src/rabbit_mgmt_wm_environment.erl
@@ -0,0 +1,33 @@
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
%%

-module(rabbit_mgmt_wm_environment).

-export([init/2, to_plain/2, content_types_provided/2, is_authorized/2]).
-export([variances/2]).

-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").

%%--------------------------------------------------------------------

init(Req, _State) ->
{cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}.

variances(Req, Context) ->
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.

content_types_provided(ReqData, Context) ->
{[{{ <<"text">>, <<"plain">>, '*'}, to_plain}], ReqData, Context}.

to_plain(ReqData, Context) ->
Res = rabbit:environment(),
Req = cowboy_req:reply(200, #{}, io_lib:format("~p~n",[Res]), ReqData),
{stop, Req, Context}.

is_authorized(ReqData, Context) ->
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).
26 changes: 25 additions & 1 deletion deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl
Expand Up @@ -18,6 +18,7 @@
assert_keys/2, assert_no_keys/2,
http_get/2, http_get/3, http_get/5,
http_get_no_auth/3,
http_get_no_decode/5,
http_put/4, http_put/6,
http_post/4, http_post/6,
http_upload_raw/8,
Expand Down Expand Up @@ -145,7 +146,8 @@ all_tests() -> [
csp_headers_test,
auth_attempts_test,
user_limits_list_test,
user_limit_set_test
user_limit_set_test,
config_environment_test
].

%% -------------------------------------------------------------------
Expand Down Expand Up @@ -258,6 +260,10 @@ end_per_testcase0(permissions_vhost_test, Config) ->
rabbit_ct_broker_helpers:delete_user(Config, <<"myuser1">>),
rabbit_ct_broker_helpers:delete_user(Config, <<"myuser2">>),
Config;
end_per_testcase0(config_environment_test, Config) ->
rabbit_ct_broker_helpers:rpc(Config, 0, application, unset_env,
[rabbit, config_environment_test_env]),
Config;
end_per_testcase0(_, Config) -> Config.

%% -------------------------------------------------------------------
Expand Down Expand Up @@ -2905,6 +2911,7 @@ extensions_test(Config) ->
[#{javascript := <<"dispatcher.js">>}] = http_get(Config, "/extensions", ?OK),
passed.


cors_test(Config) ->
%% With CORS disabled. No header should be received.
R = req(Config, get, "/overview", [auth_header("guest", "guest")]),
Expand Down Expand Up @@ -3430,6 +3437,23 @@ auth_attempts_test(Config) ->

passed.


config_environment_test(Config) ->
rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
[rabbitmq_management,
config_environment_test_env,
config_environment_test_value]),
ResultString = http_get_no_decode(Config, "/config/effective",
"guest", "guest", ?OK),
CleanString = re:replace(ResultString, "\\s+", "", [global,{return,list}]),
{ok, Tokens, _} = erl_scan:string(CleanString++"."),
{ok, AbsForm} = erl_parse:parse_exprs(Tokens),
{value, EnvList, _} = erl_eval:exprs(AbsForm, erl_eval:new_bindings()),
V = proplists:get_value(config_environment_test_env,
proplists:get_value(rabbitmq_management, EnvList)),
?assertEqual(config_environment_test_value, V).


%% -------------------------------------------------------------------
%% Helpers.
%% -------------------------------------------------------------------
Expand Down

0 comments on commit a2533a6

Please sign in to comment.