Skip to content

Commit 7543837

Browse files
Add var expansion to vhost and resource access
1 parent bac204d commit 7543837

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,25 @@ user_login_authorization(Username, AuthProps) ->
8787
check_vhost_access(#auth_user{impl = DecodedTokenFun},
8888
VHost, _AuthzData) ->
8989
with_decoded_token(DecodedTokenFun(),
90-
fun(_Token) ->
90+
fun(Token) ->
9191
DecodedToken = DecodedTokenFun(),
92-
Scopes = get_scope(DecodedToken),
93-
ScopeString = rabbit_oauth2_scope:concat_scopes(Scopes, ","),
94-
rabbit_log:debug("Matching virtual host '~ts' against the following scopes: ~ts", [VHost, ScopeString]),
92+
Scopes = get_expanded_scopes(Token, #resource{virtual_host = VHost}),
9593
rabbit_oauth2_scope:vhost_access(VHost, Scopes)
9694
end).
9795

9896
check_resource_access(#auth_user{impl = DecodedTokenFun},
9997
Resource, Permission, _AuthzContext) ->
10098
with_decoded_token(DecodedTokenFun(),
10199
fun(Token) ->
102-
Scopes = get_scope(Token),
100+
Scopes = get_expanded_scopes(Token, Resource),
103101
rabbit_oauth2_scope:resource_access(Resource, Permission, Scopes)
104102
end).
105103

106104
check_topic_access(#auth_user{impl = DecodedTokenFun},
107105
Resource, Permission, Context) ->
108106
with_decoded_token(DecodedTokenFun(),
109107
fun(Token) ->
110-
Scopes = get_expanded_scopes(Token, Resource),
108+
Scopes = get_expanded_scopes(Token, Resource),
111109
rabbit_oauth2_scope:topic_access(Resource, Permission, Context, Scopes)
112110
end).
113111

deps/rabbitmq_auth_backend_oauth2/test/unit_SUITE.erl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -785,25 +785,35 @@ test_successful_access_with_a_token_that_uses_single_scope_alias_with_var_expans
785785
Alias = <<"client-alias-1">>,
786786
set_env(scope_aliases, #{
787787
Alias => [
788-
<<"rabbitmq.configure:{vhost}/q-{sub}/{client_id}**">>
789-
]
788+
<<"rabbitmq.configure:{vhost}/q-{sub}/rk-{client_id}**">>
789+
]
790790
}),
791791

792792
VHost = <<"vhost">>,
793793
Username = <<"bob">>,
794+
ClientId = <<"rmq">>,
794795
Token = ?UTIL_MOD:sign_token_hs(?UTIL_MOD:token_with_sub(
795796
?UTIL_MOD:token_with_claim(
796-
?UTIL_MOD:token_with_scope_alias_in_scope_field(Alias), <<"client_id">>, <<"rmq">>),
797+
?UTIL_MOD:token_with_scope_alias_in_scope_field(Alias), <<"client_id">>, ClientId),
797798
Username), Jwk),
798799

799800
{ok, #auth_user{username = Username} = AuthUser} =
800801
user_login_authentication(Username, [{password, Token}]),
801-
802-
assert_topic_access_refused(AuthUser, VHost, <<"q-bob">>, read,
803-
#{routing_key => <<"rmq/#">>}),
802+
803+
%% vhost access
804+
assert_vhost_access_granted(AuthUser, ClientId),
805+
806+
%% resource access
807+
assert_resource_access_denied(AuthUser, VHost, <<"none">>, read),
808+
assert_resource_access_granted(AuthUser, VHost, <<"q-bob">>, configure),
809+
810+
%% topic access
811+
assert_topic_access_refused(AuthUser, VHost, <<"q-bob">>, configure,
812+
#{routing_key => <<"rk-r2mq/#">>}),
804813
assert_topic_access_granted(AuthUser, VHost, <<"q-bob">>, configure,
805-
#{routing_key => <<"rmq/#">>}),
814+
#{routing_key => <<"rk-rmq/#">>}),
806815

816+
807817
application:unset_env(rabbitmq_auth_backend_oauth2, scope_aliases),
808818
application:unset_env(rabbitmq_auth_backend_oauth2, key_config).
809819

0 commit comments

Comments
 (0)