aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLianhao Lu <lianhao.lu@intel.com>2018-07-26 17:50:28 +0800
committerLianhao Lu <lianhao.lu@intel.com>2018-07-26 17:50:28 +0800
commit979471fd238cba6847c7e16400c3d8fdb4cc9711 (patch)
treee78d36b85346c03575fdc9ded6857efdb12f99af
parentf570936d2f1be946c0fa3a7f7d23c92310a6e2a9 (diff)
Added remote file digest verification
Change-Id: If91dc29c40e074737baed39805aba43458911952 Issue-ID: VNFSDK-294 Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
-rw-r--r--tests/packager/test_utils.py8
-rw-r--r--vnfsdk_pkgtools/packager/manifest.py8
-rw-r--r--vnfsdk_pkgtools/packager/utils.py18
3 files changed, 26 insertions, 8 deletions
diff --git a/tests/packager/test_utils.py b/tests/packager/test_utils.py
index 03b3f24..91fc72b 100644
--- a/tests/packager/test_utils.py
+++ b/tests/packager/test_utils.py
@@ -26,3 +26,11 @@ def test_cal_file_hash(tmpdir):
p.write(CONTENT)
assert SHA512 == utils.cal_file_hash("", str(p), 'SHA512')
assert SHA256 == utils.cal_file_hash(p.dirname, p.basename, 'sha256')
+
+def test_cal_file_hash_remote(mocker):
+ class FakeRequest(object):
+ def __init__(self, *args):
+ self.status_code = 200
+ self.content = CONTENT
+ mocker.patch('requests.get', new=FakeRequest)
+ assert SHA256 == utils.cal_file_hash("", "http://fake", 'sha256')
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)