Skip to content

Commit 35137e8

Browse files
author
Simon MacMullen
committed
Tighten up HA policy validation.
1 parent 8ecca24 commit 35137e8

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/rabbit_mirror_queue_misc.erl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,24 +316,30 @@ update_mirrors0(OldQ = #amqqueue{name = QName},
316316
%%----------------------------------------------------------------------------
317317

318318
validate_policy(KeyList) ->
319-
Mode = proplists:get_value(<<"ha-mode">>, KeyList),
319+
Mode = proplists:get_value(<<"ha-mode">>, KeyList, none),
320320
Params = proplists:get_value(<<"ha-params">>, KeyList, none),
321-
case Mode of
322-
undefined -> ok;
323-
_ -> case module(Mode) of
324-
{ok, M} -> case M:validate_policy(Params) of
325-
ok -> validate_sync_mode(KeyList);
326-
E -> E
327-
end;
328-
_ -> {error,
329-
"~p is not a valid ha-mode value", [Mode]}
330-
end
321+
SyncMode = proplists:get_value(<<"ha-sync-mode">>, KeyList, none),
322+
case {Mode, Params, SyncMode} of
323+
{none, none, none} ->
324+
ok;
325+
{none, _, _} ->
326+
{error, "ha-mode must be specified to specify ha-params or "
327+
"ha-sync-mode", []};
328+
_ ->
329+
case module(Mode) of
330+
{ok, M} -> case M:validate_policy(Params) of
331+
ok -> validate_sync_mode(SyncMode);
332+
E -> E
333+
end;
334+
_ -> {error, "~p is not a valid ha-mode value", [Mode]}
335+
end
331336
end.
332337

333-
validate_sync_mode(KeyList) ->
334-
case proplists:get_value(<<"ha-sync-mode">>, KeyList, <<"manual">>) of
338+
validate_sync_mode(SyncMode) ->
339+
case SyncMode of
335340
<<"automatic">> -> ok;
336341
<<"manual">> -> ok;
342+
none -> ok;
337343
Mode -> {error, "ha-sync-mode must be \"manual\" "
338344
"or \"automatic\", got ~p", [Mode]}
339345
end.

src/rabbit_tests.erl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ all_tests() ->
6060
passed = test_user_management(),
6161
passed = test_runtime_parameters(),
6262
passed = test_policy_validation(),
63+
passed = test_ha_policy_validation(),
6364
passed = test_server_status(),
6465
passed = test_amqp_connection_refusal(),
6566
passed = test_confirms(),
@@ -1101,6 +1102,34 @@ test_policy_validation() ->
11011102
rabbit_runtime_parameters_test:unregister_policy_validator(),
11021103
passed.
11031104

1105+
test_ha_policy_validation() ->
1106+
Set = fun (JSON) -> control_action(set_policy, ["name", ".*", JSON]) end,
1107+
OK = fun (JSON) -> ok = Set(JSON) end,
1108+
Fail = fun (JSON) -> {error_string, _} = Set(JSON) end,
1109+
1110+
OK ("{\"ha-mode\":\"all\"}"),
1111+
Fail("{\"ha-mode\":\"made_up\"}"),
1112+
1113+
Fail("{\"ha-mode\":\"nodes\"}"),
1114+
Fail("{\"ha-mode\":\"nodes\",\"ha-params\":2}"),
1115+
Fail("{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",2]}"),
1116+
OK ("{\"ha-mode\":\"nodes\",\"ha-params\":[\"a\",\"b\"]}"),
1117+
Fail("{\"ha-params\":[\"a\",\"b\"]}"),
1118+
1119+
Fail("{\"ha-mode\":\"exactly\"}"),
1120+
Fail("{\"ha-mode\":\"exactly\",\"ha-params\":[\"a\",\"b\"]}"),
1121+
OK ("{\"ha-mode\":\"exactly\",\"ha-params\":2}"),
1122+
Fail("{\"ha-params\":2}"),
1123+
1124+
OK ("{\"ha-mode\":\"all\",\"ha-sync-mode\":\"manual\"}"),
1125+
OK ("{\"ha-mode\":\"all\",\"ha-sync-mode\":\"automatic\"}"),
1126+
Fail("{\"ha-mode\":\"all\",\"ha-sync-mode\":\"made_up\"}"),
1127+
Fail("{\"ha-sync-mode\":\"manual\"}"),
1128+
Fail("{\"ha-sync-mode\":\"automatic\"}"),
1129+
1130+
ok = control_action(clear_policy, ["name"]),
1131+
passed.
1132+
11041133
test_server_status() ->
11051134
%% create a few things so there is some useful information to list
11061135
{_Writer, Limiter, Ch} = test_channel(),

0 commit comments

Comments
 (0)