Skip to content

Commit baa36af

Browse files
Use proxy_options to call httpc
1 parent 28b9935 commit baa36af

File tree

3 files changed

+127
-21
lines changed

3 files changed

+127
-21
lines changed

deps/oauth2_client/include/types.hrl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
-type openid_configuration() :: #openid_configuration{}.
2222

2323
-record(proxy_options, {
24-
proxy :: uri_string:uri_string(),
24+
https :: boolean(),
25+
hostname :: uri_string:uri_string(),
26+
port :: integer(),
2527
username :: option(string() | binary()),
2628
password :: option(string() | binary())
2729
}).

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
merge_openid_configuration/2,
1414
merge_oauth_provider/2,
1515
extract_ssl_options_as_list/1,
16-
format_ssl_options/1, format_oauth_provider/1, format_oauth_provider_id/1
16+
format_ssl_options/1, format_oauth_provider/1, format_oauth_provider_id/1,
17+
extract_proxy_options_from_url/1
1718
]).
1819

1920
-include("oauth2_client.hrl").
@@ -29,8 +30,9 @@ get_access_token(OAuthProvider, Request) ->
2930
Type = ?CONTENT_URLENCODED,
3031
Body = build_access_token_request_body(Request),
3132
HTTPOptions = get_ssl_options_if_any(OAuthProvider) ++
32-
get_timeout_of_default(Request#access_token_request.timeout),
33-
Options = [],
33+
get_timeout_of_default(Request#access_token_request.timeout) ++
34+
get_proxy_auth_if_any(OAuthProvider#oauth_provider.proxy_options),
35+
Options = get_proxy_if_any(OAuthProvider#oauth_provider.proxy_options),
3436
Response = httpc:request(post, {URL, Header, Type, Body}, HTTPOptions, Options),
3537
parse_access_token_response(Response).
3638

@@ -43,8 +45,9 @@ refresh_access_token(OAuthProvider, Request) ->
4345
Type = ?CONTENT_URLENCODED,
4446
Body = build_refresh_token_request_body(Request),
4547
HTTPOptions = get_ssl_options_if_any(OAuthProvider) ++
46-
get_timeout_of_default(Request#refresh_token_request.timeout),
47-
Options = [],
48+
get_timeout_of_default(Request#refresh_token_request.timeout) ++
49+
get_proxy_auth_if_any(OAuthProvider#oauth_provider.proxy_options),
50+
Options = get_proxy_if_any(OAuthProvider#oauth_provider.proxy_options),
4851
Response = httpc:request(post, {URL, Header, Type, Body}, HTTPOptions, Options),
4952
parse_access_token_response(Response).
5053

@@ -96,11 +99,12 @@ drop_trailing_path_separator(Path) when is_list(Path) ->
9699
-spec get_openid_configuration(DiscoveryEndpoint :: uri_string:uri_string(),
97100
ssl:tls_option() | [], proxy_options() | undefined) ->
98101
{ok, openid_configuration()} | {error, term()}.
99-
get_openid_configuration(DiscoverEndpoint, TLSOptions, _ProxyOptions) ->
102+
get_openid_configuration(DiscoverEndpoint, TLSOptions, ProxyOptions) ->
100103
rabbit_log:debug("get_openid_configuration from ~p (~p)", [DiscoverEndpoint,
101104
format_ssl_options(TLSOptions)]),
102-
Options = [],
103-
Response = httpc:request(get, {DiscoverEndpoint, []}, TLSOptions, Options),
105+
Options = get_proxy_if_any(ProxyOptions),
106+
Response = httpc:request(get, {DiscoverEndpoint, []},
107+
TLSOptions ++ get_proxy_auth_if_any(ProxyOptions), Options),
104108
parse_openid_configuration_response(Response).
105109

106110
-spec merge_openid_configuration(openid_configuration(), oauth_provider()) ->
@@ -401,13 +405,28 @@ lookup_root_oauth_provider() ->
401405
proxy_options = extract_proxy_options(Map)
402406
}.
403407

404-
-spec extract_proxy_options(#{atom() => any()}|list()) -> proxy_options().
408+
-spec extract_proxy_options_from_url(list()|binary()) -> proxy_options().
409+
extract_proxy_options_from_url(URL) when is_binary(URL) ->
410+
extract_proxy_options_from_url(binary_to_list(URL));
411+
extract_proxy_options_from_url(URL) when is_list(URL) ->
412+
Parsed = uri_string:parse(URL),
413+
#proxy_options{
414+
https =
415+
case maps:get("scheme", Parsed, "http") of
416+
"http" -> false;
417+
"https" -> true
418+
end,
419+
hostname = maps:get("host", Parsed, undefined),
420+
port = maps:get("port", Parsed, undefined)
421+
}.
422+
423+
-spec extract_proxy_options(#{atom() => any()}|list()) -> proxy_options() | undefined.
405424
extract_proxy_options(List) when is_list(List) ->
406425
case proplists:get_value(proxy, List, undefined) of
407426
undefined -> undefined;
408427
URL ->
409-
#proxy_options{
410-
proxy = URL,
428+
Options = extract_proxy_options_from_url(URL),
429+
Options#proxy_options{
411430
username = proplists:get_value(proxy_username, List, undefined),
412431
password = proplists:get_value(proxy_password, List, undefined)
413432
}
@@ -416,8 +435,8 @@ extract_proxy_options(Map) ->
416435
case maps:get(proxy, Map, undefined) of
417436
undefined -> undefined;
418437
URL ->
419-
#proxy_options{
420-
proxy = URL,
438+
Options = extract_proxy_options_from_url(URL),
439+
Options#proxy_options{
421440
username = maps:get(proxy_username, Map, undefined),
422441
password = maps:get(proxy_password, Map, undefined)
423442
}
@@ -559,6 +578,30 @@ get_timeout_of_default(Timeout) ->
559578
Timeout -> [{timeout, Timeout}]
560579
end.
561580

581+
get_proxy_if_any(ProxyOptions) ->
582+
case ProxyOptions of
583+
undefined ->
584+
[];
585+
Proxy ->
586+
P = {Proxy#proxy_options.hostname, Proxy#proxy_options.port},
587+
case Proxy#proxy_options.https of
588+
true -> [{https_proxy, P}];
589+
false -> [{proxy, P}]
590+
end
591+
end.
592+
593+
get_proxy_auth_if_any(ProxyOptions) ->
594+
case ProxyOptions of
595+
undefined ->
596+
[];
597+
Proxy ->
598+
case {Proxy#proxy_options.username, Proxy#proxy_options.password} of
599+
{undefined, _} -> [];
600+
{_, undefined} -> [];
601+
{_, _} = Auth -> [{proxy_auth, Auth}]
602+
end
603+
end.
604+
562605
is_json(?CONTENT_JSON) -> true;
563606
is_json(_) -> false.
564607

@@ -654,14 +697,16 @@ format_ssl_options(TlsOptions) ->
654697
proplists:get_value(cacertfile, TlsOptions),
655698
CaCertsCount])).
656699

657-
-spec format_proxy_options(proxy_options()) -> string().
700+
-spec format_proxy_options(proxy_options()|undefined) -> string().
658701
format_proxy_options(undefined) ->
659702
lists:flatten(io_lib:format("{no proxy}", []));
660703

661704
format_proxy_options(ProxyOptions) ->
662-
lists:flatten(io_lib:format("{proxy: ~p, username: ~p, " ++
705+
lists:flatten(io_lib:format("{https: ~p, hostname: ~p, port: ~p, username: ~p, " ++
663706
"password: ~p }", [
664-
ProxyOptions#proxy_options.proxy,
707+
ProxyOptions#proxy_options.https,
708+
ProxyOptions#proxy_options.hostname,
709+
ProxyOptions#proxy_options.port,
665710
ProxyOptions#proxy_options.username,
666711
ProxyOptions#proxy_options.password])).
667712

deps/rabbitmq_auth_backend_oauth2/test/rabbit_oauth2_provider_SUITE.erl

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ verify_provider() -> [
6868
]},
6969
{oauth_provider_with_proxy, [], [
7070
get_oauth_provider_has_proxy
71+
]},
72+
{oauth_provider_with_https_proxy, [], [
73+
get_oauth_provider_has_https_proxy
7174
]}
7275
].
7376

@@ -154,7 +157,7 @@ init_per_group(with_resource_server_id, Config) ->
154157

155158
init_per_group(oauth_provider_with_proxy, Config) ->
156159
Proxy = [
157-
{proxy, build_url_to_oauth_provider(<<"/">>)},
160+
{proxy, "http://idp:8080"},
158161
{proxy_username, <<"user1">>},
159162
{proxy_password, <<"pwd1">>}
160163
],
@@ -167,7 +170,30 @@ init_per_group(oauth_provider_with_proxy, Config) ->
167170
OAuthProvider = maps:get(Id, OAuthProviders, []),
168171
set_env(oauth_providers, maps:put(Id, Proxy ++ OAuthProvider, OAuthProviders))
169172
end,
170-
Proxy ++ Config;
173+
[{proxy_hostname, "idp"},
174+
{proxy_port, 8080},
175+
{proxy_username, <<"user1">>},
176+
{proxy_password, <<"pwd1">>}] ++ Config;
177+
178+
init_per_group(oauth_provider_with_https_proxy, Config) ->
179+
Proxy = [
180+
{proxy, "https://idp:8843"},
181+
{proxy_username, <<"user1">>},
182+
{proxy_password, <<"pwd1">>}
183+
],
184+
case ?config(oauth_provider_id, Config) of
185+
root ->
186+
KeyConfig = get_env(key_config, []),
187+
set_env(key_config, KeyConfig ++ Proxy);
188+
Id ->
189+
OAuthProviders = get_env(oauth_providers, #{}),
190+
OAuthProvider = maps:get(Id, OAuthProviders, []),
191+
set_env(oauth_providers, maps:put(Id, Proxy ++ OAuthProvider, OAuthProviders))
192+
end,
193+
[{proxy_hostname, "idp"},
194+
{proxy_port, 8843},
195+
{proxy_username, <<"user1">>},
196+
{proxy_password, <<"pwd1">>}] ++ Config;
171197

172198
init_per_group(with_algorithms, Config) ->
173199
KeyConfig = get_env(key_config, []),
@@ -222,6 +248,8 @@ end_per_group(oauth_provider_with_proxy, Config) ->
222248
unset_oauth_provider_properties(Id, [proxy, proxy_username, proxy_password])
223249
end,
224250
Config;
251+
end_per_group(oauth_provider_with_https_proxy, Config) ->
252+
end_per_group(oauth_provider_with_proxy, Config);
225253

226254
end_per_group(with_root_static_signing_keys, Config) ->
227255
KeyConfig = call_get_env(Config, key_config, []),
@@ -448,9 +476,40 @@ get_oauth_provider_has_proxy(Config) ->
448476
?config(oauth_provider_id, Config), [jwks_uri]),
449477
ct:log("key_config: ~p",
450478
[ application:get_all_env(rabbitmq_auth_backend_oauth2)]),
479+
ct:log("oauthprovider: ~p", [OAuthProvider]),
480+
?assertEqual(false,
481+
OAuthProvider#oauth_provider.proxy_options#proxy_options.https),
482+
483+
ct:log("Parsed : ~p", [uri_string:parse("http://idp:8080")]),
451484

452-
?assertEqual(?config(proxy, Config),
453-
OAuthProvider#oauth_provider.proxy_options#proxy_options.proxy),
485+
Options = oauth2_client:extract_proxy_options_from_url("http://idp:8080"),
486+
ct:log("Options1: ~p", [Options]),
487+
Options2 = oauth2_client:extract_proxy_options_from_url(<<"http://idp:8080">>),
488+
ct:log("Options2: ~p", [Options2]),
489+
490+
?assertEqual(?config(proxy_port, Config),
491+
OAuthProvider#oauth_provider.proxy_options#proxy_options.port),
492+
?assertEqual(?config(proxy_hostname, Config),
493+
OAuthProvider#oauth_provider.proxy_options#proxy_options.hostname),
494+
?assertEqual(?config(proxy_username, Config),
495+
OAuthProvider#oauth_provider.proxy_options#proxy_options.username),
496+
?assertEqual(?config(proxy_password, Config),
497+
OAuthProvider#oauth_provider.proxy_options#proxy_options.password).
498+
499+
500+
get_oauth_provider_has_https_proxy(Config) ->
501+
{ok, OAuthProvider} = get_oauth_provider(
502+
?config(oauth_provider_id, Config), [jwks_uri]),
503+
ct:log("key_config: ~p",
504+
[ application:get_all_env(rabbitmq_auth_backend_oauth2)]),
505+
ct:log("oauthprovider: ~p", [OAuthProvider]),
506+
?assertEqual(true,
507+
OAuthProvider#oauth_provider.proxy_options#proxy_options.https),
508+
509+
?assertEqual(?config(proxy_port, Config),
510+
OAuthProvider#oauth_provider.proxy_options#proxy_options.port),
511+
?assertEqual(?config(proxy_hostname, Config),
512+
OAuthProvider#oauth_provider.proxy_options#proxy_options.hostname),
454513
?assertEqual(?config(proxy_username, Config),
455514
OAuthProvider#oauth_provider.proxy_options#proxy_options.username),
456515
?assertEqual(?config(proxy_password, Config),

0 commit comments

Comments
 (0)