Skip to content

Commit 9b946c9

Browse files
Include proxy_options into oauth_provider type
1 parent 263306e commit 9b946c9

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ ensure_oauth_provider_has_attributes(OAuthProvider, ListOfRequiredAttributes) ->
305305

306306
get_root_oauth_provider(ListOfRequiredAttributes) ->
307307
OAuthProvider = lookup_root_oauth_provider(),
308-
rabbit_log:debug("Using root oauth_provider ~p",
308+
ct:log("Using root oauth_provider ~p",
309309
[format_oauth_provider(OAuthProvider)]),
310310
case find_missing_attributes(OAuthProvider, ListOfRequiredAttributes) of
311311
[] ->
@@ -397,9 +397,32 @@ lookup_root_oauth_provider() ->
397397
token_endpoint = get_env(token_endpoint),
398398
authorization_endpoint = get_env(authorization_endpoint),
399399
end_session_endpoint = get_env(end_session_endpoint),
400-
ssl_options = extract_ssl_options_as_list(Map)
400+
ssl_options = extract_ssl_options_as_list(Map),
401+
proxy_options = extract_proxy_options(Map)
401402
}.
402403

404+
-spec extract_proxy_options(#{atom() => any()}|list()) -> proxy_options().
405+
extract_proxy_options(List) when is_list(List) ->
406+
case proplists:get_value(proxy, List, undefined) of
407+
undefined -> undefined;
408+
URL ->
409+
#proxy_options{
410+
proxy = URL,
411+
username = proplists:get_value(proxy_username, List, undefined),
412+
password = proplists:get_value(proxy_password, List, undefined)
413+
}
414+
end;
415+
extract_proxy_options(Map) ->
416+
case maps:get(proxy, Map, undefined) of
417+
undefined -> undefined;
418+
URL ->
419+
#proxy_options{
420+
proxy = URL,
421+
username = maps:get(proxy_username, Map, undefined),
422+
password = maps:get(proxy_password, Map, undefined)
423+
}
424+
end.
425+
403426
-spec extract_ssl_options_as_list(#{atom() => any()}) -> proplists:proplist().
404427
extract_ssl_options_as_list(Map) ->
405428
{Verify, CaCerts, CaCertFile} = case get_verify_or_peer_verification(Map, verify_peer) of
@@ -591,7 +614,9 @@ map_to_oauth_provider(PropList) when is_list(PropList) ->
591614
proplists:get_value(jwks_uri, PropList, undefined),
592615
ssl_options =
593616
extract_ssl_options_as_list(maps:from_list(
594-
proplists:get_value(https, PropList, [])))
617+
proplists:get_value(https, PropList, []))),
618+
proxy_options =
619+
extract_proxy_options(PropList)
595620
}.
596621
map_to_access_token_response(Code, Reason, Headers, Body) ->
597622
case decode_body(proplists:get_value("content-type", Headers, ?CONTENT_JSON), Body) of
@@ -629,6 +654,17 @@ format_ssl_options(TlsOptions) ->
629654
proplists:get_value(cacertfile, TlsOptions),
630655
CaCertsCount])).
631656

