Merge ~seyeongkim/charm-ubuntu-advantage:adding_proxy_configs into charm-ubuntu-advantage:master

Proposed by Seyeong Kim
Status: Merged
Approved by: Allan Vidal
Approved revision: 712bd97c8b8ae7edc1336f8ba215a8bfd829646b
Merged at revision: 94fed8b7a73b66d4e416430a02147c1ecbcd2036
Proposed branch: ~seyeongkim/charm-ubuntu-advantage:adding_proxy_configs
Merge into: charm-ubuntu-advantage:master
Diff against target: 237 lines (+150/-28)
4 files modified
README.md (+11/-0)
config.yaml (+8/-0)
src/charm.py (+25/-10)
tests/unit/test_charm.py (+106/-18)
Reviewer Review Type Date Requested Status
Allan Vidal Approve
Andrew Polukhin Approve
Review via email: mp+458565@code.launchpad.net

Commit message

Adding override-http-proxy

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Andrew Polukhin (andrew-polukhin) wrote :

LGTM and thanks for the contribution! Please wait for the second review.

review: Approve
Revision history for this message
Allan Vidal (alnvdl) wrote :

Hi Seyeong,

Thanks for this patch! I deployed it, and it seems to be working. I do have a few suggestions though:

1. If you change the config parameters, it's not getting updated up by the charm. That's because you need to reload the env config whenever the config changes:

    def config_changed(self, event):
        """Install and configure ubuntu-advantage tools and attachment."""
        logger.info("Beginning config_changed")
        self.unit.status = MaintenanceStatus("Configuring")
        self._setup_proxy_env() <-- This is missing
        self._handle_ppa_state()
        self._handle_package_state()
        self._handle_subscription_state()
        ...

2. It would be best to also try to capture the behavior above in a unit test (something like `test_config_changed_set_and_unset_proxy`).

3. I think `no-proxy` is not supported by the Pro client, so maybe we should
   just remove this override? See the output of
   `sudo ubuntu-advantage config show` for details.

4. I think documentation could be improved a bit in the README as well. Here's
   my suggestion to add there (feel free to tweak it to your liking):

   ## Proxy config

    By default, this charm will pick up the proxy configuration from the Juju
    model. If you want to use a different proxy instead for the units, you can
    override that with configs `override-http-proxy` and `override-https-proxy`.

    Please note that in all cases, the Ubuntu Pro client will check if these proxy
    configs are valid. If it cannot use them (e.g., proxy is not reachable), you
    will most likely get a `hook failed: "config-changed"` message. You can check
    the causes with `juju debug-log`.

Allan

Revision history for this message
Seyeong Kim (seyeongkim) wrote :

Hey Allen, Thanks for the review.

I re-uploaded as you mentioned.

I'm not familiar with unit test yet, Please check it again if it is ok.

Also, I removed no_proxy in test_setup_proxy_env. but I'm not sure i need to remove as it is not included in my patch.

Thanks.

Revision history for this message
Allan Vidal (alnvdl) wrote :

Thanks for the progress Seyeong! I have a few suggestions still.

review: Needs Fixing
Revision history for this message
Seyeong Kim (seyeongkim) wrote :

Thanks a lot. I changed it and reformatted with black

Revision history for this message
Allan Vidal (alnvdl) wrote :

LGTM, thanks for this work @seyeongkim!

