|
| 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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | +-module(rabbit_auth_backend_internal_loopback_SUITE). |
| 8 | + |
| 9 | +-include_lib("common_test/include/ct.hrl"). |
| 10 | +-include_lib("eunit/include/eunit.hrl"). |
| 11 | + |
| 12 | +-compile(export_all). |
| 13 | + |
| 14 | +-define(NO_SOCKET_OR_ADDRESS_REJECTION_MESSAGE, |
| 15 | + "user '~ts' attempted to log in, but no socket or address was provided " |
| 16 | + "to the internal_loopback auth backend, so cannot verify if connection " |
| 17 | + "is from localhost or not."). |
| 18 | + |
| 19 | +-define(NOT_LOOPBACK_REJECTION_MESSAGE, |
| 20 | + "user '~ts' attempted to log in, but the socket or address was not from " |
| 21 | + "loopback/localhost, which is prohibited by the internal loopback authN " |
| 22 | + "backend."). |
| 23 | + |
| 24 | +-define(LOOPBACK_USER, #{username => <<"TestLoopbackUser">>, |
| 25 | + password => <<"TestLoopbackUser">>, |
| 26 | + expected_credentials => [username, password], |
| 27 | + tags => [policymaker, monitoring]}). |
| 28 | + |
| 29 | +-define(NONLOOPBACK_USER, #{username => <<"TestNonLoopbackUser">>, |
| 30 | + password => <<"TestNonLoopbackUser">>, |
| 31 | + expected_credentials => [username, password], |
| 32 | + tags => [policymaker, monitoring]}). |
| 33 | +-define(LOCALHOST_ADDR, {127,0,0,1}). |
| 34 | +-define(NONLOCALHOST_ADDR, {192,168,1,1}). |
| 35 | + |
| 36 | +all() -> |
| 37 | + [ |
| 38 | + {group, localhost_connection}, |
| 39 | + {group, nonlocalhost_connection} |
| 40 | + ]. |
| 41 | + |
| 42 | +groups() -> |
| 43 | + [ |
| 44 | + {localhost_connection, [], [ |
| 45 | + login_from_localhost_with_loopback_user, |
| 46 | + login_from_localhost_with_nonloopback_user |
| 47 | + ]}, |
| 48 | + {nonlocalhost_connection, [], [ |
| 49 | + login_from_nonlocalhost_with_loopback_user, |
| 50 | + login_from_nonlocalhost_with_nonloopback_user |
| 51 | + ]} |
| 52 | + ]. |
| 53 | + |
| 54 | +init_per_suite(Config) -> |
| 55 | + rabbit_ct_helpers:log_environment(), |
| 56 | + rabbit_ct_helpers:run_setup_steps(Config, rabbit_ct_broker_helpers:setup_steps() ++ [ fun setup_env/1 ]). |
| 57 | + |
| 58 | +setup_env(Config) -> |
| 59 | + application:set_env(rabbit, auth_backends, [rabbit_auth_backend_internal_loopback]), |
| 60 | + Config. |
| 61 | + |
| 62 | +end_per_suite(Config) -> |
| 63 | + rabbit_ct_helpers:run_teardown_steps(Config, rabbit_ct_broker_helpers:teardown_steps()). |
| 64 | + |
| 65 | +init_per_group(localhost_connection, Config) -> |
| 66 | + ok = rabbit_ct_broker_helpers:add_user(Config, maps:get(username, ?LOOPBACK_USER)), |
| 67 | + ok = rabbit_ct_broker_helpers:add_user(Config, maps:get(username, ?NONLOOPBACK_USER)), |
| 68 | + [{sockOrAddr, ?LOCALHOST_ADDR} | Config]; |
| 69 | +init_per_group(nonlocalhost_connection, Config) -> |
| 70 | + [{sockOrAddr, ?NONLOCALHOST_ADDR} | Config]; |
| 71 | +init_per_group(_, Config) -> |
| 72 | + Config. |
| 73 | + |
| 74 | +end_per_group(_, Config) -> |
| 75 | + Config. |
| 76 | + |
| 77 | +% Test cases for localhost connections |
| 78 | +login_from_localhost_with_loopback_user(Config) -> |
| 79 | + AuthProps = build_auth_props(maps:get(password, ?LOOPBACK_USER), ?LOCALHOST_ADDR), |
| 80 | + {ok, _AuthUser} = rpc(Config, rabbit_auth_backend_internal_loopback, user_login_authentication, |
| 81 | + [maps:get(username, ?LOOPBACK_USER), AuthProps]). |
| 82 | + |
| 83 | +login_from_localhost_with_nonloopback_user(Config) -> |
| 84 | + AuthProps = build_auth_props(maps:get(password, ?NONLOOPBACK_USER), ?LOCALHOST_ADDR), |
| 85 | + {ok, _AuthUser} = rpc(Config, rabbit_auth_backend_internal_loopback, user_login_authentication, |
| 86 | + [maps:get(username, ?NONLOOPBACK_USER), AuthProps]). |
| 87 | + |
| 88 | +% Test cases for non-localhost connections |
| 89 | +login_from_nonlocalhost_with_loopback_user(Config) -> |
| 90 | + AuthProps = build_auth_props(maps:get(password, ?LOOPBACK_USER), ?NONLOCALHOST_ADDR), |
| 91 | + {refused, _FailMsg, _FailArgs} = rpc(Config, rabbit_auth_backend_internal_loopback, user_login_authentication, |
| 92 | + [maps:get(username, ?LOOPBACK_USER), AuthProps]). |
| 93 | + |
| 94 | +login_from_nonlocalhost_with_nonloopback_user(Config) -> |
| 95 | + AuthProps = build_auth_props(maps:get(password, ?NONLOOPBACK_USER), ?NONLOCALHOST_ADDR), |
| 96 | + {refused, _FailMsg, _FailArgs} = rpc(Config, rabbit_auth_backend_internal_loopback, user_login_authentication, |
| 97 | + [maps:get(username, ?NONLOOPBACK_USER), AuthProps]). |
| 98 | + |
| 99 | +rpc(Config, M, F, A) -> |
| 100 | + rabbit_ct_broker_helpers:rpc(Config, 0, M, F, A). |
| 101 | + |
| 102 | +build_auth_props(Pass, Socket) -> |
| 103 | + [{password, Pass}, {sockOrAddr, Socket}]. |
0 commit comments