Skip to content

Commit 243a8bf

Browse files
Use profile when using Erlang http client
1 parent e28301c commit 243a8bf

File tree

4 files changed

+88
-107
lines changed

4 files changed

+88
-107
lines changed

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
-module(oauth2_client).
88
-export([get_access_token/2, get_expiration_time/1,
99
refresh_access_token/2,
10-
get_jwks/2, get_jwks/3,
10+
get_jwks/1,
1111
get_oauth_provider/1, get_oauth_provider/2,
12-
get_openid_configuration/2,get_openid_configuration/3,
12+
get_openid_configuration/1,
1313
build_openid_discovery_endpoint/3,
1414
merge_openid_configuration/2,
1515
merge_oauth_provider/2,
@@ -30,52 +30,66 @@ get_access_token(OAuthProvider, Request) ->
3030
rabbit_log:debug("get_access_token using OAuthProvider:~p and client_id:~p",
3131
[OAuthProvider, Request#access_token_request.client_id]),
3232
URL = OAuthProvider#oauth_provider.token_endpoint,
33+
Id = OAuthProvider#oauth_provider.id,
3334
Header = [],
3435
Type = ?CONTENT_URLENCODED,
3536
Body = build_access_token_request_body(Request),
3637
HTTPOptions =
3738
map_ssl_options_to_httpc_option(OAuthProvider#oauth_provider.ssl_options) ++
3839
map_timeout_to_httpc_option(Request#access_token_request.timeout),
39-
Response = http_post(URL, Header, Type, Body, HTTPOptions,
40+
Response = http_post(Id, URL, Header, Type, Body, HTTPOptions,
4041
OAuthProvider#oauth_provider.proxy_options),
4142
parse_access_token_response(Response).
4243

4344
-spec refresh_access_token(oauth_provider(), refresh_token_request()) ->
4445
{ok, successful_access_token_response()} |
4546
{error, unsuccessful_access_token_response() | any()}.
4647
refresh_access_token(OAuthProvider, Request) ->
48+
Id = OAuthProvider#oauth_provider.id,
4749
URL = OAuthProvider#oauth_provider.token_endpoint,
4850
Header = [],
4951
Type = ?CONTENT_URLENCODED,
5052
Body = build_refresh_token_request_body(Request),
5153
HTTPOptions =
5254
map_ssl_options_to_httpc_option(OAuthProvider#oauth_provider.ssl_options) ++
5355
map_timeout_to_httpc_option(Request#refresh_token_request.timeout),
54-
Response = http_post(URL, Header, Type, Body, HTTPOptions,
56+
Response = http_post(Id, URL, Header, Type, Body, HTTPOptions,
5557
OAuthProvider#oauth_provider.proxy_options),
5658
parse_access_token_response(Response).
5759

58-
http_post(URL, Header, Type, Body, HTTPOptions, ProxyOptions) ->
59-
case ProxyOptions of
60-
undefined -> httpc:request(post, {URL, Header, Type, Body}, HTTPOptions, []);
61-
_ ->
62-
case httpc:set_options(map_proxy_to_httpc_option(ProxyOptions)) of
63-
ok ->
64-
httpc:request(post, {URL, Header, Type, Body},
65-
HTTPOptions ++ map_proxy_auth_to_httpc_option(ProxyOptions), []);
66-
{error, _} = Error -> Error
67-
end
60+
ensure_http_client_started(Id) ->
61+
Profile = case Id of
62+
root -> root;
63+
_ -> binary_to_atom(Id)
64+
end,
65+
case inets:start(httpc, [{profile, Profile}]) of
66+
ok -> {ok, Profile};
67+
{error, {already_started, _}} -> {ok, Profile};
68+
Error -> Error
6869
end.
69-
http_get(URL, HTTPOptions, ProxyOptions) ->
70-
case ProxyOptions of
71-
undefined -> httpc:request(get, {URL, []}, HTTPOptions, []);
72-
_ ->
73-
case httpc:set_options(map_proxy_to_httpc_option(ProxyOptions)) of
74-
ok ->
75-
httpc:request(get, {URL, []},
76-
HTTPOptions ++ map_proxy_auth_to_httpc_option(ProxyOptions), []);
77-
{error, _} = Error -> Error
78-
end
70+
http_post(Id, URL, Header, Type, Body, HTTPOptions, ProxyOptions) ->
71+
http_request(Id, post, {URL, Header, Type, Body}, HTTPOptions, ProxyOptions).
72+
http_get(Id, URL, HTTPOptions, ProxyOptions) ->
73+
ct:log("~p ~p", [Id, URL]),
74+
http_request(Id, get, {URL, []}, HTTPOptions, ProxyOptions).
75+
http_request(Id, Method, Payload, HTTPOptions, ProxyOptions) ->
76+
case ensure_http_client_started(Id) of
77+
{ok, Profile} ->
78+
case ProxyOptions of
79+
undefined ->
80+
httpc:request(Method, Payload, HTTPOptions, [], Profile);
81+
_ ->
82+
case httpc:set_options(map_proxy_to_httpc_option(ProxyOptions),
83+
Profile) of
84+
ok ->
85+
httpc:request(Method, Payload,
86+
HTTPOptions ++ map_proxy_auth_to_httpc_option(ProxyOptions),
87+
[],
88+
Profile);
89+
{error, _} = Error -> Error
90+
end
91+
end;
92+
{error, _} = Error -> Error
7993
end.
8094

8195
append_paths(Path1, Path2) ->
@@ -123,38 +137,27 @@ drop_trailing_path_separator(Path) when is_list(Path) ->
123137
_ -> Path
124138
end.
125139

126-
-spec get_openid_configuration(DiscoveryEndpoint :: uri_string:uri_string(),
127-
ssl:tls_option() | []) -> {ok, openid_configuration()} | {error, term()}.
128-
get_openid_configuration(DiscoverEndpoint, TLSOptions) ->
129-
get_openid_configuration(DiscoverEndpoint, TLSOptions, undefined).
130-
131-
-spec get_openid_configuration(DiscoveryEndpoint :: uri_string:uri_string(),
132-
ssl:tls_option() | [], proxy_options() | undefined | 'none') ->
133-
{ok, openid_configuration()} | {error, term()}.
134-
get_openid_configuration(DiscoverEndpoint, TLSOptions, ProxyOptions) ->
135-
rabbit_log:debug("get_openid_configuration from ~p (~p) [~p]", [DiscoverEndpoint,
136-
format_ssl_options(TLSOptions), format_proxy_options(ProxyOptions)]),
140+
-spec get_openid_configuration(oauth_provider()) -> {ok, openid_configuration()} | {error, term()}.
141+
get_openid_configuration(#oauth_provider{id = Id, discovery_endpoint = Endpoint,
142+
ssl_options = SslOptions, proxy_options = ProxyOptions}) ->
143+
rabbit_log:debug("get_openid_configuration from ~p (~p) [~p]", [Endpoint,
144+
format_ssl_options(SslOptions), format_proxy_options(ProxyOptions)]),
137145
HTTPOptions =
138-
map_ssl_options_to_httpc_option(TLSOptions) ++
146+
map_ssl_options_to_httpc_option(SslOptions) ++
139147
map_timeout_to_httpc_option(?DEFAULT_HTTP_TIMEOUT),
140-
Response = http_get(DiscoverEndpoint, HTTPOptions, ProxyOptions),
148+
149+
Response = http_get(Id, Endpoint, HTTPOptions, ProxyOptions),
141150
parse_openid_configuration_response(Response).
142151

143-
-spec get_jwks(JWKSEndpoint :: uri_string:uri_string(),
144-
ssl:tls_option() | []) -> {ok, openid_configuration()} | {error, term()}.
145-
get_jwks(JWKSEndpoint, TLSOptions) ->
146-
get_jwks(JWKSEndpoint, TLSOptions, undefined).
147-
148-
-spec get_jwks(JWKSEndpoint :: uri_string:uri_string(),
149-
ssl:tls_option() | [], proxy_options() | undefined | 'none')
150-
-> {ok, openid_configuration()} | {error, term()}.
151-
get_jwks(JWKSEndpoint, TLSOptions, ProxyOptions) ->
152-
rabbit_log:debug("get_jwks from ~p (~p) [~p]", [JWKSEndpoint,
153-
format_ssl_options(TLSOptions), format_proxy_options(ProxyOptions)]),
152+
-spec get_jwks(oauth_provider()) -> {ok, term()} | {error, term()}.
153+
get_jwks(#oauth_provider{id = Id, jwks_uri = JwksUrl,
154+
ssl_options = SslOptions, proxy_options = ProxyOptions}) ->
155+
rabbit_log:debug("get_jwks from ~p (~p) [~p]", [JwksUrl,
156+
format_ssl_options(SslOptions), format_proxy_options(ProxyOptions)]),
154157
HTTPOptions =
155-
map_ssl_options_to_httpc_option(TLSOptions) ++
158+
map_ssl_options_to_httpc_option(SslOptions) ++
156159
map_timeout_to_httpc_option(?DEFAULT_HTTP_TIMEOUT),
157-
http_get(JWKSEndpoint, HTTPOptions, ProxyOptions).
160+
http_get(Id, JwksUrl, HTTPOptions, ProxyOptions).
158161

159162
-spec merge_openid_configuration(openid_configuration(), oauth_provider()) ->
160163
oauth_provider().
@@ -337,9 +340,7 @@ download_oauth_provider(OAuthProvider) ->
337340
undefined -> {error, {missing_oauth_provider_attributes, [issuer]}};
338341
URL ->
339342
rabbit_log:debug("Downloading oauth_provider using ~p ", [URL]),
340-
case get_openid_configuration(URL,
341-
OAuthProvider#oauth_provider.ssl_options,
342-
OAuthProvider#oauth_provider.proxy_options) of
343+
case get_openid_configuration(OAuthProvider) of
343344
{ok, OpenIdConfiguration} ->
344345
{ok, update_oauth_provider_endpoints_configuration(
345346
merge_openid_configuration(OpenIdConfiguration, OAuthProvider))};

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ get_openid_configuration_http_expectation(TestCaseAtom) ->
184184
nomatch -> ?DEFAULT_OPENID_CONFIGURATION_PATH;
185185
_ -> ?CUSTOM_OPENID_CONFIGURATION_ENDPOINT
186186
end,
187+
ct:log("Expect path: ~p and endpoint: ~p", [Path, Endpoint]),
187188
build_http_mock_behaviour(build_http_get_openid_configuration_request(Endpoint, Path),
188189
build_http_200_json_response(Payload)).
189190

@@ -322,14 +323,25 @@ build_openid_discovery_endpoint(Issuer, Path) ->
322323

323324
get_openid_configuration(Config) ->
324325
ExpectedOAuthProvider = ?config(oauth_provider, Config),
325-
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
326326
{ok, ActualOpenId} = oauth2_client:get_openid_configuration(
327-
build_openid_discovery_endpoint(build_issuer("https")),
328-
SslOptions,
329-
ExpectedOAuthProvider#oauth_provider.proxy_options),
327+
ensure_discovery_endpoint(ExpectedOAuthProvider)),
330328
ExpectedOpenId = map_oauth_provider_to_openid_configuration(ExpectedOAuthProvider),
331329
assertOpenIdConfiguration(ExpectedOpenId, ActualOpenId).
332330

331+
ensure_issuer(OAuthProvider, IssuerURL) ->
332+
OAuthProvider#oauth_provider{
333+
issuer = IssuerURL
334+
}.
335+
ensure_discovery_endpoint(OAuthProvider) ->
336+
OAuthProvider#oauth_provider{
337+
discovery_endpoint = build_openid_discovery_endpoint(OAuthProvider#oauth_provider.issuer)
338+
}.
339+
ensure_discovery_endpoint(OAuthProvider, DiscoveryEndpointPath) ->
340+
OAuthProvider#oauth_provider{
341+
discovery_endpoint = build_openid_discovery_endpoint(
342+
OAuthProvider#oauth_provider.issuer,
343+
DiscoveryEndpointPath)
344+
}.
333345
map_oauth_provider_to_openid_configuration(OAuthProvider) ->
334346
#openid_configuration{
335347
issuer = OAuthProvider#oauth_provider.issuer,
@@ -345,39 +357,30 @@ get_openid_configuration_returns_partial_payload(Config) ->
345357
token_endpoint = ExpectedOAuthProvider0#oauth_provider.token_endpoint,
346358
jwks_uri = ExpectedOAuthProvider0#oauth_provider.jwks_uri},
347359

348-
SslOptions = ExpectedOAuthProvider0#oauth_provider.ssl_options,
349360
{ok, Actual} = oauth2_client:get_openid_configuration(
350-
build_openid_discovery_endpoint(build_issuer("https")),
351-
SslOptions,
352-
ExpectedOAuthProvider0#oauth_provider.proxy_options),
361+
ensure_discovery_endpoint(ExpectedOAuthProvider0)),
353362
ExpectedOpenId = map_oauth_provider_to_openid_configuration(ExpectedOAuthProvider),
354363
assertOpenIdConfiguration(ExpectedOpenId, Actual).
355364

356365
get_openid_configuration_using_path(Config) ->
357366
ExpectedOAuthProvider = ?config(oauth_provider, Config),
358-
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
359-
{ok, Actual} = oauth2_client:get_openid_configuration(
360-
build_openid_discovery_endpoint(build_issuer("https", ?ISSUER_PATH)),
361-
SslOptions,
362-
ExpectedOAuthProvider#oauth_provider.proxy_options),
367+
{ok, Actual} = oauth2_client:get_openid_configuration(
368+
ensure_discovery_endpoint(
369+
ensure_issuer(ExpectedOAuthProvider, build_issuer("https", ?ISSUER_PATH)))),
363370
ExpectedOpenId = map_oauth_provider_to_openid_configuration(ExpectedOAuthProvider),
364371
assertOpenIdConfiguration(ExpectedOpenId,Actual).
365372
get_openid_configuration_using_path_and_custom_endpoint(Config) ->
366-
ExpectedOAuthProvider = ?config(oauth_provider, Config),
367-
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
373+
ExpectedOAuthProvider = ?config(oauth_provider, Config),
368374
{ok, Actual} = oauth2_client:get_openid_configuration(
369-
build_openid_discovery_endpoint(build_issuer("https", ?ISSUER_PATH),
370-
?CUSTOM_OPENID_CONFIGURATION_ENDPOINT), SslOptions,
371-
ExpectedOAuthProvider#oauth_provider.proxy_options),
375+
ensure_discovery_endpoint(
376+
ensure_issuer(ExpectedOAuthProvider, build_issuer("https", ?ISSUER_PATH)),
377+
?CUSTOM_OPENID_CONFIGURATION_ENDPOINT)),
372378
ExpectedOpenId = map_oauth_provider_to_openid_configuration(ExpectedOAuthProvider),
373379
assertOpenIdConfiguration(ExpectedOpenId, Actual).
374380
get_openid_configuration_using_custom_endpoint(Config) ->
375381
ExpectedOAuthProvider = ?config(oauth_provider, Config),
376-
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
377382
{ok, Actual} = oauth2_client:get_openid_configuration(
378-
build_openid_discovery_endpoint(build_issuer("https"),
379-
?CUSTOM_OPENID_CONFIGURATION_ENDPOINT), SslOptions,
380-
ExpectedOAuthProvider#oauth_provider.proxy_options),
383+
ensure_discovery_endpoint(ExpectedOAuthProvider, ?CUSTOM_OPENID_CONFIGURATION_ENDPOINT)),
381384
ExpectedOpenId = map_oauth_provider_to_openid_configuration(ExpectedOAuthProvider),
382385
assertOpenIdConfiguration(ExpectedOpenId, Actual).
383386

deps/rabbitmq_auth_backend_oauth2/src/uaa_jwks.erl

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,15 @@
22
-export([get/2, get/3]).
33

44
-import(oauth2_client, [
5-
map_ssl_options_to_httpc_option/1,
6-
map_timeout_to_httpc_option/1,
7-
map_proxy_auth_to_httpc_option/1,
8-
map_proxy_to_httpc_option/1]).
5+
get_jwks/2, get_jwks/3]).
96

107
-spec get(uri_string:uri_string(), list()) -> {ok, term()} | {error, term()}.
118
get(JwksUrl, SslOptions) ->
12-
http_get(JwksUrl, SslOptions, undefined).
9+
get_jwks(JwksUrl, SslOptions).
1310

14-
-spec get(uri_string:uri_string(), list(), oauth2_client:proxy_options() | undefined | 'none') ->
15-
{ok, term()} | {error, term()}.
11+
-spec get(uri_string:uri_string(), list(),
12+
oauth2_client:proxy_options() | undefined | 'none') -> {ok, term()} | {error, term()}.
1613
get(JwksUrl, SslOptions, undefined) ->
17-
get(JwksUrl, SslOptions);
14+
get_jwks(JwksUrl, SslOptions);
1815
get(JwksUrl, SslOptions, ProxyOptions) ->
19-
http_get(JwksUrl, SslOptions, ProxyOptions).
20-
21-
http_get(URL, SslOptions, ProxyOptions) ->
22-
HttpOptions = map_timeout_to_httpc_option(60000)
23-
++ map_ssl_options_to_httpc_option(SslOptions),
24-
{HttpProxyOptions, SetOptions} =
25-
case ProxyOptions of
26-
undefined -> {[], ok};
27-
_ ->
28-
case httpc:set_options(map_proxy_to_httpc_option(ProxyOptions)) of
29-
ok -> {map_proxy_auth_to_httpc_option(ProxyOptions), ok};
30-
{error, _} = Error -> {undefined, Error}
31-
end
32-
end,
33-
case SetOptions of
34-
ok -> httpc:request(get, {URL, []}, HttpOptions ++ HttpProxyOptions, []);
35-
{error, _} -> SetOptions
36-
end.
37-
16+
get_jwks(JwksUrl, SslOptions, ProxyOptions).

deps/rabbitmq_auth_backend_oauth2/src/uaa_jwt.erl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ add_signing_key(KeyId, Type, Value) ->
4242
end.
4343

4444
-spec update_jwks_signing_keys(oauth_provider()) -> ok | {error, term()}.
45-
update_jwks_signing_keys(#oauth_provider{id = Id, jwks_uri = JwksUrl,
46-
ssl_options = SslOptions, proxy_options = ProxyOptions}) ->
47-
rabbit_log:debug("Downloading signing keys from ~tp (TLS options: ~p)",
48-
[JwksUrl, format_ssl_options(SslOptions)]),
49-
case uaa_jwks:get(JwksUrl, SslOptions, ProxyOptions) of
45+
update_jwks_signing_keys(#oauth_provider{id = Id} = OAuthProvider) ->
46+
rabbit_log:debug("Downloading signing keys from OauthProvider: ~tp", [Id]),
47+
case oauth2_client:get_jwks(OAuthProvider) of
5048
{ok, {_, _, JwksBody}} ->
5149
KeyList = maps:get(<<"keys">>,
5250
jose:decode(erlang:iolist_to_binary(JwksBody)), []),

0 commit comments

Comments
 (0)