Skip to content

Commit 6968791

Browse files
SimonUngemergify[bot]
authored andcommitted
See #5957. HTTP api to generate hashed password from cleartext password
(cherry picked from commit 7fecfcd) (cherry picked from commit 17059b7)
1 parent 2df494e commit 6968791

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_dispatcher.erl

+2-1
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,6 @@ dispatcher() ->
183183
{"/auth/attempts/:node", rabbit_mgmt_wm_auth_attempts, [all]},
184184
{"/auth/attempts/:node/source", rabbit_mgmt_wm_auth_attempts, [by_source]},
185185
{"/login", rabbit_mgmt_wm_login, []},
186-
{"/config/effective", rabbit_mgmt_wm_environment, []}
186+
{"/config/effective", rabbit_mgmt_wm_environment, []},
187+
{"/auth/hash_password/:password", rabbit_mgmt_wm_hash_password, []}
187188
].
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
6+
%%
7+
8+
-module(rabbit_mgmt_wm_hash_password).
9+
10+
-export([init/2, to_json/2, content_types_provided/2, is_authorized/2]).
11+
-export([variances/2, allowed_methods/2]).
12+
13+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
14+
-include_lib("rabbit_common/include/rabbit.hrl").
15+
16+
%%--------------------------------------------------------------------
17+
18+
init(Req, _State) ->
19+
{cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}.
20+
21+
variances(Req, Context) ->
22+
{[<<"accept-encoding">>, <<"origin">>], Req, Context}.
23+
24+
allowed_methods(ReqData, Context) ->
25+
{[<<"GET">>, <<"OPTIONS">>], ReqData, Context}.
26+
27+
content_types_provided(ReqData, Context) ->
28+
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
29+
30+
to_json(ReqData, Context) ->
31+
Password = rabbit_mgmt_util:id(password, ReqData),
32+
HashedPassword = rabbit_password:hash(Password),
33+
rabbit_mgmt_util:reply([{ok, base64:encode(HashedPassword)}], ReqData, Context).
34+
35+
is_authorized(ReqData, Context) ->
36+
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).

deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ all_tests() -> [
7171
users_legacy_administrator_test,
7272
adding_a_user_with_password_test,
7373
adding_a_user_with_password_hash_test,
74+
adding_a_user_with_generated_password_hash_test,
7475
adding_a_user_with_permissions_in_single_operation_test,
7576
adding_a_user_without_tags_fails_test,
7677
adding_a_user_without_password_or_hash_test,
@@ -630,6 +631,17 @@ adding_a_user_with_password_hash_test(Config) ->
630631
[?CREATED, ?NO_CONTENT]),
631632
http_delete(Config, "/users/user11", ?NO_CONTENT).
632633

634+
adding_a_user_with_generated_password_hash_test(Config) ->
635+
#{ok := HashedPassword} = http_get(Config, "/auth/hash_password/some_password"),
636+
637+
http_put(Config, "/users/user12", [{tags, <<"administrator">>},
638+
{password_hash, HashedPassword}],
639+
[?CREATED, ?NO_CONTENT]),
640+
% If the get succeeded, the hashed password generation is correct
641+
User = http_get(Config, "/users/user12", "user12", "some_password", ?OK),
642+
?assertEqual(maps:get(password_hash, User), HashedPassword),
643+
http_delete(Config, "/users/user12", ?NO_CONTENT).
644+
633645
adding_a_user_with_permissions_in_single_operation_test(Config) ->
634646
QArgs = #{},
635647
PermArgs = #{configure => <<".*">>,

0 commit comments

Comments
 (0)