Skip to content

Commit 007ee6b

Browse files
aaron-seomergify[bot]
authored andcommitted
Add test suite for rabbitmq_auth_backend_internal_loopback
(cherry picked from commit 614ce25)
1 parent c48c2ad commit 007ee6b

File tree

4 files changed

+106
-9
lines changed

4 files changed

+106
-9
lines changed

deps/rabbitmq_auth_backend_internal_loopback/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ define PROJECT_APP_EXTRA_KEYS
1212
{broker_version_requirements, []}
1313
endef
1414

15-
LOCAL_DEPS = ssl inets crypto public_key
16-
DEPS = rabbit_common rabbit amqp_client
17-
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers cowboy
15+
DEPS = rabbit_common rabbit
16+
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers
1817

1918
DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk
2019
DEP_PLUGINS = rabbit_common/mk/rabbitmq-plugin.mk

deps/rabbitmq_auth_backend_internal_loopback/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ for RabbitMQ for basic authentication for only (loopback) localhost connections.
55

66
## Installation
77

8-
As of 4.1.0, this plugin is distributed with RabbitMQ. Enable it with
8+
As of 4.1.1, this plugin is distributed with RabbitMQ. Enable it with
99

1010
rabbitmq-plugins enable rabbitmq_auth_backend_internal_loopback
1111

deps/rabbitmq_auth_backend_internal_loopback/src/rabbit_auth_backend_internal_loopback.erl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@
4646

4747
-export([hashing_module_for_user/1, expand_topic_permission/2]).
4848

49-
-ifdef(TEST).
50-
-export([extract_user_permission_params/2,
51-
extract_topic_permission_params/2]).
52-
-endif.
53-
5449
-import(rabbit_data_coercion, [to_atom/1, to_list/1, to_binary/1]).
5550

5651
%%----------------------------------------------------------------------------
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)