diff options
| author | John Estabrook <jestabro@vyos.io> | 2025-09-10 12:50:52 -0500 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2025-09-10 14:13:47 -0500 |
| commit | a0a14ea1ee38ce78f7433b3a93f7eea61c8163fc (patch) | |
| tree | 01f9d3e2b76d35ee2b459f14f5e2ffff96e7833f /src | |
| parent | 19d7e20f5cb7786f87e04a334e19415f18af6562 (diff) | |
| download | vyconf-a0a14ea1ee38ce78f7433b3a93f7eea61c8163fc.tar.gz vyconf-a0a14ea1ee38ce78f7433b3a93f7eea61c8163fc.zip | |
T7737: add user and sudo_user fields to session for use in commit
Add optional protobuf fields to convey environment variables USER and
SUDO_USER, as needed for some config_mode scripts. Add non-option fields
to the session record type, defaulting to the empty string.
Diffstat (limited to 'src')
| -rw-r--r-- | src/session.ml | 8 | ||||
| -rw-r--r-- | src/session.mli | 7 | ||||
| -rw-r--r-- | src/vycli.ml | 10 | ||||
| -rw-r--r-- | src/vyconf_cli.ml | 10 | ||||
| -rw-r--r-- | src/vyconf_client.ml | 10 | ||||
| -rw-r--r-- | src/vyconf_client.mli | 2 | ||||
| -rw-r--r-- | src/vyconf_client_session.ml | 12 | ||||
| -rw-r--r-- | src/vyconf_pbt.ml | 36 | ||||
| -rw-r--r-- | src/vyconf_pbt.mli | 4 | ||||
| -rw-r--r-- | src/vyconfd.ml | 13 |
10 files changed, 98 insertions, 14 deletions
diff --git a/src/session.ml b/src/session.ml index 5efc9f5..23c3d19 100644 --- a/src/session.ml +++ b/src/session.ml @@ -26,17 +26,19 @@ type session_data = { conf_mode: bool; changeset: cfg_op list; client_app: string; - user: string; client_pid: int32; + client_user: string; + client_sudo_user: string; } -let make world client_app user pid = { +let make world client_app sudo_user user pid = { proposed_config = world.running_config; modified = false; conf_mode = false; changeset = []; client_app = client_app; - user = user; + client_user = user; + client_sudo_user = sudo_user; client_pid = pid; } diff --git a/src/session.mli b/src/session.mli index abe5165..5dbbaf0 100644 --- a/src/session.mli +++ b/src/session.mli @@ -15,13 +15,14 @@ type session_data = { conf_mode: bool; changeset: cfg_op list; client_app: string; - user: string; - client_pid: int32 + client_pid: int32; + client_user: string; + client_sudo_user: string; } exception Session_error of string -val make : world -> string -> string -> int32 -> session_data +val make : world -> string -> string -> string -> int32 -> session_data val set_modified : session_data -> session_data diff --git a/src/vycli.ml b/src/vycli.ml index 174d6f4..6b13bdd 100644 --- a/src/vycli.ml +++ b/src/vycli.ml @@ -68,7 +68,15 @@ let main socket op path out_format config_format = end | OpSetupSession -> let pid = Int32.of_int (Unix.getppid ()) in - let%lwt resp = setup_session client "vycli" pid in + let user = + try Sys.getenv "USER" + with Not_found -> "" + in + let sudo_user = + try Sys.getenv "SUDO_USER" + with Not_found -> "" + in + let%lwt resp = setup_session client "vycli" sudo_user user pid in begin match resp with | Ok c -> get_token c diff --git a/src/vyconf_cli.ml b/src/vyconf_cli.ml index 0d1535e..b2ee630 100644 --- a/src/vyconf_cli.ml +++ b/src/vyconf_cli.ml @@ -39,6 +39,14 @@ let in_cli_config_session () = let get_session () = let pid = Int32.of_int (Unix.getppid()) in + let user = + try Sys.getenv "USER" + with Not_found -> "" + in + let sudo_user = + try Sys.getenv "SUDO_USER" + with Not_found -> "" + in let socket = "/var/run/vyconfd.sock" in let config_format = config_format_of_string "curly" in let out_format = output_format_of_string "plain" in @@ -47,7 +55,7 @@ let get_session () = in let%lwt resp = session_of_pid client pid in match resp with - | Error _ -> setup_session client "vyconf_cli" pid + | Error _ -> setup_session client "vyconf_cli" sudo_user user pid | _ as c -> c |> Lwt.return let close_session () = diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml index 05b5548..25b923f 100644 --- a/src/vyconf_client.ml +++ b/src/vyconf_client.ml @@ -53,10 +53,16 @@ let prompt client = let%lwt resp = do_request client req in Lwt.return resp -let setup_session ?(on_behalf_of=None) client client_app pid = +let setup_session ?(on_behalf_of=None) client client_app sudo_user user pid = if Option.is_some client.session then Lwt.return (Error "Client is already associated with a session") else let id = on_behalf_of |> (function None -> None | Some x -> (Some (Int32.of_int x))) in - let req = Setup_session {client_application=(Some client_app); on_behalf_of=id; client_pid=pid} in + let req = + Setup_session { client_application=(Some client_app); + on_behalf_of=id; + client_sudo_user=Some sudo_user; + client_user=Some user; + client_pid=pid; } + in let%lwt resp = do_request client req in match resp.status with | Success -> diff --git a/src/vyconf_client.mli b/src/vyconf_client.mli index 762495d..9c63652 100644 --- a/src/vyconf_client.mli +++ b/src/vyconf_client.mli @@ -8,7 +8,7 @@ val shutdown : t -> t Lwt.t val prompt : t -> Vyconf_connect.Vyconf_pbt.response Lwt.t -val setup_session : ?on_behalf_of:(int option) -> t -> string -> int32 -> (t, string) result Lwt.t +val setup_session : ?on_behalf_of:(int option) -> t -> string -> string -> string -> int32 -> (t, string) result Lwt.t val session_of_pid : t -> int32 -> (t, string) result Lwt.t diff --git a/src/vyconf_client_session.ml b/src/vyconf_client_session.ml index 93068fa..035f191 100644 --- a/src/vyconf_client_session.ml +++ b/src/vyconf_client_session.ml @@ -34,7 +34,17 @@ let call_op ?(out_format="plain") ?(config_format="curly") socket token op path match o with | OpSetupSession -> let pid = Int32.of_int (Unix.getppid ()) in - let%lwt resp = Vyconf_client.setup_session client "vyconf_client_session" pid in + let user = + try Sys.getenv "USER" + with Not_found -> "" + in + let sudo_user = + try Sys.getenv "SUDO_USER" + with Not_found -> "" + in + let%lwt resp = + Vyconf_client.setup_session client "vyconf_client_session" sudo_user user pid + in begin match resp with | Ok c -> Vyconf_client.get_token c diff --git a/src/vyconf_pbt.ml b/src/vyconf_pbt.ml index 96ba497..a89f9a8 100644 --- a/src/vyconf_pbt.ml +++ b/src/vyconf_pbt.ml @@ -14,6 +14,8 @@ type request_setup_session = { client_pid : int32; client_application : string option; on_behalf_of : int32 option; + client_user : string option; + client_sudo_user : string option; } type request_session_of_pid = { @@ -205,10 +207,14 @@ let rec default_request_setup_session ?client_pid:((client_pid:int32) = 0l) ?client_application:((client_application:string option) = None) ?on_behalf_of:((on_behalf_of:int32 option) = None) + ?client_user:((client_user:string option) = None) + ?client_sudo_user:((client_sudo_user:string option) = None) () : request_setup_session = { client_pid; client_application; on_behalf_of; + client_user; + client_sudo_user; } let rec default_request_session_of_pid @@ -433,12 +439,16 @@ type request_setup_session_mutable = { mutable client_pid : int32; mutable client_application : string option; mutable on_behalf_of : int32 option; + mutable client_user : string option; + mutable client_sudo_user : string option; } let default_request_setup_session_mutable () : request_setup_session_mutable = { client_pid = 0l; client_application = None; on_behalf_of = None; + client_user = None; + client_sudo_user = None; } type request_session_of_pid_mutable = { @@ -730,6 +740,8 @@ let rec pp_request_setup_session fmt (v:request_setup_session) = Pbrt.Pp.pp_record_field ~first:true "client_pid" Pbrt.Pp.pp_int32 fmt v.client_pid; Pbrt.Pp.pp_record_field ~first:false "client_application" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.client_application; Pbrt.Pp.pp_record_field ~first:false "on_behalf_of" (Pbrt.Pp.pp_option Pbrt.Pp.pp_int32) fmt v.on_behalf_of; + Pbrt.Pp.pp_record_field ~first:false "client_user" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.client_user; + Pbrt.Pp.pp_record_field ~first:false "client_sudo_user" (Pbrt.Pp.pp_option Pbrt.Pp.pp_string) fmt v.client_sudo_user; in Pbrt.Pp.pp_brk pp_i fmt () @@ -1008,6 +1020,18 @@ let rec encode_pb_request_setup_session (v:request_setup_session) encoder = Pbrt.Encoder.key 3 Pbrt.Varint encoder; | None -> (); end; + begin match v.client_user with + | Some x -> + Pbrt.Encoder.string x encoder; + Pbrt.Encoder.key 4 Pbrt.Bytes encoder; + | None -> (); + end; + begin match v.client_sudo_user with + | Some x -> + Pbrt.Encoder.string x encoder; + Pbrt.Encoder.key 5 Pbrt.Bytes encoder; + | None -> (); + end; () let rec encode_pb_request_session_of_pid (v:request_session_of_pid) encoder = @@ -1464,6 +1488,16 @@ let rec decode_pb_request_setup_session d = end | Some (3, pk) -> Pbrt.Decoder.unexpected_payload "Message(request_setup_session), field(3)" pk + | Some (4, Pbrt.Bytes) -> begin + v.client_user <- Some (Pbrt.Decoder.string d); + end + | Some (4, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_setup_session), field(4)" pk + | Some (5, Pbrt.Bytes) -> begin + v.client_sudo_user <- Some (Pbrt.Decoder.string d); + end + | Some (5, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_setup_session), field(5)" pk | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind done; begin if not !client_pid_is_set then Pbrt.Decoder.missing_field "client_pid" end; @@ -1471,6 +1505,8 @@ let rec decode_pb_request_setup_session d = client_pid = v.client_pid; client_application = v.client_application; on_behalf_of = v.on_behalf_of; + client_user = v.client_user; + client_sudo_user = v.client_sudo_user; } : request_setup_session) let rec decode_pb_request_session_of_pid d = diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli index e2c2245..2755f61 100644 --- a/src/vyconf_pbt.mli +++ b/src/vyconf_pbt.mli @@ -21,6 +21,8 @@ type request_setup_session = { client_pid : int32; client_application : string option; on_behalf_of : int32 option; + client_user : string option; + client_sudo_user : string option; } type request_session_of_pid = { @@ -218,6 +220,8 @@ val default_request_setup_session : ?client_pid:int32 -> ?client_application:string option -> ?on_behalf_of:int32 option -> + ?client_user:string option -> + ?client_sudo_user:string option -> unit -> request_setup_session (** [default_request_setup_session ()] is the default value for type [request_setup_session] *) diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 51014c7..0a0d246 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -67,9 +67,18 @@ let make_session_token () = let setup_session world (req: request_setup_session) = let token = make_session_token () in let pid = req.client_pid in - let user = "unknown user" in + let user = + match req.client_user with + | None -> "" + | Some u -> u + in + let sudo_user = + match req.client_sudo_user with + | None -> "" + | Some u -> u + in let client_app = Option.value req.client_application ~default:"unknown client" in - let () = Hashtbl.add sessions token (Session.make world client_app user pid) in + let () = Hashtbl.add sessions token (Session.make world client_app sudo_user user pid) in {response_tmpl with output=(Some token)} let session_of_pid _world (req: request_session_of_pid) = |
