Skip to content

Commit e3bbdfe

Browse files
authored
Merge pull request #2676 from rabbitmq/rabbitmq-server-2667
Definition export: change user tags to a JSON array
2 parents 55c830b + 335c7b4 commit e3bbdfe

File tree

7 files changed

+98
-33
lines changed

7 files changed

+98
-33
lines changed

deps/rabbit/src/rabbit_auth_backend_internal.erl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
%% for testing
4040
-export([hashing_module_for_user/1, expand_topic_permission/2]).
4141

42+
-import(rabbit_data_coercion, [to_atom/1, to_list/1, to_binary/1]).
43+
4244
%%----------------------------------------------------------------------------
4345

4446
-type regexp() :: binary().
@@ -662,9 +664,8 @@ put_user(User, Version, ActingUser) ->
662664
true -> [administrator];
663665
false -> []
664666
end;
665-
{TagsS, _} ->
666-
[list_to_atom(string:strip(T)) ||
667-
T <- string:tokens(binary_to_list(TagsS), ",")]
667+
{TagsVal, _} ->
668+
tag_list_from(TagsVal)
668669
end,
669670

670671
%% pre-configured, only applies to newly created users
@@ -813,6 +814,11 @@ clear_user_limits(Username, LimitType, ActingUser) ->
813814
end),
814815
notify_limit_clear(Username, ActingUser).
815816

817+
tag_list_from(Tags) when is_list(Tags) ->
818+
[to_atom(string:strip(to_list(T))) || T <- Tags];
819+
tag_list_from(Tags) when is_binary(Tags) ->
820+
[to_atom(string:strip(T)) || T <- string:tokens(to_list(Tags), ",")].
821+
816822
flatten_errors(L) ->
817823
case [{F, A} || I <- lists:flatten([L]), {error, F, A} <- [I]] of
818824
[] -> ok;

deps/rabbit/src/rabbit_definitions.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
-export([decode/1, decode/2, args/1]).
2727

2828
-import(rabbit_misc, [pget/2]).
29+
-import(rabbit_data_coercion, [to_binary/1]).
2930

3031
%%
3132
%% API
@@ -765,4 +766,4 @@ topic_permission_definition(P0) ->
765766
maps:from_list(P).
766767

767768
tags_as_binaries(Tags) ->
768-
list_to_binary(string:join([atom_to_list(T) || T <- Tags], ",")).
769+
[to_binary(T) || T <- Tags].