657+
-spec format_proxy_options(proxy_options()) -> string().
658+
format_proxy_options(undefined) ->
659+
lists:flatten(io_lib:format("{no proxy}", []));
660+
661+
format_proxy_options(ProxyOptions) ->
662+
lists:flatten(io_lib:format("{proxy: ~p, username: ~p, " ++
663+
"password: ~p }", [
664+
ProxyOptions#proxy_options.proxy,
665+
ProxyOptions#proxy_options.username,
666+
ProxyOptions#proxy_options.password])).
667+
632668
format_oauth_provider_id(root) -> "<from keyconfig>";
633669
format_oauth_provider_id(Id) -> binary_to_list(Id).
634670

@@ -637,15 +673,16 @@ format_oauth_provider(OAuthProvider) ->
637673
lists:flatten(io_lib:format("{id: ~p, issuer: ~p, discovery_endpoint: ~p, " ++
638674
" token_endpoint: ~p, " ++
639675
"authorization_endpoint: ~p, end_session_endpoint: ~p, " ++
640-
"jwks_uri: ~p, ssl_options: ~p }", [
676+
"jwks_uri: ~p, ssl_options: ~p, proxy_options: ~p}", [
641677
format_oauth_provider_id(OAuthProvider#oauth_provider.id),
642678
OAuthProvider#oauth_provider.issuer,
643679
OAuthProvider#oauth_provider.discovery_endpoint,
644680
OAuthProvider#oauth_provider.token_endpoint,
645681
OAuthProvider#oauth_provider.authorization_endpoint,
646682
OAuthProvider#oauth_provider.end_session_endpoint,
647683
OAuthProvider#oauth_provider.jwks_uri,
648-
format_ssl_options(OAuthProvider#oauth_provider.ssl_options)])).
684+
format_ssl_options(OAuthProvider#oauth_provider.ssl_options),
685+
format_proxy_options(OAuthProvider#oauth_provider.proxy_options)])).
649686

650687
get_env(Par) ->
651688
application:get_env(rabbitmq_auth_backend_oauth2, Par, undefined).

deps/rabbitmq_auth_backend_oauth2/test/rabbit_oauth2_provider_SUITE.erl

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,21 @@ init_per_group(with_resource_server_id, Config) ->
153153
Config;
154154

155155
init_per_group(oauth_provider_with_proxy, Config) ->
156-
KeyConfig = get_env(key_config, []),
157-
set_env(key_config, KeyConfig ++ [
156+
Proxy = [
158157
{proxy, build_url_to_oauth_provider(<<"/">>)},
159158
{proxy_username, <<"user1">>},
160159
{proxy_password, <<"pwd1">>}
161-
]),
162-
Config;
160+
],
161+
case ?config(oauth_provider_id, Config) of
162+
root ->
163+
KeyConfig = get_env(key_config, []),
164+
set_env(key_config, KeyConfig ++ Proxy);
165+
Id ->
166+
OAuthProviders = get_env(oauth_providers, #{}),
167+
OAuthProvider = maps:get(Id, OAuthProviders, []),
168+
set_env(oauth_providers, maps:put(Id, Proxy ++ OAuthProvider, OAuthProviders))
169+
end,
170+
Proxy ++ Config;
163171

164172
init_per_group(with_algorithms, Config) ->
165173
KeyConfig = get_env(key_config, []),
@@ -203,11 +211,16 @@ end_per_group(with_rabbitmq_node, Config) ->
203211
rabbit_ct_helpers:run_steps(Config, rabbit_ct_broker_helpers:teardown_steps());
204212

205213
end_per_group(oauth_provider_with_proxy, Config) ->
206-
KeyConfig = get_env(key_config, []),
207-
KeyConfig0 = proplists:delete(proxy, KeyConfig),
208-
KeyConfig1 = proplists:delete(proxy_username, KeyConfig0),
209-
KeyConfig2 = proplists:delete(proxy_password, KeyConfig1),
210-
set_env(key_config, KeyConfig2),
214+
case ?config(oauth_provider_id, Config) of
215+
root ->
216+
KeyConfig = get_env(key_config, []),
217+
KeyConfig0 = proplists:delete(proxy, KeyConfig),
218+
KeyConfig1 = proplists:delete(proxy_username, KeyConfig0),
219+
KeyConfig2 = proplists:delete(proxy_password, KeyConfig1),
220+
set_env(key_config, KeyConfig2);
221+
Id ->
222+
unset_oauth_provider_properties(Id, [proxy, proxy_username, proxy_password])
223+
end,
211224
Config;
212225

213226
end_per_group(with_root_static_signing_keys, Config) ->
@@ -427,15 +440,21 @@ get_oauth_provider_with_jwks_uri_returns_error(Config) ->
427440

428441
get_oauth_provider_has_jwks_uri(Config) ->
429442
{ok, OAuthProvider} = get_oauth_provider(
430-
?config(oauth_provider_id, Config), [jwks_uri]),
431-
ct:log("OAuthProvider: ~p", [OAuthProvider]),
443+
?config(oauth_provider_id, Config), [jwks_uri]),
432444
?assertEqual(?config(jwks_uri, Config), OAuthProvider#oauth_provider.jwks_uri).
433445

434446
get_oauth_provider_has_proxy(Config) ->
435447
{ok, OAuthProvider} = get_oauth_provider(
436-
?config(oauth_provider_id, Config), [jwks_uri]),
437-
ct:log("OAuthProvider: ~p", [OAuthProvider]),
438-
?assertEqual(?config(jwks_uri, Config), OAuthProvider#oauth_provider.jwks_uri).
448+
?config(oauth_provider_id, Config), [jwks_uri]),
449+
ct:log("key_config: ~p",
450+
[ application:get_all_env(rabbitmq_auth_backend_oauth2)]),
451+
452+
?assertEqual(?config(proxy, Config),
453+
OAuthProvider#oauth_provider.proxy_options#proxy_options.proxy),
454+
?assertEqual(?config(proxy_username, Config),
455+
OAuthProvider#oauth_provider.proxy_options#proxy_options.username),
456+
?assertEqual(?config(proxy_password, Config),
457+
OAuthProvider#oauth_provider.proxy_options#proxy_options.password).
439458

440459

441460
%% ---- Utility functions

0 commit comments

Comments
 (0)