summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2014-04-08 16:00:55 +0100
committerSimon MacMullen <simon@rabbitmq.com>2014-04-08 16:00:55 +0100
commit6f8a4ef1ba794b2005dcdccb600c9fa1c621130c (patch)
treecda350d3bb58d95b49c40c8663949a6d19b3cf94
parent2f16bf43db30da94a1f3115bd19e12c3b4f196ea (diff)
downloadrabbitmq-server-6f8a4ef1ba794b2005dcdccb600c9fa1c621130c.tar.gz
Allow parameter validation to know about the user making the change.
-rw-r--r--src/rabbit_control_main.erl5
-rw-r--r--src/rabbit_policy.erl2
-rw-r--r--src/rabbit_runtime_parameter.erl2
-rw-r--r--src/rabbit_runtime_parameters.erl35
-rw-r--r--src/rabbit_runtime_parameters_test.erl13
-rw-r--r--src/rabbit_tests.erl2
6 files changed, 34 insertions, 25 deletions
diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl
index f9e59078..451f4d70 100644
--- a/src/rabbit_control_main.erl
+++ b/src/rabbit_control_main.erl
@@ -484,8 +484,9 @@ action(set_parameter, Node, [Component, Key, Value], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
Inform("Setting runtime parameter ~p for component ~p to ~p",
[Key, Component, Value]),
- rpc_call(Node, rabbit_runtime_parameters, parse_set,
- [VHostArg, list_to_binary(Component), list_to_binary(Key), Value]);
+ rpc_call(
+ Node, rabbit_runtime_parameters, parse_set,
+ [VHostArg, list_to_binary(Component), list_to_binary(Key), Value, none]);
action(clear_parameter, Node, [Component, Key], Opts, Inform) ->
VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)),
diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl
index 632f2c66..4483cf54 100644
--- a/src/rabbit_policy.erl
+++ b/src/rabbit_policy.erl
@@ -150,7 +150,7 @@ set(VHost, Name, Pattern, Definition, Priority, ApplyTo) ->
set0(VHost, Name, PolicyProps).
set0(VHost, Name, Term) ->
- rabbit_runtime_parameters:set_any(VHost, <<"policy">>, Name, Term).
+ rabbit_runtime_parameters:set_any(VHost, <<"policy">>, Name, Term, none).
delete(VHost, Name) ->
rabbit_runtime_parameters:clear_any(VHost, <<"policy">>, Name).
diff --git a/src/rabbit_runtime_parameter.erl b/src/rabbit_runtime_parameter.erl
index df297297..3e81ea74 100644
--- a/src/rabbit_runtime_parameter.erl
+++ b/src/rabbit_runtime_parameter.erl
@@ -22,7 +22,7 @@
'ok' | {error, string(), [term()]} | [validate_results()]).
-callback validate(rabbit_types:vhost(), binary(), binary(),
- term()) -> validate_results().
+ term(), rabbit_types:user()) -> validate_results().
-callback notify(rabbit_types:vhost(), binary(), binary(), term()) -> 'ok'.
-callback notify_clear(rabbit_types:vhost(), binary(), binary()) -> 'ok'.
diff --git a/src/rabbit_runtime_parameters.erl b/src/rabbit_runtime_parameters.erl
index ba6b9538..7307330b 100644
--- a/src/rabbit_runtime_parameters.erl
+++ b/src/rabbit_runtime_parameters.erl
@@ -18,7 +18,7 @@
-include("rabbit.hrl").
--export([parse_set/4, set/4, set_any/4, clear/3, clear_any/3, list/0, list/1,
+-export([parse_set/5, set/5, set_any/5, clear/3, clear_any/3, list/0, list/1,
list_component/1, list/2, list_formatted/1, lookup/3,
value/3, value/4, info_keys/0]).
@@ -30,12 +30,12 @@
-type(ok_or_error_string() :: 'ok' | {'error_string', string()}).
--spec(parse_set/4 :: (rabbit_types:vhost(), binary(), binary(), string())
- -> ok_or_error_string()).
--spec(set/4 :: (rabbit_types:vhost(), binary(), binary(), term())
- -> ok_or_error_string()).
--spec(set_any/4 :: (rabbit_types:vhost(), binary(), binary(), term())
- -> ok_or_error_string()).
+-spec(parse_set/5 :: (rabbit_types:vhost(), binary(), binary(), string(),
+ rabbit_types:user() | 'none') -> ok_or_error_string()).
+-spec(set/5 :: (rabbit_types:vhost(), binary(), binary(), term(),
+ rabbit_types:user() | 'none') -> ok_or_error_string()).
+-spec(set_any/5 :: (rabbit_types:vhost(), binary(), binary(), term(),
+ rabbit_types:user() | 'none') -> ok_or_error_string()).
-spec(set_global/2 :: (atom(), term()) -> 'ok').
-spec(clear/3 :: (rabbit_types:vhost(), binary(), binary())
-> ok_or_error_string()).
@@ -65,19 +65,19 @@
%%---------------------------------------------------------------------------
-parse_set(_, <<"policy">>, _, _) ->
+parse_set(_, <<"policy">>, _, _, _) ->
{error_string, "policies may not be set using this method"};
-parse_set(VHost, Component, Name, String) ->
+parse_set(VHost, Component, Name, String, User) ->
case rabbit_misc:json_decode(String) of
{ok, JSON} -> set(VHost, Component, Name,
- rabbit_misc:json_to_term(JSON));
+ rabbit_misc:json_to_term(JSON), User);
error -> {error_string, "JSON decoding error"}
end.
-set(_, <<"policy">>, _, _) ->
+set(_, <<"policy">>, _, _, _) ->
{error_string, "policies may not be set using this method"};
-set(VHost, Component, Name, Term) ->
- set_any(VHost, Component, Name, Term).
+set(VHost, Component, Name, Term, User) ->
+ set_any(VHost, Component, Name, Term, User).
set_global(Name, Term) ->
mnesia_update(Name, Term),
@@ -86,16 +86,17 @@ set_global(Name, Term) ->
format_error(L) ->
{error_string, rabbit_misc:format_many([{"Validation failed~n", []} | L])}.
-set_any(VHost, Component, Name, Term) ->
- case set_any0(VHost, Component, Name, Term) of
+set_any(VHost, Component, Name, Term, User) ->
+ case set_any0(VHost, Component, Name, Term, User) of
ok -> ok;
{errors, L} -> format_error(L)
end.
-set_any0(VHost, Component, Name, Term) ->
+set_any0(VHost, Component, Name, Term, User) ->
case lookup_component(Component) of
{ok, Mod} ->
- case flatten_errors(Mod:validate(VHost, Component, Name, Term)) of
+ case flatten_errors(
+ Mod:validate(VHost, Component, Name, Term, User)) of
ok ->
case mnesia_update(VHost, Component, Name, Term) of
{old, Term} -> ok;
diff --git a/src/rabbit_runtime_parameters_test.erl b/src/rabbit_runtime_parameters_test.erl
index 67956535..d889a29f 100644
--- a/src/rabbit_runtime_parameters_test.erl
+++ b/src/rabbit_runtime_parameters_test.erl
@@ -18,7 +18,9 @@
-behaviour(rabbit_runtime_parameter).
-behaviour(rabbit_policy_validator).
--export([validate/4, notify/4, notify_clear/3]).
+-include("rabbit.hrl").
+
+-export([validate/5, notify/4, notify_clear/3]).
-export([register/0, unregister/0]).
-export([validate_policy/1]).
-export([register_policy_validator/0, unregister_policy_validator/0]).
@@ -31,9 +33,12 @@ register() ->
unregister() ->
rabbit_registry:unregister(runtime_parameter, <<"test">>).
-validate(_, <<"test">>, <<"good">>, _Term) -> ok;
-validate(_, <<"test">>, <<"maybe">>, <<"good">>) -> ok;
-validate(_, <<"test">>, _, _) -> {error, "meh", []}.
+validate(_, <<"test">>, <<"good">>, _Term, _User) -> ok;
+validate(_, <<"test">>, <<"maybe">>, <<"good">>, _User) -> ok;
+validate(_, <<"test">>, <<"admin">>, _Term, none) -> ok;
+validate(_, <<"test">>, <<"admin">>, _Term, User) ->
+ lists:member(administrator, User#user.tags);
+validate(_, <<"test">>, _, _, _) -> {error, "meh", []}.
notify(_, _, _, _) -> ok.
notify_clear(_, _, _) -> ok.
diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl
index 2dc8a482..9e5cf2c0 100644
--- a/src/rabbit_tests.erl
+++ b/src/rabbit_tests.erl
@@ -1063,11 +1063,13 @@ test_runtime_parameters() ->
%% Test actual validation hook
Good(["test", "maybe", "\"good\""]),
Bad(["test", "maybe", "\"bad\""]),
+ Good(["test", "admin", "\"ignore\""]), %% ctl means 'user' -> none
ok = control_action(list_parameters, []),
ok = control_action(clear_parameter, ["test", "good"]),
ok = control_action(clear_parameter, ["test", "maybe"]),
+ ok = control_action(clear_parameter, ["test", "admin"]),
{error_string, _} =
control_action(clear_parameter, ["test", "neverexisted"]),