Skip to content

Commit 20d9f3c

Browse files
committed
Return 422 when auth fails, begin adding more tests
1 parent 9127acb commit 20d9f3c

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

deps/rabbitmq_auth_backend_ldap/src/rabbit_auth_backend_ldap_mgmt.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ content_types_accepted(ReqData, Context) ->
3535
{[{'*', accept_content}], ReqData, Context}.
3636

3737
allowed_methods(ReqData, Context) ->
38-
{[<<"HEAD">>, <<"PUT">>, <<"OPTIONS">>], ReqData, Context}.
38+
{[<<"PUT">>, <<"OPTIONS">>], ReqData, Context}.
3939

4040
resource_exists(ReqData, Context) ->
4141
{true, ReqData, Context}.
@@ -65,16 +65,16 @@ accept_content(ReqData0, Context) ->
6565
ok ->
6666
{true, ReqData1, Context};
6767
{error, invalidCredentials} ->
68-
rabbit_mgmt_util:not_authorised("invalid credentials", ReqData1, Context);
68+
rabbit_mgmt_util:unprocessable_entity("Invalid LDAP credentials", ReqData1, Context);
6969
{error, unwillingToPerform} ->
70-
rabbit_mgmt_util:not_authorised("invalid credentials", ReqData1, Context);
70+
rabbit_mgmt_util:unprocessable_entity("Invalid LDAP credentials", ReqData1, Context);
7171
{error, E} ->
7272
Reason = unicode_format(E),
73-
rabbit_mgmt_util:bad_request(Reason, ReqData1, Context)
73+
rabbit_mgmt_util:unprocessable_entity(Reason, ReqData1, Context)
7474
end;
7575
Error ->
7676
Reason = unicode_format(Error),
77-
rabbit_mgmt_util:bad_request(Reason, ReqData1, Context)
77+
rabbit_mgmt_util:unprocessable_entity(Reason, ReqData1, Context)
7878
end,
7979
eldap:close(LDAP),
8080
Result;

deps/rabbitmq_auth_backend_ldap/test/system_SUITE.erl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,42 @@ validate_ldap_configuration_via_api(Config) ->
316316
'servers' => ["localhost"],
317317
'port' => LdapPort
318318
}, ?METHOD_NOT_ALLOWED),
319+
%% Invalid JSON should return 400 Bad Request
320+
rabbit_mgmt_test_util:http_put_raw(Config, "/ldap/validate/simple-bind",
321+
"{invalid json", ?BAD_REQUEST),
322+
323+
%% HTTP Method coverage tests
324+
%% GET method - should return 405 Method Not Allowed
325+
?assertMatch({ok, {{_, ?METHOD_NOT_ALLOWED, _}, _Headers, _ResBody}},
326+
rabbit_mgmt_test_util:req(Config, 0, get, "/ldap/validate/simple-bind",
327+
[rabbit_mgmt_test_util:auth_header("guest", "guest")])),
328+
329+
%% HEAD method - should return 405 Method Not Allowed (same as GET)
330+
?assertMatch({ok, {{_, ?METHOD_NOT_ALLOWED, _}, _Headers, _ResBody}},
331+
rabbit_mgmt_test_util:req(Config, 0, head, "/ldap/validate/simple-bind",
332+
[rabbit_mgmt_test_util:auth_header("guest", "guest")])),
333+
334+
%% POST method - should return 405 Method Not Allowed
335+
?assertMatch({ok, {{_, ?METHOD_NOT_ALLOWED, _}, _Headers, _ResBody}},
336+
rabbit_mgmt_test_util:req(Config, 0, post, "/ldap/validate/simple-bind",
337+
[rabbit_mgmt_test_util:auth_header("guest", "guest")], "{}")),
338+
339+
%% DELETE method - should return 405 Method Not Allowed
340+
?assertMatch({ok, {{_, ?METHOD_NOT_ALLOWED, _}, _Headers, _ResBody}},
341+
rabbit_mgmt_test_util:req(Config, 0, delete, "/ldap/validate/simple-bind",
342+
[rabbit_mgmt_test_util:auth_header("guest", "guest")])),
343+
344+
%% OPTIONS method - should return 200 with Allow header showing only PUT, OPTIONS
345+
{ok, {{_, OptionsCode, _}, OptionsHeaders, _OptionsResBody}} =
346+
rabbit_mgmt_test_util:req(Config, 0, options, "/ldap/validate/simple-bind",
347+
[rabbit_mgmt_test_util:auth_header("guest", "guest")]),
348+
?assertEqual(?OK, OptionsCode),
349+
AllowHeader = proplists:get_value("allow", OptionsHeaders),
350+
?assert(string:str(string:to_upper(AllowHeader), "PUT") > 0),
351+
?assert(string:str(string:to_upper(AllowHeader), "OPTIONS") > 0),
352+
%% Should NOT contain GET or HEAD
353+
?assertEqual(0, string:str(string:to_upper(AllowHeader), "GET")),
354+
?assertEqual(0, string:str(string:to_upper(AllowHeader), "HEAD")),
319355
http_put(Config, "/ldap/validate/simple-bind",
320356
#{
321357
'user_dn' => AliceUserDN,
@@ -329,7 +365,7 @@ validate_ldap_configuration_via_api(Config) ->
329365
'password' => Password,
330366
'servers' => ["localhost"],
331367
'port' => LdapPort
332-
}, ?NOT_AUTHORISED),
368+
}, ?UNPROCESSABLE_ENTITY),
333369
http_put(Config, "/ldap/validate/simple-bind",
334370
#{
335371
'user_dn' => AliceUserDN,

deps/rabbitmq_ct_helpers/include/rabbit_mgmt_test.hrl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
-define(BAD_REQUEST, 400).
99
-define(NOT_AUTHORISED, 401).
1010
-define(METHOD_NOT_ALLOWED, 405).
11+
-define(UNPROCESSABLE_ENTITY, 422).
1112
%%-define(NOT_FOUND, 404). Defined for AMQP by amqp_client.hrl (as 404)
1213
%% httpc seems to get racy when using HTTP 1.1
1314
-define(HTTPC_OPTS, [{version, "HTTP/1.0"}, {autoredirect, false}]).

deps/rabbitmq_management/src/rabbit_mgmt_util.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
-export([user/1]).
2121
-export([bad_request/3, service_unavailable/3, not_authorised/3, bad_request_exception/4,
2222
internal_server_error/3, internal_server_error/4, precondition_failed/3,
23+
unprocessable_entity/3,
2324
id/2, parse_bool/1, parse_int/1, redirect_to_home/3]).
2425
-export([with_decode/4, not_found/3]).
2526
-export([with_channel/4, with_channel/5]).
@@ -665,6 +666,9 @@ a2b(B) -> B.
665666
bad_request(Reason, ReqData, Context) ->
666667
halt_response(400, bad_request, Reason, ReqData, Context).
667668

669+
unprocessable_entity(Reason, ReqData, Context) ->
670+
halt_response(422, unprocessable_entity, Reason, ReqData, Context).
671+
668672
service_unavailable(Reason, ReqData, Context) ->
669673
halt_response(503, service_unavailable, Reason, ReqData, Context).
670674

0 commit comments

Comments
 (0)