Skip to content

Commit cd67b65

Browse files
Fix all test in unit_SUITE
1 parent 006e735 commit cd67b65

File tree

6 files changed

+141
-144
lines changed

6 files changed

+141
-144
lines changed

deps/rabbitmq_auth_backend_oauth2/app.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def all_srcs(name = "all_srcs"):
105105
"src/resource_server.erl",
106106
"src/oauth2_schema.erl",
107107
"src/rar.erl",
108-
"src/keycloak.erl",
108+
"src/keycloak.erl",
109109
"src/rabbit_oauth2_scope.erl",
110110
"src/uaa_jwks.erl",
111111
"src/uaa_jwt.erl",
@@ -233,9 +233,11 @@ def test_suite_beam_files(name = "test_suite_beam_files"):
233233
testonly = True,
234234
srcs = ["test/unit_SUITE.erl"],
235235
outs = ["test/unit_SUITE.beam"],
236+
hdrs = ["include/oauth2.hrl"],
236237
app_name = "rabbitmq_auth_backend_oauth2",
237238
erlc_opts = "//:test_erlc_opts",
238-
deps = ["//deps/rabbit_common:erlang_app"],
239+
deps = ["//deps/rabbit_common:erlang_app",
240+
"//deps/oauth2_client:erlang_app"],
239241
)
240242
erlang_bytecode(
241243
name = "wildcard_match_SUITE_beam_files",

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@
163163
%% auth_oauth2.resource_servers.rabbitmq.token_endpoint_params.audience
164164
%% auth_oauth2.resource_servers.rabbitmq.jkws_uri_params.appId =
165165

166-
{mapping,
167-
"auth_oauth2.authorization_endpoint_params.$param",
168-
"rabbitmq_auth_backend_oauth2.oauth_providers",
169-
[{datatype, string}]}.
170-
171-
{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
172-
fun(Conf) ->
173-
oauth2_schema:translate_authorization_endpoint_params(Conf)
174-
end}.
166+
%%{mapping,
167+
%% "auth_oauth2.authorization_endpoint_params.$param",
168+
%% "rabbitmq_auth_backend_oauth2.oauth_providers",
169+
%% [{datatype, string}]}.
170+
171+
%%{translation, "rabbitmq_auth_backend_oauth2.authorization_endpoint_params",
172+
%% fun(Conf) ->
173+
%% oauth2_schema:translate_authorization_endpoint_params(Conf)
174+
%% end}.
175175

176176
{mapping,
177177
"auth_oauth2.oauth_providers.$name.algorithms.$algorithm",

deps/rabbitmq_auth_backend_oauth2/src/oauth2_schema.erl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
-export([
1212
translate_oauth_providers/1,
1313
translate_resource_servers/1,
14-
translate_signing_keys/1,
15-
translate_authorization_endpoint_params/1
14+
translate_signing_keys/1 %,
15+
%%translate_authorization_endpoint_params/1
1616
]).
1717

1818
extract_key_as_binary({Name,_}) -> list_to_binary(Name).
@@ -64,10 +64,10 @@ translate_list_of_signing_keys(ListOfKidPath) ->
6464
end,
6565
maps:map(fun(_K, Path) -> {pem, TryReadingFileFun(Path)} end, maps:from_list(ListOfKidPath)).
6666

67-
-spec translate_authorization_endpoint_params([{list(), binary()}]) -> map().
68-
translate_authorization_endpoint_params(Conf) ->
69-
Params = cuttlefish_variable:filter_by_prefix("auth_oauth2.authorization_endpoint_params", Conf),
70-
lists:map(fun({Id, Value}) -> {list_to_binary(lists:last(Id)), Value} end, Params).
67+
%%-spec translate_authorization_endpoint_params([{list(), binary()}]) -> map().
68+
%%translate_authorization_endpoint_params(Conf) ->
69+
%% Params = cuttlefish_variable:filter_by_prefix("auth_oauth2.authorization_endpoint_params", Conf),
70+
%% lists:map(fun({Id, Value}) -> {list_to_binary(lists:last(Id)), Value} end, Params).
7171

7272
validator_file_exists(Attr, Filename) ->
7373
case file:read_file(Filename) of

deps/rabbitmq_auth_backend_oauth2/src/rabbit_auth_backend_oauth2.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ authenticate(_, AuthProps0) ->
133133
Token = token_from_context(AuthProps),
134134
case resolve_resource_server(Token) of
135135
{error, _} = Err0 ->
136-
Err0;
136+
{refused, "Authentication using OAuth 2/JWT token failed: ~tp", [Err0]};
137137
{ResourceServer, _} = Tuple ->
138138
case check_token(Token, Tuple) of
139139
{error, _} = E -> E;
@@ -189,7 +189,7 @@ check_token(Token, {ResourceServer, InternalOAuthProvider}) ->
189189
case decode_and_verify(Token, ResourceServer, InternalOAuthProvider) of
190190
{error, Reason} -> {refused, {error, Reason}};
191191
{true, Payload} -> {ok, normalize_token_scope(ResourceServer, Payload)};
192-
{false, _, _} -> {refused, signature_invalid}
192+
{false, _} -> {refused, signature_invalid}
193193
end.
194194

195195
-spec normalize_token_scope(

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_scope.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ parse_resource_pattern(Pattern, Permission) ->
9696
-spec filter_matching_scope_prefix_and_drop_it(list(), binary()|list()) -> list().
9797
filter_matching_scope_prefix_and_drop_it(Scopes, <<"">>) -> Scopes;
9898
filter_matching_scope_prefix_and_drop_it(Scopes, PrefixPattern) ->
99-
ct:log("filter_matching_scope_prefix_and_drop_it ~p ~p", [Scopes, PrefixPattern]),
10099
PatternLength = byte_size(PrefixPattern),
101100
lists:filtermap(
102101
fun(ScopeEl) ->

deps/rabbitmq_auth_backend_oauth2/test/unit_SUITE.erl

Lines changed: 120 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ all() ->
3636
test_token_expiration,
3737
test_invalid_signature,
3838
test_incorrect_kid,
39-
test_post_process_token_payload,
40-
test_post_process_token_payload_keycloak,
41-
test_post_process_payload_rich_auth_request,
42-
test_post_process_payload_rich_auth_request_using_regular_expression_with_cluster,
39+
normalize_token_scope_with_keycloak_scopes,
40+
normalize_token_scope_with_rich_auth_request,
41+
normalize_token_scope_with_rich_auth_request_using_regular_expression_with_cluster,
4342
test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_scope_field,
4443
test_unsuccessful_access_with_a_token_that_uses_missing_scope_alias_in_extra_scope_source_field,
4544
test_username_from,
@@ -62,7 +61,7 @@ groups() ->
6261
test_successful_authentication_without_scopes,
6362
test_successful_access_with_a_token_that_uses_single_scope_alias_in_extra_scope_source_field,
6463
test_successful_access_with_a_token_that_uses_multiple_scope_aliases_in_extra_scope_source_field,
65-
test_post_process_token_payload_complex_claims,
64+
normalize_token_scope_with_additional_scopes_complex_claims,
6665
test_successful_access_with_a_token_that_uses_single_scope_alias_in_scope_field_and_custom_scope_prefix
6766

6867
]}
@@ -121,70 +120,67 @@ end_per_group(_, Config) ->
121120
-define(DEFAULT_SCOPE_PREFIX, <<"rabbitmq.">>).
122121

123122

124-
test_post_process_token_payload_keycloak(_) ->
123+
normalize_token_scope_with_keycloak_scopes(_) ->
125124
Pairs = [
126125
%% common case
127-
{
128-
#{<<"permissions">> =>
129-
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
130-
<<"rsname">> => <<"allvhost">>,
131-
<<"scopes">> => [<<"rabbitmq-resource.read:*/*">>]},
132-
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
133-
<<"rsname">> => <<"vhost1">>,
134-
<<"scopes">> => [<<"rabbitmq-resource.write:vhost1/*">>]},
135-
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
136-
<<"rsname">> => <<"Default Resource">>,
137-
<<"scopes">> => [<<"unknown-resource.write:vhost1/*">>]}
138-
]
139-
},
140-
[<<"read:*/*">>, <<"write:vhost1/*">>]
141-
},
142-
143-
%% one scopes field with a string instead of an array
144-
{
145-
#{<<"permissions">> =>
146-
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
147-
<<"rsname">> => <<"allvhost">>,
148-
<<"scopes">> => <<"rabbitmq-resource.read:*/*">>},
149-
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
150-
<<"rsname">> => <<"vhost1">>,
151-
<<"scopes">> => [<<"unknown-resource-read">>]},
152-
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
153-
<<"rsname">> => <<"Default Resource">>}]},
154-
[<<"rabbitmq-resource.read:*/*">>]
126+
{
127+
"common case",
128+
#{<<"permissions">> =>
129+
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
130+
<<"rsname">> => <<"allvhost">>,
131+
<<"scopes">> => [<<"rabbitmq-resource.read:*/*">>]},
132+
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
133+
<<"rsname">> => <<"vhost1">>,
134+
<<"scopes">> => [<<"rabbitmq-resource.write:vhost1/*">>]},
135+
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
136+
<<"rsname">> => <<"Default Resource">>,
137+
<<"scopes">> => [<<"unknown-resource.write:vhost1/*">>]}
138+
]
155139
},
156-
157-
%% no scopes field in permissions
158-
{
159-
#{<<"permissions">> =>
160-
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
161-
<<"rsname">> => <<"allvhost">>},
162-
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
163-
<<"rsname">> => <<"vhost1">>},
164-
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
165-
<<"rsname">> => <<"Default Resource">>}]},
166-
[]
167-
},
168-
169-
%% no permissions
170-
{
171-
#{<<"permissions">> => []},
140+
[<<"read:*/*">>, <<"write:vhost1/*">>]
141+
},
142+
{
143+
"one scopes field with a string instead of an array",
144+
#{<<"permissions">> =>
145+
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
146+
<<"rsname">> => <<"allvhost">>,
147+
<<"scopes">> => <<"rabbitmq-resource.read:*/*">>},
148+
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
149+
<<"rsname">> => <<"vhost1">>,
150+
<<"scopes">> => [<<"unknown-resource-read">>]},
151+
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
152+
<<"rsname">> => <<"Default Resource">>}]},
153+
[<<"read:*/*">>]
154+
},
155+
{
156+
"no scopes field in permissions",
157+
#{<<"permissions">> =>
158+
[#{<<"rsid">> => <<"2c390fe4-02ad-41c7-98a2-cebb8c60ccf1">>,
159+
<<"rsname">> => <<"allvhost">>},
160+
#{<<"rsid">> => <<"e7f12e94-4c34-43d8-b2b1-c516af644cee">>,
161+
<<"rsname">> => <<"vhost1">>},
162+
#{<<"rsid">> => <<"12ac3d1c-28c2-4521-8e33-0952eff10bd9">>,
163+
<<"rsname">> => <<"Default Resource">>}]},
164+
[]
165+
},
166+
{
167+
"no permissions",
168+
#{<<"permissions">> => []},
172169
[]
173-
},
174-
%% missing permissions key
175-
{#{}, []}
170+
},
171+
{"missing permissions key", #{}, []}
176172
],
177-
lists:foreach(fun({Authorization, ExpectedScope}) ->
173+
174+
lists:foreach(fun({Case, Authorization, ExpectedScope}) ->
178175
ResourceServer = resource_server:new_resource_server(<<"rabbitmq-resource">>),
179176
Token0 = #{<<"authorization">> => Authorization},
180177
Token = normalize_token_scope(ResourceServer, Token0),
181-
?assertEqual(ExpectedScope, uaa_jwt:get_scope(Token))
178+
?assertEqual(ExpectedScope, uaa_jwt:get_scope(Token), Case)
182179
end, Pairs).
183180

184-
test_post_process_payload_rich_auth_request_using_regular_expression_with_cluster(_) ->
181+
normalize_token_scope_with_rich_auth_request_using_regular_expression_with_cluster(_) ->
185182

186183
Pairs = [
187-
188184
{ "should filter out those permisions whose locations do not refer to cluster : {resource_server_id}",
189185
[ #{<<"type">> => ?RESOURCE_SERVER_TYPE,
190186
<<"locations">> => [<<"cluster:rabbitmq-test">>],
@@ -230,7 +226,7 @@ test_post_process_payload_rich_auth_request_using_regular_expression_with_cluste
230226
lists:sort(uaa_jwt:get_scope(Token)), Case)
231227
end, Pairs).
232228

233-
test_post_process_payload_rich_auth_request(_) ->
229+
normalize_token_scope_with_rich_auth_request(_) ->
234230

235231
Pairs = [
236232
{ "should merge all permissions for the current cluster",
@@ -546,75 +542,75 @@ test_post_process_payload_rich_auth_request(_) ->
546542
?assertEqual(ExpectedScopes, ActualScopes, Case)
547543
end, Pairs).
548544

549-
test_post_process_token_payload_complex_claims(_) ->
545+
normalize_token_scope_with_additional_scopes_complex_claims(_) ->
550546
Pairs = [
551-
%% claims in form of binary
552-
{
553-
<<"rabbitmq.rabbitmq-resource.read:*/* rabbitmq.rabbitmq-resource-read">>,
554-
[<<"rabbitmq.rabbitmq-resource.read:*/*">>, <<"rabbitmq.rabbitmq-resource-read">>]
555-
},
556-
%% claims in form of binary - empty result
557-
{<<>>, []},
558-
%% claims in form of list
559-
{
560-
[<<"rabbitmq.rabbitmq-resource.read:*/*">>,
547+
{
548+
"claims in form of binary",
549+
<<"rabbitmq.rabbitmq-resource.read:*/* rabbitmq.rabbitmq-resource-read">>,
550+
[<<"read:*/*">>]
551+
},
552+
{"claims in form of binary - empty result", <<>>, []},
553+
{
554+
"claims in form of list",
555+
[<<"rabbitmq.rabbitmq-resource.read:*/*">>,
561556
<<"rabbitmq2.rabbitmq-resource-read">>],
562-
[<<"rabbitmq.rabbitmq-resource.read:*/*">>, <<"rabbitmq2.rabbitmq-resource-read">>]
563-
},
564-
%% claims in form of list - empty result
565-
{[], []},
566-
%% claims are map with list content
567-
{
568-
#{<<"rabbitmq">> =>
569-
[<<"rabbitmq-resource.read:*/*">>,
570-
<<"rabbitmq-resource-read">>],
571-
<<"rabbitmq3">> =>
572-
[<<"rabbitmq-resource.write:*/*">>,
573-
<<"rabbitmq-resource-write">>]},
574-
[<<"rabbitmq.rabbitmq-resource.read:*/*">>, <<"rabbitmq.rabbitmq-resource-read">>]
575-
},
576-
%% claims are map with list content - empty result
577-
{
578-
#{<<"rabbitmq2">> =>
579-
[<<"rabbitmq-resource.read:*/*">>,
580-
<<"rabbitmq-resource-read">>]},
581-
[]
582-
},
583-
%% claims are map with binary content
584-
{
585-
#{<<"rabbitmq">> => <<"rabbitmq-resource.read:*/* rabbitmq-resource-read">>,
586-
<<"rabbitmq3">> => <<"rabbitmq-resource.write:*/* rabbitmq-resource-write">>},
587-
[<<"rabbitmq.rabbitmq-resource.read:*/*">>, <<"rabbitmq.rabbitmq-resource-read">>]
588-
},
589-
%% claims are map with binary content - empty result
590-
{
591-
#{<<"rabbitmq2">> => <<"rabbitmq-resource.read:*/* rabbitmq-resource-read">>}, []
592-
},
593-
%% claims are map with empty binary content - empty result
594-
{
595-
#{<<"rabbitmq">> => <<>>}, []
596-
},
597-
%% claims are map with empty list content - empty result
598-
{
599-
#{<<"rabbitmq">> => []}, []
557+
[<<"read:*/*">>]
558+
},
559+
{"claims in form of list - empty result", [], []},
560+
{
561+
"claims are map with list content",
562+
#{<<"rabbitmq">> =>
563+
[<<"rabbitmq-resource.read:*/*">>,
564+
<<"rabbitmq-resource-read">>],
565+
<<"rabbitmq3">> =>
566+
[<<"rabbitmq-resource.write:*/*">>,
567+
<<"rabbitmq-resource-write">>]},
568+
[<<"read:*/*">>, <<"rabbitmq.rabbitmq-resource-read">>]
569+
},
570+
{
571+
"claims are map with list content - empty result",
572+
#{<<"rabbitmq2">> =>
573+
[<<"rabbitmq-resource.read:*/*">>,
574+
<<"rabbitmq-resource-read">>]},
575+
[]
576+
},
577+
{
578+
"claims are map with binary content",
579+
#{ <<"rabbitmq">> => <<"rabbitmq-resource.read:*/* rabbitmq-resource-read">>,
580+
<<"rabbitmq3">> => <<"rabbitmq-resource.write:*/* rabbitmq-resource-write">>},
581+
[<<"rabbitmq.rabbitmq-resource.read:*/*">>, <<"rabbitmq.rabbitmq-resource-read">>]
582+
},
583+
{
584+
"claims are map with binary content - empty result",
585+
#{<<"rabbitmq2">> => <<"rabbitmq-resource.read:*/* rabbitmq-resource-read">>}, []
586+
},
587+
{
588+
"claims are map with empty binary content - empty result",
589+
#{<<"rabbitmq">> => <<>>}, []
590+
},
591+
{
592+
"claims are map with empty list content - empty result",
593+
#{<<"rabbitmq">> => []}, []
594+
},
595+
{
596+
"no extra claims provided",
597+
[], []
598+
},
599+
{
600+
"no extra claims provided", #{}, []
601+
}],
602+
lists:foreach(fun({Case, Authorization, ExpectedScope0}) ->
603+
ResourceServer0 = resource_server:new_resource_server(?RESOURCE_SERVER_ID),
604+
ResourceServer = ResourceServer0#resource_server{
605+
scope_prefix = <<"rabbitmq.rabbitmq-resource.">>,
606+
additional_scopes_key = <<"custom-key">>
600607
},
601-
%% no extra claims provided
602-
{[], []},
603-
%% no extra claims provided
604-
{#{}, []}
605-
],
606-
lists:foreach(
607-
fun({Authorization, ExpectedScope}) ->
608-
Payload = post_process_payload_with_complex_claim_authorization(<<"rabbitmq-resource">>, Authorization),
609-
?assertEqual(ExpectedScope, maps:get(<<"scope">>, Payload))
610-
end, Pairs).
611-
612-
post_process_payload_with_complex_claim_authorization(ResourceServerId, Authorization) ->
613-
Jwk = ?UTIL_MOD:fixture_jwk(),
614-
Token = maps:put(<<"additional_rabbitmq_scopes">>, Authorization, ?UTIL_MOD:fixture_token_with_scopes([])),
615-
{_, EncodedToken} = ?UTIL_MOD:sign_token_hs(Token, Jwk),
616-
{true, Payload} = uaa_jwt_jwt:decode_and_verify(Jwk, EncodedToken),
617-
rabbit_auth_backend_oauth2:post_process_payload(ResourceServerId, Payload).
608+
Token0 = #{<<"custom-key">> => Authorization},
609+
Token = normalize_token_scope(ResourceServer, Token0),
610+
ExpectedScopes = lists:sort(ExpectedScope0),
611+
ActualScopes = lists:sort(uaa_jwt:get_scope(Token)),
612+
?assertEqual(ExpectedScopes, ActualScopes, Case)
613+
end, Pairs).
618614

619615
test_successful_authentication_without_scopes(_) ->
620616
Jwk = ?UTIL_MOD:fixture_jwk(),

0 commit comments

Comments
 (0)