|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(upstream_validation_SUITE). |
| 9 | + |
| 10 | +-include_lib("common_test/include/ct.hrl"). |
| 11 | +-include_lib("proper/include/proper.hrl"). |
| 12 | +-include_lib("eunit/include/eunit.hrl"). |
| 13 | + |
| 14 | +-compile(export_all). |
| 15 | + |
| 16 | +-define(ITERATIONS, 100). |
| 17 | + |
| 18 | +-define(USER, <<"upstream-validation-user">>). |
| 19 | +-define(PASSWORD, <<"upstream-validation-password">>). |
| 20 | +-define(ALLOWED_VHOST, <<"upstream-validation-allowed">>). |
| 21 | +-define(DENIED_VHOST, <<"upstream-validation-denied">>). |
| 22 | +-define(NONEXISTENT_VHOST, <<"upstream-validation-nonexistent">>). |
| 23 | + |
| 24 | +all() -> |
| 25 | + [ |
| 26 | + {group, tests} |
| 27 | + ]. |
| 28 | + |
| 29 | +groups() -> |
| 30 | + [ |
| 31 | + {tests, [], [ |
| 32 | + direct_uri_allowed, |
| 33 | + direct_uri_denied, |
| 34 | + direct_uri_nonexistent_vhost, |
| 35 | + direct_uri_no_user, |
| 36 | + network_uri_allowed, |
| 37 | + uri_list_denied, |
| 38 | + uri_list_allowed, |
| 39 | + map_uri_denied, |
| 40 | + uri_list_mixed_network_and_denied_direct, |
| 41 | + prop_uri_list_validation |
| 42 | + ]} |
| 43 | + ]. |
| 44 | + |
| 45 | +%% ------------------------------------------------------------------- |
| 46 | +%% Testsuite setup/teardown |
| 47 | +%% ------------------------------------------------------------------- |
| 48 | + |
| 49 | +init_per_suite(Config) -> |
| 50 | + rabbit_ct_helpers:log_environment(), |
| 51 | + Config1 = rabbit_ct_helpers:set_config(Config, [{rmq_nodename_suffix, ?MODULE}]), |
| 52 | + Config2 = rabbit_ct_helpers:run_setup_steps(Config1, |
| 53 | + rabbit_ct_broker_helpers:setup_steps() ++ |
| 54 | + rabbit_ct_client_helpers:setup_steps()), |
| 55 | + ok = rabbit_ct_broker_helpers:add_vhost(Config2, ?ALLOWED_VHOST), |
| 56 | + ok = rabbit_ct_broker_helpers:add_vhost(Config2, ?DENIED_VHOST), |
| 57 | + ok = rabbit_ct_broker_helpers:add_user(Config2, ?USER, ?PASSWORD), |
| 58 | + ok = rabbit_ct_broker_helpers:set_full_permissions(Config2, ?USER, ?ALLOWED_VHOST), |
| 59 | + Config2. |
| 60 | + |
| 61 | +end_per_suite(Config) -> |
| 62 | + rabbit_ct_broker_helpers:delete_user(Config, ?USER), |
| 63 | + rabbit_ct_broker_helpers:delete_vhost(Config, ?ALLOWED_VHOST), |
| 64 | + rabbit_ct_broker_helpers:delete_vhost(Config, ?DENIED_VHOST), |
| 65 | + rabbit_ct_helpers:run_teardown_steps(Config, |
| 66 | + rabbit_ct_client_helpers:teardown_steps() ++ |
| 67 | + rabbit_ct_broker_helpers:teardown_steps()). |
| 68 | + |
| 69 | +init_per_group(_, Config) -> |
| 70 | + Config. |
| 71 | + |
| 72 | +end_per_group(_, Config) -> |
| 73 | + Config. |
| 74 | + |
| 75 | +init_per_testcase(Testcase, Config) -> |
| 76 | + rabbit_ct_helpers:testcase_started(Config, Testcase). |
| 77 | + |
| 78 | +end_per_testcase(Testcase, Config) -> |
| 79 | + rabbit_ct_helpers:testcase_finished(Config, Testcase). |
| 80 | + |
| 81 | +%% ------------------------------------------------------------------- |
| 82 | +%% Testcases |
| 83 | +%% ------------------------------------------------------------------- |
| 84 | + |
| 85 | +direct_uri_allowed(Config) -> |
| 86 | + ?assertEqual([ok], validate_with_user(Config, direct_uri(?ALLOWED_VHOST))). |
| 87 | + |
| 88 | +direct_uri_denied(Config) -> |
| 89 | + ?assertMatch([{error, _, _}], validate_with_user(Config, direct_uri(?DENIED_VHOST))). |
| 90 | + |
| 91 | +direct_uri_nonexistent_vhost(Config) -> |
| 92 | + ?assertMatch([{error, _, _}], validate_with_user(Config, direct_uri(?NONEXISTENT_VHOST))). |
| 93 | + |
| 94 | +direct_uri_no_user(Config) -> |
| 95 | + ?assertEqual([ok], validate_with_none(Config, direct_uri(?DENIED_VHOST))). |
| 96 | + |
| 97 | +%% A network URI authenticates over the wire on connect, so it is unchecked here. |
| 98 | +network_uri_allowed(Config) -> |
| 99 | + ?assertEqual([ok], validate_with_user(Config, network_uri(?DENIED_VHOST))). |
| 100 | + |
| 101 | +uri_list_denied(Config) -> |
| 102 | + Uris = [direct_uri(?ALLOWED_VHOST), direct_uri(?DENIED_VHOST)], |
| 103 | + ?assertMatch([{error, _, _}], validate_with_user(Config, Uris)). |
| 104 | + |
| 105 | +uri_list_allowed(Config) -> |
| 106 | + Uris = [direct_uri(?ALLOWED_VHOST), direct_uri(?ALLOWED_VHOST)], |
| 107 | + ?assertEqual([ok], validate_with_user(Config, Uris)). |
| 108 | + |
| 109 | +map_uri_denied(Config) -> |
| 110 | + Term = #{<<"uri">> => direct_uri(?DENIED_VHOST)}, |
| 111 | + ?assertMatch([{error, _, _}], validate_term_with_user(Config, Term)). |
| 112 | + |
| 113 | +uri_list_mixed_network_and_denied_direct(Config) -> |
| 114 | + Uris = [network_uri(?DENIED_VHOST), direct_uri(?DENIED_VHOST)], |
| 115 | + ?assertMatch([{error, _, _}], validate_with_user(Config, Uris)). |
| 116 | + |
| 117 | +%% ------------------------------------------------------------------- |
| 118 | +%% Property-based test |
| 119 | +%% ------------------------------------------------------------------- |
| 120 | + |
| 121 | +%% Runs on the broker node so each example is a local call, not an RPC. |
| 122 | +prop_uri_list_validation(Config) -> |
| 123 | + rabbit_ct_broker_helpers:rpc(Config, ?MODULE, run_prop_uri_list_validation, []). |
| 124 | + |
| 125 | +run_prop_uri_list_validation() -> |
| 126 | + {ok, User} = rabbit_access_control:check_user_login(?USER, [{password, ?PASSWORD}]), |
| 127 | + rabbit_ct_proper_helpers:run_proper( |
| 128 | + fun() -> |
| 129 | + ?FORALL( |
| 130 | + Choices, non_empty(list(oneof([allowed, denied]))), |
| 131 | + begin |
| 132 | + Uris = [direct_uri(vhost_for(Choice)) || Choice <- Choices], |
| 133 | + Result = validate(Uris, User), |
| 134 | + ExpectAllowed = lists:all(fun(C) -> C =:= allowed end, Choices), |
| 135 | + outcome(Result) =:= (case ExpectAllowed of |
| 136 | + true -> allowed; |
| 137 | + false -> denied |
| 138 | + end) |
| 139 | + end) |
| 140 | + end, [], ?ITERATIONS). |
| 141 | + |
| 142 | +outcome([ok]) -> allowed; |
| 143 | +outcome([{error, _, _}]) -> denied. |
| 144 | + |
| 145 | +%% ------------------------------------------------------------------- |
| 146 | +%% Helpers |
| 147 | +%% ------------------------------------------------------------------- |
| 148 | + |
| 149 | +vhost_for(allowed) -> ?ALLOWED_VHOST; |
| 150 | +vhost_for(denied) -> ?DENIED_VHOST; |
| 151 | +vhost_for(nonexistent) -> ?NONEXISTENT_VHOST. |
| 152 | + |
| 153 | +direct_uri(VHost) -> |
| 154 | + <<"amqp:///", VHost/binary>>. |
| 155 | + |
| 156 | +network_uri(VHost) -> |
| 157 | + <<"amqp://localhost:5672/", VHost/binary>>. |
| 158 | + |
| 159 | +validate_with_user(Config, Uris) -> |
| 160 | + validate_term_with_user(Config, [{<<"uri">>, Uris}]). |
| 161 | + |
| 162 | +validate_term_with_user(Config, Term) -> |
| 163 | + rabbit_ct_broker_helpers:rpc(Config, ?MODULE, validate_term_with_user_1, [Term]). |
| 164 | + |
| 165 | +validate_term_with_user_1(Term) -> |
| 166 | + {ok, User} = rabbit_access_control:check_user_login(?USER, [{password, ?PASSWORD}]), |
| 167 | + validate_term(Term, User). |
| 168 | + |
| 169 | +validate_with_none(Config, Uris) -> |
| 170 | + rabbit_ct_broker_helpers:rpc(Config, ?MODULE, validate_with_none_1, [Uris]). |
| 171 | + |
| 172 | +validate_with_none_1(Uris) -> |
| 173 | + validate(Uris, none). |
| 174 | + |
| 175 | +validate(Uris, User) -> |
| 176 | + validate_term([{<<"uri">>, Uris}], User). |
| 177 | + |
| 178 | +validate_term(Term, User) -> |
| 179 | + rabbit_federation_parameters:validate( |
| 180 | + <<"/">>, <<"federation-upstream">>, <<"a-name">>, Term, User). |
0 commit comments