Skip to content

Commit 9b772b0

Browse files
Simplify proxy configuration
1 parent 8d2c95a commit 9b772b0

File tree

7 files changed

+88
-118
lines changed

7 files changed

+88
-118
lines changed

deps/oauth2_client/include/types.hrl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
-type openid_configuration() :: #openid_configuration{}.
2222

2323
-record(proxy_options, {
24-
https :: boolean(),
25-
host :: uri_string:uri_string(),
24+
host :: string(),
2625
port :: integer(),
2726
username :: option(string() | binary()),
2827
password :: option(string() | binary())

deps/oauth2_client/src/oauth2_client.erl

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
map_proxy_to_httpc_option/1,
1818
map_ssl_options_to_httpc_option/1,
1919
map_timeout_to_httpc_option/1,
20-
format_ssl_options/1, format_oauth_provider/1, format_oauth_provider_id/1,
21-
extract_proxy_options_from_url/1
20+
format_ssl_options/1, format_oauth_provider/1, format_oauth_provider_id/1
2221
]).
2322

2423
-include("oauth2_client.hrl").
@@ -433,39 +432,29 @@ lookup_root_oauth_provider() ->
433432
ssl_options = extract_ssl_options_as_list(Map),
434433
proxy_options = extract_proxy_options(Map)
435434
}.
436-
437-
-spec extract_proxy_options_from_url(list()|binary()) -> proxy_options().
438-
extract_proxy_options_from_url(URL) when is_binary(URL) ->
439-
extract_proxy_options_from_url(binary_to_list(URL));
440-
extract_proxy_options_from_url(URL) when is_list(URL) ->
441-
#{host := Host, port := Port, scheme := Scheme} = uri_string:parse(URL),
442-
#proxy_options{
443-
https =
444-
case Scheme of
445-
"http" -> false;
446-
"https" -> true
447-
end,
448-
host = Host,
449-
port = Port
450-
}.
451-
435+
452436
-spec extract_proxy_options(#{atom() => any()}|list()) -> proxy_options() | undefined.
453437
extract_proxy_options(List) when is_list(List) ->
454-
case proplists:get_value(proxy, List, undefined) of
455-
undefined -> undefined;
456-
URL ->
457-
Options = extract_proxy_options_from_url(URL),
458-
Options#proxy_options{
438+
case {proplists:get_value(proxy_host, List, undefined),
439+
proplists:get_value(proxy_port, List, 0)} of
440+
{undefined, _} -> undefined;
441+
{_, 0} -> undefined;
442+
{H, P} ->
443+
#proxy_options{
444+
host = H,
445+
port = P,
459446
username = proplists:get_value(proxy_username, List, undefined),
460447
password = proplists:get_value(proxy_password, List, undefined)
461448
}
462449
end;
463450
extract_proxy_options(Map) ->
464-
case maps:get(proxy, Map, undefined) of
465-
undefined -> undefined;
466-
URL ->
467-
Options = extract_proxy_options_from_url(URL),
468-
Options#proxy_options{
451+
case {maps:get(proxy_host, Map, undefined), maps:get(proxy_port, Map, 0)} of
452+
{undefined, _} -> undefined;
453+
{_, 0} -> undefined;
454+
{H, P} ->
455+
#proxy_options{
456+
host = H,
457+
port = P,
469458
username = maps:get(proxy_username, Map, undefined),
470459
password = maps:get(proxy_password, Map, undefined)
471460
}
@@ -617,10 +606,7 @@ map_proxy_to_httpc_option(ProxyOptions) ->
617606
{undefined, _} -> [];
618607
{Host, Port} ->
619608
P = {{Host, Port},[]},
620-
case Proxy#proxy_options.https of
621-
true -> [{https_proxy, P}];
622-
false -> [{proxy, P}]
623-
end
609+
[{proxy, P}]
624610
end
625611
end.
626612

@@ -736,9 +722,8 @@ format_proxy_options(undefined) ->
736722
lists:flatten(io_lib:format("{no proxy}", []));
737723

