Skip to content

Commit 2778096

Browse files
committed
CA-414418: Code refine for comments
- Rename session_timeout -> session_timed_out - Assoc List -> Map to store the check result - Apply check direclty instead of remembering check status Signed-off-by: Lin Liu <[email protected]>
1 parent 92377bf commit 2778096

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

ocaml/xapi/xapi_session.ml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Listext = Xapi_stdext_std.Listext
3232
open Client
3333
open Auth_signature
3434
open Extauth
35+
module SessionValidateMap = Map.Make (String)
3536

3637
module AuthFail : sig
3738
(* stats are reset each time you query, so if there hasn't
@@ -455,7 +456,7 @@ let revalidate_external_session ~__context acc session =
455456
(Db.Session.get_validation_time ~__context ~self:session)
456457
in
457458
let now = Date.now () in
458-
let session_timeout =
459+
let session_timed_out =
459460
Date.to_unix_time now
460461
> session_last_validation_time +. session_lifespan +. random_lifespan
461462
in
@@ -464,25 +465,24 @@ let revalidate_external_session ~__context acc session =
464465
let authenticated_user_sid =
465466
Db.Session.get_auth_user_sid ~__context ~self:session
466467
in
467-
let user_validated =
468-
(* acc is [(sid, check_result)] , true for check pass, false for check failed *)
469-
match List.assoc_opt authenticated_user_sid acc with
468+
let validate_with_memo acc f =
469+
match SessionValidateMap.find_opt authenticated_user_sid acc with
470470
| None ->
471-
false
472-
| Some v ->
473-
if v = false && session_timeout then (
474-
debug
475-
"Destory session %s as previous check for user %s not pass"
476-
(trackid session) authenticated_user_sid ;
477-
destroy_db_session ~__context ~self:session
478-
) ;
479-
debug "Skip check session %s as previous check for user %s exists"
471+
f acc
472+
| Some false ->
473+
debug "Destory session %s as previous check for user %s not pass"
474+
(trackid session) authenticated_user_sid ;
475+
destroy_db_session ~__context ~self:session ;
476+
acc
477+
| Some true ->
478+
debug "Skip check session %s as previous check for user %s pass"
480479
(trackid session) authenticated_user_sid ;
481-
true
480+
acc
482481
in
483482

484-
if session_timeout && not user_validated then (
483+
if session_timed_out then (
485484
(* if so, then:*)
485+
validate_with_memo acc @@ fun acc ->
486486
debug "session %s needs revalidation" (trackid session) ;
487487

488488
(* 2a. revalidate external authentication *)
@@ -498,7 +498,7 @@ let revalidate_external_session ~__context acc session =
498498
authenticated_user_sid (trackid session) ;
499499
(* we must destroy the session in this case *)
500500
destroy_db_session ~__context ~self:session ;
501-
(authenticated_user_sid, false) :: acc
501+
SessionValidateMap.add authenticated_user_sid false acc
502502
) else
503503
try
504504
(* if the user is not in the external directory service anymore, this call raises Not_found *)
@@ -536,7 +536,7 @@ let revalidate_external_session ~__context acc session =
536536
debug "%s" msg ;
537537
(* we must destroy the session in this case *)
538538
destroy_db_session ~__context ~self:session ;
539-
(authenticated_user_sid, false) :: acc
539+
SessionValidateMap.add authenticated_user_sid false acc
540540
) else (
541541
(* non-empty intersection: externally-authenticated subject still has login rights in the pool *)
542542

@@ -565,7 +565,7 @@ let revalidate_external_session ~__context acc session =
565565
(trackid session) authenticated_user_sid
566566
) ;
567567
debug "end revalidation of session %s " (trackid session) ;
568-
(authenticated_user_sid, true) :: acc
568+
SessionValidateMap.add authenticated_user_sid true acc
569569
with Not_found ->
570570
(* subject ref for intersection's sid does not exist in our metadata!!! *)
571571
(* this should never happen, it's an internal metadata inconsistency between steps 2b and 2c *)
@@ -578,7 +578,7 @@ let revalidate_external_session ~__context acc session =
578578
debug "%s" msg ;
579579
(* we must destroy the session in this case *)
580580
destroy_db_session ~__context ~self:session ;
581-
(authenticated_user_sid, false) :: acc
581+
SessionValidateMap.add authenticated_user_sid false acc
582582
)
583583
with Auth_signature.Subject_cannot_be_resolved | Not_found ->
584584
(* user was not found in external directory in order to obtain group membership *)
@@ -592,7 +592,7 @@ let revalidate_external_session ~__context acc session =
592592
debug "%s" msg ;
593593
(* user is not in the external directory anymore: we must destroy the session in this case *)
594594
destroy_db_session ~__context ~self:session ;
595-
(authenticated_user_sid, false) :: acc
595+
SessionValidateMap.add authenticated_user_sid false acc
596596
) else
597597
acc
598598
else
@@ -620,7 +620,9 @@ let revalidate_all_sessions ~__context =
620620
&& not (Db.Session.get_client_certificate ~__context ~self:session)
621621
)
622622
|> (* revalidate each external session *)
623-
List.fold_left (revalidate_external_session ~__context) []
623+
List.fold_left
624+
(revalidate_external_session ~__context)
625+
SessionValidateMap.empty
624626
|> ignore
625627
with e ->
626628
(*unexpected exception: we absorb it and print out a debug line *)

0 commit comments

Comments
 (0)