Merge ~hloeung/content-cache-charm:master into content-cache-charm:master

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 03773df540b72c74b5a64c858dea6b579ca50827
Merged at revision: 4b43be183bfb3670d2ea4ad4acd0ad13b37e274e
Proposed branch: ~hloeung/content-cache-charm:master
Merge into: content-cache-charm:master
Diff against target: 91 lines (+24/-3)
6 files modified
lib/utils.py (+10/-0)
reactive/content_cache.py (+2/-1)
tests/unit/files/config_test_config.txt (+1/-0)
tests/unit/files/nginx_config_rendered_test_output-site5.txt (+1/-1)
tests/unit/test_nginx.py (+3/-1)
tests/unit/test_utils.py (+7/-0)
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Review via email: mp+366153@code.launchpad.net

Commit message

Allow overriding backend/proxy path

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
Joel Sing (jsing) wrote :

LGTM, see comments inline.

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

Merge proposal is approved, but source revision has changed, setting status to needs review.

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

Change successfully merged at revision 4b43be183bfb3670d2ea4ad4acd0ad13b37e274e

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/utils.py b/lib/utils.py
2index e1288e4..cff2405 100644
3--- a/lib/utils.py
4+++ b/lib/utils.py
5@@ -57,3 +57,13 @@ def generate_token(signing_secret, url_path, expiry_time):
6 digest = hmac.new(signing_secret.encode(), string_to_sign.encode(),
7 hashlib.sha1)
8 return "{0}_{1}".format(expiration, digest.hexdigest())
9+
10+
11+def generate_uri(host, port=80, path=None, scheme='http'):
12+ if not path:
13+ path = ''
14+ # XXX: Fix to handle if host is an IPv6 literal
15+ if path and not path.startswith('/'):
16+ path = '/{}'.format(path)
17+ uri = '{scheme}://{host}:{port}{path}'.format(scheme=scheme, host=host, port=port, path=path)
18+ return uri
19diff --git a/reactive/content_cache.py b/reactive/content_cache.py
20index 7f4f3a3..6ab5eb3 100644
21--- a/reactive/content_cache.py
22+++ b/reactive/content_cache.py
23@@ -103,7 +103,8 @@ def configure_nginx(conf_path=None):
24
25 backend_port = loc_conf.get('backend_port')
26 if backend_port:
27- lc['backend'] = 'http://localhost:{}'.format(backend_port)
28+ backend_path = loc_conf.get('backend-path')
29+ lc['backend'] = utils.generate_uri('localhost', backend_port, backend_path)
30
31 # Per site secret HMAC key, if it exists. We pass this through to
32 # the caching layer to activate the bit to restrict access.
33diff --git a/tests/unit/files/config_test_config.txt b/tests/unit/files/config_test_config.txt
34index e074496..9b29970 100644
35--- a/tests/unit/files/config_test_config.txt
36+++ b/tests/unit/files/config_test_config.txt
37@@ -66,3 +66,4 @@ site5:
38 modifier: '='
39 backends:
40 - 127.0.1.11:80
41+ backend-path: /auth-check/
42diff --git a/tests/unit/files/nginx_config_rendered_test_output-site5.txt b/tests/unit/files/nginx_config_rendered_test_output-site5.txt
43index 9425247..8827016 100644
44--- a/tests/unit/files/nginx_config_rendered_test_output-site5.txt
45+++ b/tests/unit/files/nginx_config_rendered_test_output-site5.txt
46@@ -18,7 +18,7 @@ server {
47
48
49 location = /auth {
50- proxy_pass http://localhost:8084;
51+ proxy_pass http://localhost:8084/auth-check/;
52 proxy_set_header Host "site5.local";
53 proxy_cache site5-cache;
54 proxy_cache_background_update on;
55diff --git a/tests/unit/test_nginx.py b/tests/unit/test_nginx.py
56index a58ef7e..31d6a9e 100644
57--- a/tests/unit/test_nginx.py
58+++ b/tests/unit/test_nginx.py
59@@ -7,6 +7,7 @@ import yaml
60
61 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
62 from lib import nginx # NOQA: E402
63+from lib import utils # NOQA: E402
64
65 BASE_LISTEN_PORT = 6080
66 BASE_BACKEND_PORT = 8080
67@@ -59,7 +60,8 @@ class TestLibNginx(unittest.TestCase):
68
69 if loc_conf.get('backends'):
70 backend_port += 1
71- lc['backend'] = 'http://localhost:{}'.format(backend_port)
72+ backend_path = loc_conf.get('backend-path')
73+ lc['backend'] = utils.generate_uri('localhost', backend_port, backend_path)
74
75 lc['signed-url-hmac-key'] = loc_conf.get('signed-url-hmac-key')
76 lc['origin-headers'] = loc_conf.get('origin-headers')
77diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
78index 89604c8..8a0fd9c 100644
79--- a/tests/unit/test_utils.py
80+++ b/tests/unit/test_utils.py
81@@ -68,3 +68,10 @@ class TestLibUtils(unittest.TestCase):
82 expiry_time = datetime.datetime.now() + datetime.timedelta(days=1)
83 want = '1553299200_d5257bb9f1e5e27065f2e7c986ca8c95f4cc3680'
84 self.assertEqual(utils.generate_token(signing_key, '/', expiry_time), want)
85+
86+ def test_generate_uri(self):
87+ self.assertEqual(utils.generate_uri('localhost'), 'http://localhost:80')
88+ self.assertEqual(utils.generate_uri('localhost', 8080), 'http://localhost:8080')
89+ self.assertEqual(utils.generate_uri('localhost', path='mypath'), 'http://localhost:80/mypath')
90+ self.assertEqual(utils.generate_uri('localhost', path='/mypath'), 'http://localhost:80/mypath')
91+ self.assertEqual(utils.generate_uri('10.0.0.1', path='/mypath'), 'http://10.0.0.1:80/mypath')

Subscribers

People subscribed via source and target branches