738724
format_proxy_options(ProxyOptions) ->
739-
lists:flatten(io_lib:format("{https: ~p, host: ~p, port: ~p, username: ~p, " ++
725+
lists:flatten(io_lib:format("{host: ~p, port: ~p, username: ~p, " ++
740726
"password: ~p }", [
741-
ProxyOptions#proxy_options.https,
742727
ProxyOptions#proxy_options.host,
743728
ProxyOptions#proxy_options.port,
744729
ProxyOptions#proxy_options.username,

deps/oauth2_client/test/system_SUITE.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ build_openid_discovery_endpoint(Issuer, Path) ->
322322

323323
get_openid_configuration(Config) ->
324324
ExpectedOAuthProvider = ?config(oauth_provider, Config),
325-
SslOptions = [{ssl, ExpectedOAuthProvider#oauth_provider.ssl_options}],
325+
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
326326
{ok, ActualOpenId} = oauth2_client:get_openid_configuration(
327327
build_openid_discovery_endpoint(build_issuer("https")),
328328
SslOptions,
@@ -345,7 +345,7 @@ get_openid_configuration_returns_partial_payload(Config) ->
345345
token_endpoint = ExpectedOAuthProvider0#oauth_provider.token_endpoint,
346346
jwks_uri = ExpectedOAuthProvider0#oauth_provider.jwks_uri},
347347

348-
SslOptions = [{ssl, ExpectedOAuthProvider0#oauth_provider.ssl_options}],
348+
SslOptions = ExpectedOAuthProvider0#oauth_provider.ssl_options,
349349
{ok, Actual} = oauth2_client:get_openid_configuration(
350350
build_openid_discovery_endpoint(build_issuer("https")),
351351
SslOptions,
@@ -355,7 +355,7 @@ get_openid_configuration_returns_partial_payload(Config) ->
355355

356356
get_openid_configuration_using_path(Config) ->
357357
ExpectedOAuthProvider = ?config(oauth_provider, Config),
358-
SslOptions = [{ssl, ExpectedOAuthProvider#oauth_provider.ssl_options}],
358+
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
359359
{ok, Actual} = oauth2_client:get_openid_configuration(
360360
build_openid_discovery_endpoint(build_issuer("https", ?ISSUER_PATH)),
361361
SslOptions,
@@ -364,7 +364,7 @@ get_openid_configuration_using_path(Config) ->
364364
assertOpenIdConfiguration(ExpectedOpenId,Actual).
365365
get_openid_configuration_using_path_and_custom_endpoint(Config) ->
366366
ExpectedOAuthProvider = ?config(oauth_provider, Config),
367-
SslOptions = [{ssl, ExpectedOAuthProvider#oauth_provider.ssl_options}],
367+
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
368368
{ok, Actual} = oauth2_client:get_openid_configuration(
369369
build_openid_discovery_endpoint(build_issuer("https", ?ISSUER_PATH),
370370
?CUSTOM_OPENID_CONFIGURATION_ENDPOINT), SslOptions,
@@ -373,7 +373,7 @@ get_openid_configuration_using_path_and_custom_endpoint(Config) ->
373373
assertOpenIdConfiguration(ExpectedOpenId, Actual).
374374
get_openid_configuration_using_custom_endpoint(Config) ->
375375
ExpectedOAuthProvider = ?config(oauth_provider, Config),
376-
SslOptions = [{ssl, ExpectedOAuthProvider#oauth_provider.ssl_options}],
376+
SslOptions = ExpectedOAuthProvider#oauth_provider.ssl_options,
377377
{ok, Actual} = oauth2_client:get_openid_configuration(
378378
build_openid_discovery_endpoint(build_issuer("https"),
379379
?CUSTOM_OPENID_CONFIGURATION_ENDPOINT), SslOptions,

deps/rabbitmq_auth_backend_oauth2/priv/schema/rabbitmq_auth_backend_oauth2.schema

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,23 @@
221221
end}.
222222

223223
{mapping,
224-
"auth_oauth2.proxy",
225-
"rabbitmq_auth_backend_oauth2.key_config.proxy",
226-
[{datatype, string}, {validators, ["uri"]}]}.
224+
"auth_oauth2.proxy.host",
225+
"rabbitmq_auth_backend_oauth2.proxy.host",
226+
[{datatype, string}]}.
227227

228228
{mapping,
229-
"auth_oauth2.proxy_username",
230-
"rabbitmq_auth_backend_oauth2.key_config.proxy_username",
229+
"auth_oauth2.proxy.port",
230+
"rabbitmq_auth_backend_oauth2.proxy.port",
231+
[{datatype, integer}]}.
232+
233+
{mapping,
234+
"auth_oauth2.proxy.username",
235+
"rabbitmq_auth_backend_oauth2.proxy.username",
231236
[{datatype, string}]}.
232237

233238
{mapping,
234-
"auth_oauth2.proxy_password",
235-
"rabbitmq_auth_backend_oauth2.key_config.proxy_password",
239+
"auth_oauth2.proxy.password",
240+
"rabbitmq_auth_backend_oauth2.proxy.password",
236241
[{datatype, string}]}.
237242

238243
{mapping,
@@ -338,17 +343,22 @@
338343
[{datatype, integer}]}.
339344

340345
{mapping,
341-
"auth_oauth2.oauth_providers.$name.proxy",
346+
"auth_oauth2.oauth_providers.$name.proxy.host",
342347
"rabbitmq_auth_backend_oauth2.oauth_providers",
343-
[{datatype, string}, {validators, ["uri"]}]}.
348+
[{datatype, string}]}.
349+
350+
{mapping,
351+
"auth_oauth2.oauth_providers.$name.proxy.port",
352+
"rabbitmq_auth_backend_oauth2.oauth_providers",
353+
[{datatype, integer}]}.
344354

345355
{mapping,
346-
"auth_oauth2.oauth_providers.$name.proxy_username",
356+
"auth_oauth2.oauth_providers.$name.proxy.username",
347357
"rabbitmq_auth_backend_oauth2.oauth_providers",
348358
[{datatype, string}]}.
349359

350360
{mapping,
351-
"auth_oauth2.oauth_providers.$name.proxy_password",
361+
"auth_oauth2.oauth_providers.$name.proxy.password",
352362
"rabbitmq_auth_backend_oauth2.oauth_providers",
353363
[{datatype, string}]}.
354364

deps/rabbitmq_auth_backend_oauth2/src/rabbit_oauth2_schema.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ translate_oauth_providers(Conf) ->
158158
Settings),
159159
extract_oauth_providers_algorithm(Settings),
160160
extract_oauth_providers_https(Settings),
161+
extract_oauth_providers_proxy(Settings),
161162
extract_oauth_providers_signing_keys(Settings)
162163
]).
163164

@@ -261,12 +262,18 @@ mapOauthProviderProperty({Key, Value}) ->
261262
cuttlefish:invalid(io_lib:format(
262263
"Invalid attribute (~p) value: should be a map of Key,Value pairs",
263264
[Key]));
264-
proxy -> validator_uri(Key, Value);
265-
proxy_username -> binary_to_list(Value);
266-
proxy_password -> binary_to_list(Value);
267265
_ -> Value
268266
end}.
269267

268+
extract_oauth_providers_proxy(Settings) ->
269+
ExtractProviderNameFun = fun extract_key_as_binary/1,
270+
AttributesPerProvider = [{Name, mapProxyProperty({list_to_atom(Key), V})} ||
271+
{[?AUTH_OAUTH2, ?OAUTH_PROVIDERS, Name, "proxy", Key], V} <- Settings ],
272+
273+
maps:map(fun(_K,V)-> [{proxy, V}] end,
274+
maps:groups_from_list(ExtractProviderNameFun, fun({_, V}) -> V end,
275+
AttributesPerProvider)).
276+
270277
extract_oauth_providers_https(Settings) ->
271278
ExtractProviderNameFun = fun extract_key_as_binary/1,
272279

@@ -283,6 +290,9 @@ mapHttpProperty({Key, Value}) ->
283290
_ -> Value
284291
end}.
285292

293+
mapProxyProperty({Key, Value}) ->
294+
{Key, Value}.
295+
286296
extract_oauth_providers_algorithm(Settings) ->
287297
KeyFun = fun extract_key_as_binary/1,
288298

deps/rabbitmq_auth_backend_oauth2/test/config_schema_SUITE_data/rabbitmq_auth_backend_oauth2.snippets

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,25 +318,31 @@
318318
], []
319319
},
320320
{proxy,
321-
"auth_oauth2.proxy = http://localproxy:8080
322-
auth_oauth2.proxy_username = proxyuser
323-
auth_oauth2.proxy_password = proxypwd
324-
auth_oauth2.oauth_providers.keycloak.proxy = http://localproxy2:8080
325-
auth_oauth2.oauth_providers.keycloak.proxy_username = proxyuser2
326-
auth_oauth2.oauth_providers.keycloak.proxy_password = proxypwd2",
321+
"auth_oauth2.proxy.host = localproxy
322+
auth_oauth2.proxy.port = 8080
323+
auth_oauth2.proxy.username = proxyuser
324+
auth_oauth2.proxy.password = proxypwd
325+
auth_oauth2.oauth_providers.keycloak.proxy.host = localproxy2
326+
auth_oauth2.oauth_providers.keycloak.proxy.port = 8080
327+
auth_oauth2.oauth_providers.keycloak.proxy.username = proxyuser2
328+
auth_oauth2.oauth_providers.keycloak.proxy.password = proxypwd2",
327329
[
328330
{rabbitmq_auth_backend_oauth2, [
329-
{key_config, [
330-
{proxy_password, "proxypwd"},
331-
{proxy_username, "proxyuser"},
332-
{proxy, "http://localproxy:8080"}
331+
{proxy, [
332+
{host, "localproxy"},
333+
{password, "proxypwd"},
334+
{port, 8080},
335+
{username, "proxyuser"}
333336
]},
334337
{oauth_providers,
335338
#{
336339
<<"keycloak">> => [
337-
{proxy_password, "proxypwd2"},
338-
{proxy_username, "proxyuser2"},
339-
{proxy, "http://localproxy2:8080"}
340+
{proxy, [
341+
{password, "proxypwd2"},
342+
{username, "proxyuser2"},
343+
{port, 8080},
344+
{host, "localproxy2"}
345+
]}
340346
]
341347
}
342348
}

deps/rabbitmq_auth_backend_oauth2/test/rabbit_oauth2_provider_SUITE.erl

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ verify_provider() -> [
6969
,{oauth_provider_with_proxy, [], [
7070
get_oauth_provider_has_proxy
7171
]}
72-
,{oauth_provider_with_https_proxy, [], [
73-
get_oauth_provider_has_https_proxy
74-
]}
7572
].
7673

7774
init_per_suite(Config) ->
@@ -157,7 +154,8 @@ init_per_group(with_resource_server_id, Config) ->
157154

158155
init_per_group(oauth_provider_with_proxy, Config) ->
159156
Proxy = [
160-
{proxy, "http://idp:8080"},
157+
{proxy_host, "idp"},
158+
{proxy_port, 8080},
161159
{proxy_username, <<"user1">>},
162160
{proxy_password, <<"pwd1">>}
163161
],
@@ -175,26 +173,6 @@ init_per_group(oauth_provider_with_proxy, Config) ->
175173
{proxy_username, <<"user1">>},
176174
{proxy_password, <<"pwd1">>}] ++ Config;
177175

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;
197-
198176
init_per_group(with_algorithms, Config) ->
199177
KeyConfig = get_env(key_config, []),
200178
set_env(key_config, KeyConfig ++ [{algorithms, [<<"HS256">>, <<"RS256">>]}]),
@@ -240,16 +218,16 @@ end_per_group(oauth_provider_with_proxy, Config) ->
240218
case ?config(oauth_provider_id, Config) of
241219
root ->
242220
KeyConfig = get_env(key_config, []),
243-
KeyConfig0 = proplists:delete(proxy, KeyConfig),
244-
KeyConfig1 = proplists:delete(proxy_username, KeyConfig0),
245-
KeyConfig2 = proplists:delete(proxy_password, KeyConfig1),
246-
set_env(key_config, KeyConfig2);
221+
KeyConfig0 = proplists:delete(proxy_host, KeyConfig),
222+
KeyConfig1 = proplists:delete(proxy_port, KeyConfig0),
223+
KeyConfig2 = proplists:delete(proxy_username, KeyConfig1),
224+
KeyConfig3 = proplists:delete(proxy_password, KeyConfig2),
225+
set_env(key_config, KeyConfig3);
247226
Id ->
248-
unset_oauth_provider_properties(Id, [proxy, proxy_username, proxy_password])
227+
unset_oauth_provider_properties(Id,
228+
[proxy_host, proxy_port, proxy_username, proxy_password])
249229
end,
250230
Config;
251-
end_per_group(oauth_provider_with_https_proxy, Config) ->
252-
end_per_group(oauth_provider_with_proxy, Config);
253231

254232
end_per_group(with_root_static_signing_keys, Config) ->
255233
KeyConfig = call_get_env(Config, key_config, []),
@@ -474,25 +452,7 @@ get_oauth_provider_has_jwks_uri(Config) ->
474452
get_oauth_provider_has_proxy(Config) ->
475453
{ok, OAuthProvider} = get_oauth_provider(
476454
?config(oauth_provider_id, Config), [jwks_uri]),
477-
?assertEqual(false,
478-
OAuthProvider#oauth_provider.proxy_options#proxy_options.https),
479-
480-
?assertEqual(?config(proxy_port, Config),
481-
OAuthProvider#oauth_provider.proxy_options#proxy_options.port),
482-
?assertEqual(?config(proxy_hostname, Config),
483-
OAuthProvider#oauth_provider.proxy_options#proxy_options.host),
484-
?assertEqual(?config(proxy_username, Config),
485-
OAuthProvider#oauth_provider.proxy_options#proxy_options.username),
486-
?assertEqual(?config(proxy_password, Config),
487-
OAuthProvider#oauth_provider.proxy_options#proxy_options.password).
488-
489-
490-
get_oauth_provider_has_https_proxy(Config) ->
491-
{ok, OAuthProvider} = get_oauth_provider(
492-
?config(oauth_provider_id, Config), [jwks_uri]),
493-
?assertEqual(true,
494-
OAuthProvider#oauth_provider.proxy_options#proxy_options.https),
495-
455+
496456
?assertEqual(?config(proxy_port, Config),
497457
OAuthProvider#oauth_provider.proxy_options#proxy_options.port),
498458
?assertEqual(?config(proxy_hostname, Config),

0 commit comments

Comments
 (0)