Skip to content

Commit addd62e

Browse files
committed
* Continue adding /api/ldap/validate/simple-bind tests
1 parent 15c153e commit addd62e

File tree

2 files changed

+93
-8
lines changed

2 files changed

+93
-8
lines changed

deps/rabbitmq_auth_backend_ldap/src/rabbit_auth_backend_ldap_mgmt.erl

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ maybe_add_ssl_options(Options0, true, BodyMap) ->
126126

127127
tls_options(BodyMap) when is_map_key(ssl_options, BodyMap) ->
128128
SslOptionsMap = maps:get(ssl_options, BodyMap),
129+
case is_map(SslOptionsMap) of
130+
false ->
131+
throw({bad_request, "ssl_options must be a map/object"});
132+
true ->
133+
ok
134+
end,
129135
CaCertfile = maps:get(<<"cacertfile">>, SslOptionsMap, undefined),
130136
CaCertPemData = maps:get(<<"cacert_pem_data">>, SslOptionsMap, undefined),
131137
TlsOpts0 = case {CaCertfile, CaCertPemData} of
@@ -146,12 +152,20 @@ tls_options(BodyMap) when is_map_key(ssl_options, BodyMap) ->
146152
TlsOpts1;
147153
CaCertPems when is_list(CaCertPems) ->
148154
F0 = fun (P) ->
149-
case public_key:pem_decode(P) of
150-
[{'Certificate', CaCertDerEncoded, not_encrypted}] ->
151-
{true, CaCertDerEncoded};
152-
_Unexpected ->
153-
throw({bad_request, "unexpected cacert_pem_data passed to "
154-
"/ldap/validate/simple-bind ssl_options.cacerts"})
155+
try
156+
case public_key:pem_decode(P) of
157+
[{'Certificate', CaCertDerEncoded, not_encrypted}] ->
158+
{true, CaCertDerEncoded};
159+
[] ->
160+
throw({bad_request, "invalid PEM data in cacert_pem_data: "
161+
"no valid certificates found"});
162+
_Unexpected ->
163+
throw({bad_request, "unexpected cacert_pem_data passed to "
164+
"/ldap/validate/simple-bind ssl_options.cacerts"})
165+
end
166+
catch
167+
error:Reason ->
168+
throw({bad_request, unicode_format("invalid PEM data in cacert_pem_data: ~tp", [Reason])})
155169
end
156170
end,
157171
CaCertsDerEncoded = lists:filtermap(F0, CaCertPems),
@@ -163,8 +177,14 @@ tls_options(BodyMap) when is_map_key(ssl_options, BodyMap) ->
163177
undefined ->
164178
TlsOpts2;
165179
Verify ->
166-
VerifyStr = unicode:characters_to_list(Verify),
167-
[{verify, list_to_existing_atom(VerifyStr)} | TlsOpts2]
180+
try
181+
VerifyStr = unicode:characters_to_list(Verify),
182+
[{verify, list_to_existing_atom(VerifyStr)} | TlsOpts2]
183+
catch
184+
error:badarg ->
185+
throw({bad_request, "invalid verify option passed to "
186+
"/ldap/validate/simple-bind ssl_options.verify"})
187+
end
168188
end,
169189
TlsOpts4 = case maps:get(<<"server_name_indication">>, SslOptionsMap, disable) of
170190
disable ->

deps/rabbitmq_auth_backend_ldap/test/system_SUITE.erl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,71 @@ validate_ldap_configuration_via_api(Config) ->
523523
?assertEqual(<<"unprocessable_entity">>, maps:get(<<"error">>, BothTlsJson)),
524524
?assertEqual(<<"TLS configuration error: cannot use StartTLS on an SSL connection (use_ssl and use_starttls cannot both be true)">>,
525525
maps:get(<<"reason">>, BothTlsJson)),
526+
527+
%% Invalid certificate file path
528+
http_put(Config, "/ldap/validate/simple-bind",
529+
#{
530+
'user_dn' => AliceUserDN,
531+
'password' => Password,
532+
'servers' => ["localhost"],
533+
'port' => LdapTlsPort,
534+
'use_ssl' => true,
535+
'ssl_options' => #{
536+
'cacertfile' => "/nonexistent/path/cert.pem"
537+
}
538+
}, ?BAD_REQUEST),
539+
540+
%% Invalid PEM data - should now return 400 Bad Request
541+
http_put(Config, "/ldap/validate/simple-bind",
542+
#{
543+
'user_dn' => AliceUserDN,
544+
'password' => Password,
545+
'servers' => ["localhost"],
546+
'port' => LdapTlsPort,
547+
'use_ssl' => true,
548+
'ssl_options' => #{
549+
'cacert_pem_data' => ["not-valid-pem-data"]
550+
}
551+
}, ?BAD_REQUEST),
552+
553+
%% Invalid SSL options structure - not a map
554+
http_put(Config, "/ldap/validate/simple-bind",
555+
#{
556+
'user_dn' => AliceUserDN,
557+
'password' => Password,
558+
'servers' => ["localhost"],
559+
'port' => LdapTlsPort,
560+
'use_ssl' => true,
561+
'ssl_options' => "not_a_map"
562+
}, ?BAD_REQUEST),
563+
564+
%% Invalid TLS versions
565+
http_put(Config, "/ldap/validate/simple-bind",
566+
#{
567+
'user_dn' => AliceUserDN,
568+
'password' => Password,
569+
'servers' => ["localhost"],
570+
'port' => LdapTlsPort,
571+
'use_ssl' => true,
572+
'ssl_options' => #{
573+
'versions' => ["invalid_version", "tlsv1.2"],
574+
'cacertfile' => CaCertfile
575+
}
576+
}, ?BAD_REQUEST),
577+
578+
%% Invalid verify option
579+
http_put(Config, "/ldap/validate/simple-bind",
580+
#{
581+
'user_dn' => AliceUserDN,
582+
'password' => Password,
583+
'servers' => ["localhost"],
584+
'port' => LdapTlsPort,
585+
'use_ssl' => true,
586+
'ssl_options' => #{
587+
'verify' => "invalid_verify_option",
588+
'cacertfile' => CaCertfile
589+
}
590+
}, ?BAD_REQUEST),
526591
http_put(Config, "/ldap/validate/simple-bind",
527592
#{
528593
'user_dn' => AliceUserDN,

0 commit comments

Comments
 (0)