From 979471fd238cba6847c7e16400c3d8fdb4cc9711 Mon Sep 17 00:00:00 2001 From: Lianhao Lu Date: Thu, 26 Jul 2018 17:50:28 +0800 Subject: Added remote file digest verification Change-Id: If91dc29c40e074737baed39805aba43458911952 Issue-ID: VNFSDK-294 Signed-off-by: Lianhao Lu --- vnfsdk_pkgtools/packager/manifest.py | 8 +++----- vnfsdk_pkgtools/packager/utils.py | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) (limited to 'vnfsdk_pkgtools') diff --git a/vnfsdk_pkgtools/packager/manifest.py b/vnfsdk_pkgtools/packager/manifest.py index d819a70..e5bceb0 100644 --- a/vnfsdk_pkgtools/packager/manifest.py +++ b/vnfsdk_pkgtools/packager/manifest.py @@ -116,11 +116,9 @@ class Manifest(object): if desc['Algorithm'] not in SUPPORTED_HASH_ALGO: raise ManifestException("Unsupported hash algorithm: %s" % desc['Algorithm']) # validate file digest hash - # TODO need to support remote file - if "://" not in desc['Source']: - hash = utils.cal_file_hash(self.root, desc['Source'], desc['Algorithm']) - if hash != desc['Hash']: - raise ManifestException("Mismatched hash for file %s" % desc['Source']) + hash = utils.cal_file_hash(self.root, desc['Source'], desc['Algorithm']) + if hash != desc['Hash']: + raise ManifestException("Mismatched hash for file %s" % desc['Source']) # nothing is wrong, let's store this self.digests[desc['Source']] = (desc['Algorithm'], desc['Hash']) elif key: diff --git a/vnfsdk_pkgtools/packager/utils.py b/vnfsdk_pkgtools/packager/utils.py index 78c7b0f..2d74943 100644 --- a/vnfsdk_pkgtools/packager/utils.py +++ b/vnfsdk_pkgtools/packager/utils.py @@ -14,7 +14,12 @@ # import hashlib +from io import BytesIO import os +import urlparse + +import requests + def _hash_value_for_file(f, hash_function, block_size=2**20): while True: @@ -27,7 +32,14 @@ def _hash_value_for_file(f, hash_function, block_size=2**20): def cal_file_hash(root, path, algo): - with open(os.path.join(root, path), 'rb') as fp: - h = hashlib.new(algo) + h = hashlib.new(algo) + if urlparse.urlparse(path).scheme: + r = requests.get(path) + if r.status_code != 200: + raise ValueError('Server at {0} returned a {1} status code' + .format(path, r.status_code)) + fp = BytesIO(r.content) return _hash_value_for_file(fp, h) - + else: + with open(os.path.join(root, path), 'rb') as fp: + return _hash_value_for_file(fp, h) -- cgit 1.2.3-korg