qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Wainer dos Santos Moschetta <wainersm@redhat.com>
Cc: fam@euphon.net, peter.maydell@linaro.org, berrange@redhat.com,
	lersek@redhat.com, qemu-devel@nongnu.org, pbonzini@redhat.com,
	philmd@redhat.com
Subject: Re: [Qemu-devel] [PATCH 3/5] tests/vm: Detect the image changed on server
Date: Wed, 29 May 2019 20:05:36 +0100	[thread overview]
Message-ID: <87a7f55ben.fsf@zen.linaroharston> (raw)
In-Reply-To: <20190329210804.22121-4-wainersm@redhat.com>


Wainer dos Santos Moschetta <wainersm@redhat.com> writes:

> The current implementation of basevm does not check if the image
> file to be downloaded has changed on server side before honouring
> the cache. So any change on server-side file can go unnoticed,
> keeping the cached image.
>
> This change implements a simple mechanism to detect the image
> file changed by using the sha256sum file stored on server. It
> compares with the expected checksum and then abort the execution
> on mismatch.
>
> Signed-off-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
> ---
>  tests/vm/basevm.py | 31 ++++++++++++++++++++++++++++++-
>  1 file changed, 30 insertions(+), 1 deletion(-)
>
> diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
> index 083befce9f..4dfad2dc9b 100755
> --- a/tests/vm/basevm.py
> +++ b/tests/vm/basevm.py
> @@ -27,6 +27,7 @@ import tempfile
>  import shutil
>  import multiprocessing
>  import traceback
> +import urllib.request
>
>  SSH_KEY = open(os.path.join(os.path.dirname(__file__),
>                 "..", "keys", "id_rsa")).read()
> @@ -81,6 +82,18 @@ class BaseVM(object):
>          self._data_args = []
>
>      def _download_with_cache(self, url, sha256sum=None):
> +
> +        def fetch_image_hash(url):
> +            fetch_url = "%s.sha256sum" % url

OK this fails with the FreeBSD code as they use the form:

  https://download.freebsd.org/ftp/releases/ISO-IMAGES/12.0/CHECKSUM.SHA256-FreeBSD-12.0-RELEASE-amd64

I guess we need to have a method that can be overridden for this.

> +            try:
> +                with urllib.request.urlopen(fetch_url) as response:
> +                    content = response.read()
> +            except  urllib.error.URLError as error:
> +                logging.error("Failed to fetch image checksum file: %s",
> +                        fetch_url)
> +                raise error
> +            return content.decode().strip()
> +
>          def check_sha256sum(fname):
>              if not sha256sum:
>                  return True
> @@ -91,8 +104,24 @@ class BaseVM(object):
>          if not os.path.exists(cache_dir):
>              os.makedirs(cache_dir)
>          fname = os.path.join(cache_dir, hashlib.sha1(url.encode()).hexdigest())
> -        if os.path.exists(fname) and check_sha256sum(fname):
> +
> +        if os.path.exists(fname) and sha256sum is None:
>              return fname
> +
> +        if sha256sum:
> +            image_checksum = fetch_image_hash(url)
> +            # Check the url points to a known image file.
> +            if image_checksum != sha256sum:
> +                logging.error("Image %s checksum (%s) does not match " +
> +                        "expected (%s).", url, image_checksum, sha256sum)
> +                raise Exception("Image checksum failed.")
> +            # Check the cached image is up to date.
> +            if os.path.exists(fname):
> +                if check_sha256sum(fname):
> +                    return fname
> +                logging.warning("Invalid cached image. Attempt to download " +
> +                        "the updated one.")
> +
>          logging.debug("Downloading %s to %s...", url, fname)
>          subprocess.check_call(["wget", "-c", url, "-O", fname + ".download"],
>                                stdout=self._stdout, stderr=self._stderr)


--
Alex Bennée


      parent reply	other threads:[~2019-05-29 19:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190329210804.22121-1-wainersm@redhat.com>
     [not found] ` <20190329210804.22121-3-wainersm@redhat.com>
2019-04-01 11:34   ` [Qemu-devel] [PATCH 2/5] tests/vm: Port basevm to Python 3 Philippe Mathieu-Daudé
2019-04-23 19:28 ` [Qemu-devel] [PATCH 0/5] tests/vm: Python 3, improve image caching, and misc Wainer dos Santos Moschetta
2019-04-23 19:28   ` Wainer dos Santos Moschetta
2019-04-24  9:07   ` Laszlo Ersek
2019-04-24  9:07     ` Laszlo Ersek
2019-04-25 18:02     ` Wainer dos Santos Moschetta
2019-04-25 18:02       ` Wainer dos Santos Moschetta
2019-05-29 16:50   ` Alex Bennée
     [not found] ` <20190329210804.22121-4-wainersm@redhat.com>
2019-05-29 19:05   ` Alex Bennée [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a7f55ben.fsf@zen.linaroharston \
    --to=alex.bennee@linaro.org \
    --cc=berrange@redhat.com \
    --cc=fam@euphon.net \
    --cc=lersek@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wainersm@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).