From 8e3398cd6cbfdfce33ffd79de5dc6929ee6aae43 Mon Sep 17 00:00:00 2001 From: Lunfan Zhang Date: Mon, 14 Aug 2023 17:48:50 +0800 Subject: [PATCH] CP-40651 Validate the login username and provide friendly error message - update exception data format Signed-off-by: Lunfan Zhang --- ocaml/tests/test_session.ml | 9 ++++++++- ocaml/xapi/xapi_http.ml | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ocaml/tests/test_session.ml b/ocaml/tests/test_session.ml index 636a7fa9699..4dea0e44312 100644 --- a/ocaml/tests/test_session.ml +++ b/ocaml/tests/test_session.ml @@ -6,10 +6,17 @@ let future = Date.of_string "2020-09-22T15:03:13Z" let fail_login ~__context ~uname ~originator ~now () = try + let user_name = Option.get uname in Xapi_session._record_login_failure ~__context ~now ~uname ~originator ~record:`log_and_alert (fun () -> if Random.bool () then - raise Api_errors.(Server_error (session_authentication_failed, [uname, "Authentication failed"])) + raise + Api_errors.( + Server_error + ( session_authentication_failed + , [user_name; "Authentication failed"] + ) + ) else raise (Auth_signature.Auth_failure "Auth failure") ) diff --git a/ocaml/xapi/xapi_http.ml b/ocaml/xapi/xapi_http.ml index fc3b66e3bad..12e270c02ab 100644 --- a/ocaml/xapi/xapi_http.ml +++ b/ocaml/xapi/xapi_http.ml @@ -261,7 +261,11 @@ let with_context ?(dummy = false) label (req : Request.t) (s : Unix.file_descr) with Http.Unauthorised _ as e -> let fail __context = TaskHelper.failed ~__context - (Api_errors.Server_error (Api_errors.session_authentication_failed, [uname, "Authentication required to access the resource"])) + (Api_errors.Server_error + ( Api_errors.session_authentication_failed + , [""; "Authentication required to access the resource"] + ) + ) in debug "No authentication provided to http handler: returning 401 unauthorised" ;