summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmile Joubert <emile@rabbitmq.com>2012-10-09 14:19:39 +0100
committerEmile Joubert <emile@rabbitmq.com>2012-10-09 14:19:39 +0100
commitc0db9df803e28355acf8978923061473e0f302fe (patch)
tree460dea7b4f1d379b017f2b841cce7540cf17631e
parent939f5a70e3f855bdec85730a579ebd8a745b27fd (diff)
downloadrabbitmq-server-c0db9df803e28355acf8978923061473e0f302fe.tar.gz
Stricter HA validation
-rw-r--r--src/rabbit_mirror_queue_misc.erl35
-rw-r--r--src/rabbit_policy.erl14
2 files changed, 31 insertions, 18 deletions
diff --git a/src/rabbit_mirror_queue_misc.erl b/src/rabbit_mirror_queue_misc.erl
index f0c555c9..9a5f0475 100644
--- a/src/rabbit_mirror_queue_misc.erl
+++ b/src/rabbit_mirror_queue_misc.erl
@@ -340,27 +340,32 @@ validate_policy(TagList) ->
[<<"all">>] ->
ok;
[<<"nodes">>] ->
- validate_params(fun erlang:is_binary/1, lists:append(Params),
- "~p has invalid node names when ha-mode=nodes");
+ validate_params(lists:append(Params),
+ fun erlang:is_binary/1,
+ "~p has invalid node names when ha-mode=nodes",
+ fun (N) -> N > 0 end,
+ "at least one node expected when ha-mode=nodes");
[<<"exactly">>] ->
- case Params of
- [_] -> validate_params(
- fun (N) -> is_integer(N) andalso N >= 0 end,
- Params, "~p must be a positive integer");
- X -> {error, "ha-params must be supplied with one number "
- "when ha-mode=exactly. found ~p arguments",
- [length(X)]}
- end;
+ validate_params(Params,
+ fun (N) -> is_integer(N) andalso N >= 0 end,
+ "~p must be a positive integer",
+ fun (N) -> N == 1 end,
+ "ha-params must be supplied with one number "
+ "when ha-mode=exactly");
[_, _|_] ->
{error, "ha-mode may appear at most once", []};
[Other] ->
{error, "~p is not a valid ha-mode value", [Other]}
end.
-validate_params(FilterFun, Params, Msg) when is_list(Params) ->
- case lists:filter(fun (P) -> not FilterFun(P) end, Params) of
- [] -> ok;
- X -> {error, Msg, [X]}
+validate_params(Params, FilterPred, FilterMsg, SizePred, SizeMsg)
+ when is_list(Params) ->
+ case SizePred(length(Params)) of
+ true -> case lists:filter(fun (P) -> not FilterPred(P) end, Params) of
+ [] -> ok;
+ X -> {error, FilterMsg, [X]}
+ end;
+ false -> {error, SizeMsg, []}
end;
-validate_params(_, Params, _) ->
+validate_params(Params, _, _, _, _) ->
{error, "~p was expected to be a list", [Params]}.
diff --git a/src/rabbit_policy.erl b/src/rabbit_policy.erl
index cc9e05c5..d9ac89ee 100644
--- a/src/rabbit_policy.erl
+++ b/src/rabbit_policy.erl
@@ -146,6 +146,16 @@ validation(_Name, Terms) when is_list(Terms) ->
rabbit_registry:lookup_all(policy_validator)),
[] = lists:usort(Tags -- lists:usort(Tags)), %% ASSERTION
Validators = lists:zipwith(fun (M, T) -> {M, a2b(T)} end, Modules, Tags),
+
+ {TermKeys, _} = lists:unzip(Terms),
+ case TermKeys -- lists:usort(TermKeys) of
+ [] -> validation0(Validators, Terms);
+ Dup -> {error, "~p duplicate keys not allowed", [Dup]}
+ end;
+validation(_Name, Term) ->
+ {error, "parse error while reading policy: ~p", [Term]}.
+
+validation0(Validators, Terms) ->
case lists:foldl(
fun (_, {Error, _} = Acc) when Error /= ok ->
Acc;
@@ -163,8 +173,6 @@ validation(_Name, Terms) when is_list(Terms) ->
{error, "~p are not recognised policy settings", Unvalidated};
{Error, _} ->
Error
- end;
-validation(_Name, Term) ->
- {error, "parse error while reading policy: ~p", [Term]}.
+ end.
a2b(A) -> list_to_binary(atom_to_list(A)).