sts-sponsors team mailing list archive
-
sts-sponsors team
-
Mailing list archive
-
Message #06203
[Merge] ~troyanov/maas:fix-cli-cert-race into maas:master
Anton Troyanov has proposed merging ~troyanov/maas:fix-cli-cert-race into maas:master.
Commit message:
fix(cli): reuse existing CA cert
Resolves LP:2012139
Requested reviews:
MAAS Maintainers (maas-maintainers)
For more details, see:
https://code.launchpad.net/~troyanov/maas/+git/maas/+merge/439212
--
Your team MAAS Committers is subscribed to branch maas:master.
diff --git a/src/maascli/api.py b/src/maascli/api.py
index 1a4c14c..cb408ae 100644
--- a/src/maascli/api.py
+++ b/src/maascli/api.py
@@ -519,17 +519,17 @@ def materialize_certificate(profile, cert_dir="~/.maascli.certs"):
File is needed for httplib2 ca_cert (for server cert validation)
"""
+ profile_name = profile["name"]
+ cert_path = Path(cert_dir).expanduser() / (profile_name + ".pem")
+
+ if cert_path.exists():
+ return cert_path
+
cacerts = profile.get("cacerts")
if cacerts is None:
return None
- cert_dir = Path(cert_dir).expanduser()
- if not cert_dir.exists():
- cert_dir.mkdir()
-
- profile_name = profile["name"]
- cert_path = cert_dir / (profile_name + ".pem")
- cert_path = Path(cert_path)
-
+ 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