← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~troyanov/maas:backport-ca3a5d0-3.2 into maas:3.2

 

Anton Troyanov has proposed merging ~troyanov/maas:backport-ca3a5d0-3.2 into maas:3.2.

Commit message:
fix(cli): reuse existing CA cert

Resolves LP:2012139

(cherry picked from commit ca3a5d0826694d0d12ff9ab93f813e2be446cb2e)

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~troyanov/maas/+git/maas/+merge/439278
-- 
Your team MAAS Committers is subscribed to branch maas:3.2.
diff --git a/src/maascli/api.py b/src/maascli/api.py
index 1a4c14c..89db8b7 100644
--- a/src/maascli/api.py
+++ b/src/maascli/api.py
@@ -519,17 +519,20 @@ def materialize_certificate(profile, cert_dir="~/.maascli.certs"):
     File is needed for httplib2 ca_cert (for server cert validation)
     """
 
-    cacerts = profile.get("cacerts")
-    if cacerts is None:
+    profile_name = profile.get("name")
+    if profile_name is None:
         return None
 
-    cert_dir = Path(cert_dir).expanduser()
-    if not cert_dir.exists():
-        cert_dir.mkdir()
+    cert_path = Path(cert_dir).expanduser() / (profile_name + ".pem")
 
-    profile_name = profile["name"]
-    cert_path = cert_dir / (profile_name + ".pem")
-    cert_path = Path(cert_path)
+    if cert_path.exists():
+        return cert_path
 
+    cacerts = profile.get("cacerts")
+    if cacerts is None:
+        return None
+
+    cert_path.parent.mkdir(exist_ok=True, parents=True)
     cert_path.write_text(cacerts)
+
     return cert_path
diff --git a/src/maascli/tests/test_api.py b/src/maascli/tests/test_api.py
index 9b2f659..9138eee 100644
--- a/src/maascli/tests/test_api.py
+++ b/src/maascli/tests/test_api.py
@@ -220,6 +220,17 @@ class TestFunctions(MAASTestCase):
             cert_path = temp_dir = temp_dir / (profile["name"] + ".pem")
             self.assertEqual(cert, cert_path.open().read())
 
+    def test_materialize_certificate_should_return_existing_cert(self):
+        mock_write_cert = self.patch(Path, "write_text")
+        profile = make_profile()
+        with tempfile.TemporaryDirectory() as tmp:
+            cert_file = Path(tmp) / (profile["name"] + ".pem")
+            cert_file.touch()
+            self.assertEqual(
+                cert_file, api.materialize_certificate(profile, tmp)
+            )
+            mock_write_cert.assert_not_called()
+
 
 class TestAction(MAASTestCase):
     """Tests for :class:`maascli.api.Action`."""

Follow ups