Skip to content

Commit 7f1952c

Browse files
Test translation function of scope_aliases
1 parent 8562e4d commit 7f1952c

File tree

2 files changed

+112
-49
lines changed

2 files changed

+112
-49
lines changed

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ extract_oauth_providers_signing_keys(Settings) ->
210210
KeyFun = fun extract_key_as_binary/1,
211211

212212
IndexedSigningKeys = [{Name, {list_to_binary(Kid), list_to_binary(V)}} ||
213-
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "signing_keys", Kid], V}
213+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, ?SIGNING_KEYS, Kid], V}
214214
<- Settings ],
215215
maps:map(fun(_K,V)-> [{signing_keys, translate_list_of_signing_keys(V)}] end,
216216
maps:groups_from_list(KeyFun, fun({_, V}) -> V end, IndexedSigningKeys)).

deps/rabbitmq_auth_backend_oauth2/test/rabbit_oauth2_schema_SUITE.erl

Lines changed: 111 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ all() ->
2727
test_without_resource_servers,
2828
test_with_one_resource_server,
2929
test_with_many_resource_servers,
30-
test_resource_servers_attributes
30+
test_resource_servers_attributes,
31+
test_scope_aliases
3132

3233
].
3334

@@ -39,21 +40,27 @@ test_without_resource_servers(_) ->
3940
#{} = rabbit_oauth2_schema:translate_resource_servers([]).
4041

4142
test_with_one_oauth_provider(_) ->
42-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://rabbit"}
43-
],
43+
Conf = [
44+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
45+
"https://rabbit"}
46+
],
4447
#{<<"keycloak">> := [{issuer, <<"https://rabbit">>}]
4548
} = rabbit_oauth2_schema:translate_oauth_providers(Conf).
4649

4750
test_with_one_resource_server(_) ->
48-
Conf = [{["auth_oauth2","resource_servers","rabbitmq1","id"],"rabbitmq1"}
49-
],
51+
Conf = [
52+
{["auth_oauth2","resource_servers","rabbitmq1","id"],"rabbitmq1"}
53+
],
5054
#{<<"rabbitmq1">> := [{id, <<"rabbitmq1">>}]
5155
} = rabbit_oauth2_schema:translate_resource_servers(Conf).
5256

5357
test_with_many_oauth_providers(_) ->
54-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
55-
{["auth_oauth2","oauth_providers","uaa","issuer"],"https://uaa"}
56-
],
58+
Conf = [
59+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
60+
"https://keycloak"},
61+
{["auth_oauth2","oauth_providers","uaa","issuer"],
62+
"https://uaa"}
63+
],
5764
#{<<"keycloak">> := [{issuer, <<"https://keycloak">>}
5865
],
5966
<<"uaa">> := [{issuer, <<"https://uaa">>}
@@ -62,31 +69,43 @@ test_with_many_oauth_providers(_) ->
6269

6370

6471
test_with_many_resource_servers(_) ->
65-
Conf = [{["auth_oauth2","resource_servers","rabbitmq1","id"],"rabbitmq1"},
66-
{["auth_oauth2","resource_servers","rabbitmq2","id"],"rabbitmq2"}
67-
],
72+
Conf = [
73+
{["auth_oauth2","resource_servers","rabbitmq1","id"],
74+
"rabbitmq1"},
75+
{["auth_oauth2","resource_servers","rabbitmq2","id"],
76+
"rabbitmq2"}
77+
],
6878
#{<<"rabbitmq1">> := [{id, <<"rabbitmq1">>}
6979
],
7080
<<"rabbitmq2">> := [{id, <<"rabbitmq2">>}
7181
]
7282
} = rabbit_oauth2_schema:translate_resource_servers(Conf).
7383

7484
test_oauth_providers_attributes(_) ->
75-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
76-
{["auth_oauth2","oauth_providers","keycloak","default_key"],"token-key"}
77-
],
85+
Conf = [
86+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
87+
"https://keycloak"},
88+
{["auth_oauth2","oauth_providers","keycloak","default_key"],
89+
"token-key"}
90+
],
7891
#{<<"keycloak">> := [{default_key, <<"token-key">>},
7992
{issuer, <<"https://keycloak">>}
8093
]
8194
} = sort_settings(rabbit_oauth2_schema:translate_oauth_providers(Conf)).
8295