deps/rabbit/test/definition_import_SUITE.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ groups() ->
4040
import_case11,
4141
import_case12,
4242
import_case13,
43-
import_case14
43+
import_case14,
44+
import_case15
4445
]},
4546
{boot_time_import, [], [
4647
import_on_a_booting_node
@@ -143,6 +144,8 @@ import_case13(Config) ->
143144
end.
144145

145146
import_case14(Config) -> import_file_case(Config, "case14").
147+
%% contains a user with tags as a list
148+
import_case15(Config) -> import_file_case(Config, "case15").
146149

147150
export_import_round_trip_case1(Config) ->
148151
%% case 6 has runtime parameters that do not depend on any plugins
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"bindings": [],
3+
"exchanges": [],
4+
"global_parameters": [
5+
{
6+
"name": "cluster_name",
7+
"value": "rabbit@rabbitmq"
8+
}
9+
],
10+
"parameters": [],
11+
"permissions": [
12+
{
13+
"configure": ".*",
14+
"read": ".*",
15+
"user": "guest",
16+
"vhost": "/",
17+
"write": ".*"
18+
}
19+
],
20+
"policies": [],
21+
"queues": [],
22+
"rabbit_version": "3.9.0",
23+
"rabbitmq_version": "3.9.0",
24+
"topic_permissions": [],
25+
"users": [
26+
{
27+
"hashing_algorithm": "rabbit_password_hashing_sha256",
28+
"name": "guest",
29+
"password_hash": "BYipq3D94qlyiZVOAAYLVdN1v8H0BOrOpM9SH6ma5aB354FA",
30+
"tags": "administrator"
31+
},
32+
{
33+
"hashing_algorithm": "rabbit_password_hashing_sha256",
34+
"name": "tagged-user",
35+
"password_hash": "t/Ah03PwU/ol8vkarb+oEYpylRSBWXFAau3eXz3lrjGxtGEK",
36+
"tags": [
37+
"monitoring",
38+
"policymaker",
39+
"impersonator"
40+
]
41+
}
42+
],
43+
"vhosts": [
44+
{
45+
"limits": [],
46+
"metadata": {
47+
"description": "Default virtual host",
48+
"tags": []
49+
},
50+
"name": "/"
51+
}
52+
]
53+
}

deps/rabbitmq_management/priv/www/js/global.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ var user;
630630
function setup_global_vars() {
631631
var overview = JSON.parse(sync_get('/overview'));
632632
rates_mode = overview.rates_mode;
633-
user_tags = expand_user_tags(user.tags.split(","));
633+
user_tags = expand_user_tags(user.tags);
634634
user_administrator = jQuery.inArray("administrator", user_tags) != -1;
635635
is_user_policymaker = jQuery.inArray("policymaker", user_tags) != -1;
636636
user_monitor = jQuery.inArray("monitoring", user_tags) != -1;

deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -491,43 +491,43 @@ vhosts_trace_test(Config) ->
491491
passed.
492492

493493
users_test(Config) ->
494-
assert_item(#{name => <<"guest">>, tags => <<"administrator">>},
494+
assert_item(#{name => <<"guest">>, tags => [<<"administrator">>]},
495495
http_get(Config, "/whoami")),
496496
rabbit_ct_broker_helpers:rpc(Config, 0, application, set_env,
497497
[rabbitmq_management, login_session_timeout, 100]),
498498
assert_item(#{name => <<"guest">>,
499-
tags => <<"administrator">>,
499+
tags => [<<"administrator">>],
500500
login_session_timeout => 100},
501501
http_get(Config, "/whoami")),
502502
http_get(Config, "/users/myuser", ?NOT_FOUND),
503503
http_put_raw(Config, "/users/myuser", "Something not JSON", ?BAD_REQUEST),
504504
http_put(Config, "/users/myuser", [{flim, <<"flam">>}], ?BAD_REQUEST),
505-
http_put(Config, "/users/myuser", [{tags, <<"management">>},
505+
http_put(Config, "/users/myuser", [{tags, [<<"management">>]},
506506
{password, <<"myuser">>}],
507507
{group, '2xx'}),
508508
http_put(Config, "/users/myuser", [{password_hash, <<"not_hash">>}], ?BAD_REQUEST),
509509
http_put(Config, "/users/myuser", [{password_hash,
510510
<<"IECV6PZI/Invh0DL187KFpkO5Jc=">>},
511511
{tags, <<"management">>}], {group, '2xx'}),
512-
assert_item(#{name => <<"myuser">>, tags => <<"management">>,
512+
assert_item(#{name => <<"myuser">>, tags => [<<"management">>],
513513
password_hash => <<"IECV6PZI/Invh0DL187KFpkO5Jc=">>,
514514
hashing_algorithm => <<"rabbit_password_hashing_sha256">>},
515515
http_get(Config, "/users/myuser")),
516516

517517
http_put(Config, "/users/myuser", [{password_hash,
518518
<<"IECV6PZI/Invh0DL187KFpkO5Jc=">>},
519519
{hashing_algorithm, <<"rabbit_password_hashing_md5">>},
520-
{tags, <<"management">>}], {group, '2xx'}),
521-
assert_item(#{name => <<"myuser">>, tags => <<"management">>,
520+
{tags, [<<"management">>]}], {group, '2xx'}),
521+
assert_item(#{name => <<"myuser">>, tags => [<<"management">>],
522522
password_hash => <<"IECV6PZI/Invh0DL187KFpkO5Jc=">>,
523523
hashing_algorithm => <<"rabbit_password_hashing_md5">>},
524524
http_get(Config, "/users/myuser")),
525525
http_put(Config, "/users/myuser", [{password, <<"password">>},
526-
{tags, <<"administrator, foo">>}], {group, '2xx'}),
527-
assert_item(#{name => <<"myuser">>, tags => <<"administrator,foo">>},
526+
{tags, [<<"administrator">>, <<"foo">>]}], {group, '2xx'}),
527+
assert_item(#{name => <<"myuser">>, tags => [<<"administrator">>, <<"foo">>]},
528528
http_get(Config, "/users/myuser")),
529-
assert_list(lists:sort([#{name => <<"myuser">>, tags => <<"administrator,foo">>},
530-
#{name => <<"guest">>, tags => <<"administrator">>}]),
529+
assert_list(lists:sort([#{name => <<"myuser">>, tags => [<<"administrator">>, <<"foo">>]},
530+
#{name => <<"guest">>, tags => [<<"administrator">>]}]),
531531
lists:sort(http_get(Config, "/users"))),
532532
test_auth(Config, ?OK, [auth_header("myuser", "password")]),
533533
http_delete(Config, "/users/myuser", {group, '2xx'}),
@@ -536,7 +536,7 @@ users_test(Config) ->
536536
passed.
537537

538538
without_permissions_users_test(Config) ->
539-
assert_item(#{name => <<"guest">>, tags => <<"administrator">>},
539+
assert_item(#{name => <<"guest">>, tags => [<<"administrator">>]},
540540
http_get(Config, "/whoami")),
541541
http_put(Config, "/users/myuser", [{password_hash,
542542
<<"IECV6PZI/Invh0DL187KFpkO5Jc=">>},
@@ -546,7 +546,7 @@ without_permissions_users_test(Config) ->
546546
http_put(Config, "/users/myuserwithoutpermissions", [{password_hash,
547547
<<"IECV6PZI/Invh0DL187KFpkO5Jc=">>},
548548
{tags, <<"management">>}], {group, '2xx'}),
549-
assert_list([#{name => <<"myuserwithoutpermissions">>, tags => <<"management">>,
549+
assert_list([#{name => <<"myuserwithoutpermissions">>, tags => [<<"management">>],
550550
hashing_algorithm => <<"rabbit_password_hashing_sha256">>,
551551
password_hash => <<"IECV6PZI/Invh0DL187KFpkO5Jc=">>}],
552552
http_get(Config, "/users/without-permissions")),
@@ -555,7 +555,7 @@ without_permissions_users_test(Config) ->
555555
passed.
556556

557557
users_bulk_delete_test(Config) ->
558-
assert_item(#{name => <<"guest">>, tags => <<"administrator">>},
558+
assert_item(#{name => <<"guest">>, tags => [<<"administrator">>]},
559559
http_get(Config, "/whoami")),
560560
http_put(Config, "/users/myuser1", [{tags, <<"management">>}, {password, <<"myuser">>}],
561561
{group, '2xx'}),
@@ -584,9 +584,9 @@ users_legacy_administrator_test(Config) ->
584584
http_put(Config, "/users/myuser2", [{administrator, <<"false">>},
585585
{password, <<"myuser2">>}],
586586
{group, '2xx'}),
587-
assert_item(#{name => <<"myuser1">>, tags => <<"administrator">>},
587+
assert_item(#{name => <<"myuser1">>, tags => [<<"administrator">>]},
588588
http_get(Config, "/users/myuser1")),
589-
assert_item(#{name => <<"myuser2">>, tags => <<"">>},
589+
assert_item(#{name => <<"myuser2">>, tags => []},
590590
http_get(Config, "/users/myuser2")),
591591
http_delete(Config, "/users/myuser1", {group, '2xx'}),
592592
http_delete(Config, "/users/myuser2", {group, '2xx'}),
@@ -685,7 +685,7 @@ updating_a_user_without_password_or_hash_clears_password_test(Config) ->
685685
%% clear users' credentials
686686
http_put(Config, "/users/myuser", [{tags, <<"management">>}], [?CREATED, ?NO_CONTENT]),
687687
assert_item(#{name => <<"myuser">>,
688-
tags => <<"management">>,
688+
tags => [<<"management">>],
689689
password_hash => <<>>,
690690
hashing_algorithm => <<"rabbit_password_hashing_sha256">>},
691691
http_get(Config, "/users/myuser")),
@@ -723,21 +723,21 @@ updating_tags_of_a_passwordless_user_test(Config) ->
723723
%% clear user's password
724724
http_put(Config, "/users/abc", [{tags, <<"management">>}], [?CREATED, ?NO_CONTENT]),
725725
assert_item(#{name => ?NON_GUEST_USERNAME,
726-
tags => <<"management">>,
726+
tags => [<<"management">>],
727727
password_hash => <<>>,
728728
hashing_algorithm => <<"rabbit_password_hashing_sha256">>},
729729
http_get(Config, "/users/abc")),
730730

731731
http_put(Config, "/users/abc", [{tags, <<"impersonator">>}], [?CREATED, ?NO_CONTENT]),
732732
assert_item(#{name => ?NON_GUEST_USERNAME,
733-
tags => <<"impersonator">>,
733+
tags => [<<"impersonator">>],
734734
password_hash => <<>>,
735735
hashing_algorithm => <<"rabbit_password_hashing_sha256">>},
736736
http_get(Config, "/users/abc")),
737737

738738
http_put(Config, "/users/abc", [{tags, <<"">>}], [?CREATED, ?NO_CONTENT]),
739739
assert_item(#{name => ?NON_GUEST_USERNAME,
740-
tags => <<"">>,
740+
tags => [],
741741
password_hash => <<>>,
742742
hashing_algorithm => <<"rabbit_password_hashing_sha256">>},
743743
http_get(Config, "/users/abc")),
@@ -1613,7 +1613,7 @@ definitions_test(Config) ->
16131613
#{name => <<"myuser">>,
16141614
password_hash => <<"WAbU0ZIcvjTpxM3Q3SbJhEAM2tQ=">>,
16151615
hashing_algorithm => <<"rabbit_password_hashing_sha256">>,
1616-
tags => <<"management">>}),
1616+
tags => [<<"management">>]}),
16171617
defs(Config, vhosts, "/vhosts/myvhost", put,
16181618
#{name => <<"myvhost">>}),
16191619
defs(Config, permissions, "/permissions/%2F/guest", put,
@@ -1788,7 +1788,7 @@ definitions_password_test(Config) ->
17881788
Expected35 = #{name => <<"myuser">>,
17891789
password_hash => <<"WAbU0ZIcvjTpxM3Q3SbJhEAM2tQ=">>,
17901790
hashing_algorithm => <<"rabbit_password_hashing_md5">>,
1791-
tags => <<"management">>},
1791+
tags => [<<"management">>]},
17921792
http_post(Config, "/definitions", Config35, {group, '2xx'}),
17931793
Definitions35 = http_get(Config, "/definitions", ?OK),
17941794
ct:pal("Definitions35: ~p", [Definitions35]),
@@ -1804,7 +1804,7 @@ definitions_password_test(Config) ->
18041804
Expected36 = #{name => <<"myuser">>,
18051805
password_hash => <<"WAbU0ZIcvjTpxM3Q3SbJhEAM2tQ=">>,
18061806
hashing_algorithm => <<"rabbit_password_hashing_sha256">>,
1807-
tags => <<"management">>},
1807+
tags => [<<"management">>]},
18081808
http_post(Config, "/definitions", Config36, {group, '2xx'}),
18091809

18101810
Definitions36 = http_get(Config, "/definitions", ?OK),
@@ -1824,7 +1824,7 @@ definitions_password_test(Config) ->
18241824
ExpectedDefault = #{name => <<"myuser">>,
18251825
password_hash => <<"WAbU0ZIcvjTpxM3Q3SbJhEAM2tQ=">>,
18261826
hashing_algorithm => <<"rabbit_password_hashing_sha512">>,
1827-
tags => <<"management">>},
1827+
tags => [<<"management">>]},
18281828
http_post(Config, "/definitions", ConfigDefault, {group, '2xx'}),
18291829

18301830
DefinitionsDefault = http_get(Config, "/definitions", ?OK),

deps/rabbitmq_management_agent/src/rabbit_mgmt_format.erl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
-export([args_hash/1]).
3131

3232
-import(rabbit_misc, [pget/2, pget/3, pset/3]).
33+
-import(rabbit_data_coercion, [to_binary/1]).
3334

3435
-include_lib("rabbit_common/include/rabbit.hrl").
3536
-include_lib("rabbit_common/include/rabbit_framing.hrl").
@@ -215,15 +216,16 @@ internal_user(User) ->
215216
{password_hash, base64:encode(internal_user:get_password_hash(User))},
216217
{hashing_algorithm, rabbit_auth_backend_internal:hashing_module_for_user(
217218
User)},
218-
{tags, tags(internal_user:get_tags(User))},
219+
{tags, tags_as_binaries(internal_user:get_tags(User))},
219220
{limits, internal_user:get_limits(User)}].
220221

221222
user(User) ->
222223
[{name, User#user.username},
223-
{tags, tags(User#user.tags)}].
224+
{tags, tags_as_binaries(User#user.tags)}].
225+
226+
tags_as_binaries(Tags) ->
227+
[to_binary(T) || T <- Tags].
224228

225-
tags(Tags) ->
226-
list_to_binary(string:join([atom_to_list(T) || T <- Tags], ",")).
227229

228230
listener(#listener{node = Node, protocol = Protocol,
229231
ip_address = IPAddress, port = Port, opts=Opts}) ->

0 commit comments

Comments
 (0)