Skip to content

Commit e69ba42

Browse files
author
Jon Ludlam
authored
Merge pull request xapi-project#227 from jonludlam/ely-CA-228573
CA-228573: Require a content-length on pool database restore HTTP han…
2 parents 97c70a4 + 2fd47e8 commit e69ba42

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

ocaml/xapi/pool_db_backup.ml

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let version_check db =
5050
end
5151

5252
(** Makes a new database suitable for xapi by rewriting some configuration from the current
53-
database. *)
53+
database. *)
5454
let prepare_database_for_restore ~old_context ~new_context =
5555

5656
(* To prevent duplicate installation_uuids or duplicate IP address confusing the
@@ -125,11 +125,11 @@ let prepare_database_for_restore ~old_context ~new_context =
125125
(* We only need to rewrite the MAC addresses of physical PIFs *)
126126
if physical then begin
127127
(* If this is a physical PIF but we can't find the device name
128-
on the restore target, bail out. *)
128+
on the restore target, bail out. *)
129129
if not(List.mem_assoc device device_to_ref)
130130
then raise (Api_errors.Server_error(Api_errors.restore_target_missing_device, [ device ]));
131131
(* Otherwise rewrite the MAC address to match the current machine
132-
and set the management flag accordingly *)
132+
and set the management flag accordingly *)
133133
let existing_pif = List.assoc device device_to_ref in
134134
Db.PIF.set_MAC ~__context:new_context ~self ~value:(Db.PIF.get_MAC ~__context:old_context ~self:existing_pif)
135135
end;
@@ -174,32 +174,36 @@ let push_database_restore_handler (req: Http.Request.t) s _ =
174174
debug "received request to restore db from xml dump";
175175
Xapi_http.with_context "Reading database as XML" req s
176176
(fun __context ->
177-
debug "sending headers";
178-
Http_svr.headers s (Http.http_200_ok ~keep_alive:false ());
179-
debug "sent headers";
180-
(* XXX: write to temp file *)
181-
let tmp_xml_file = Filename.temp_file "" "xml_file" in
182-
let xml_file_fd = Unix.openfile tmp_xml_file [ Unix.O_WRONLY ] 0o600 in
183-
let () = Pervasiveext.finally
184-
(fun ()->ignore (Unixext.copy_file s xml_file_fd))
185-
(fun ()->Unix.close xml_file_fd) in
186-
187-
let dry_run = List.mem_assoc "dry_run" req.Http.Request.query && (List.assoc "dry_run" req.Http.Request.query = "true") in
188-
if dry_run
189-
then debug "performing dry-run database restore"
190-
else debug "performing full restore and restart";
191-
Unixext.unlink_safe Xapi_globs.db_temporary_restore_path;
192-
restore_from_xml __context dry_run tmp_xml_file;
193-
Unixext.unlink_safe tmp_xml_file;
194-
if not(dry_run) then begin
195-
(* We will restart as a master *)
196-
Pool_role.set_role Pool_role.Master;
197-
198-
(* now restart *)
199-
debug "xapi has received new database via xml; will reboot and use that db...";
200-
info "Rebooting to use restored database after delay of: %f" !Xapi_globs.db_restore_fuse_time;
201-
Xapi_fuse.light_fuse_and_reboot ~fuse_length:!Xapi_globs.db_restore_fuse_time ();
202-
end
177+
match req.Http.Request.content_length with
178+
| None -> Http_svr.headers s (Http.http_400_badrequest ())
179+
| Some l -> begin
180+
debug "sending headers";
181+
Http_svr.headers s (Http.http_200_ok ~keep_alive:false ());
182+
debug "sent headers";
183+
(* XXX: write to temp file *)
184+
let tmp_xml_file = Filename.temp_file "" "xml_file" in
185+
let xml_file_fd = Unix.openfile tmp_xml_file [ Unix.O_WRONLY ] 0o600 in
186+
let () = Pervasiveext.finally
187+
(fun ()->ignore (Unixext.copy_file ~limit:l s xml_file_fd))
188+
(fun ()->Unix.close xml_file_fd) in
189+
190+
let dry_run = List.mem_assoc "dry_run" req.Http.Request.query && (List.assoc "dry_run" req.Http.Request.query = "true") in
191+
if dry_run
192+
then debug "performing dry-run database restore"
193+
else debug "performing full restore and restart";
194+
Unixext.unlink_safe Xapi_globs.db_temporary_restore_path;
195+
restore_from_xml __context dry_run tmp_xml_file;
196+
Unixext.unlink_safe tmp_xml_file;
197+
if not(dry_run) then begin
198+
(* We will restart as a master *)
199+
Pool_role.set_role Pool_role.Master;
200+
201+
(* now restart *)
202+
debug "xapi has received new database via xml; will reboot and use that db...";
203+
info "Rebooting to use restored database after delay of: %f" !Xapi_globs.db_restore_fuse_time;
204+
Xapi_fuse.light_fuse_and_reboot ~fuse_length:!Xapi_globs.db_restore_fuse_time ();
205+
end
206+
end
203207
)
204208

205209
let http_fetch_db ~master_address ~pool_secret =

0 commit comments

Comments
 (0)