diff --git a/deps/rabbit/src/rabbit_definitions.erl b/deps/rabbit/src/rabbit_definitions.erl index d076b74dc9c..1dca5163f76 100644 --- a/deps/rabbit/src/rabbit_definitions.erl +++ b/deps/rabbit/src/rabbit_definitions.erl @@ -230,9 +230,8 @@ import_parsed_with_hashing(Body0) when is_map(Body0) -> binary:part(rabbit_misc:hexify(PreviousHash), 0, 10), binary:part(rabbit_misc:hexify(Other), 0, 10) ]), - Result = apply_defs(Body, ?INTERNAL_USER), - rabbit_definitions_hashing:store_global_hash(Other), - Result + apply_defs(Body, ?INTERNAL_USER, + fun() -> rabbit_definitions_hashing:store_global_hash(Other) end) end end. @@ -258,9 +257,9 @@ import_parsed_with_hashing(Body0, VHost) -> binary:part(rabbit_misc:hexify(PreviousHash), 0, 10), binary:part(rabbit_misc:hexify(Other), 0, 10) ]), - Result = apply_defs(Body, ?INTERNAL_USER, fun() -> ok end, VHost), - rabbit_definitions_hashing:store_vhost_specific_hash(VHost, Other, ?INTERNAL_USER), - Result + apply_defs(Body, ?INTERNAL_USER, + fun() -> rabbit_definitions_hashing:store_vhost_specific_hash(VHost, Other, ?INTERNAL_USER) end, + VHost) end end. @@ -541,21 +540,16 @@ apply_defs(Map, ActingUser) -> apply_defs(Map, ActingUser, VHost) when is_binary(VHost) -> apply_defs(Map, ActingUser, fun () -> ok end, VHost); apply_defs(Map, ActingUser, SuccessFun) when is_function(SuccessFun) -> - Version = maps:get(rabbitmq_version, Map, maps:get(rabbit_version, Map, undefined)), - - %% If any of the queues or exchanges do not have virtual hosts set, - %% this definition file was a virtual-host specific import. They cannot be applied - %% as "complete" definition imports, most notably, imported on boot. - AnyOrphaned = any_orphaned_in_doc(Map), - - case AnyOrphaned of + case any_orphaned_in_doc(Map) of true -> log_an_error_about_orphaned_objects(), - throw({error, invalid_definitions_file}); + {error, format(invalid_definitions_file)}; false -> - ok - end, + apply_cluster_defs(Map, ActingUser, SuccessFun) + end. +apply_cluster_defs(Map, ActingUser, SuccessFun) -> + Version = maps:get(rabbitmq_version, Map, maps:get(rabbit_version, Map, undefined)), try concurrent_for_all(users, ActingUser, Map, fun(User, _Username) -> diff --git a/deps/rabbit/src/rabbit_definitions_import_https.erl b/deps/rabbit/src/rabbit_definitions_import_https.erl index d951e52188f..4c5af11a24c 100644 --- a/deps/rabbit/src/rabbit_definitions_import_https.erl +++ b/deps/rabbit/src/rabbit_definitions_import_https.erl @@ -58,7 +58,8 @@ load(Proplist) -> HTTPOptions = http_options(TLSOptions), load_from_url(URL, HTTPOptions). --spec load_with_hashing(Proplist :: list() | map(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> binary() | 'undefined'. +-spec load_with_hashing(Proplist :: list() | map(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> + binary() | 'undefined' | {error, term()}. load_with_hashing(Proplist, PreviousHash, Algo) -> URL = pget(url, Proplist), ?LOG_INFO("Applying definitions from a remote URL"), @@ -78,8 +79,10 @@ load_with_hashing(Proplist, PreviousHash, Algo) -> PreviousHash -> PreviousHash; Other -> ?LOG_DEBUG("New hash: ~ts", [rabbit_misc:hexify(Other)]), - _ = import_raw(Body), - Other + maybe + ok ?= import_raw(Body), + Other + end end; {ok, {{_, Code, _}, _Headers, _Body}} when Code >= 400 -> ?LOG_DEBUG("Requested definitions from remote URL '~ts', response code: ~b", [URL, Code]), diff --git a/deps/rabbit/src/rabbit_definitions_import_local_filesystem.erl b/deps/rabbit/src/rabbit_definitions_import_local_filesystem.erl index 682c48218a2..a877d340b90 100644 --- a/deps/rabbit/src/rabbit_definitions_import_local_filesystem.erl +++ b/deps/rabbit/src/rabbit_definitions_import_local_filesystem.erl @@ -67,7 +67,8 @@ load(Map) when is_map(Map) -> load(IsDir, Path) when is_boolean(IsDir) -> load_from_local_path(IsDir, Path). --spec load_with_hashing(Proplist :: list() | map(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> binary() | 'undefined'. +-spec load_with_hashing(Proplist :: list() | map(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> + binary() | 'undefined' | {error, term()}. load_with_hashing(Proplist, PreviousHash, Algo) -> case pget(local_path, Proplist, undefined) of undefined -> {error, "local definition file path is not configured: local_path is not set"}; @@ -76,7 +77,8 @@ load_with_hashing(Proplist, PreviousHash, Algo) -> load_with_hashing(IsDir, Path, PreviousHash, Algo) end. --spec load_with_hashing(IsDir :: boolean(), Path :: file:name_all(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> binary() | 'undefined'. +-spec load_with_hashing(IsDir :: boolean(), Path :: file:name_all(), PreviousHash :: binary() | 'undefined', Algo :: crypto:sha1() | crypto:sha2()) -> + binary() | 'undefined' | {error, term()}. load_with_hashing(IsDir, Path, PreviousHash, Algo) when is_boolean(IsDir) -> ?LOG_DEBUG("Loading definitions with content hashing enabled, path: ~ts, is directory?: ~tp, previous hash value: ~ts", [Path, IsDir, rabbit_misc:hexify(PreviousHash)]), @@ -91,8 +93,10 @@ load_with_hashing(IsDir, Path, PreviousHash, Algo) when is_boolean(IsDir) -> PreviousHash -> PreviousHash; Other -> ?LOG_DEBUG("New hash: ~ts", [rabbit_misc:hexify(Other)]), - _ = load_from_local_path(IsDir, Path), - Other + maybe + ok ?= load_from_local_path(IsDir, Path), + Other + end end; false -> ?LOG_ERROR("Definitions file at path ~p failed validation. The file must be a valid JSON document " diff --git a/deps/rabbit/test/definition_import_SUITE.erl b/deps/rabbit/test/definition_import_SUITE.erl index 8d90144af6f..be2ba734920 100644 --- a/deps/rabbit/test/definition_import_SUITE.erl +++ b/deps/rabbit/test/definition_import_SUITE.erl @@ -91,6 +91,12 @@ groups() -> ]}, {skip_if_unchanged, [], [ + rejected_import_preserves_hash, + failed_import_preserves_hash, + failed_vhost_import_preserves_hash, + failed_file_import_preserves_hash, + failed_directory_import_preserves_hash, + failed_http_import_preserves_hash, %% these all must import the same definition file import_on_a_booting_node_using_skip_if_unchanged, import_case5 @@ -649,6 +655,93 @@ import_on_a_booting_node_using_public_https_source(Config) -> {error, timeout} -> ct:fail("virtual host ~tp was not imported on boot", [VHost]) end. +rejected_import_preserves_hash(Config) -> + Defs = #{<<"queues">> => [#{<<"name">> => <<"vhost-scoped-queue">>}]}, + StoredHash = fun() -> + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions_hashing, stored_global_hash, []) + end, + Hash = StoredHash(), + ?assertEqual({error, <<"invalid_definitions_file">>}, + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions, + import_parsed_with_hashing, [Defs])), + ?assertEqual(Hash, StoredHash()). + +failed_import_preserves_hash(Config) -> + Defs = #{<<"bindings">> => [invalid_binding(#{<<"vhost">> => <<"/">>})]}, + StoredHash = fun() -> + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions_hashing, stored_global_hash, []) + end, + Hash = StoredHash(), + ?assertMatch({error, _}, + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions, + import_parsed_with_hashing, [Defs])), + ?assertEqual(Hash, StoredHash()). + +failed_vhost_import_preserves_hash(Config) -> + VHost = <<"/">>, + Defs = #{<<"bindings">> => [invalid_binding(#{})]}, + StoredHash = fun() -> + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions_hashing, + stored_vhost_specific_hash, [VHost]) + end, + Hash = StoredHash(), + ?assertMatch({error, _}, + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions, + import_parsed_with_hashing, [Defs, VHost])), + ?assertEqual(Hash, StoredHash()). + +failed_file_import_preserves_hash(Config) -> + Path = write_failing_definitions(Config, ?FUNCTION_NAME), + assert_failed_load_preserves_hash( + Config, maybe_load_definitions_from_local_filesystem_if_unchanged, + [rabbit_definitions_import_local_filesystem, false, Path]). + +failed_directory_import_preserves_hash(Config) -> + Path = write_failing_definitions(Config, ?FUNCTION_NAME), + assert_failed_load_preserves_hash( + Config, maybe_load_definitions_from_local_filesystem_if_unchanged, + [rabbit_definitions_import_local_filesystem, true, filename:dirname(Path)]). + +failed_http_import_preserves_hash(Config) -> + Path = write_failing_definitions(Config, ?FUNCTION_NAME), + {ok, Httpd} = inets:start(httpd, [{port, 0}, + {bind_address, {127, 0, 0, 1}}, + {server_name, "localhost"}, + {server_root, filename:dirname(Path)}, + {document_root, filename:dirname(Path)}]), + try + [{port, Port}] = httpd:info(Httpd, [port]), + URL = rabbit_misc:format("http://127.0.0.1:~b/~ts", [Port, filename:basename(Path)]), + assert_failed_load_preserves_hash( + Config, maybe_load_definitions_from_pluggable_source_if_unchanged, + [rabbit_definitions_import_https, [{url, URL}]]) + after + inets:stop(httpd, Httpd) + end. + +assert_failed_load_preserves_hash(Config, Fun, Args) -> + StoredHash = fun() -> + rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions_hashing, stored_global_hash, []) + end, + Load = fun() -> rabbit_ct_broker_helpers:rpc(Config, 0, rabbit_definitions, Fun, Args) end, + Hash = StoredHash(), + ?assertMatch({error, _}, Load()), + ?assertEqual(Hash, StoredHash()), + ?assertMatch({error, _}, Load()). + +write_failing_definitions(Config, Name) -> + Path = filename:join([?config(priv_dir, Config), Name, "definitions.json"]), + ok = filelib:ensure_dir(Path), + Defs = #{<<"bindings">> => [invalid_binding(#{<<"vhost">> => <<"/">>})]}, + ok = file:write_file(Path, rabbit_json:encode(Defs)), + Path. + +invalid_binding(Binding) -> + Binding#{<<"source">> => <<"amq.direct">>, + <<"destination">> => <<"q">>, + <<"destination_type">> => <<"neither">>, + <<"routing_key">> => <<>>}. + import_on_a_booting_node_using_skip_if_unchanged(Config) -> %% see case5.json VHost = <<"vhost2">>, diff --git a/deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl b/deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl index e686298ba05..c5707224007 100644 --- a/deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl +++ b/deps/rabbitmq_management/test/rabbit_mgmt_http_SUITE.erl @@ -118,6 +118,7 @@ definitions_group3_tests() -> definitions_group4_tests() -> [ definitions_vhost_test, + cluster_import_rejects_vhost_definitions, definitions_etag_test ]. @@ -2722,6 +2723,14 @@ defs_vhost(Config, Key, URI0, Rep1, VHost1, VHost2, CreateMethod, Args, DeleteFun(URI3), passed. +cluster_import_rejects_vhost_definitions(Config) -> + Definitions = #{queues => [#{name => <<"vhost-scoped-queue">>, durable => true}]}, + {ok, {{_, ?BAD_REQUEST, _}, _, Body}} = + req(Config, 0, post, "/definitions", [auth_header("guest", "guest")], + format_for_upload(Definitions)), + ?assertMatch(#{error := <<"bad_request">>, reason := <<"invalid_definitions_file">>}, + decode_body(Body)). + definitions_vhost_test(Config) -> %% Ensures that definitions can be exported/imported from a single virtual %% host to another