From 60874d019785adcabac0b1029d1af77be870932d Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 13 Mar 2018 11:59:28 -0700 Subject: [PATCH] Check to make sure image file exists for /compose/image/ Return an error 400 with an error message if it is missing. --- src/pylorax/api/v0.py | 8 +++++++- src/pylorax/creator.py | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index e0ffe9af..b7974f98 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -898,6 +898,7 @@ DELETE `/api/v0/compose/delete/` import logging log = logging.getLogger("lorax-composer") +import os from flask import jsonify, request, Response, send_file from pylorax.api.compose import start_build, compose_types @@ -1564,8 +1565,13 @@ def v0_api(api): return jsonify(status=False, uuid=uuid, msg="Build not in FINISHED or FAILED.") else: image_name, image_path = uuid_image(api.config["COMPOSER_CFG"], uuid) - image_name = uuid + "-" + image_name + # Make sure it really exists + if not os.path.exists(image_path): + return jsonify(status=False, uuid=uuid, error={"msg":"Build %s is missing image file %s" % (uuid, image_name)}), 400 + + # Make the image name unique + image_name = uuid + "-" + image_name # XXX - Will mime type guessing work for all our output? return send_file(image_path, as_attachment=True, attachment_filename=image_name, add_etags=False) diff --git a/src/pylorax/creator.py b/src/pylorax/creator.py index 7812735d..84333dbf 100644 --- a/src/pylorax/creator.py +++ b/src/pylorax/creator.py @@ -433,7 +433,8 @@ def make_image(opts, ks, callback_func=None): log.error("Install failed: %s", e) if not opts.keep_image: log.info("Removing bad disk image") - os.unlink(disk_img) + if os.path.exists(disk_img): + os.unlink(disk_img) raise log.info("Disk Image install successful")