Skip to content

Commit 88fad63

Browse files
committed
Add tests
1 parent dcb6f98 commit 88fad63

File tree

2 files changed

+100
-21
lines changed

2 files changed

+100
-21
lines changed

deps/rabbitmq_amqp_client/src/rabbitmq_amqp_client.erl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
declare_exchange/3,
2929
bind_exchange/5,
3030
unbind_exchange/5,
31-
delete_exchange/2
31+
delete_exchange/2,
32+
33+
set_token/2
3234
].
3335

3436
-define(TIMEOUT, 20_000).
@@ -381,6 +383,23 @@ delete_exchange(LinkPair, ExchangeName) ->
381383
Err
382384
end.
383385

386+
%% Renew OAuth 2.0 token.
387+
-spec set_token(link_pair(), binary()) ->
388+
ok | {error, term()}.
389+
set_token(LinkPair, Token) ->
390+
Props = #{subject => <<"PUT">>,
391+
to => <<"/auth/tokens">>},
392+
Body = {binary, Token},
393+
case request(LinkPair, Props, Body) of
394+
{ok, Resp} ->
395+
case is_success(Resp) of
396+
true -> ok;
397+
false -> {error, Resp}
398+
end;
399+
Err ->
400+
Err
401+
end.
402+
384403
-spec request(link_pair(), amqp10_msg:amqp10_properties(), amqp10_prim()) ->
385404
{ok, Response :: amqp10_msg:amqp10_msg()} | {error, term()}.
386405
request(#link_pair{session = Session,

deps/rabbitmq_auth_backend_oauth2/test/system_SUITE.erl

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ groups() ->
4646
more_than_one_resource_server_id_not_allowed_in_one_token,
4747
mqtt_expired_token,
4848
mqtt_expirable_token,
49-
web_mqtt_expirable_token,
50-
amqp_expirable_token
49+
web_mqtt_expirable_token
5150
]},
5251

5352
{token_refresh, [], [
@@ -73,7 +72,18 @@ groups() ->
7372
]},
7473
{rich_authorization_requests, [], [
7574
test_successful_connection_with_rich_authorization_request_token
76-
]}
75+
]},
76+
{amqp, [shuffle],
77+
[
78+
amqp_token_expire,
79+
amqp_token_refresh,
80+
amqp_token_refresh_vhost_permission,
81+
amqp_token_refresh_exchange_write_permission,
82+
amqp_token_refresh_queue_read_permission,
83+
amqp_token_refresh_anon_term_exchange_write_permission,
84+
amqp_token_refresh_topic_write_permission,
85+
amqp_token_refresh_expire
86+
]}
7787
].
7888

7989
%%
@@ -100,7 +110,9 @@ init_per_suite(Config) ->
100110
end_per_suite(Config) ->
101111
rabbit_ct_helpers:run_teardown_steps(Config, rabbit_ct_broker_helpers:teardown_steps()).
102112

103-
113+
init_per_group(amqp, Config) ->
114+
{ok, _} = application:ensure_all_started(rabbitmq_amqp_client),
115+
Config;
104116
init_per_group(_Group, Config) ->
105117
%% The broker is managed by {init,end}_per_testcase().
106118
lists:foreach(fun(Value) ->
@@ -109,6 +121,8 @@ init_per_group(_Group, Config) ->
109121
[<<"vhost1">>, <<"vhost2">>, <<"vhost3">>, <<"vhost4">>]),
110122
Config.
111123

