Merge ~andreserl/maas:2.3_lp1778710 into maas:2.3

Proposed by Andres Rodriguez
Status: Merged
Approved by: Andres Rodriguez
Approved revision: 3b68242a9223db014f1b1ca398f023edbc94f33a
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~andreserl/maas:2.3_lp1778710
Merge into: maas:2.3
Diff against target: 123 lines (+63/-1)
5 files modified
src/maasserver/api/tests/test_maas.py (+11/-0)
src/maasserver/bootsources.py (+15/-1)
src/maasserver/forms/settings.py (+19/-0)
src/maasserver/models/config.py (+1/-0)
src/maasserver/tests/test_bootsources.py (+17/-0)
Reviewer Review Type Date Requested Status
Andres Rodriguez (community) Approve
Review via email: mp+349492@code.launchpad.net

Commit message

Backport cf12161 - LP: 1778710 - Add 'boot_images_no_proxy' setting

Allows setting no_proxy for the host of the MAAS image repo if it is
behind a proxy.

To post a comment you must log in.
Revision history for this message
Andres Rodriguez (andreserl) wrote :

selfie!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/api/tests/test_maas.py b/src/maasserver/api/tests/test_maas.py
2index b1f3342..fbf89d5 100644
3--- a/src/maasserver/api/tests/test_maas.py
4+++ b/src/maasserver/api/tests/test_maas.py
5@@ -292,3 +292,14 @@ class MAASHandlerAPITest(APITestCase.ForUser):
6 })
7 self.assertEqual(http.client.OK, response.status_code)
8 self.assertTrue(Config.objects.get_config("use_peer_proxy"))
9+
10+ def test_set_config_boot_images_no_proxy(self):
11+ self.become_admin()
12+ response = self.client.post(
13+ reverse('maas_handler'), {
14+ "op": "set_config",
15+ "name": "boot_images_no_proxy",
16+ "value": True,
17+ })
18+ self.assertEqual(http.client.OK, response.status_code)
19+ self.assertTrue(Config.objects.get_config("boot_images_no_proxy"))
20diff --git a/src/maasserver/bootsources.py b/src/maasserver/bootsources.py
21index c5fd77d..ea1bde4 100644
22--- a/src/maasserver/bootsources.py
23+++ b/src/maasserver/bootsources.py
24@@ -12,6 +12,7 @@ __all__ = [
25
26 import html
27 import os
28+from urllib.parse import urlparse
29
30 from maasserver.components import (
31 discard_persistent_error,
32@@ -91,7 +92,20 @@ def get_simplestreams_env():
33 # entire process, including controller refresh. When the region
34 # needs to refresh itself it sends itself results over HTTP to
35 # 127.0.0.1.
36- env['no_proxy'] = '127.0.0.1,localhost'
37+ no_proxy_hosts = '127.0.0.1,localhost'
38+ # When using a proxy and using an image mirror, we may not want
39+ # to use the proxy to download the images, as they could be
40+ # localted in the local network, hence it makes no sense to use
41+ # it. With this, we add the image mirror localtion(s) to the
42+ # no proxy variable, which ensures MAAS contacts the mirror
43+ # directly instead of through the proxy.
44+ no_proxy = Config.objects.get_config('boot_images_no_proxy')
45+ if no_proxy:
46+ sources = get_boot_sources()
47+ for source in sources:
48+ host = urlparse(source["url"]).netloc.split(':')[0]
49+ no_proxy_hosts = ",".join((no_proxy_hosts, host))
50+ env['no_proxy'] = no_proxy_hosts
51 return env
52
53
54diff --git a/src/maasserver/forms/settings.py b/src/maasserver/forms/settings.py
55index e659d0b..ce47aad 100644
56--- a/src/maasserver/forms/settings.py
57+++ b/src/maasserver/forms/settings.py
58@@ -465,6 +465,25 @@ CONFIG_ITEMS = {
59 (IMPORT_RESOURCES_SERVICE_PERIOD.total_seconds() / 60.0))
60 }
61 },
62+ 'boot_images_no_proxy': {
63+ 'default': False,
64+ 'form': forms.BooleanField,
65+ 'form_kwargs': {
66+ 'required': False,
67+ 'label': (
68+ "Set no_proxy with the image repository address when MAAS "
69+ "is behind (or set with) a proxy."),
70+ 'help_text': (
71+ "By default, when MAAS is behind (and set with) a proxy, it "
72+ "is used to download images from the image repository. In "
73+ "some situations (e.g. when using a local image repository) "
74+ "it doesn't make sense for MAAS to use the proxy to download "
75+ "images because it can access them directly. Setting this "
76+ "option allows MAAS to access the (local) image repository "
77+ "directly by setting the no_proxy variable for the MAAS env "
78+ "with the address of the image repository.")
79+ }
80+ },
81 'curtin_verbose': {
82 'default': False,
83 'form': forms.BooleanField,
84diff --git a/src/maasserver/models/config.py b/src/maasserver/models/config.py
85index 756b17b..f5837dc 100644
86--- a/src/maasserver/models/config.py
87+++ b/src/maasserver/models/config.py
88@@ -86,6 +86,7 @@ def get_default_config():
89 'uuid': None,
90 # Images.
91 'boot_images_auto_import': True,
92+ 'boot_images_no_proxy': False,
93 # Third Party
94 'enable_third_party_drivers': True,
95 # Disk erasing.
96diff --git a/src/maasserver/tests/test_bootsources.py b/src/maasserver/tests/test_bootsources.py
97index 3679ff8..5a1f313 100644
98--- a/src/maasserver/tests/test_bootsources.py
99+++ b/src/maasserver/tests/test_bootsources.py
100@@ -258,6 +258,23 @@ class TestPrivateCacheBootSources(MAASTransactionServerTestCase):
101 capture.env['http_proxy'], capture.env['https_proxy'],
102 capture.env['no_proxy']))
103
104+ def test__has_env_http_and_https_proxy_set_with_custom_no_proxy(self):
105+ proxy_address = factory.make_name('proxy')
106+ Config.objects.set_config('http_proxy', proxy_address)
107+ Config.objects.set_config('boot_images_no_proxy', True)
108+ capture = (
109+ patch_and_capture_env_for_download_all_image_descriptions(self))
110+ factory.make_BootSource(
111+ keyring_data=b'1234',
112+ url=b'http://192.168.1.100:8080/ephemeral-v3/')
113+ cache_boot_sources()
114+ no_proxy_hosts = '127.0.0.1,localhost,192.168.1.100'
115+ self.assertEqual(
116+ (proxy_address, proxy_address, no_proxy_hosts),
117+ (
118+ capture.env['http_proxy'], capture.env['https_proxy'],
119+ capture.env['no_proxy']))
120+
121 def test__passes_user_agent_with_maas_version(self):
122 mock_download = self.patch(
123 bootsources, 'download_all_image_descriptions')

Subscribers

People subscribed via source and target branches