Added remote file digest verification 81/57681/1
authorLianhao Lu <lianhao.lu@intel.com>
Thu, 26 Jul 2018 09:50:28 +0000 (17:50 +0800)
committerLianhao Lu <lianhao.lu@intel.com>
Thu, 26 Jul 2018 09:50:28 +0000 (17:50 +0800)
Change-Id: If91dc29c40e074737baed39805aba43458911952
Issue-ID: VNFSDK-294
Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
tests/packager/test_utils.py
vnfsdk_pkgtools/packager/manifest.py
vnfsdk_pkgtools/packager/utils.py

index 03b3f24..91fc72b 100644 (file)
@@ -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')
index d819a70..e5bceb0 100644 (file)
@@ -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:
index 78c7b0f..2d74943 100644 (file)
 #
 
 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)