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

Proposed by Anton Troyanov
Status: Merged
Approved by: Anton Troyanov
Approved revision: 0b3a64dfedb943cc4fad610d1befc974d9f9e9ea
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~troyanov/maas:backport-ca3a5d0-3.2
Merge into: maas:3.2
Diff against target: 55 lines (+22/-8)
2 files modified
src/maascli/api.py (+11/-8)
src/maascli/tests/test_api.py (+11/-0)
Reviewer Review Type Date Requested Status
Anton Troyanov Approve
MAAS Lander Approve
Review via email: mp+439278@code.launchpad.net

Commit message

fix(cli): reuse existing CA cert

Resolves LP:2012139

(cherry picked from commit ca3a5d0826694d0d12ff9ab93f813e2be446cb2e)

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b backport-ca3a5d0-3.2 lp:~troyanov/maas/+git/maas into -b 3.2 lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/2189/consoleText
COMMIT: 0b3a64dfedb943cc4fad610d1befc974d9f9e9ea

review: Needs Fixing
Revision history for this message
Anton Troyanov (troyanov) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b backport-ca3a5d0-3.2 lp:~troyanov/maas/+git/maas into -b 3.2 lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/2196/consoleText
COMMIT: 0b3a64dfedb943cc4fad610d1befc974d9f9e9ea

review: Needs Fixing
Revision history for this message
Anton Troyanov (troyanov) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b backport-ca3a5d0-3.2 lp:~troyanov/maas/+git/maas into -b 3.2 lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 0b3a64dfedb943cc4fad610d1befc974d9f9e9ea

review: Approve
Revision history for this message
Anton Troyanov (troyanov) wrote :

Self approve backport to 3.2

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maascli/api.py b/src/maascli/api.py
2index 1a4c14c..89db8b7 100644
3--- a/src/maascli/api.py
4+++ b/src/maascli/api.py
5@@ -519,17 +519,20 @@ def materialize_certificate(profile, cert_dir="~/.maascli.certs"):
6 File is needed for httplib2 ca_cert (for server cert validation)
7 """
8
9- cacerts = profile.get("cacerts")
10- if cacerts is None:
11+ profile_name = profile.get("name")
12+ if profile_name is None:
13 return None
14
15- cert_dir = Path(cert_dir).expanduser()
16- if not cert_dir.exists():
17- cert_dir.mkdir()
18+ cert_path = Path(cert_dir).expanduser() / (profile_name + ".pem")
19
20- profile_name = profile["name"]
21- cert_path = cert_dir / (profile_name + ".pem")
22- cert_path = Path(cert_path)
23+ if cert_path.exists():
24+ return cert_path
25
26+ cacerts = profile.get("cacerts")
27+ if cacerts is None:
28+ return None
29+
30+ cert_path.parent.mkdir(exist_ok=True, parents=True)
31 cert_path.write_text(cacerts)
32+
33 return cert_path
34diff --git a/src/maascli/tests/test_api.py b/src/maascli/tests/test_api.py
35index 9b2f659..9138eee 100644
36--- a/src/maascli/tests/test_api.py
37+++ b/src/maascli/tests/test_api.py
38@@ -220,6 +220,17 @@ class TestFunctions(MAASTestCase):
39 cert_path = temp_dir = temp_dir / (profile["name"] + ".pem")
40 self.assertEqual(cert, cert_path.open().read())
41
42+ def test_materialize_certificate_should_return_existing_cert(self):
43+ mock_write_cert = self.patch(Path, "write_text")
44+ profile = make_profile()
45+ with tempfile.TemporaryDirectory() as tmp:
46+ cert_file = Path(tmp) / (profile["name"] + ".pem")
47+ cert_file.touch()
48+ self.assertEqual(
49+ cert_file, api.materialize_certificate(profile, tmp)
50+ )
51+ mock_write_cert.assert_not_called()
52+
53
54 class TestAction(MAASTestCase):
55 """Tests for :class:`maascli.api.Action`."""

Subscribers

People subscribed via source and target branches