8396
test_resource_servers_attributes(_) ->
84-
Conf = [{["auth_oauth2","resource_servers","rabbitmq1","id"],"rabbitmq1xxx"},
85-
{["auth_oauth2","resource_servers","rabbitmq1","scope_prefix"],"somescope."},
86-
{["auth_oauth2","resource_servers","rabbitmq1","additional_scopes_key"],"roles"},
87-
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","1"],"userid"},
88-
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","2"],"groupid"}
89-
],
97+
Conf = [
98+
{["auth_oauth2","resource_servers","rabbitmq1","id"],
99+
"rabbitmq1xxx"},
100+
{["auth_oauth2","resource_servers","rabbitmq1","scope_prefix"],
101+
"somescope."},
102+
{["auth_oauth2","resource_servers","rabbitmq1","additional_scopes_key"],
103+
"roles"},
104+
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","1"],
105+
"userid"},
106+
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","2"],
107+
"groupid"}
108+
],
90109
#{<<"rabbitmq1xxx">> := [{additional_scopes_key, <<"roles">>},
91110
{id, <<"rabbitmq1xxx">>},
92111
{preferred_username_claims, [<<"userid">>, <<"groupid">>]},
@@ -95,11 +114,15 @@ test_resource_servers_attributes(_) ->
95114
} = sort_settings(rabbit_oauth2_schema:translate_resource_servers(Conf)),
96115

97116
Conf2 = [
98-
{["auth_oauth2","resource_servers","rabbitmq1","scope_prefix"],"somescope."},
99-
{["auth_oauth2","resource_servers","rabbitmq1","additional_scopes_key"],"roles"},
100-
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","1"],"userid"},
101-
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","2"],"groupid"}
102-
],
117+
{["auth_oauth2","resource_servers","rabbitmq1","scope_prefix"],
118+
"somescope."},
119+
{["auth_oauth2","resource_servers","rabbitmq1","additional_scopes_key"],
120+
"roles"},
121+
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","1"],
122+
"userid"},
123+
{["auth_oauth2","resource_servers","rabbitmq1","preferred_username_claims","2"],
124+
"groupid"}
125+
],
103126
#{<<"rabbitmq1">> := [{additional_scopes_key, <<"roles">>},
104127
{id, <<"rabbitmq1">>},
105128
{preferred_username_claims, [<<"userid">>, <<"groupid">>]},
@@ -108,36 +131,52 @@ test_resource_servers_attributes(_) ->
108131
} = sort_settings(rabbit_oauth2_schema:translate_resource_servers(Conf2)).
109132

110133
test_oauth_providers_attributes_with_invalid_uri(_) ->
111-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"http://keycloak"},
112-
{["auth_oauth2","oauth_providers","keycloak","default_key"],"token-key"}
113-
],
134+
Conf = [
135+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
136+
"http://keycloak"},
137+
{["auth_oauth2","oauth_providers","keycloak","default_key"],
138+
"token-key"}
139+
],
114140
try sort_settings(rabbit_oauth2_schema:translate_oauth_providers(Conf)) of
115141
_ -> {throw, should_have_failed}
116142
catch
117143
_ -> ok
118144
end.
119145

120146
test_oauth_providers_algorithms(_) ->
121-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
122-
{["auth_oauth2","oauth_providers","keycloak","algorithms","2"],"HS256"},
123-
{["auth_oauth2","oauth_providers","keycloak","algorithms","1"],"RS256"}
124-
],
147+
Conf = [
148+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
149+
"https://keycloak"},
150+
{["auth_oauth2","oauth_providers","keycloak","algorithms","2"],
151+
"HS256"},
152+
{["auth_oauth2","oauth_providers","keycloak","algorithms","1"],
153+
"RS256"}
154+
],
125155
#{<<"keycloak">> := [{algorithms, [<<"RS256">>, <<"HS256">>]},
126156
{issuer, <<"https://keycloak">>}
127157
]
128158
} = sort_settings(rabbit_oauth2_schema:translate_oauth_providers(Conf)).
129159

130160
test_oauth_providers_https(Conf) ->
131161

