Skip to content

Commit cbdc58f

Browse files
authored
Document Rbac module (#6107)
Introduces an interface file for the Rbac module within xapi in order to document the intent of each of its functions. --- Cherry picked from another branch concerning `server.ml` generation; I want to reduce the surface area of these modules so we can be clear about what they are doing.
2 parents 670d811 + 3e2e970 commit cbdc58f

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

ocaml/xapi/rbac.ml

-5
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,6 @@ let assert_permission_name ~__context ~permission =
243243
let assert_permission ~__context ~permission =
244244
assert_permission_name ~__context ~permission:permission.role_name_label
245245

246-
(* this is necessary to break dependency cycle between rbac and taskhelper *)
247-
let init_task_helper_rbac_has_permission_fn =
248-
if !TaskHelper.rbac_assert_permission_fn = None then
249-
TaskHelper.rbac_assert_permission_fn := Some assert_permission
250-
251246
let has_permission_name ~__context ~permission =
252247
let session_id = get_session_of_context ~__context ~permission in
253248
is_access_allowed ~__context ~session_id ~permission

ocaml/xapi/rbac.mli

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
(*
2+
* Copyright (c) Cloud Software Group, Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
val is_access_allowed :
16+
__context:Context.t
17+
-> session_id:[`session] Ref.t
18+
-> permission:string
19+
-> bool
20+
(** Determines whether the session associated with the provided
21+
context has the specified permission. The permission set is cached
22+
(on the coordinator only) to benefit successive queries for the
23+
same session. *)
24+
25+
val check :
26+
?extra_dmsg:string
27+
-> ?extra_msg:string
28+
-> ?args:(string * Rpc.t) list
29+
-> ?keys:string list
30+
-> __context:Context.t
31+
-> fn:(unit -> 'a)
32+
-> [`session] Ref.t
33+
-> string
34+
-> 'a
35+
(** [check] executes a function associated with an action if the
36+
session associated with the provided context is authorised to
37+
perform the action.
38+
39+
The [?extra_dmsg] and [?extra_msg] parameters allow for extra
40+
information in debugging and error messages.
41+
42+
The [?keys] parameter specifies which fields of a (string -> _)
43+
map are RBAC-protected. It is primarily associated with
44+
auto-generated methods such as add_to_other_config. However, if
45+
[?keys] is non-empty, then [?args] must also be consulted as the
46+
related methods that require this protection specify their key
47+
name as a parameter. Otherwise, [?args] is mostly used to log
48+
calls within the RBAC audit log. *)
49+
50+
val check_with_new_task :
51+
?extra_dmsg:string
52+
-> ?extra_msg:string
53+
-> ?task_desc:string
54+
-> ?args:(string * Rpc.t) list
55+
-> fn:(unit -> 'a)
56+
-> [`session] Ref.t
57+
-> string
58+
-> 'a
59+
(** Defined in terms of [check] but using a context associated with a
60+
freshly-created task. *)
61+
62+
val assert_permission_name : __context:Context.t -> permission:string -> unit
63+
(** Performs a dry run of the [check] function with a no-op action
64+
guarded by the provided permission (as a name). *)
65+
66+
val assert_permission :
67+
__context:Context.t -> permission:Db_actions.role_t -> unit
68+
(** Performs a dry run of the [check] function with a no-op action
69+
guarded by the provided permission (as a database role). *)
70+
71+
val has_permission : __context:Context.t -> permission:Db_actions.role_t -> bool
72+
(** [has_permission ctx p] determines if the session associated with
73+
the context [ctx] is authorised to perform a specific action.
74+
75+
[p] is of the type defined by the generated [Db_actions] module,
76+
as [Xapi_role] simulates a database for the checking of static
77+
role sets (as emitted in [Rbac_static]) and only appeals to the
78+
xapi DB for additional roles. *)
79+
80+
val is_rbac_enabled_for_http_action : string -> bool
81+
(** [is_rbac_enabled_for_http_action route] determines whether RBAC
82+
checks should be applied to the provided HTTP [route].
83+
84+
Some routes are precluded from RBAC checks because they are
85+
assumed to only be used by code paths where RBAC has already been
86+
checked or will be checked internally (e.g. /post_cli). *)
87+
88+
val permission_of_action :
89+
?args:(string * Rpc.t) list -> keys:string list -> string -> string
90+
(** Constructs the name of a permission associated with using an
91+
RBAC-protected key with a specified action.
92+
93+
For example, if [keys] specifies "folder" as a protected key name
94+
for the action SR.remove_from_other_config, the permission name
95+
associated with that is "SR.remove_from_other_config/key:folder"
96+
- which is consistent with the format that [Rbac_static] contains. *)
97+
98+
val nofn : unit -> unit
99+
(** Named function that does nothing, e.g. (fun _ -> ()).
100+
Used as a dummy action for RBAC checking. *)
101+
102+
val destroy_session_permissions_tbl : session_id:[`session] Ref.t -> unit
103+
(** Removes any cached permission set for the given session. This is
104+
called when xapi destroys the DB entry for a session. *)

quality-gate.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ verify-cert () {
2525
}
2626

2727
mli-files () {
28-
N=499
28+
N=498
2929
# do not count ml files from the tests in ocaml/{tests/perftest/quicktest}
3030
MLIS=$(git ls-files -- '**/*.mli' | grep -vE "ocaml/tests|ocaml/perftest|ocaml/quicktest|ocaml/message-switch/core_test" | xargs -I {} sh -c "echo {} | cut -f 1 -d '.'" \;)
3131
MLS=$(git ls-files -- '**/*.ml' | grep -vE "ocaml/tests|ocaml/perftest|ocaml/quicktest|ocaml/message-switch/core_test" | xargs -I {} sh -c "echo {} | cut -f 1 -d '.'" \;)

0 commit comments

Comments
 (0)