From 92b9c5e1a47be12b1e5dd7c6f069e69d28465eac Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 23 Oct 2024 18:50:46 -0500 Subject: T6718: add rule to generate protobuf with name change Regenerate protobuf files: ocaml-protoc --ml_out src/ data/vyconf.proto The generated files vyconf.* are renamed vyconf_pbt.* instead of the split into vyconf_pb/vyconf_types as in the original implementation. --- src/vyconf_pbt.mli | 576 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 576 insertions(+) create mode 100644 src/vyconf_pbt.mli (limited to 'src/vyconf_pbt.mli') diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli new file mode 100644 index 0000000..fc0df2f --- /dev/null +++ b/src/vyconf_pbt.mli @@ -0,0 +1,576 @@ + +(** Code for vyconf.proto *) + +(* generated from "data/vyconf.proto", do not edit *) + + + +(** {2 Types} *) + +type request_config_format = + | Curly + | Json + +type request_output_format = + | Out_plain + | Out_json + +type request_status = unit + +type request_setup_session = { + client_application : string option; + on_behalf_of : int32 option; +} + +type request_set = { + path : string list; + ephemeral : bool option; +} + +type request_delete = { + path : string list; +} + +type request_rename = { + edit_level : string list; + from : string; + to_ : string; +} + +type request_copy = { + edit_level : string list; + from : string; + to_ : string; +} + +type request_comment = { + path : string list; + comment : string; +} + +type request_commit = { + confirm : bool option; + confirm_timeout : int32 option; + comment : string option; +} + +type request_rollback = { + revision : int32; +} + +type request_load = { + location : string; + format : request_config_format option; +} + +type request_merge = { + location : string; + format : request_config_format option; +} + +type request_save = { + location : string; + format : request_config_format option; +} + +type request_show_config = { + path : string list; + format : request_config_format option; +} + +type request_exists = { + path : string list; +} + +type request_get_value = { + path : string list; + output_format : request_output_format option; +} + +type request_get_values = { + path : string list; + output_format : request_output_format option; +} + +type request_list_children = { + path : string list; + output_format : request_output_format option; +} + +type request_run_op_mode = { + path : string list; + output_format : request_output_format option; +} + +type request_confirm = unit + +type request_enter_configuration_mode = { + exclusive : bool; + override_exclusive : bool; +} + +type request_exit_configuration_mode = unit + +type request = + | Status + | Setup_session of request_setup_session + | Set of request_set + | Delete of request_delete + | Rename of request_rename + | Copy of request_copy + | Comment of request_comment + | Commit of request_commit + | Rollback of request_rollback + | Merge of request_merge + | Save of request_save + | Show_config of request_show_config + | Exists of request_exists + | Get_value of request_get_value + | Get_values of request_get_values + | List_children of request_list_children + | Run_op_mode of request_run_op_mode + | Confirm + | Configure of request_enter_configuration_mode + | Exit_configure + | Teardown of string + +type request_envelope = { + token : string option; + request : request; +} + +type status = + | Success + | Fail + | Invalid_path + | Invalid_value + | Commit_in_progress + | Configuration_locked + | Internal_error + | Permission_denied + | Path_already_exists + +type response = { + status : status; + output : string option; + error : string option; + warning : string option; +} + + +(** {2 Basic values} *) + +val default_request_config_format : unit -> request_config_format +(** [default_request_config_format ()] is the default value for type [request_config_format] *) + +val default_request_output_format : unit -> request_output_format +(** [default_request_output_format ()] is the default value for type [request_output_format] *) + +val default_request_status : unit +(** [default_request_status ()] is the default value for type [request_status] *) + +val default_request_setup_session : + ?client_application:string option -> + ?on_behalf_of:int32 option -> + unit -> + request_setup_session +(** [default_request_setup_session ()] is the default value for type [request_setup_session] *) + +val default_request_set : + ?path:string list -> + ?ephemeral:bool option -> + unit -> + request_set +(** [default_request_set ()] is the default value for type [request_set] *) + +val default_request_delete : + ?path:string list -> + unit -> + request_delete +(** [default_request_delete ()] is the default value for type [request_delete] *) + +val default_request_rename : + ?edit_level:string list -> + ?from:string -> + ?to_:string -> + unit -> + request_rename +(** [default_request_rename ()] is the default value for type [request_rename] *) + +val default_request_copy : + ?edit_level:string list -> + ?from:string -> + ?to_:string -> + unit -> + request_copy +(** [default_request_copy ()] is the default value for type [request_copy] *) + +val default_request_comment : + ?path:string list -> + ?comment:string -> + unit -> + request_comment +(** [default_request_comment ()] is the default value for type [request_comment] *) + +val default_request_commit : + ?confirm:bool option -> + ?confirm_timeout:int32 option -> + ?comment:string option -> + unit -> + request_commit +(** [default_request_commit ()] is the default value for type [request_commit] *) + +val default_request_rollback : + ?revision:int32 -> + unit -> + request_rollback +(** [default_request_rollback ()] is the default value for type [request_rollback] *) + +val default_request_load : + ?location:string -> + ?format:request_config_format option -> + unit -> + request_load +(** [default_request_load ()] is the default value for type [request_load] *) + +val default_request_merge : + ?location:string -> + ?format:request_config_format option -> + unit -> + request_merge +(** [default_request_merge ()] is the default value for type [request_merge] *) + +val default_request_save : + ?location:string -> + ?format:request_config_format option -> + unit -> + request_save +(** [default_request_save ()] is the default value for type [request_save] *) + +val default_request_show_config : + ?path:string list -> + ?format:request_config_format option -> + unit -> + request_show_config +(** [default_request_show_config ()] is the default value for type [request_show_config] *) + +val default_request_exists : + ?path:string list -> + unit -> + request_exists +(** [default_request_exists ()] is the default value for type [request_exists] *) + +val default_request_get_value : + ?path:string list -> + ?output_format:request_output_format option -> + unit -> + request_get_value +(** [default_request_get_value ()] is the default value for type [request_get_value] *) + +val default_request_get_values : + ?path:string list -> + ?output_format:request_output_format option -> + unit -> + request_get_values +(** [default_request_get_values ()] is the default value for type [request_get_values] *) + +val default_request_list_children : + ?path:string list -> + ?output_format:request_output_format option -> + unit -> + request_list_children +(** [default_request_list_children ()] is the default value for type [request_list_children] *) + +val default_request_run_op_mode : + ?path:string list -> + ?output_format:request_output_format option -> + unit -> + request_run_op_mode +(** [default_request_run_op_mode ()] is the default value for type [request_run_op_mode] *) + +val default_request_confirm : unit +(** [default_request_confirm ()] is the default value for type [request_confirm] *) + +val default_request_enter_configuration_mode : + ?exclusive:bool -> + ?override_exclusive:bool -> + unit -> + request_enter_configuration_mode +(** [default_request_enter_configuration_mode ()] is the default value for type [request_enter_configuration_mode] *) + +val default_request_exit_configuration_mode : unit +(** [default_request_exit_configuration_mode ()] is the default value for type [request_exit_configuration_mode] *) + +val default_request : unit -> request +(** [default_request ()] is the default value for type [request] *) + +val default_request_envelope : + ?token:string option -> + ?request:request -> + unit -> + request_envelope +(** [default_request_envelope ()] is the default value for type [request_envelope] *) + +val default_status : unit -> status +(** [default_status ()] is the default value for type [status] *) + +val default_response : + ?status:status -> + ?output:string option -> + ?error:string option -> + ?warning:string option -> + unit -> + response +(** [default_response ()] is the default value for type [response] *) + + +(** {2 Formatters} *) + +val pp_request_config_format : Format.formatter -> request_config_format -> unit +(** [pp_request_config_format v] formats v *) + +val pp_request_output_format : Format.formatter -> request_output_format -> unit +(** [pp_request_output_format v] formats v *) + +val pp_request_status : Format.formatter -> request_status -> unit +(** [pp_request_status v] formats v *) + +val pp_request_setup_session : Format.formatter -> request_setup_session -> unit +(** [pp_request_setup_session v] formats v *) + +val pp_request_set : Format.formatter -> request_set -> unit +(** [pp_request_set v] formats v *) + +val pp_request_delete : Format.formatter -> request_delete -> unit +(** [pp_request_delete v] formats v *) + +val pp_request_rename : Format.formatter -> request_rename -> unit +(** [pp_request_rename v] formats v *) + +val pp_request_copy : Format.formatter -> request_copy -> unit +(** [pp_request_copy v] formats v *) + +val pp_request_comment : Format.formatter -> request_comment -> unit +(** [pp_request_comment v] formats v *) + +val pp_request_commit : Format.formatter -> request_commit -> unit +(** [pp_request_commit v] formats v *) + +val pp_request_rollback : Format.formatter -> request_rollback -> unit +(** [pp_request_rollback v] formats v *) + +val pp_request_load : Format.formatter -> request_load -> unit +(** [pp_request_load v] formats v *) + +val pp_request_merge : Format.formatter -> request_merge -> unit +(** [pp_request_merge v] formats v *) + +val pp_request_save : Format.formatter -> request_save -> unit +(** [pp_request_save v] formats v *) + +val pp_request_show_config : Format.formatter -> request_show_config -> unit +(** [pp_request_show_config v] formats v *) + +val pp_request_exists : Format.formatter -> request_exists -> unit +(** [pp_request_exists v] formats v *) + +val pp_request_get_value : Format.formatter -> request_get_value -> unit +(** [pp_request_get_value v] formats v *) + +val pp_request_get_values : Format.formatter -> request_get_values -> unit +(** [pp_request_get_values v] formats v *) + +val pp_request_list_children : Format.formatter -> request_list_children -> unit +(** [pp_request_list_children v] formats v *) + +val pp_request_run_op_mode : Format.formatter -> request_run_op_mode -> unit +(** [pp_request_run_op_mode v] formats v *) + +val pp_request_confirm : Format.formatter -> request_confirm -> unit +(** [pp_request_confirm v] formats v *) + +val pp_request_enter_configuration_mode : Format.formatter -> request_enter_configuration_mode -> unit +(** [pp_request_enter_configuration_mode v] formats v *) + +val pp_request_exit_configuration_mode : Format.formatter -> request_exit_configuration_mode -> unit +(** [pp_request_exit_configuration_mode v] formats v *) + +val pp_request : Format.formatter -> request -> unit +(** [pp_request v] formats v *) + +val pp_request_envelope : Format.formatter -> request_envelope -> unit +(** [pp_request_envelope v] formats v *) + +val pp_status : Format.formatter -> status -> unit +(** [pp_status v] formats v *) + +val pp_response : Format.formatter -> response -> unit +(** [pp_response v] formats v *) + + +(** {2 Protobuf Encoding} *) + +val encode_pb_request_config_format : request_config_format -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_config_format v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_output_format : request_output_format -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_output_format v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_status : request_status -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_status v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_setup_session : request_setup_session -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_setup_session v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_set : request_set -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_set v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_delete : request_delete -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_delete v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_rename : request_rename -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_rename v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_copy : request_copy -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_copy v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_comment : request_comment -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_comment v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_commit : request_commit -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_commit v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_rollback : request_rollback -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_rollback v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_load : request_load -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_load v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_merge : request_merge -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_merge v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_save : request_save -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_save v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_show_config : request_show_config -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_show_config v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_exists : request_exists -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_exists v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_get_value : request_get_value -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_get_value v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_get_values : request_get_values -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_get_values v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_list_children : request_list_children -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_list_children v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_run_op_mode : request_run_op_mode -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_run_op_mode v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_confirm : request_confirm -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_confirm v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_enter_configuration_mode : request_enter_configuration_mode -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_enter_configuration_mode v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_exit_configuration_mode : request_exit_configuration_mode -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_exit_configuration_mode v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request : request -> Pbrt.Encoder.t -> unit +(** [encode_pb_request v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_request_envelope : request_envelope -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_envelope v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_status : status -> Pbrt.Encoder.t -> unit +(** [encode_pb_status v encoder] encodes [v] with the given [encoder] *) + +val encode_pb_response : response -> Pbrt.Encoder.t -> unit +(** [encode_pb_response v encoder] encodes [v] with the given [encoder] *) + + +(** {2 Protobuf Decoding} *) + +val decode_pb_request_config_format : Pbrt.Decoder.t -> request_config_format +(** [decode_pb_request_config_format decoder] decodes a [request_config_format] binary value from [decoder] *) + +val decode_pb_request_output_format : Pbrt.Decoder.t -> request_output_format +(** [decode_pb_request_output_format decoder] decodes a [request_output_format] binary value from [decoder] *) + +val decode_pb_request_status : Pbrt.Decoder.t -> request_status +(** [decode_pb_request_status decoder] decodes a [request_status] binary value from [decoder] *) + +val decode_pb_request_setup_session : Pbrt.Decoder.t -> request_setup_session +(** [decode_pb_request_setup_session decoder] decodes a [request_setup_session] binary value from [decoder] *) + +val decode_pb_request_set : Pbrt.Decoder.t -> request_set +(** [decode_pb_request_set decoder] decodes a [request_set] binary value from [decoder] *) + +val decode_pb_request_delete : Pbrt.Decoder.t -> request_delete +(** [decode_pb_request_delete decoder] decodes a [request_delete] binary value from [decoder] *) + +val decode_pb_request_rename : Pbrt.Decoder.t -> request_rename +(** [decode_pb_request_rename decoder] decodes a [request_rename] binary value from [decoder] *) + +val decode_pb_request_copy : Pbrt.Decoder.t -> request_copy +(** [decode_pb_request_copy decoder] decodes a [request_copy] binary value from [decoder] *) + +val decode_pb_request_comment : Pbrt.Decoder.t -> request_comment +(** [decode_pb_request_comment decoder] decodes a [request_comment] binary value from [decoder] *) + +val decode_pb_request_commit : Pbrt.Decoder.t -> request_commit +(** [decode_pb_request_commit decoder] decodes a [request_commit] binary value from [decoder] *) + +val decode_pb_request_rollback : Pbrt.Decoder.t -> request_rollback +(** [decode_pb_request_rollback decoder] decodes a [request_rollback] binary value from [decoder] *) + +val decode_pb_request_load : Pbrt.Decoder.t -> request_load +(** [decode_pb_request_load decoder] decodes a [request_load] binary value from [decoder] *) + +val decode_pb_request_merge : Pbrt.Decoder.t -> request_merge +(** [decode_pb_request_merge decoder] decodes a [request_merge] binary value from [decoder] *) + +val decode_pb_request_save : Pbrt.Decoder.t -> request_save +(** [decode_pb_request_save decoder] decodes a [request_save] binary value from [decoder] *) + +val decode_pb_request_show_config : Pbrt.Decoder.t -> request_show_config +(** [decode_pb_request_show_config decoder] decodes a [request_show_config] binary value from [decoder] *) + +val decode_pb_request_exists : Pbrt.Decoder.t -> request_exists +(** [decode_pb_request_exists decoder] decodes a [request_exists] binary value from [decoder] *) + +val decode_pb_request_get_value : Pbrt.Decoder.t -> request_get_value +(** [decode_pb_request_get_value decoder] decodes a [request_get_value] binary value from [decoder] *) + +val decode_pb_request_get_values : Pbrt.Decoder.t -> request_get_values +(** [decode_pb_request_get_values decoder] decodes a [request_get_values] binary value from [decoder] *) + +val decode_pb_request_list_children : Pbrt.Decoder.t -> request_list_children +(** [decode_pb_request_list_children decoder] decodes a [request_list_children] binary value from [decoder] *) + +val decode_pb_request_run_op_mode : Pbrt.Decoder.t -> request_run_op_mode +(** [decode_pb_request_run_op_mode decoder] decodes a [request_run_op_mode] binary value from [decoder] *) + +val decode_pb_request_confirm : Pbrt.Decoder.t -> request_confirm +(** [decode_pb_request_confirm decoder] decodes a [request_confirm] binary value from [decoder] *) + +val decode_pb_request_enter_configuration_mode : Pbrt.Decoder.t -> request_enter_configuration_mode +(** [decode_pb_request_enter_configuration_mode decoder] decodes a [request_enter_configuration_mode] binary value from [decoder] *) + +val decode_pb_request_exit_configuration_mode : Pbrt.Decoder.t -> request_exit_configuration_mode +(** [decode_pb_request_exit_configuration_mode decoder] decodes a [request_exit_configuration_mode] binary value from [decoder] *) + +val decode_pb_request : Pbrt.Decoder.t -> request +(** [decode_pb_request decoder] decodes a [request] binary value from [decoder] *) + +val decode_pb_request_envelope : Pbrt.Decoder.t -> request_envelope +(** [decode_pb_request_envelope decoder] decodes a [request_envelope] binary value from [decoder] *) + +val decode_pb_status : Pbrt.Decoder.t -> status +(** [decode_pb_status decoder] decodes a [status] binary value from [decoder] *) + +val decode_pb_response : Pbrt.Decoder.t -> response +(** [decode_pb_response decoder] decodes a [response] binary value from [decoder] *) -- cgit v1.2.3 From bd17726d30991619eca09bfe478659915bc12fe4 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 23 Oct 2024 18:50:46 -0500 Subject: T6718: add independent validate field and methods --- data/vyconf.proto | 10 +++++-- src/session.ml | 16 ++++++++--- src/session.mli | 2 ++ src/vycli.ml | 3 ++ src/vyconf_client.ml | 7 +++++ src/vyconf_client.mli | 2 ++ src/vyconf_pbt.ml | 78 +++++++++++++++++++++++++++++++++++++++++++++++++-- src/vyconf_pbt.mli | 22 +++++++++++++++ src/vyconfd.ml | 8 ++++++ 9 files changed, 140 insertions(+), 8 deletions(-) (limited to 'src/vyconf_pbt.mli') diff --git a/data/vyconf.proto b/data/vyconf.proto index 6bd2796..f2245aa 100644 --- a/data/vyconf.proto +++ b/data/vyconf.proto @@ -17,6 +17,11 @@ message Request { optional int32 OnBehalfOf = 2; } + message Validate { + repeated string Path = 1; + optional OutputFormat output_format = 2; + } + message Set { repeated string Path = 1; optional bool Ephemeral = 3; @@ -129,8 +134,9 @@ message Request { Confirm confirm = 18; EnterConfigurationMode configure = 19; ExitConfigurationMode exit_configure = 20; - string teardown = 21; - } + Validate validate = 21; + string teardown = 22; + } } message RequestEnvelope { diff --git a/src/session.ml b/src/session.ml index db3b039..a8eccad 100644 --- a/src/session.ml +++ b/src/session.ml @@ -64,9 +64,17 @@ let rec apply_changes changeset config = | [] -> config | c :: cs -> apply_changes cs (apply_cfg_op c config) +let validate w _s path = + try + RT.validate_path D.(w.dirs.validators) w.reference_tree path + with RT.Validation_error x -> raise (Session_error x) + +let split_path w _s path = + RT.split_path w.reference_tree path + let set w s path = - let path, value = RT.validate_path D.(w.dirs.validators) - w.reference_tree path in + let _ = validate w s path in + let path, value = split_path w s path in let refpath = RT.refpath w.reference_tree path in let value_behaviour = if RT.is_multi w.reference_tree refpath then CT.AddValue else CT.ReplaceValue in let op = CfgSet (path, value, value_behaviour) in @@ -74,8 +82,8 @@ let set w s path = {s with proposed_config=config; changeset=(op :: s.changeset)} let delete w s path = - let path, value = RT.validate_path D.(w.dirs.validators) - w.reference_tree path in + let _ = validate w s path in + let path, value = split_path w s path in let op = CfgDelete (path, value) in let config = apply_cfg_op op s.proposed_config in {s with proposed_config=config; changeset=(op :: s.changeset)} diff --git a/src/session.mli b/src/session.mli index 8d10707..16d8e35 100644 --- a/src/session.mli +++ b/src/session.mli @@ -26,6 +26,8 @@ val set_modified : session_data -> session_data val apply_changes : cfg_op list -> Vyos1x.Config_tree.t -> Vyos1x.Config_tree.t +val validate : world -> session_data -> string list -> unit + val set : world -> session_data -> string list -> session_data val delete : world -> session_data -> string list -> session_data diff --git a/src/vycli.ml b/src/vycli.ml index 4310cbd..83c5eb1 100644 --- a/src/vycli.ml +++ b/src/vycli.ml @@ -10,6 +10,7 @@ type op_t = | OpGetValue | OpGetValues | OpListChildren + | OpValidate let token : string option ref = ref None let conf_format_opt = ref "curly" @@ -34,6 +35,7 @@ let args = [ ("--list-children", Arg.Unit (fun () -> op := Some OpListChildren), "List children of the node at the specified path"); ("--show-config", Arg.Unit (fun () -> op := Some OpShowConfig), "Show the configuration at the specified path"); ("--status", Arg.Unit (fun () -> op := Some OpStatus), "Send a status/keepalive message"); + ("--validate", Arg.Unit (fun () -> op := Some OpValidate), "Validate path"); ] let config_format_of_string s = @@ -74,6 +76,7 @@ let main socket op path out_format config_format = | OpGetValues -> get_values client path | OpListChildren -> list_children client path | OpShowConfig -> show_config client path + | OpValidate -> validate client path | _ -> Error "Unimplemented" |> Lwt.return end in match result with diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml index f6ce448..5cbd798 100644 --- a/src/vyconf_client.ml +++ b/src/vyconf_client.ml @@ -101,3 +101,10 @@ let show_config client path = | Success -> unwrap resp.output |> Lwt.return | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return +let validate client path = + let req = Validate {path=path; output_format=(Some client.out_format)} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> Lwt.return (Ok "") + | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return + | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return diff --git a/src/vyconf_client.mli b/src/vyconf_client.mli index dbf9e25..ec78780 100644 --- a/src/vyconf_client.mli +++ b/src/vyconf_client.mli @@ -38,3 +38,5 @@ val get_values : t -> string list -> (string, string) result Lwt.t val list_children : t -> string list -> (string, string) result Lwt.t val show_config : t -> string list -> (string, string) result Lwt.t + +val validate : t -> string list -> (string, string) result Lwt.t diff --git a/src/vyconf_pbt.ml b/src/vyconf_pbt.ml index 7e0aaad..1e481b9 100644 --- a/src/vyconf_pbt.ml +++ b/src/vyconf_pbt.ml @@ -15,6 +15,11 @@ type request_setup_session = { on_behalf_of : int32 option; } +type request_validate = { + path : string list; + output_format : request_output_format option; +} + type request_set = { path : string list; ephemeral : bool option; @@ -125,6 +130,7 @@ type request = | Confirm | Configure of request_enter_configuration_mode | Exit_configure + | Validate of request_validate | Teardown of string type request_envelope = { @@ -164,6 +170,14 @@ let rec default_request_setup_session on_behalf_of; } +let rec default_request_validate + ?path:((path:string list) = []) + ?output_format:((output_format:request_output_format option) = None) + () : request_validate = { + path; + output_format; +} + let rec default_request_set ?path:((path:string list) = []) ?ephemeral:((ephemeral:bool option) = None) @@ -338,6 +352,16 @@ let default_request_setup_session_mutable () : request_setup_session_mutable = { on_behalf_of = None; } +type request_validate_mutable = { + mutable path : string list; + mutable output_format : request_output_format option; +} + +let default_request_validate_mutable () : request_validate_mutable = { + path = []; + output_format = None; +} + type request_set_mutable = { mutable path : string list; mutable ephemeral : bool option; @@ -559,6 +583,13 @@ let rec pp_request_setup_session fmt (v:request_setup_session) = in Pbrt.Pp.pp_brk pp_i fmt () +let rec pp_request_validate fmt (v:request_validate) = + let pp_i fmt () = + Pbrt.Pp.pp_record_field ~first:true "path" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.path; + Pbrt.Pp.pp_record_field ~first:false "output_format" (Pbrt.Pp.pp_option pp_request_output_format) fmt v.output_format; + in + Pbrt.Pp.pp_brk pp_i fmt () + let rec pp_request_set fmt (v:request_set) = let pp_i fmt () = Pbrt.Pp.pp_record_field ~first:true "path" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.path; @@ -712,6 +743,7 @@ let rec pp_request fmt (v:request) = | Confirm -> Format.fprintf fmt "Confirm" | Configure x -> Format.fprintf fmt "@[Configure(@,%a)@]" pp_request_enter_configuration_mode x | Exit_configure -> Format.fprintf fmt "Exit_configure" + | Validate x -> Format.fprintf fmt "@[Validate(@,%a)@]" pp_request_validate x | Teardown x -> Format.fprintf fmt "@[Teardown(@,%a)@]" Pbrt.Pp.pp_string x let rec pp_request_envelope fmt (v:request_envelope) = @@ -774,6 +806,19 @@ let rec encode_pb_request_setup_session (v:request_setup_session) encoder = end; () +let rec encode_pb_request_validate (v:request_validate) encoder = + Pbrt.List_util.rev_iter_with (fun x encoder -> + Pbrt.Encoder.string x encoder; + Pbrt.Encoder.key 1 Pbrt.Bytes encoder; + ) v.path encoder; + begin match v.output_format with + | Some x -> + encode_pb_request_output_format x encoder; + Pbrt.Encoder.key 2 Pbrt.Varint encoder; + | None -> (); + end; + () + let rec encode_pb_request_set (v:request_set) encoder = Pbrt.List_util.rev_iter_with (fun x encoder -> Pbrt.Encoder.string x encoder; @@ -1031,9 +1076,12 @@ let rec encode_pb_request (v:request) encoder = | Exit_configure -> Pbrt.Encoder.key 20 Pbrt.Bytes encoder; Pbrt.Encoder.empty_nested encoder + | Validate x -> + Pbrt.Encoder.nested encode_pb_request_validate x encoder; + Pbrt.Encoder.key 21 Pbrt.Bytes encoder; | Teardown x -> Pbrt.Encoder.string x encoder; - Pbrt.Encoder.key 21 Pbrt.Bytes encoder; + Pbrt.Encoder.key 22 Pbrt.Bytes encoder; end let rec encode_pb_request_envelope (v:request_envelope) encoder = @@ -1128,6 +1176,31 @@ let rec decode_pb_request_setup_session d = on_behalf_of = v.on_behalf_of; } : request_setup_session) +let rec decode_pb_request_validate d = + let v = default_request_validate_mutable () in + let continue__= ref true in + while !continue__ do + match Pbrt.Decoder.key d with + | None -> ( + v.path <- List.rev v.path; + ); continue__ := false + | Some (1, Pbrt.Bytes) -> begin + v.path <- (Pbrt.Decoder.string d) :: v.path; + end + | Some (1, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_validate), field(1)" pk + | Some (2, Pbrt.Varint) -> begin + v.output_format <- Some (decode_pb_request_output_format d); + end + | Some (2, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_validate), field(2)" pk + | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind + done; + ({ + path = v.path; + output_format = v.output_format; + } : request_validate) + let rec decode_pb_request_set d = let v = default_request_set_mutable () in let continue__= ref true in @@ -1614,7 +1687,8 @@ let rec decode_pb_request d = Pbrt.Decoder.empty_nested d ; (Exit_configure : request) end - | Some (21, _) -> (Teardown (Pbrt.Decoder.string d) : request) + | Some (21, _) -> (Validate (decode_pb_request_validate (Pbrt.Decoder.nested d)) : request) + | Some (22, _) -> (Teardown (Pbrt.Decoder.string d) : request) | Some (n, payload_kind) -> ( Pbrt.Decoder.skip d payload_kind; loop () diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli index fc0df2f..2cc550f 100644 --- a/src/vyconf_pbt.mli +++ b/src/vyconf_pbt.mli @@ -22,6 +22,11 @@ type request_setup_session = { on_behalf_of : int32 option; } +type request_validate = { + path : string list; + output_format : request_output_format option; +} + type request_set = { path : string list; ephemeral : bool option; @@ -132,6 +137,7 @@ type request = | Confirm | Configure of request_enter_configuration_mode | Exit_configure + | Validate of request_validate | Teardown of string type request_envelope = { @@ -176,6 +182,13 @@ val default_request_setup_session : request_setup_session (** [default_request_setup_session ()] is the default value for type [request_setup_session] *) +val default_request_validate : + ?path:string list -> + ?output_format:request_output_format option -> + unit -> + request_validate +(** [default_request_validate ()] is the default value for type [request_validate] *) + val default_request_set : ?path:string list -> ?ephemeral:bool option -> @@ -338,6 +351,9 @@ val pp_request_status : Format.formatter -> request_status -> unit val pp_request_setup_session : Format.formatter -> request_setup_session -> unit (** [pp_request_setup_session v] formats v *) +val pp_request_validate : Format.formatter -> request_validate -> unit +(** [pp_request_validate v] formats v *) + val pp_request_set : Format.formatter -> request_set -> unit (** [pp_request_set v] formats v *) @@ -422,6 +438,9 @@ val encode_pb_request_status : request_status -> Pbrt.Encoder.t -> unit val encode_pb_request_setup_session : request_setup_session -> Pbrt.Encoder.t -> unit (** [encode_pb_request_setup_session v encoder] encodes [v] with the given [encoder] *) +val encode_pb_request_validate : request_validate -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_validate v encoder] encodes [v] with the given [encoder] *) + val encode_pb_request_set : request_set -> Pbrt.Encoder.t -> unit (** [encode_pb_request_set v encoder] encodes [v] with the given [encoder] *) @@ -506,6 +525,9 @@ val decode_pb_request_status : Pbrt.Decoder.t -> request_status val decode_pb_request_setup_session : Pbrt.Decoder.t -> request_setup_session (** [decode_pb_request_setup_session decoder] decodes a [request_setup_session] binary value from [decoder] *) +val decode_pb_request_validate : Pbrt.Decoder.t -> request_validate +(** [decode_pb_request_validate decoder] decodes a [request_validate] binary value from [decoder] *) + val decode_pb_request_set : Pbrt.Decoder.t -> request_set (** [decode_pb_request_set decoder] decodes a [request_set] binary value from [decoder] *) diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 9117e46..2bb3253 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -136,6 +136,13 @@ let show_config world token (req: request_show_config) = {response_tmpl with output=(Some conf_str)} with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)} +let validate world token (req: request_validate) = + try + let () = (Lwt_log.debug @@ Printf.sprintf "[%s]\n" (Vyos1x.Util.string_of_list req.path)) |> Lwt.ignore_result in + let () = Session.validate world (find_session token) req.path in + response_tmpl + with Session.Session_error msg -> {response_tmpl with status=Fail; error=(Some msg)} + let send_response oc resp = let enc = Pbrt.Encoder.create () in let%lwt () = encode_pb_response resp enc |> return in @@ -169,6 +176,7 @@ let rec handle_connection world ic oc fd () = | Some t, Get_values r -> get_values world t r | Some t, List_children r -> list_children world t r | Some t, Show_config r -> show_config world t r + | Some t, Validate r -> validate world t r | _ -> failwith "Unimplemented" end) |> Lwt.return in -- cgit v1.2.3 From a5473033fc8ea92891d6ebab9bdeaccbe742d565 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 23 Oct 2024 18:50:46 -0500 Subject: T6718: fix teardown method An empty protobuf message does not correctly generate encode/decode functions. This appears to be fixed upstream, but for now, use workaround of adding a reasonable field. --- data/vyconf.proto | 6 +++++- src/vyconf_client.ml | 9 ++++++++ src/vyconf_client.mli | 2 ++ src/vyconf_pbt.ml | 59 +++++++++++++++++++++++++++++++++++++++++++++++---- src/vyconf_pbt.mli | 21 +++++++++++++++++- src/vyconfd.ml | 6 +++--- 6 files changed, 94 insertions(+), 9 deletions(-) (limited to 'src/vyconf_pbt.mli') diff --git a/data/vyconf.proto b/data/vyconf.proto index f2245aa..d989fb3 100644 --- a/data/vyconf.proto +++ b/data/vyconf.proto @@ -17,6 +17,10 @@ message Request { optional int32 OnBehalfOf = 2; } + message Teardown { + optional int32 OnBehalfOf = 1; + } + message Validate { repeated string Path = 1; optional OutputFormat output_format = 2; @@ -135,7 +139,7 @@ message Request { EnterConfigurationMode configure = 19; ExitConfigurationMode exit_configure = 20; Validate validate = 21; - string teardown = 22; + Teardown teardown = 22; } } diff --git a/src/vyconf_client.ml b/src/vyconf_client.ml index 5cbd798..94348b2 100644 --- a/src/vyconf_client.ml +++ b/src/vyconf_client.ml @@ -65,6 +65,15 @@ let setup_session ?(on_behalf_of=None) client client_app = | None -> Error "setup_session did not return a session token!") |> Lwt.return | _ -> Error (Option.value resp.error ~default:"Unknown error") |> Lwt.return +let teardown_session ?(on_behalf_of=None) client = + let id = on_behalf_of |> (function None -> None | Some x -> (Some (Int32.of_int x))) in + let req = Teardown {on_behalf_of=id} in + let%lwt resp = do_request client req in + match resp.status with + | Success -> Ok "" |> Lwt.return + | Fail -> Error (Option.value resp.error ~default:"") |> Lwt.return + | _ -> Error (Option.value resp.error ~default:"") |> Lwt.return + let exists client path = let req = Exists {path=path} in let%lwt resp = do_request client req in diff --git a/src/vyconf_client.mli b/src/vyconf_client.mli index ec78780..0feb063 100644 --- a/src/vyconf_client.mli +++ b/src/vyconf_client.mli @@ -29,6 +29,8 @@ val get_status : t -> response Lwt.t val setup_session : ?on_behalf_of:(int option) -> t -> string -> (t, string) result Lwt.t +val teardown_session : ?on_behalf_of:(int option) -> t -> (string, string) result Lwt.t + val exists : t -> string list -> (string, string) result Lwt.t val get_value : t -> string list -> (string, string) result Lwt.t diff --git a/src/vyconf_pbt.ml b/src/vyconf_pbt.ml index 1e481b9..4c7fcc6 100644 --- a/src/vyconf_pbt.ml +++ b/src/vyconf_pbt.ml @@ -15,6 +15,10 @@ type request_setup_session = { on_behalf_of : int32 option; } +type request_teardown = { + on_behalf_of : int32 option; +} + type request_validate = { path : string list; output_format : request_output_format option; @@ -131,7 +135,7 @@ type request = | Configure of request_enter_configuration_mode | Exit_configure | Validate of request_validate - | Teardown of string + | Teardown of request_teardown type request_envelope = { token : string option; @@ -170,6 +174,12 @@ let rec default_request_setup_session on_behalf_of; } +let rec default_request_teardown + ?on_behalf_of:((on_behalf_of:int32 option) = None) + () : request_teardown = { + on_behalf_of; +} + let rec default_request_validate ?path:((path:string list) = []) ?output_format:((output_format:request_output_format option) = None) @@ -352,6 +362,14 @@ let default_request_setup_session_mutable () : request_setup_session_mutable = { on_behalf_of = None; } +type request_teardown_mutable = { + mutable on_behalf_of : int32 option; +} + +let default_request_teardown_mutable () : request_teardown_mutable = { + on_behalf_of = None; +} + type request_validate_mutable = { mutable path : string list; mutable output_format : request_output_format option; @@ -583,6 +601,12 @@ let rec pp_request_setup_session fmt (v:request_setup_session) = in Pbrt.Pp.pp_brk pp_i fmt () +let rec pp_request_teardown fmt (v:request_teardown) = + let pp_i fmt () = + Pbrt.Pp.pp_record_field ~first:true "on_behalf_of" (Pbrt.Pp.pp_option Pbrt.Pp.pp_int32) fmt v.on_behalf_of; + in + Pbrt.Pp.pp_brk pp_i fmt () + let rec pp_request_validate fmt (v:request_validate) = let pp_i fmt () = Pbrt.Pp.pp_record_field ~first:true "path" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.path; @@ -744,7 +768,7 @@ let rec pp_request fmt (v:request) = | Configure x -> Format.fprintf fmt "@[Configure(@,%a)@]" pp_request_enter_configuration_mode x | Exit_configure -> Format.fprintf fmt "Exit_configure" | Validate x -> Format.fprintf fmt "@[Validate(@,%a)@]" pp_request_validate x - | Teardown x -> Format.fprintf fmt "@[Teardown(@,%a)@]" Pbrt.Pp.pp_string x + | Teardown x -> Format.fprintf fmt "@[Teardown(@,%a)@]" pp_request_teardown x let rec pp_request_envelope fmt (v:request_envelope) = let pp_i fmt () = @@ -806,6 +830,15 @@ let rec encode_pb_request_setup_session (v:request_setup_session) encoder = end; () +let rec encode_pb_request_teardown (v:request_teardown) encoder = + begin match v.on_behalf_of with + | Some x -> + Pbrt.Encoder.int32_as_varint x encoder; + Pbrt.Encoder.key 1 Pbrt.Varint encoder; + | None -> (); + end; + () + let rec encode_pb_request_validate (v:request_validate) encoder = Pbrt.List_util.rev_iter_with (fun x encoder -> Pbrt.Encoder.string x encoder; @@ -1080,7 +1113,7 @@ let rec encode_pb_request (v:request) encoder = Pbrt.Encoder.nested encode_pb_request_validate x encoder; Pbrt.Encoder.key 21 Pbrt.Bytes encoder; | Teardown x -> - Pbrt.Encoder.string x encoder; + Pbrt.Encoder.nested encode_pb_request_teardown x encoder; Pbrt.Encoder.key 22 Pbrt.Bytes encoder; end @@ -1176,6 +1209,24 @@ let rec decode_pb_request_setup_session d = on_behalf_of = v.on_behalf_of; } : request_setup_session) +let rec decode_pb_request_teardown d = + let v = default_request_teardown_mutable () in + let continue__= ref true in + while !continue__ do + match Pbrt.Decoder.key d with + | None -> ( + ); continue__ := false + | Some (1, Pbrt.Varint) -> begin + v.on_behalf_of <- Some (Pbrt.Decoder.int32_as_varint d); + end + | Some (1, pk) -> + Pbrt.Decoder.unexpected_payload "Message(request_teardown), field(1)" pk + | Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind + done; + ({ + on_behalf_of = v.on_behalf_of; + } : request_teardown) + let rec decode_pb_request_validate d = let v = default_request_validate_mutable () in let continue__= ref true in @@ -1688,7 +1739,7 @@ let rec decode_pb_request d = (Exit_configure : request) end | Some (21, _) -> (Validate (decode_pb_request_validate (Pbrt.Decoder.nested d)) : request) - | Some (22, _) -> (Teardown (Pbrt.Decoder.string d) : request) + | Some (22, _) -> (Teardown (decode_pb_request_teardown (Pbrt.Decoder.nested d)) : request) | Some (n, payload_kind) -> ( Pbrt.Decoder.skip d payload_kind; loop () diff --git a/src/vyconf_pbt.mli b/src/vyconf_pbt.mli index 2cc550f..da94655 100644 --- a/src/vyconf_pbt.mli +++ b/src/vyconf_pbt.mli @@ -22,6 +22,10 @@ type request_setup_session = { on_behalf_of : int32 option; } +type request_teardown = { + on_behalf_of : int32 option; +} + type request_validate = { path : string list; output_format : request_output_format option; @@ -138,7 +142,7 @@ type request = | Configure of request_enter_configuration_mode | Exit_configure | Validate of request_validate - | Teardown of string + | Teardown of request_teardown type request_envelope = { token : string option; @@ -182,6 +186,12 @@ val default_request_setup_session : request_setup_session (** [default_request_setup_session ()] is the default value for type [request_setup_session] *) +val default_request_teardown : + ?on_behalf_of:int32 option -> + unit -> + request_teardown +(** [default_request_teardown ()] is the default value for type [request_teardown] *) + val default_request_validate : ?path:string list -> ?output_format:request_output_format option -> @@ -351,6 +361,9 @@ val pp_request_status : Format.formatter -> request_status -> unit val pp_request_setup_session : Format.formatter -> request_setup_session -> unit (** [pp_request_setup_session v] formats v *) +val pp_request_teardown : Format.formatter -> request_teardown -> unit +(** [pp_request_teardown v] formats v *) + val pp_request_validate : Format.formatter -> request_validate -> unit (** [pp_request_validate v] formats v *) @@ -438,6 +451,9 @@ val encode_pb_request_status : request_status -> Pbrt.Encoder.t -> unit val encode_pb_request_setup_session : request_setup_session -> Pbrt.Encoder.t -> unit (** [encode_pb_request_setup_session v encoder] encodes [v] with the given [encoder] *) +val encode_pb_request_teardown : request_teardown -> Pbrt.Encoder.t -> unit +(** [encode_pb_request_teardown v encoder] encodes [v] with the given [encoder] *) + val encode_pb_request_validate : request_validate -> Pbrt.Encoder.t -> unit (** [encode_pb_request_validate v encoder] encodes [v] with the given [encoder] *) @@ -525,6 +541,9 @@ val decode_pb_request_status : Pbrt.Decoder.t -> request_status val decode_pb_request_setup_session : Pbrt.Decoder.t -> request_setup_session (** [decode_pb_request_setup_session decoder] decodes a [request_setup_session] binary value from [decoder] *) +val decode_pb_request_teardown : Pbrt.Decoder.t -> request_teardown +(** [decode_pb_request_teardown decoder] decodes a [request_teardown] binary value from [decoder] *) + val decode_pb_request_validate : Pbrt.Decoder.t -> request_validate (** [decode_pb_request_validate decoder] decodes a [request_validate] binary value from [decoder] *) diff --git a/src/vyconfd.ml b/src/vyconfd.ml index 2bb3253..a445864 100644 --- a/src/vyconfd.ml +++ b/src/vyconfd.ml @@ -84,10 +84,10 @@ let exit_conf_mode world token = in Hashtbl.replace sessions token session; response_tmpl -let teardown_session token = +let teardown token = try Hashtbl.remove sessions token; - response_tmpl + {response_tmpl with status=Success} with Not_found -> {response_tmpl with status=Fail; error=(Some "Session not found")} @@ -168,7 +168,7 @@ let rec handle_connection world ic oc fd () = | _, Status -> response_tmpl | _, Setup_session r -> setup_session world r | None, _ -> {response_tmpl with status=Fail; output=(Some "Operation requires session token")} - | Some t, Teardown _ -> teardown_session t + | Some t, Teardown _ -> teardown t | Some t, Configure r -> enter_conf_mode r t | Some t, Exit_configure -> exit_conf_mode world t | Some t, Exists r -> exists world t r -- cgit v1.2.3