review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 94fed8b7a73b66d4e416430a02147c1ecbcd2036

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/README.md b/README.md
2index e9bd371..87ecf55 100644
3--- a/README.md
4+++ b/README.md
5@@ -30,3 +30,14 @@ not promulgated.
6 In the past, it was also published as https://charmhub.io/ubuntu-pro (also
7 not promulgated), but that was discontinued until proper support for charm
8 renames is introduced.
9+
10+## Proxy config
11+
12+By default, this charm will pick up the proxy configuration from the Juju
13+model. If you want to use a different proxy instead for the units, you can
14+override that with configs `override-http-proxy` and `override-https-proxy`.
15+
16+Please note that in all cases, the Ubuntu Pro client will check if these proxy
17+configs are valid. If it cannot use them (e.g., proxy is not reachable), you
18+will most likely get a `hook failed: "config-changed"` message. You can check
19+the causes with `juju debug-log`.
20diff --git a/config.yaml b/config.yaml
21index a6fb1a7..e81d8b5 100644
22--- a/config.yaml
23+++ b/config.yaml
24@@ -16,3 +16,11 @@ options:
25 default: ""
26 description: Ubuntu Pro token obtained from https://ubuntu.com/pro.
27 type: string
28+ override-http-proxy:
29+ default: ""
30+ description: http-proxy which can override juju http-proxy
31+ type: string
32+ override-https-proxy:
33+ default: ""
34+ description: https-proxy which can override juju https-proxy
35+ type: string
36diff --git a/src/charm.py b/src/charm.py
37index 04bd738..bf5051c 100755
38--- a/src/charm.py
39+++ b/src/charm.py
40@@ -76,8 +76,12 @@ class UbuntuAdvantageCharm(CharmBase):
41 def _setup_proxy_env(self):
42 """Setup proxy variables from model."""
43 self.env = dict(os.environ)
44- self.env["http_proxy"] = self.env.get("JUJU_CHARM_HTTP_PROXY", "")
45- self.env["https_proxy"] = self.env.get("JUJU_CHARM_HTTPS_PROXY", "")
46+ self.env["http_proxy"] = self.config.get("override-http-proxy") or self.env.get(
47+ "JUJU_CHARM_HTTP_PROXY", ""
48+ )
49+ self.env["https_proxy"] = self.config.get("override-https-proxy") or self.env.get(
50+ "JUJU_CHARM_HTTPS_PROXY", ""
51+ )
52 self.env["no_proxy"] = self.env.get("JUJU_CHARM_NO_PROXY", "")
53
54 # The values for 'http_proxy' and 'https_proxy' will be used for the
55@@ -95,6 +99,7 @@ class UbuntuAdvantageCharm(CharmBase):
56 """Install and configure ubuntu-advantage tools and attachment."""
57 logger.info("Beginning config_changed")
58 self.unit.status = MaintenanceStatus("Configuring")
59+ self._setup_proxy_env()
60 self._handle_ppa_state()
61 self._handle_package_state()
62 self._handle_subscription_state()
63@@ -181,14 +186,24 @@ class UbuntuAdvantageCharm(CharmBase):
64 def _configure_ua_proxy(self):
65 """Configure the proxy options for the ubuntu-advantage client."""
66 for config_key in ("http_proxy", "https_proxy"):
67- subprocess.check_call(
68- [
69- "ubuntu-advantage",
70- "config",
71- "set",
72- "{}={}".format(config_key, self.env[config_key]),
73- ]
74- )
75+ if self.env[config_key]:
76+ subprocess.check_call(
77+ [
78+ "ubuntu-advantage",
79+ "config",
80+ "set",
81+ "{}={}".format(config_key, self.env[config_key]),
82+ ]
83+ )
84+ else:
85+ subprocess.check_call(
86+ [
87+ "ubuntu-advantage",
88+ "config",
89+ "unset",
90+ config_key,
91+ ]
92+ )
93
94
95 if __name__ == "__main__": # pragma: nocover
96diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
97index f544c99..da7c0f0 100644
98--- a/tests/unit/test_charm.py
99+++ b/tests/unit/test_charm.py
100@@ -442,6 +442,65 @@ class TestCharm(TestCase):
101 self.mocks["call"].assert_not_called()
102 self.assertEqual(self.harness.model.unit.status, BlockedStatus("No token configured"))
103
104+ def test_config_changed_set_and_unset_proxy_override(self):
105+ # Set proxy override once.
106+ self.mocks["check_output"].side_effect = [STATUS_DETACHED]
107+ self.harness.update_config(
108+ {
109+ "override-http-proxy": "http://localhost:3128",
110+ "override-https-proxy": "http://localhost:3128",
111+ }
112+ )
113+ self.mocks["check_call"].assert_has_calls(
114+ [
115+ call(["ubuntu-advantage", "config", "set", "http_proxy=http://localhost:3128"]),
116+ call(["ubuntu-advantage", "config", "set", "https_proxy=http://localhost:3128"]),
117+ ]
118+ )
119+ self.mocks["check_call"].reset_mock()
120+
121+ # Set proxy override again.
122+ self.mocks["check_output"].side_effect = [STATUS_DETACHED]
123+ self.harness.update_config(
124+ {
125+ "override-http-proxy": "http://squid.internal:3128",
126+ "override-https-proxy": "http://squid.internal:3128",
127+ }
128+ )
129+ self.mocks["check_call"].assert_has_calls(
130+ [
131+ call(
132+ ["ubuntu-advantage", "config", "set", "http_proxy=http://squid.internal:3128"]
133+ ),
134+ call(
135+ ["ubuntu-advantage", "config", "set", "https_proxy=http://squid.internal:3128"]
136+ ),
137+ ]
138+ )
139+ self.mocks["check_call"].reset_mock()
140+
141+ # Unset proxy override.
142+ self.mocks["check_output"].side_effect = [STATUS_DETACHED]
143+ self.harness.update_config({"override-http-proxy": "", "override-https-proxy": ""})
144+ self.mocks["check_call"].assert_has_calls(
145+ [
146+ call(["ubuntu-advantage", "config", "unset", "http_proxy"]),
147+ call(["ubuntu-advantage", "config", "unset", "https_proxy"]),
148+ ]
149+ )
150+
151+ def test_setup_proxy_config(self):
152+ self.harness.update_config(
153+ {
154+ "override-http-proxy": TEST_PROXY_URL,
155+ "override-https-proxy": TEST_PROXY_URL,
156+ }
157+ )
158+
159+ self.harness.charm._setup_proxy_env()
160+ self.assertEqual(self.harness.charm.env["http_proxy"], TEST_PROXY_URL)
161+ self.assertEqual(self.harness.charm.env["https_proxy"], TEST_PROXY_URL)
162+
163 def test_setup_proxy_env(self):
164 self.mocks["environ"].update(
165 {
166@@ -458,24 +517,53 @@ class TestCharm(TestCase):
167
168 def _add_ua_proxy_setup_calls(self, call_list, append=True):
169 """Helper to generate the calls used for UA proxy setup."""
170- proxy_calls = [
171- call(
172- [
173- "ubuntu-advantage",
174- "config",
175- "set",
176- "http_proxy={}".format(self.env["http_proxy"]),
177- ]
178- ),
179- call(
180- [
181- "ubuntu-advantage",
182- "config",
183- "set",
184- "https_proxy={}".format(self.env["https_proxy"]),
185- ]
186- ),
187- ]
188+ proxy_calls = []
189+ if self.env["http_proxy"]:
190+ proxy_calls.append(
191+ call(
192+ [
193+ "ubuntu-advantage",
194+ "config",
195+ "set",
196+ "http_proxy={}".format(self.env["http_proxy"]),
197+ ]
198+ )
199+ )
200+ else:
201+ proxy_calls.append(
202+ call(
203+ [
204+ "ubuntu-advantage",
205+ "config",
206+ "unset",
207+ "http_proxy",
208+ ]
209+ )
210+ )
211+
212+ if self.env["https_proxy"]:
213+ proxy_calls.append(
214+ call(
215+ [
216+ "ubuntu-advantage",
217+ "config",
218+ "set",
219+ "https_proxy={}".format(self.env["https_proxy"]),
220+ ]
221+ )
222+ )
223+ else:
224+ proxy_calls.append(
225+ call(
226+ [
227+ "ubuntu-advantage",
228+ "config",
229+ "unset",
230+ "https_proxy",
231+ ]
232+ )
233+ )
234+
235 return call_list + proxy_calls if append else proxy_calls + call_list
236
237 def _assert_apt_calls(self):

Subscribers

People subscribed via source and target branches