Repository: Azure/WALinuxAgent
Pull request #1225 (2018-06-21 21:29:56)

disable wrong private key tests


Patch:
diff --git a/azurelinuxagent/common/cgroups.py b/azurelinuxagent/common/cgroups.py
index 1c0bad30..2815fe37 100644
--- a/azurelinuxagent/common/cgroups.py
+++ b/azurelinuxagent/common/cgroups.py
@@ -258,12 +258,15 @@ class CGroupsTelemetry(object):
     def collect_all_tracked():
         """
         Return a dictionary mapping from the name of a tracked cgroup to the list of collected metrics for that cgroup.
+        Collecting metrics is not guaranteed to be a fast operation; it's possible some other thread might add or remove
+        tracking for a cgroup while we're doing it. To avoid "dictionary changed size during iteration" exceptions,
+        work from a shallow copy of the _tracked dictionary.
 
         :returns: Dictionary of list collected metrics (metric class, metric name, value), by cgroup
         :rtype: dict(str: [(str, str, float)])
         """
         results = {}
-        for cgroup_name, collector in CGroupsTelemetry._tracked.items():
+        for cgroup_name, collector in CGroupsTelemetry._tracked.copy().items():
             cgroup_name = cgroup_name if cgroup_name else WRAPPER_CGROUP_NAME
             results[cgroup_name] = collector.collect()
         return results
diff --git a/azurelinuxagent/common/utils/cryptutil.py b/azurelinuxagent/common/utils/cryptutil.py
index c3dd4d8c..0471a1fa 100644
--- a/azurelinuxagent/common/utils/cryptutil.py
+++ b/azurelinuxagent/common/utils/cryptutil.py
@@ -152,13 +152,13 @@ class CryptUtil(object):
     def decrypt_secret(self, encrypted_password, private_key):
         try:
             decoded = base64.b64decode(encrypted_password)
+            args = DECRYPT_SECRET_CMD.format(self.openssl_cmd, private_key).split(' ')
+            p = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
+            p.stdin.write(decoded)
+            output = p.communicate()[0]
+            retcode = p.poll()
+            if retcode:
+                raise subprocess.CalledProcessError(retcode, "openssl cms -decrypt", output=output)
+            return output.decode('utf-16')
         except Exception as e:
             raise CryptError("Error decoding secret", e)
-        args = DECRYPT_SECRET_CMD.format(self.openssl_cmd, private_key).split(' ')
-        p = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
-        p.stdin.write(decoded)
-        output = p.communicate()[0]
-        retcode = p.poll()
-        if retcode:
-            raise subprocess.CalledProcessError(retcode, "openssl cms -decrypt", output=output)
-        return output.decode('utf-16')


Test patch:
diff --git a/tests/utils/test_crypt_util.py b/tests/utils/test_crypt_util.py
index c765363d..3e5321e8 100644
--- a/tests/utils/test_crypt_util.py
+++ b/tests/utils/test_crypt_util.py
@@ -22,8 +22,10 @@ import glob
 import random
 import string
 import subprocess
+import sys
 import tempfile
 import uuid
+import unittest
 
 import azurelinuxagent.common.conf as conf
 import azurelinuxagent.common.utils.shellutil as shellutil
@@ -34,6 +36,11 @@ from azurelinuxagent.common.version import PY_VERSION_MAJOR
 from tests.tools import *
 from subprocess import CalledProcessError
 
+
+def is_python_version_26():
+    return sys.version_info[0] == 2 and sys.version_info[1] == 6
+
+
 class TestCryptoUtilOperations(AgentTestCase):
     def test_decrypt_encrypted_text(self):
         encrypted_string = load_data("wire/encrypted.enc")
@@ -49,23 +56,25 @@ class TestCryptoUtilOperations(AgentTestCase):
         encrypted_string = load_data("wire/encrypted.enc")
         prv_key = os.path.join(self.tmp_dir, "TransportPrivate.pem")
         crypto = CryptUtil(conf.get_openssl_cmd())
-        self.assertRaises(CalledProcessError, crypto.decrypt_secret, encrypted_string, "abc" + prv_key)
-    
+        self.assertRaises(CryptError, crypto.decrypt_secret, encrypted_string, "abc" + prv_key)
+
+    @skip_if_predicate_true(is_python_version_26, "Disabled on Python 2.6")
     def test_decrypt_encrypted_text_wrong_private_key(self):
         encrypted_string = load_data("wire/encrypted.enc")
         prv_key = os.path.join(self.tmp_dir, "wrong.pem")
         with open(prv_key, 'w+') as c:
             c.write(load_data("wire/trans_prv"))
         crypto = CryptUtil(conf.get_openssl_cmd())
-        self.assertRaises(CalledProcessError, crypto.decrypt_secret, encrypted_string, prv_key)
+        self.assertRaises(CryptError, crypto.decrypt_secret, encrypted_string, prv_key)
 
     def test_decrypt_encrypted_text_text_not_encrypted(self):
         encrypted_string = "abc@123"
-        prv_key = os.path.join(self.tmp_dir, "TransportPrivate.pem") 
+        prv_key = os.path.join(self.tmp_dir, "TransportPrivate.pem")
         with open(prv_key, 'w+') as c:
             c.write(load_data("wire/sample.pem"))
         crypto = CryptUtil(conf.get_openssl_cmd())
         self.assertRaises(CryptError, crypto.decrypt_secret, encrypted_string, prv_key)
 
+
 if __name__ == '__main__':
     unittest.main()