Skip to content

Commit

Permalink
CP-51391: Implement handling for /repository/enabled
Browse files Browse the repository at this point in the history
Add handler for `/repository/enabled`. Replase `/enabled` with
the current enabled repository.

Signed-off-by: Bengang Yuan <[email protected]>
  • Loading branch information
BengangY committed Oct 30, 2024
1 parent 0f093b8 commit 2d55f7a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
9 changes: 9 additions & 0 deletions ocaml/idl/datamodel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10971,6 +10971,15 @@ let http_actions =
; ( "get_repository"
, (Get, Constants.get_repository_uri, false, [], _R_LOCAL_ROOT_ONLY, [])
)
; ( "get_enabled_repository"
, ( Get
, Constants.get_enabled_repository_uri
, false
, []
, _R_LOCAL_ROOT_ONLY
, []
)
)
; ( "get_host_updates"
, ( Get
, Constants.get_host_updates_uri
Expand Down
3 changes: 3 additions & 0 deletions ocaml/xapi-consts/constants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ let get_pool_update_download_uri = "/update/"

let get_repository_uri = "/repository" (* ocaml/xapi/repository.ml *)

let get_enabled_repository_uri =
"/repository/enabled" (* ocaml/xapi/repository.ml *)

let get_host_updates_uri = "/host_updates" (* ocaml/xapi/repository.ml *)

let get_updates_uri = "/updates" (* ocaml/xapi/repository.ml *)
Expand Down
77 changes: 39 additions & 38 deletions ocaml/xapi/repository.ml
Original file line number Diff line number Diff line change
Expand Up @@ -511,50 +511,51 @@ let get_host_updates_in_json ~__context ~installed =
(ExnHelper.string_of_exn e) ;
raise Api_errors.(Server_error (get_host_updates_failed, [ref]))

let response_repo_file ~req ~s uri_path =
if Fileserver.access_forbidden req s then
Http_svr.response_forbidden ~req s
else
let root = !Xapi_globs.local_pool_repo_dir in
Fileserver.response_file s (Helpers.resolve_uri_path ~root ~uri_path)

(* Map the URI in the HTTP reuqest to local repo path *)
let map_uri_to_path ~prefix ~by uri =
let regex = prefix |> Re.str |> Re.compile in
uri |> Uri.of_string |> Uri.path |> Re.replace_string ~all:false regex ~by

let get_repository_handler (req : Http.Request.t) s _ =
let open Http in
let open Xapi_stdext_std.Xstringext in
debug "Repository.get_repository_handler URL %s" req.Request.uri ;
req.Request.close <- true ;
if Fileserver.access_forbidden req s then
try
req.Request.uri
|> map_uri_to_path ~prefix:Constants.get_repository_uri ~by:""
|> response_repo_file ~req ~s
with e ->
error "Failed to serve for request on uri %s: %s" req.Request.uri
(ExnHelper.string_of_exn e) ;
Http_svr.response_forbidden ~req s
else
let can_be_authorized =
try
Xapi_http.with_context "get_repository_handler" req s (fun _ -> ()) ;
true
with _ -> false
in
let internal_repo_access_only =
let __context =
Context.make ~origin:(Http (req, s)) "get_repository_handler"
in
Pool_features.is_enabled ~__context Features.Internal_repo_access
in
match (can_be_authorized, internal_repo_access_only) with
| false, true ->
error
"Invalid secret for authorization when Internal_repo_access is \
enabled" ;
Http_svr.response_forbidden ~req s
| _ -> (
try
let len = String.length Constants.get_repository_uri in
match String.sub_to_end req.Request.uri len with
| uri_path ->
let root = !Xapi_globs.local_pool_repo_dir in
Fileserver.response_file s (Helpers.resolve_uri_path ~root ~uri_path)
| exception e ->
let msg =
Printf.sprintf "Failed to get path from uri': %s"
(ExnHelper.string_of_exn e)
in
raise Api_errors.(Server_error (internal_error, [msg]))
with e ->
error "Failed to serve for request on uri %s: %s" req.Request.uri
(ExnHelper.string_of_exn e) ;
Http_svr.response_forbidden ~req s

let get_enabled_repository_handler (req : Http.Request.t) s _ =
let open Http in
debug "Repository.get_enabled_repository_handler URL %s" req.Request.uri ;
req.Request.close <- true ;
try
Xapi_http.with_context "get_enabled_repository_handler" req s
(fun __context ->
let enabled_repo = get_single_enabled_update_repository ~__context in
let repo_name =
get_remote_repository_name ~__context ~self:enabled_repo
in
req.Request.uri
|> map_uri_to_path ~prefix:Constants.get_enabled_repository_uri
~by:repo_name
|> response_repo_file ~req ~s
)
with e ->
error "Failed to serve for request on uri %s: %s" req.Request.uri
(ExnHelper.string_of_exn e) ;
Http_svr.response_forbidden ~req s

let consolidate_updates_of_hosts ~repository_name ~updates_info ~hosts =
Hashtbl.fold
Expand Down
2 changes: 2 additions & 0 deletions ocaml/xapi/repository.mli
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ val create_pool_repository :

val get_repository_handler : Http.Request.t -> Unix.file_descr -> 'a -> unit

val get_enabled_repository_handler : Http.Request.t -> Unix.file_descr -> 'a -> unit

val get_host_updates_in_json :
__context:Context.t -> installed:bool -> Yojson.Basic.t

Expand Down
1 change: 1 addition & 0 deletions ocaml/xapi/xapi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ let master_only_http_handlers =
("post_remote_db_access", remote_database_access_handler)
; ("post_remote_db_access_v2", remote_database_access_handler_v2)
; ("get_repository", Repository.get_repository_handler)
; ("get_enabled_repository", Repository.get_enabled_repository_handler)
; ("get_updates", Xapi_pool.get_updates_handler)
]

Expand Down

0 comments on commit 2d55f7a

Please sign in to comment.