Merge ~woutervb/snap-store-proxy-charm:SN-484 into snap-store-proxy-charm:master

Proposed by Wouter van Bommel
Status: Merged
Approved by: Wouter van Bommel
Approved revision: 60c6fe873788ffec51c10940541249b95252bd95
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~woutervb/snap-store-proxy-charm:SN-484
Merge into: snap-store-proxy-charm:master
Prerequisite: ~woutervb/snap-store-proxy-charm:SN-482
Diff against target: 200 lines (+47/-14)
4 files modified
Makefile (+1/-1)
src/charm.py (+19/-3)
src/helpers.py (+4/-0)
tests/test_charm.py (+23/-10)
Reviewer Review Type Date Requested Status
Przemysław Suliga Approve
Review via email: mp+415647@code.launchpad.net

Commit message

Determine TLS usage and update status display

Show on which url the service is configured. Take the TLS status from the registration bundle.

To post a comment you must log in.
Revision history for this message
Przemysław Suliga (suligap) wrote :

lgtm + one comment

review: Approve
Revision history for this message
Wouter van Bommel (woutervb) wrote :

> lgtm + one comment
Thanks, changed

Revision history for this message
Otto Co-Pilot (otto-copilot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/Makefile b/Makefile
2index 11342be..c09cee6 100644
3--- a/Makefile
4+++ b/Makefile
5@@ -29,7 +29,7 @@ charm: $(CHARM_FILENAME)
6 # Ensure that the building of the charm depends on the python files that make
7 # up the charm
8 $(CHARM_FILENAME): $(DEPS) $(CHARMCRAFT)
9- $(CHARMCRAFT) pack
10+ $(CHARMCRAFT) pack --verbose
11
12 $(ENV): $(OLS_WHEELS_DIR) requirements-dev.txt requirements.txt
13 @echo Starting a new virtualenv
14diff --git a/src/charm.py b/src/charm.py
15index 500e623..b75ae15 100755
16--- a/src/charm.py
17+++ b/src/charm.py
18@@ -262,6 +262,14 @@ class SnapstoreProxyCharm(CharmBase):
19 # Incomplete bundle(?) so stop doing anything
20 return
21
22+ # see if we are running on https
23+ if data["domain"].startswith("https"):
24+ self._stored.ssl = True
25+ else:
26+ self._stored.ssl = False
27+
28+ logger.debug(f"Status of tls is {self._stored.ssl}")
29+
30 # Now make 2 lists, option names and values where the indexes
31 # correspond with eachother
32 options = []
33@@ -269,16 +277,24 @@ class SnapstoreProxyCharm(CharmBase):
34 for entry in data.keys():
35 if entry == "domain":
36 options.append("proxy.domain")
37- values.append(data[entry])
38+ # We only want the hostname to be registered
39+ values.append(urlparse(data[entry]).hostname)
40+ self._stored.domain = data[entry]
41+ logger.debug(
42+ f"Domain set from the registration bundle {self._stored.domain}"
43+ )
44 elif entry == "public_key":
45 options.append("proxy.key.public")
46 values.append(data[entry])
47+ logger.debug("Stored the public key from the registration bundle")
48 elif entry == "private_key":
49 options.append("proxy.key.private")
50 values.append(data[entry])
51+ logger.debug("Stored the private key from the registration bundle")
52 elif entry == "store_id":
53 options.append("internal.store.id")
54 values.append(data[entry])
55+ logger.debug("Stored the store id from the registration bundle")
56 configure_proxy(options, values, force=True)
57
58 def handle_registration(self, event):
59@@ -359,9 +375,9 @@ class SnapstoreProxyCharm(CharmBase):
60 return
61
62 # We are in a proper state, let's open the port
63- open_port(ssl=False)
64+ open_port(ssl=getattr(self._stored, "ssl", False))
65
66- self.unit.status = ActiveStatus()
67+ self.unit.status = ActiveStatus(f"Running on: {self._stored.domain}")
68
69 def _on_update_status(self, _):
70
71diff --git a/src/helpers.py b/src/helpers.py
72index 7b440e6..1a53ad2 100644
73--- a/src/helpers.py
74+++ b/src/helpers.py
75@@ -12,6 +12,7 @@ logger = logging.getLogger(__name__)
76
77 def configure_proxy(option, value, force=False):
78 """Simple wrapper around calling snap settings"""
79+ logger.info("Configuring the snap-store-proxy snap")
80
81 if isinstance(option, list):
82 # Make sure that we set multiple values in one go
83@@ -19,12 +20,15 @@ def configure_proxy(option, value, force=False):
84 for count, entry in enumerate(option):
85 combined.append(f'{entry}="{value[count]}"')
86 command = ["snap-store-proxy", "config"] + combined
87+ logger.debug("Running proxy config for single item")
88 else:
89 logger.debug(f"Running config for option {option} with value {value}")
90 command = ["snap-store-proxy", "config", f"{option}={value}"]
91+ logger.debug("Running registration for list of items")
92
93 if force:
94 command.append("--force")
95+ logger.debug("Forcing the options to be accepted")
96
97 run(command)
98
99diff --git a/tests/test_charm.py b/tests/test_charm.py
100index 69f0ba6..9fdac99 100644
101--- a/tests/test_charm.py
102+++ b/tests/test_charm.py
103@@ -308,12 +308,15 @@ class TestRegistrationBundle(CharmTest):
104 # == "Missing key store_assertion in the registration bundle"
105 # )
106
107- def test_has_complete_bundle(self):
108+ @pytest.mark.parametrize(
109+ "domain,ssl_status", [("http://non.ssl", False), ("https://on.ssl", True)]
110+ )
111+ def test_has_complete_bundle(self, domain, ssl_status):
112 to_update = [
113 {
114 "name": "registration_bundle",
115 "value": base64.b64encode(
116- '{"domain": "sdfsd",'
117+ f'{{"domain": "{domain}",'
118 '"public_key": "10",'
119 '"store_id": "11",'
120 '"store_assertion": "0",'
121@@ -331,11 +334,14 @@ class TestRegistrationBundle(CharmTest):
122 "internal.store.id",
123 "proxy.key.private",
124 ],
125- ["sdfsd", "10", "11", "12"],
126+ # For the domain we only need the part without the protocol
127+ # and since the code uses urlparse we prevent using that here
128+ [domain.rsplit("/")[-1], "10", "11", "12"],
129 force=True,
130 )
131
132 assert getattr(self.harness.charm._stored, "registered", None)
133+ assert getattr(self.harness.charm._stored, "ssl") == ssl_status
134
135
136 class TestStatus(CharmTest):
137@@ -389,11 +395,12 @@ class TestStatus(CharmTest):
138 self.harness.charm._stored.username = "minion"
139 self.harness.charm._stored.password = "minions password"
140 self.harness.charm._stored.registered = True
141+ self.harness.charm._stored.domain = "dummy.domain"
142
143 with patch("charm.open_port"):
144 self.harness.charm.evaluate_status()
145
146- assert self.harness.charm.unit.status.message == ""
147+ assert self.harness.charm.unit.status.message == "Running on: dummy.domain"
148
149
150 class TestEvents(CharmTest):
151@@ -594,7 +601,7 @@ class TestRegistration(CharmTest):
152 self.harness.charm._stored.registered = False
153 self.harness.charm._stored.username = "user@domain.country"
154 self.harness.charm._stored.password = "password"
155- self.harness.charm._stored.domain = "http://localhost"
156+ self.harness.charm._stored.domain = "localhost"
157 event = MagicMock()
158
159 with patch("charm.register_store") as patched_registration:
160@@ -622,7 +629,9 @@ class TestSanity(CharmTest):
161 self.harness.update_config({"username": None})
162
163 assert self.harness.charm._stored.username == "Some value"
164- assert self.harness.charm.unit.status == ActiveStatus("")
165+ assert self.harness.charm.unit.status == ActiveStatus(
166+ "Running on: http://localhost.localdomain"
167+ )
168
169 def test_empty_password_after_registration_and_active_status(self):
170 self.harness.charm._stored.registered = True
171@@ -635,21 +644,25 @@ class TestSanity(CharmTest):
172 self.harness.update_config({"password": None})
173
174 assert self.harness.charm._stored.password == "Some value"
175- assert self.harness.charm.unit.status == ActiveStatus("")
176+ assert self.harness.charm.unit.status == ActiveStatus(
177+ "Running on: http://localhost.localdomain"
178+ )
179
180 def test_update_domain_after_registration(self):
181 self.harness.charm._stored.snap_install_source = "store"
182 self.harness.charm._stored.registered = True
183 self.harness.charm._stored.database_created = True
184- self.harness.charm._stored.domain = "Some value"
185+ self.harness.charm._stored.domain = "http://Some.value"
186 self.harness.charm._stored.db_uri = "postgresql://user@host/database"
187
188 # Since valid values will be pushed on to the proxy, we need to stub that here
189 with patch("charm.configure_proxy"), patch("charm.open_port"):
190 self.harness.update_config({"domain": "http://my.super.domain"})
191
192- assert self.harness.charm._stored.domain == "Some value"
193- assert self.harness.charm.unit.status == ActiveStatus("")
194+ assert self.harness.charm._stored.domain == "http://Some.value"
195+ assert self.harness.charm.unit.status == ActiveStatus(
196+ "Running on: http://Some.value"
197+ )
198
199
200 class TestInstallation(CharmTest):

Subscribers

People subscribed via source and target branches

to all changes: