tests/krb5/raw_testcase.py: Cache obtained credentials
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Tue, 15 Jun 2021 05:10:44 +0000 (17:10 +1200)
committerStefan Metzmacher <metze@samba.org>
Thu, 1 Jul 2021 17:46:31 +0000 (17:46 +0000)
If credentials are used more than once, we can now use the credentials
that we already obtained and so avoid fetching them again.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
python/samba/tests/krb5/kdc_base_test.py
python/samba/tests/krb5/raw_testcase.py

index 7ae22bc59291fc24123a9aab0586efb10cf1c94c..120084616e908b30e80f56ab48dcb0d0263ba719 100644 (file)
@@ -75,6 +75,7 @@ class KDCBaseTest(RawKerberosTest):
 
     @classmethod
     def setUpClass(cls):
+        super().setUpClass()
         cls._lp = None
         cls.host = os.environ["SERVER"]
 
index 9c0f5800b42962d876585e49deaa6afe8e6da949..5b59eede806cc0c3caa96d7799907527fe86a473 100644 (file)
@@ -371,6 +371,14 @@ class RawKerberosTest(TestCaseInTempDir):
         e = self.etype_test_permutations[idx]
         return (e['name'], e['etypes'])
 
+    @classmethod
+    def setUpClass(cls):
+        super().setUpClass()
+
+        # A dictionary containing credentials that have already been
+        # obtained.
+        cls.creds_dict = {}
+
     def setUp(self):
         super().setUp()
         self.do_asn1_print = False
@@ -441,11 +449,11 @@ class RawKerberosTest(TestCaseInTempDir):
                                                 allow_missing=allow_missing)
         return val
 
-    def _get_krb5_creds(self, prefix,
-                        default_username=None,
-                        allow_missing_password=False,
-                        allow_missing_keys=True,
-                        require_strongest_key=False):
+    def _get_krb5_creds_from_env(self, prefix,
+                                 default_username=None,
+                                 allow_missing_password=False,
+                                 allow_missing_keys=True,
+                                 require_strongest_key=False):
         c = KerberosCredentials()
         c.guess()
 
@@ -515,6 +523,26 @@ class RawKerberosTest(TestCaseInTempDir):
 
         return c
 
+    def _get_krb5_creds(self,
+                        prefix,
+                        default_username=None,
+                        allow_missing_password=False,
+                        allow_missing_keys=True,
+                        require_strongest_key=False):
+        if prefix not in self.creds_dict:
+            # We don't have the credentials already
+            creds = self._get_krb5_creds_from_env(prefix,
+                                                  default_username=default_username,
+                                                  allow_missing_password=allow_missing_password,
+                                                  allow_missing_keys=allow_missing_keys,
+                                                  require_strongest_key=require_strongest_key)
+            self.assertIsNotNone(creds)
+
+            # Save the obtained credentials
+            self.creds_dict[prefix] = creds
+
+        return self.creds_dict[prefix]
+
     def get_user_creds(self,
                        allow_missing_password=False,
                        allow_missing_keys=True):