132-
CuttlefishConf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
133-
{["auth_oauth2","oauth_providers","keycloak","https","verify"],verify_none},
134-
{["auth_oauth2","oauth_providers","keycloak","https","peer_verification"],verify_peer},
135-
{["auth_oauth2","oauth_providers","keycloak","https","depth"],2},
136-
{["auth_oauth2","oauth_providers","keycloak","https","hostname_verification"],wildcard},
137-
{["auth_oauth2","oauth_providers","keycloak","https","crl_check"],false},
138-
{["auth_oauth2","oauth_providers","keycloak","https","fail_if_no_peer_cert"],true},
139-
{["auth_oauth2","oauth_providers","keycloak","https","cacertfile"],cert_filename(Conf)}
140-
],
162+
CuttlefishConf = [
163+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
164+
"https://keycloak"},
165+
{["auth_oauth2","oauth_providers","keycloak","https","verify"],
166+
verify_none},
167+
{["auth_oauth2","oauth_providers","keycloak","https","peer_verification"],
168+
verify_peer},
169+
{["auth_oauth2","oauth_providers","keycloak","https","depth"],
170+
2},
171+
{["auth_oauth2","oauth_providers","keycloak","https","hostname_verification"],
172+
wildcard},
173+
{["auth_oauth2","oauth_providers","keycloak","https","crl_check"],
174+
false},
175+
{["auth_oauth2","oauth_providers","keycloak","https","fail_if_no_peer_cert"],
176+
true},
177+
{["auth_oauth2","oauth_providers","keycloak","https","cacertfile"],
178+
cert_filename(Conf)}
179+
],
141180
#{<<"keycloak">> := [{https, [{verify, verify_none},
142181
{peer_verification, verify_peer},
143182
{depth, 2},
@@ -152,20 +191,27 @@ test_oauth_providers_https(Conf) ->
152191

153192
test_oauth_providers_https_with_missing_cacertfile(_) ->
154193

155-
Conf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
156-
{["auth_oauth2","oauth_providers","keycloak","https","cacertfile"],"/non-existent.pem"}
157-
],
194+
Conf = [
195+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
196+
"https://keycloak"},
197+
{["auth_oauth2","oauth_providers","keycloak","https","cacertfile"],
198+
"/non-existent.pem"}
199+
],
158200
try sort_settings(rabbit_oauth2_schema:translate_oauth_providers(Conf)) of
159201
_ -> {throw, should_have_failed}
160202
catch
161203
_ -> ok
162204
end.
163205

164206
test_oauth_providers_signing_keys(Conf) ->
165-
CuttlefishConf = [{["auth_oauth2","oauth_providers","keycloak","issuer"],"https://keycloak"},
166-
{["auth_oauth2","oauth_providers","keycloak","signing_keys","2"], cert_filename(Conf)},
167-
{["auth_oauth2","oauth_providers","keycloak","signing_keys","1"], cert_filename(Conf)}
168-
],
207+
CuttlefishConf = [
208+
{["auth_oauth2","oauth_providers","keycloak","issuer"],
209+
"https://keycloak"},
210+
{["auth_oauth2","oauth_providers","keycloak","signing_keys","2"],
211+
cert_filename(Conf)},
212+
{["auth_oauth2","oauth_providers","keycloak","signing_keys","1"],
213+
cert_filename(Conf)}
214+
],
169215
#{<<"keycloak">> := [{issuer, <<"https://keycloak">>},
170216
{signing_keys, SigningKeys}
171217
]
@@ -175,6 +221,23 @@ test_oauth_providers_signing_keys(Conf) ->
175221
<<"2">> := {pem, <<"I'm not a certificate">>}
176222
} = SigningKeys.
177223

224+
test_scope_aliases(_) ->
225+
CuttlefishConf = [
226+
{["auth_oauth2","scope_aliases","1","alias"],
227+
"admin"},
228+
{["auth_oauth2","scope_aliases","1","scope"],
229+
"rabbitmq.tag:administrator"},
230+
{["auth_oauth2","scope_aliases","2","alias"],
231+
"developer"},
232+
{["auth_oauth2","scope_aliases","2","scope"],
233+
"rabbitmq.tag:management rabbitmq.read:*/*"}
234+
],
235+
#{
236+
<<"admin">> := [<<"rabbitmq.tag:administrator">>],
237+
<<"developer">> := [<<"rabbitmq.tag:management">>, <<"rabbitmq.read:*/*">>]
238+
} = rabbit_oauth2_schema:translate_scope_aliases(CuttlefishConf).
239+
240+
178241
cert_filename(Conf) ->
179242
string:concat(?config(data_dir, Conf), "certs/cert.pem").
180243

0 commit comments

Comments
 (0)