Skip to content

Commit 76ad85f

Browse files
authored
CA-407687/XSI-1834: get_subject_information_from_identifier should (#6344)
query xapi db, then fallback to query domain DC get_subject_information_from_identifier query subject details from subject id. It triggers some DNS query to do kerberos query, this causes the problem that authenticating to XAPI with an AD account causes large amounts of Kerberos / DNS traffic The subject details are actually cached in xapi db and refreshed default in every 10 minutes. get_subject_information_from_identifier should query subject details from xapi DB and only fallback to DC when xapi DB does not have it.
2 parents 75a34ed + 76b46f6 commit 76ad85f

File tree

4 files changed

+51
-43
lines changed

4 files changed

+51
-43
lines changed

ocaml/xapi/extauth.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,19 @@ let call_extauth_hook_script_in_pool ~__context event_name =
193193
event_name ;
194194
[]
195195
)
196+
197+
let call_with_exception_handler fn =
198+
try fn () with
199+
| Extauth_is_disabled ->
200+
raise (Api_errors.Server_error (Api_errors.auth_is_disabled, []))
201+
| Unknown_extauth_type msg ->
202+
raise (Api_errors.Server_error (Api_errors.auth_unknown_type, [msg]))
203+
| Not_found | Auth_signature.Subject_cannot_be_resolved ->
204+
raise (Api_errors.Server_error (Api_errors.subject_cannot_be_resolved, []))
205+
| Auth_signature.Auth_service_error (_, msg) ->
206+
raise (Api_errors.Server_error (Api_errors.auth_service_error, [msg]))
207+
| e ->
208+
raise
209+
(Api_errors.Server_error
210+
(Api_errors.auth_service_error, [ExnHelper.string_of_exn e])
211+
)

ocaml/xapi/xapi_auth.ml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,6 @@
1818
open Auth_signature
1919
open Extauth
2020

21-
let call_with_exception_handler fn =
22-
try fn () with
23-
| Extauth.Extauth_is_disabled ->
24-
raise (Api_errors.Server_error (Api_errors.auth_is_disabled, []))
25-
| Extauth.Unknown_extauth_type msg ->
26-
raise (Api_errors.Server_error (Api_errors.auth_unknown_type, [msg]))
27-
| Not_found | Auth_signature.Subject_cannot_be_resolved ->
28-
raise (Api_errors.Server_error (Api_errors.subject_cannot_be_resolved, []))
29-
| Auth_signature.Auth_service_error (_, msg) ->
30-
raise (Api_errors.Server_error (Api_errors.auth_service_error, [msg]))
31-
| e ->
32-
raise
33-
(Api_errors.Server_error
34-
(Api_errors.auth_service_error, [ExnHelper.string_of_exn e])
35-
)
36-
3721
(* PRECONDITION: All of these additional calls require a valid session to be presented.*)
3822
(* ==> the session validity is already checked in every server.ml call by using Session_check.check *)
3923

@@ -49,5 +33,12 @@ let get_group_membership ~__context ~subject_identifier =
4933

5034
let get_subject_information_from_identifier ~__context ~subject_identifier =
5135
call_with_exception_handler (fun () ->
52-
(Ext_auth.d ()).query_subject_information ~__context subject_identifier
36+
try
37+
(* Query from xapi db first *)
38+
Xapi_subject.query_subject_information_from_db ~__context
39+
subject_identifier
40+
with Auth_signature.Subject_cannot_be_resolved ->
41+
(* Not found, fall back to query AD *)
42+
Xapi_subject.query_subject_information_from_AD ~__context
43+
subject_identifier
5344
)

ocaml/xapi/xapi_pool.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,9 +2661,8 @@ let revalidate_subjects ~__context =
26612661
let subj_id = Db.Subject.get_subject_identifier ~__context ~self in
26622662
debug "Revalidating subject %s" subj_id ;
26632663
try
2664-
let open Auth_signature in
2665-
ignore
2666-
((Extauth.Ext_auth.d ()).query_subject_information ~__context subj_id)
2664+
Xapi_subject.query_subject_information_from_AD ~__context subj_id
2665+
|> ignore
26672666
with Not_found ->
26682667
debug "Destroying subject %s" subj_id ;
26692668
Xapi_subject.destroy ~__context ~self

ocaml/xapi/xapi_subject.ml

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ let asynchronously_run_hook_script_after_subject_add =
3939
At_least_once_more.make "running after-subject-add hook script"
4040
run_hook_script_after_subject_add
4141

42+
let query_subject_information_from_db ~__context identifier =
43+
let open Xapi_database.Db_filter_types in
44+
match
45+
Db.Subject.get_records_where ~__context
46+
~expr:(Eq (Field "subject_identifier", Literal identifier))
47+
with
48+
| [] ->
49+
raise Auth_signature.Subject_cannot_be_resolved
50+
| x :: _ ->
51+
let subject_r = snd x in
52+
subject_r.API.subject_other_config
53+
54+
let query_subject_information_from_AD ~__context identifier =
55+
(Extauth.Ext_auth.d ()).query_subject_information ~__context identifier
56+
57+
let get_subject_information_from_identifier ~__context ~cache identifier =
58+
if cache then
59+
query_subject_information_from_db ~__context identifier
60+
else
61+
query_subject_information_from_AD ~__context identifier
62+
4263
let create ~__context ~subject_identifier ~other_config:_ =
4364
(* If at least one of the hosts uses AD external auth, then assert that the AD feature is enabled *)
4465
let hosts = Db.Host.get_all ~__context in
@@ -87,8 +108,8 @@ let create ~__context ~subject_identifier ~other_config:_ =
87108
in
88109
(* subject_info is overrided by subject info queried form DC *)
89110
let subject_info =
90-
Xapi_auth.get_subject_information_from_identifier ~__context
91-
~subject_identifier
111+
Extauth.call_with_exception_handler @@ fun () ->
112+
query_subject_information_from_AD ~__context subject_identifier
92113
in
93114
Db.Subject.create ~__context ~ref ~uuid ~subject_identifier
94115
~other_config:subject_info ~roles:default_roles ;
@@ -130,8 +151,8 @@ let update ~__context ~self =
130151
(* query external directory service *)
131152
(* this might raise an exception *)
132153
let subject_info =
133-
Xapi_auth.get_subject_information_from_identifier ~__context
134-
~subject_identifier
154+
Extauth.call_with_exception_handler @@ fun () ->
155+
query_subject_information_from_AD ~__context subject_identifier
135156
in
136157
if Db.Subject.get_other_config ~__context ~self <> subject_info then (
137158
(* update locally the fresh information received from external directory service *)
@@ -243,22 +264,3 @@ let remove_from_roles ~__context ~self ~role =
243264
(Ref.string_of role) ;
244265
raise (Api_errors.Server_error (Api_errors.role_not_found, []))
245266
)
246-
247-
let query_subject_information_from_db ~__context identifier =
248-
let open Xapi_database.Db_filter_types in
249-
match
250-
Db.Subject.get_records_where ~__context
251-
~expr:(Eq (Field "subject_identifier", Literal identifier))
252-
with
253-
| [] ->
254-
raise Auth_signature.Subject_cannot_be_resolved
255-
| x :: _ ->
256-
let subject_r = snd x in
257-
subject_r.API.subject_other_config
258-
259-
let get_subject_information_from_identifier ~__context ~cache identifier =
260-
let open Extauth in
261-
if cache then
262-
query_subject_information_from_db ~__context identifier
263-
else
264-
(Ext_auth.d ()).query_subject_information ~__context identifier

0 commit comments

Comments
 (0)