124+
end_per_group(amqp, Config) ->
125+
Config;
112126
end_per_group(_Group, Config) ->
113127
%% The broker is managed by {init,end}_per_testcase().
114128
lists:foreach(fun(Value) ->
@@ -500,29 +514,20 @@ mqtt_expirable_token0(Port, AdditionalOpts, Connect, Config) ->
500514
after Millis * 2 -> ct:fail("missing DISCONNECT packet from server")
501515
end.
502516

503-
amqp_expirable_token(Config) ->
504-
{ok, _} = application:ensure_all_started(rabbitmq_amqp_client),
505-
517+
%% Test that RabbitMQ closes the AMQP 1.0 connection when the token expires.
518+
amqp_token_expire(Config) ->
506519
Seconds = 4,
507520
Millis = Seconds * 1000,
508521
{_Algo, Token} = generate_expirable_token(Config,
509-
[<<"rabbitmq.configure:*/*">>,
510-
<<"rabbitmq.write:*/*">>,
511-
<<"rabbitmq.read:*/*">>],
522+
[<<"rabbitmq.configure:%2F/*">>,
523+
<<"rabbitmq.write:%2F/*">>,
524+
<<"rabbitmq.read:%2F/*">>],
512525
Seconds),
513526

514-
%% Send and receive a message via AMQP 1.0.
527+
%% Send and receive a message.
528+
{Connection, Session, LinkPair} = amqp_init(Token, Config),
515529
QName = atom_to_binary(?FUNCTION_NAME),
516530
Address = rabbitmq_amqp_address:queue(QName),
517-
Host = ?config(rmq_hostname, Config),
518-
Port = rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_port_amqp),
519-
OpnConf = #{address => Host,
520-
port => Port,
521-
container_id => <<"my container">>,
522-
sasl => {plain, <<"">>, Token}},
523-
{ok, Connection} = amqp10_client:open_connection(OpnConf),
524-
{ok, Session} = amqp10_client:begin_session_sync(Connection),
525-
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(Session, <<"my link pair">>),
526531
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
527532
{ok, Sender} = amqp10_client:attach_sender_link(Session, <<"my sender">>, Address),
528533
receive {amqp10_event, {link, Sender, credited}} -> ok
@@ -544,6 +549,43 @@ amqp_expirable_token(Config) ->
544549
ct:fail("server did not close our connection")
545550
end.
546551

552+
amqp_token_refresh(Config) ->
553+
ok.
554+
555+
%% Test that RabbitMQ closes the AMQP 1.0 connection if the client
556+
%% submits a new token without any permission to the vhost.
557+
amqp_token_refresh_vhost_permission(Config) ->
558+
{_Algo, Token1} = generate_valid_token(Config),
559+
{Connection, Session, LinkPair} = amqp_init(Token1, Config),
560+
561+
{_Algo, Token2} = generate_valid_token(Config,
562+
[<<"rabbitmq.configure:wrongvhost/*">>,
563+
<<"rabbitmq.write:wrongvhost/*">>,
564+
<<"rabbitmq.read:wrongvhost/*">>]),
565+
ok = rabbitmq_amqp_client:set_token(LinkPair, Token2),
566+
receive {amqp10_event,
567+
{connection, Connection,
568+
{closed, {unauthorized_access, Reason}}}} ->
569+
?assertMatch(<<"access to vhost / failed for new credential:", _/binary>>,
570+
Reason)
571+
after 5000 -> ct:fail({missing_event, ?LINE})
572+
end.
573+
574+
amqp_token_refresh_exchange_write_permission(Config) ->
575+
ok.
576+
577+
amqp_token_refresh_queue_read_permission(Config) ->
578+
ok.
579+
580+
amqp_token_refresh_anon_term_exchange_write_permission(Config) ->
581+
ok.
582+
583+
amqp_token_refresh_topic_write_permission(Config) ->
584+
ok.
585+
586+
amqp_token_refresh_expire(Config) ->
587+
ok.
588+
547589
test_successful_connection_with_complex_claim_as_a_map(Config) ->
548590
{_Algo, Token} = generate_valid_token_with_extra_fields(
549591
Config,
@@ -765,3 +807,21 @@ test_failed_connection_with_non_existent_scope_alias_in_scope_field(Config) ->
765807
more_than_one_resource_server_id_not_allowed_in_one_token(Config) ->
766808
{_Algo, Token} = generate_valid_token(Config, <<"rmq.configure:*/*">>, [<<"prod">>, <<"dev">>]),
767809
{error, _} = open_unmanaged_connection(Config, 0, <<"username">>, Token).
810+
811+
amqp_init(Token, Config) ->
812+
OpnConf = amqp_connection_config(Token, Config),
813+
{ok, Connection} = amqp10_client:open_connection(OpnConf),
814+
receive {amqp10_event, {connection, Connection, opened}} -> ok
815+
after 5000 -> ct:fail({missing_event, ?LINE})
816+
end,
817+
{ok, Session} = amqp10_client:begin_session_sync(Connection),
818+
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(Session, <<"my link pair">>),
819+
{Connection, Session, LinkPair}.
820+
821+
amqp_connection_config(Token, Config) ->
822+
Host = proplists:get_value(rmq_hostname, Config),
823+
Port = rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_port_amqp),
824+
#{address => Host,
825+
port => Port,
826+
container_id => <<"my container">>,
827+
sasl => {plain, <<>>, Token}}.

0 commit comments

Comments
 (0)