Merge lp:~jelmer/brz/fix-open-policy into lp:brz/3.0

Proposed by Jelmer Vernooij on 2019-06-09
Status: Merged
Approved by: Jelmer Vernooij on 2019-06-09
Approved revision: 7317
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/fix-open-policy
Merge into: lp:brz/3.0
Diff against target: 178 lines (+101/-8)
2 files modified
breezy/tests/test_url_policy_open.py (+55/-2)
breezy/url_policy_open.py (+46/-6)
To merge this branch: bzr merge lp:~jelmer/brz/fix-open-policy
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve on 2019-06-09
Review via email: mp+368594@code.launchpad.net

Commit message

Support redirects in URL policy opener.

Description of the change

Support redirects in URL policy opener.

This should fix the tests for lp:~cjwatson/launchpad/use-bzr-policy-open

To post a comment you must log in.
Jelmer Vernooij (jelmer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/tests/test_url_policy_open.py'
2--- breezy/tests/test_url_policy_open.py 2018-11-12 01:41:38 +0000
3+++ breezy/tests/test_url_policy_open.py 2019-06-09 19:10:13 +0000
4@@ -30,7 +30,10 @@
5 ControlDir,
6 ControlDirFormat,
7 )
8-from ..errors import NotBranchError
9+from ..errors import (
10+ NotBranchError,
11+ RedirectRequested,
12+ )
13 from ..url_policy_open import (
14 BadUrl,
15 _BlacklistPolicy,
16@@ -44,7 +47,15 @@
17 TestCase,
18 TestCaseWithTransport,
19 )
20-from ..transport import chroot
21+from ..transport import (
22+ chroot,
23+ get_transport,
24+ register_transport_proto,
25+ register_transport,
26+ unregister_transport,
27+ transport_list_registry,
28+ Transport,
29+ )
30
31
32 class TestBranchOpenerCheckAndFollowBranchReference(TestCase):
33@@ -305,6 +316,48 @@
34 set(TrackingProber.seen_urls), {b.base, a.base})
35
36
37+class TestRedirects(TestCaseWithTransport):
38+
39+ def setUp(self):
40+ super(TestRedirects, self).setUp()
41+ BranchOpener.install_hook()
42+
43+ def setup_redirect(self, target_url):
44+ class RedirectingTransport(Transport):
45+
46+ def get(self, name):
47+ raise RedirectRequested(self.base, target_url)
48+
49+ def _redirected_to(self, source, target):
50+ return get_transport(target)
51+
52+ register_transport_proto(
53+ 'redirecting://', help="Test transport that redirects.")
54+ register_transport('redirecting://', RedirectingTransport)
55+ self.addCleanup(unregister_transport, 'redirecting://', RedirectingTransport)
56+
57+ def make_branch_opener(self, allowed_urls, probers=None):
58+ policy = WhitelistPolicy(True, allowed_urls, True)
59+ return BranchOpener(policy, probers)
60+
61+ def test_redirect_forbidden(self):
62+ b = self.make_branch('b')
63+ self.setup_redirect(b.base)
64+ class TrackingProber(BzrProber):
65+ seen_urls = []
66+
67+ @classmethod
68+ def probe_transport(klass, transport):
69+ klass.seen_urls.append(transport.base)
70+ return BzrProber.probe_transport(transport)
71+
72+ opener = self.make_branch_opener(['redirecting:///'], probers=[TrackingProber])
73+ self.assertRaises(BadUrl, opener.open, 'redirecting:///')
74+
75+ opener = self.make_branch_opener(['redirecting:///', b.base], probers=[TrackingProber])
76+ opener.open('redirecting:///')
77+
78+
79 class TestOpenOnlyScheme(TestCaseWithTransport):
80 """Tests for `open_only_scheme`."""
81
82
83=== modified file 'breezy/url_policy_open.py'
84--- breezy/url_policy_open.py 2018-11-12 01:41:38 +0000
85+++ breezy/url_policy_open.py 2019-06-09 19:10:13 +0000
86@@ -22,11 +22,17 @@
87
88 from . import (
89 errors,
90+ trace,
91 urlutils,
92 )
93 from .branch import Branch
94 from .controldir import (
95 ControlDir,
96+ ControlDirFormat,
97+ )
98+from .transport import (
99+ do_catching_redirections,
100+ get_transport,
101 )
102
103
104@@ -200,6 +206,8 @@
105 """
106 self.policy = policy
107 self._seen_urls = set()
108+ if probers is None:
109+ probers = ControlDirFormat.all_probers()
110 self.probers = probers
111
112 @classmethod
113@@ -276,15 +284,47 @@
114 # spurious loop exceptions.
115 self._seen_urls = set()
116
117+ def _open_dir(self, url):
118+ """Simple BzrDir.open clone that only uses specific probers.
119+
120+ :param url: URL to open
121+ :return: ControlDir instance
122+ """
123+ def redirected(transport, e, redirection_notice):
124+ self.policy.check_one_url(e.target)
125+ redirected_transport = transport._redirected_to(
126+ e.source, e.target)
127+ if redirected_transport is None:
128+ raise errors.NotBranchError(e.source)
129+ trace.note(
130+ '%s is%s redirected to %s',
131+ transport.base, e.permanently, redirected_transport.base)
132+ return redirected_transport
133+
134+ def find_format(transport):
135+ last_error = errors.NotBranchError(transport.base)
136+ for prober_kls in self.probers:
137+ prober = prober_kls()
138+ try:
139+ return transport, prober.probe_transport(transport)
140+ except errors.NotBranchError as e:
141+ last_error = e
142+ else:
143+ raise last_error
144+ transport = get_transport(url)
145+ transport, format = do_catching_redirections(
146+ find_format, transport, redirected)
147+ return format.open(transport)
148+
149 def follow_reference(self, url):
150 """Get the branch-reference value at the specified url.
151
152 This exists as a separate method only to be overriden in unit tests.
153 """
154- controldir = ControlDir.open(url, probers=self.probers)
155+ controldir = self._open_dir(url)
156 return controldir.get_branch_reference()
157
158- def open(self, url):
159+ def open(self, url, ignore_fallbacks=False):
160 """Open the Bazaar branch at url, first checking it.
161
162 What is acceptable means is defined by the policy's `follow_reference`
163@@ -295,11 +335,11 @@
164
165 url = self.check_and_follow_branch_reference(url)
166
167- def open_branch(url):
168- dir = ControlDir.open(url, probers=self.probers)
169- return dir.open_branch()
170+ def open_branch(url, ignore_fallbacks):
171+ dir = self._open_dir(url)
172+ return dir.open_branch(ignore_fallbacks=ignore_fallbacks)
173 return self.run_with_transform_fallback_location_hook_installed(
174- open_branch, url)
175+ open_branch, url, ignore_fallbacks)
176
177
178 def open_only_scheme(allowed_scheme, url):

Subscribers

People subscribed via source and target branches