Merge ~pappacena/turnip:http-v2-protocol into turnip:master
- Git
- lp:~pappacena/turnip
- http-v2-protocol
- Merge into master
Status: | Work in progress |
---|---|
Proposed branch: | ~pappacena/turnip:http-v2-protocol |
Merge into: | turnip:master |
Diff against target: |
996 lines (+497/-58) 16 files modified
config.yaml (+5/-0) requirements.txt (+3/-2) setup.py (+1/-0) turnip/pack/extensions/__init__.py (+50/-0) turnip/pack/extensions/agent.py (+26/-0) turnip/pack/extensions/extension.py (+61/-0) turnip/pack/extensions/fetch.py (+22/-0) turnip/pack/extensions/lsrefs.py (+22/-0) turnip/pack/extensions/tests/__init__.py (+0/-0) turnip/pack/extensions/tests/test_extensions.py (+42/-0) turnip/pack/git.py (+65/-20) turnip/pack/helpers.py (+75/-19) turnip/pack/http.py (+39/-4) turnip/pack/tests/test_functional.py (+15/-2) turnip/pack/tests/test_helpers.py (+38/-11) turnip/pack/tests/test_http.py (+33/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Launchpad code reviewers | Pending | ||
Review via email: mp+388269@code.launchpad.net |
Commit message
Accepting git-protocol header for protocol version 2
Description of the change
- 6e8f1a4... by Thiago F. Pappacena
-
Refactoring the way we implement extensions
- 5132bc1... by Thiago F. Pappacena
-
delegating commands to extension
- 60583ba... by Thiago F. Pappacena
-
Merge branch 'master' into http-v2-protocol
- e126bd5... by Thiago F. Pappacena
-
Decoding v2 protocol extra parameters
- 5ba7fdf... by Thiago F. Pappacena
-
Merge branch 'decode-
v2-protocol- commands' into http-v2-protocol - 1b9bf8a... by Thiago F. Pappacena
-
Making the v2 protocol negotiation work, and cleaning up things
- 797a1ac... by Thiago F. Pappacena
-
ls-refs kind of working
- 7a76ade... by Thiago F. Pappacena
-
Checkpoint
- caefc9d... by Thiago F. Pappacena
-
HTTP v2 working ok-ish
Unmerged commits
- caefc9d... by Thiago F. Pappacena
-
HTTP v2 working ok-ish
- 7a76ade... by Thiago F. Pappacena
-
Checkpoint
- 797a1ac... by Thiago F. Pappacena
-
ls-refs kind of working
- 1b9bf8a... by Thiago F. Pappacena
-
Making the v2 protocol negotiation work, and cleaning up things
- 5ba7fdf... by Thiago F. Pappacena
-
Merge branch 'decode-
v2-protocol- commands' into http-v2-protocol - e126bd5... by Thiago F. Pappacena
-
Decoding v2 protocol extra parameters
- 60583ba... by Thiago F. Pappacena
-
Merge branch 'master' into http-v2-protocol
- 5132bc1... by Thiago F. Pappacena
-
delegating commands to extension
- 6e8f1a4... by Thiago F. Pappacena
-
Refactoring the way we implement extensions
- b37f9e2... by Thiago F. Pappacena
-
Adding git v2 protocol header for HTTP frontend
Preview Diff
1 | diff --git a/config.yaml b/config.yaml | |||
2 | index 99b0e7b..3a75467 100644 | |||
3 | --- a/config.yaml | |||
4 | +++ b/config.yaml | |||
5 | @@ -21,3 +21,8 @@ openid_provider_root: https://testopenid.test/ | |||
6 | 21 | site_name: git.launchpad.test | 21 | site_name: git.launchpad.test |
7 | 22 | main_site_root: https://launchpad.test/ | 22 | main_site_root: https://launchpad.test/ |
8 | 23 | celery_broker: pyamqp://guest@localhost// | 23 | celery_broker: pyamqp://guest@localhost// |
9 | 24 | |||
10 | 25 | # Enabled pack extension classes, relative to turnip.pack.extensions. This | ||
11 | 26 | # is only available for v2 protocol. If no extension is enabled, v2 | ||
12 | 27 | # compatibility will not be advertised. | ||
13 | 28 | pack_extensions: .agent.Agent .lsrefs.LSRefs .fetch.Fetch | ||
14 | diff --git a/requirements.txt b/requirements.txt | |||
15 | index 89895ea..4c5f050 100644 | |||
16 | --- a/requirements.txt | |||
17 | +++ b/requirements.txt | |||
18 | @@ -39,7 +39,7 @@ mock==3.0.5; python_version < "3" | |||
19 | 39 | pathlib2==2.3.5 | 39 | pathlib2==2.3.5 |
20 | 40 | Paste==2.0.2 | 40 | Paste==2.0.2 |
21 | 41 | PasteDeploy==2.1.0 | 41 | PasteDeploy==2.1.0 |
23 | 42 | pbr==5.4.4 | 42 | pbr==5.4.5 |
24 | 43 | pep8==1.5.7 | 43 | pep8==1.5.7 |
25 | 44 | psutil==5.7.0 | 44 | psutil==5.7.0 |
26 | 45 | pyasn1==0.4.8 | 45 | pyasn1==0.4.8 |
27 | @@ -60,7 +60,8 @@ scandir==1.10.0 | |||
28 | 60 | setuptools-scm==1.17.0 | 60 | setuptools-scm==1.17.0 |
29 | 61 | simplejson==3.6.5 | 61 | simplejson==3.6.5 |
30 | 62 | six==1.14.0 | 62 | six==1.14.0 |
32 | 63 | testtools==2.3.0 | 63 | testscenarios-0.5.0 |
33 | 64 | testtools==2.4.0 | ||
34 | 64 | traceback2==1.4.0 | 65 | traceback2==1.4.0 |
35 | 65 | translationstring==1.3 | 66 | translationstring==1.3 |
36 | 66 | Twisted[conch]==20.3.0 | 67 | Twisted[conch]==20.3.0 |
37 | diff --git a/setup.py b/setup.py | |||
38 | index 76bf6c2..44507da 100755 | |||
39 | --- a/setup.py | |||
40 | +++ b/setup.py | |||
41 | @@ -37,6 +37,7 @@ test_requires = [ | |||
42 | 37 | 'fixtures', | 37 | 'fixtures', |
43 | 38 | 'flake8', | 38 | 'flake8', |
44 | 39 | 'mock; python_version < "3"', | 39 | 'mock; python_version < "3"', |
45 | 40 | 'testscenarios', | ||
46 | 40 | 'testtools', | 41 | 'testtools', |
47 | 41 | 'webtest', | 42 | 'webtest', |
48 | 42 | ] | 43 | ] |
49 | diff --git a/turnip/pack/extensions/__init__.py b/turnip/pack/extensions/__init__.py | |||
50 | 43 | new file mode 100644 | 44 | new file mode 100644 |
51 | index 0000000..08b685e | |||
52 | --- /dev/null | |||
53 | +++ b/turnip/pack/extensions/__init__.py | |||
54 | @@ -0,0 +1,50 @@ | |||
55 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
56 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
57 | 3 | |||
58 | 4 | from __future__ import ( | ||
59 | 5 | absolute_import, | ||
60 | 6 | print_function, | ||
61 | 7 | unicode_literals, | ||
62 | 8 | ) | ||
63 | 9 | |||
64 | 10 | from importlib import import_module | ||
65 | 11 | |||
66 | 12 | import six | ||
67 | 13 | |||
68 | 14 | from turnip.config import config | ||
69 | 15 | from turnip.pack.helpers import encode_packet, FLUSH_PKT | ||
70 | 16 | from turnip.version_info import version_info | ||
71 | 17 | |||
72 | 18 | |||
73 | 19 | def get_available_extensions(version='2'): | ||
74 | 20 | """Reeturns the list of available PacketExtension classes.""" | ||
75 | 21 | # For now, we only have 1 and 2, but this might change in the future | ||
76 | 22 | # (2.1? 2.5?), so delay this "int" conversion as much as possible. | ||
77 | 23 | version = int(version) | ||
78 | 24 | if version == 1: | ||
79 | 25 | return [] | ||
80 | 26 | available = [] | ||
81 | 27 | for class_path in config.get('pack_extensions').split(): | ||
82 | 28 | module_path, class_name = class_path.rsplit('.', 1) | ||
83 | 29 | module = import_module(module_path, 'turnip.pack.extensions') | ||
84 | 30 | extension_cls = getattr(module, class_name) | ||
85 | 31 | if extension_cls.min_version > version: | ||
86 | 32 | continue | ||
87 | 33 | available.append(extension_cls) | ||
88 | 34 | return available | ||
89 | 35 | |||
90 | 36 | |||
91 | 37 | def get_capabilities_description(version='1'): | ||
92 | 38 | """Returns the capability advertisement binary string for the given | ||
93 | 39 | protocol version.""" | ||
94 | 40 | if version != '2': | ||
95 | 41 | return b"" | ||
96 | 42 | turnip_version = six.ensure_binary(version_info.get("revision_id", '-1')) | ||
97 | 43 | return ( | ||
98 | 44 | encode_packet(b"version 2\n") + | ||
99 | 45 | encode_packet(b"agent=turnip/%s\n" % turnip_version) + | ||
100 | 46 | encode_packet(b"ls-refs\n") + | ||
101 | 47 | encode_packet(b"fetch=shallow\n") + | ||
102 | 48 | encode_packet(b"server-option\n") + | ||
103 | 49 | FLUSH_PKT | ||
104 | 50 | ) | ||
105 | diff --git a/turnip/pack/extensions/agent.py b/turnip/pack/extensions/agent.py | |||
106 | 0 | new file mode 100644 | 51 | new file mode 100644 |
107 | index 0000000..3183c01 | |||
108 | --- /dev/null | |||
109 | +++ b/turnip/pack/extensions/agent.py | |||
110 | @@ -0,0 +1,26 @@ | |||
111 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
112 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
113 | 3 | |||
114 | 4 | from __future__ import ( | ||
115 | 5 | absolute_import, | ||
116 | 6 | print_function, | ||
117 | 7 | unicode_literals, | ||
118 | 8 | ) | ||
119 | 9 | |||
120 | 10 | import six | ||
121 | 11 | |||
122 | 12 | from turnip.pack.extensions.extension import PackExtension | ||
123 | 13 | from turnip.pack.helpers import ( | ||
124 | 14 | encode_packet, | ||
125 | 15 | ) | ||
126 | 16 | from turnip.version_info import version_info | ||
127 | 17 | |||
128 | 18 | |||
129 | 19 | class Agent(PackExtension): | ||
130 | 20 | min_version = 2 | ||
131 | 21 | |||
132 | 22 | @classmethod | ||
133 | 23 | def getCapabilityAdvertisement(cls): | ||
134 | 24 | version = version_info.get('revision_id', '-1') | ||
135 | 25 | agent = b"agent=turnip/%s\n" % six.ensure_binary(version) | ||
136 | 26 | return encode_packet(agent) | ||
137 | diff --git a/turnip/pack/extensions/extension.py b/turnip/pack/extensions/extension.py | |||
138 | 0 | new file mode 100644 | 27 | new file mode 100644 |
139 | index 0000000..58843c8 | |||
140 | --- /dev/null | |||
141 | +++ b/turnip/pack/extensions/extension.py | |||
142 | @@ -0,0 +1,61 @@ | |||
143 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
144 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
145 | 3 | |||
146 | 4 | from __future__ import ( | ||
147 | 5 | absolute_import, | ||
148 | 6 | print_function, | ||
149 | 7 | unicode_literals, | ||
150 | 8 | ) | ||
151 | 9 | |||
152 | 10 | __metaclass__ = type | ||
153 | 11 | |||
154 | 12 | import os | ||
155 | 13 | |||
156 | 14 | from turnip.api.store import open_repo | ||
157 | 15 | |||
158 | 16 | |||
159 | 17 | class PackExtension: | ||
160 | 18 | # Minimum protocol version where this extension is available. | ||
161 | 19 | min_version = 2 | ||
162 | 20 | |||
163 | 21 | # Which command this extension represents (for example, 'ls-refs', | ||
164 | 22 | # 'fetch', ...) | ||
165 | 23 | command = None | ||
166 | 24 | |||
167 | 25 | # If False, the connection with the client will be closed immediately | ||
168 | 26 | # after execute() is called. | ||
169 | 27 | is_async = False | ||
170 | 28 | |||
171 | 29 | def __init__(self, backend_protocol=None, path=None, args=None): | ||
172 | 30 | """Initialize the extension. | ||
173 | 31 | |||
174 | 32 | :param backend_protocol: The PackBackendProtocol instance requesting | ||
175 | 33 | this extension to do its job. | ||
176 | 34 | """ | ||
177 | 35 | self.backend_protocol = backend_protocol | ||
178 | 36 | self.path = path | ||
179 | 37 | self.args = args | ||
180 | 38 | self.data_buffer = None | ||
181 | 39 | self.done = False | ||
182 | 40 | |||
183 | 41 | def open_repo(self): | ||
184 | 42 | repo_store = os.path.dirname(self.path) | ||
185 | 43 | repo_name = os.path.basename(self.path) | ||
186 | 44 | return open_repo(repo_store, repo_name) | ||
187 | 45 | |||
188 | 46 | @property | ||
189 | 47 | def log(self): | ||
190 | 48 | return self.backend_protocol.log | ||
191 | 49 | |||
192 | 50 | def sendRawData(self, data): | ||
193 | 51 | self.backend_protocol.sendRawData(data) | ||
194 | 52 | |||
195 | 53 | def sendPacket(self, data): | ||
196 | 54 | self.backend_protocol.sendPacket(data) | ||
197 | 55 | |||
198 | 56 | @classmethod | ||
199 | 57 | def getCapabilityAdvertisement(self): | ||
200 | 58 | raise NotImplementedError( | ||
201 | 59 | "This must be implemented by subclasses, returning its binary " | ||
202 | 60 | "representation") | ||
203 | 61 | |||
204 | diff --git a/turnip/pack/extensions/fetch.py b/turnip/pack/extensions/fetch.py | |||
205 | 0 | new file mode 100644 | 62 | new file mode 100644 |
206 | index 0000000..8a64ae8 | |||
207 | --- /dev/null | |||
208 | +++ b/turnip/pack/extensions/fetch.py | |||
209 | @@ -0,0 +1,22 @@ | |||
210 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
211 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
212 | 3 | |||
213 | 4 | from __future__ import ( | ||
214 | 5 | absolute_import, | ||
215 | 6 | print_function, | ||
216 | 7 | unicode_literals, | ||
217 | 8 | ) | ||
218 | 9 | |||
219 | 10 | from turnip.pack.extensions.extension import PackExtension | ||
220 | 11 | from turnip.pack.helpers import encode_packet, FLUSH_PKT | ||
221 | 12 | |||
222 | 13 | |||
223 | 14 | class Fetch(PackExtension): | ||
224 | 15 | min_version = 2 | ||
225 | 16 | command = b'fetch' | ||
226 | 17 | is_write_operation = False | ||
227 | 18 | is_async = False | ||
228 | 19 | |||
229 | 20 | @classmethod | ||
230 | 21 | def getCapabilityAdvertisement(cls): | ||
231 | 22 | return encode_packet(b"fetch=shallow\n") | ||
232 | diff --git a/turnip/pack/extensions/lsrefs.py b/turnip/pack/extensions/lsrefs.py | |||
233 | 0 | new file mode 100644 | 23 | new file mode 100644 |
234 | index 0000000..9a23f03 | |||
235 | --- /dev/null | |||
236 | +++ b/turnip/pack/extensions/lsrefs.py | |||
237 | @@ -0,0 +1,22 @@ | |||
238 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
239 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
240 | 3 | |||
241 | 4 | from __future__ import ( | ||
242 | 5 | absolute_import, | ||
243 | 6 | print_function, | ||
244 | 7 | unicode_literals, | ||
245 | 8 | ) | ||
246 | 9 | |||
247 | 10 | from turnip.pack.extensions.extension import PackExtension | ||
248 | 11 | from turnip.pack.helpers import encode_packet, FLUSH_PKT | ||
249 | 12 | |||
250 | 13 | |||
251 | 14 | class LSRefs(PackExtension): | ||
252 | 15 | min_version = 2 | ||
253 | 16 | command = b'ls-refs' | ||
254 | 17 | is_write_operation = False | ||
255 | 18 | is_async = False | ||
256 | 19 | |||
257 | 20 | @classmethod | ||
258 | 21 | def getCapabilityAdvertisement(cls): | ||
259 | 22 | return encode_packet(b"ls-refs\n") | ||
260 | diff --git a/turnip/pack/extensions/tests/__init__.py b/turnip/pack/extensions/tests/__init__.py | |||
261 | 0 | new file mode 100644 | 23 | new file mode 100644 |
262 | index 0000000..e69de29 | |||
263 | --- /dev/null | |||
264 | +++ b/turnip/pack/extensions/tests/__init__.py | |||
265 | diff --git a/turnip/pack/extensions/tests/test_extensions.py b/turnip/pack/extensions/tests/test_extensions.py | |||
266 | 1 | new file mode 100644 | 24 | new file mode 100644 |
267 | index 0000000..299e786 | |||
268 | --- /dev/null | |||
269 | +++ b/turnip/pack/extensions/tests/test_extensions.py | |||
270 | @@ -0,0 +1,42 @@ | |||
271 | 1 | # Copyright 2015 Canonical Ltd. This software is licensed under the | ||
272 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
273 | 3 | |||
274 | 4 | from __future__ import ( | ||
275 | 5 | absolute_import, | ||
276 | 6 | print_function, | ||
277 | 7 | unicode_literals, | ||
278 | 8 | ) | ||
279 | 9 | |||
280 | 10 | from fixtures import EnvironmentVariable | ||
281 | 11 | from testtools import TestCase | ||
282 | 12 | |||
283 | 13 | from turnip.pack.extensions import ( | ||
284 | 14 | get_capabilities_description, | ||
285 | 15 | get_available_extensions, | ||
286 | 16 | ) | ||
287 | 17 | from turnip.pack.extensions.agent import Agent | ||
288 | 18 | from turnip.pack.extensions.lsrefs import LSRefs | ||
289 | 19 | from turnip.version_info import version_info | ||
290 | 20 | |||
291 | 21 | |||
292 | 22 | class TestCapabilities(TestCase): | ||
293 | 23 | def test_get_extensions_from_config(self): | ||
294 | 24 | self.useFixture(EnvironmentVariable( | ||
295 | 25 | "PACK_EXTENSIONS", ".agent.Agent .lsrefs.LSRefs")) | ||
296 | 26 | available = get_available_extensions('2') | ||
297 | 27 | self.assertEqual(2, len(available)) | ||
298 | 28 | self.assertEqual(available[0], Agent) | ||
299 | 29 | self.assertEqual(available[1], LSRefs) | ||
300 | 30 | |||
301 | 31 | def test_get_capabilities_description_v1_is_empty(self): | ||
302 | 32 | self.assertEqual(b"", get_capabilities_description(1)) | ||
303 | 33 | |||
304 | 34 | def test_get_capabilities_description_v2_with_agent_only(self): | ||
305 | 35 | self.useFixture(EnvironmentVariable( | ||
306 | 36 | "PACK_EXTENSIONS", ".agent.Agent .lsrefs.LSRefs")) | ||
307 | 37 | self.assertEqual( | ||
308 | 38 | b"000eversion 2\n" | ||
309 | 39 | b"003aagent=turnip/%s\n" | ||
310 | 40 | b"000cls-refs\n" | ||
311 | 41 | b"0000" % version_info['revision_id'], | ||
312 | 42 | get_capabilities_description('2')) | ||
313 | diff --git a/turnip/pack/git.py b/turnip/pack/git.py | |||
314 | index 763d5d6..43cbb32 100644 | |||
315 | --- a/turnip/pack/git.py | |||
316 | +++ b/turnip/pack/git.py | |||
317 | @@ -29,6 +29,7 @@ from turnip.api import store | |||
318 | 29 | from turnip.api.store import AlreadyExistsError | 29 | from turnip.api.store import AlreadyExistsError |
319 | 30 | from turnip.config import config | 30 | from turnip.config import config |
320 | 31 | from turnip.helpers import compose_path | 31 | from turnip.helpers import compose_path |
321 | 32 | from turnip.pack.extensions import get_available_extensions | ||
322 | 32 | from turnip.pack.helpers import ( | 33 | from turnip.pack.helpers import ( |
323 | 33 | decode_packet, | 34 | decode_packet, |
324 | 34 | decode_request, | 35 | decode_request, |
325 | @@ -37,8 +38,8 @@ from turnip.pack.helpers import ( | |||
326 | 37 | ensure_config, | 38 | ensure_config, |
327 | 38 | ensure_hooks, | 39 | ensure_hooks, |
328 | 39 | INCOMPLETE_PKT, | 40 | INCOMPLETE_PKT, |
331 | 40 | translate_xmlrpc_fault, | 41 | translate_xmlrpc_fault, FLUSH_PKT, DELIM_PKT, |
332 | 41 | ) | 42 | ) |
333 | 42 | 43 | ||
334 | 43 | 44 | ||
335 | 44 | ERROR_PREFIX = b'ERR ' | 45 | ERROR_PREFIX = b'ERR ' |
336 | @@ -77,13 +78,17 @@ class UnstoppableProducerWrapper(object): | |||
337 | 77 | pass | 78 | pass |
338 | 78 | 79 | ||
339 | 79 | 80 | ||
341 | 80 | class PackProtocol(protocol.Protocol): | 81 | class PackProtocol(protocol.Protocol, object): |
342 | 81 | 82 | ||
343 | 82 | paused = False | 83 | paused = False |
344 | 83 | raw = False | 84 | raw = False |
345 | 85 | extension = None | ||
346 | 84 | 86 | ||
347 | 85 | __buffer = b'' | 87 | __buffer = b'' |
348 | 86 | 88 | ||
349 | 89 | def getProtocolVersion(self): | ||
350 | 90 | raise NotImplementedError() | ||
351 | 91 | |||
352 | 87 | def packetReceived(self, payload): | 92 | def packetReceived(self, payload): |
353 | 88 | raise NotImplementedError() | 93 | raise NotImplementedError() |
354 | 89 | 94 | ||
355 | @@ -187,11 +192,11 @@ class PackServerProtocol(PackProxyProtocol): | |||
356 | 187 | command, pathname, params = decode_request(data) | 192 | command, pathname, params = decode_request(data) |
357 | 188 | except ValueError as e: | 193 | except ValueError as e: |
358 | 189 | self.die(str(e).encode('utf-8')) | 194 | self.die(str(e).encode('utf-8')) |
360 | 190 | return | 195 | return None |
361 | 191 | self.pauseProducing() | 196 | self.pauseProducing() |
362 | 192 | self.got_request = True | 197 | self.got_request = True |
363 | 193 | self.requestReceived(command, pathname, params) | 198 | self.requestReceived(command, pathname, params) |
365 | 194 | return | 199 | return None |
366 | 195 | if data is None: | 200 | if data is None: |
367 | 196 | self.raw = True | 201 | self.raw = True |
368 | 197 | self.peer.sendPacket(data) | 202 | self.peer.sendPacket(data) |
369 | @@ -239,15 +244,20 @@ class GitProcessProtocol(protocol.ProcessProtocol): | |||
370 | 239 | 244 | ||
371 | 240 | _err_buffer = b'' | 245 | _err_buffer = b'' |
372 | 241 | 246 | ||
374 | 242 | def __init__(self, peer): | 247 | def __init__(self, peer, cmd_input=None): |
375 | 243 | self.peer = peer | 248 | self.peer = peer |
376 | 249 | self.cmd_input = cmd_input | ||
377 | 244 | self.out_started = False | 250 | self.out_started = False |
378 | 245 | 251 | ||
379 | 246 | def connectionMade(self): | 252 | def connectionMade(self): |
380 | 247 | self.peer.setPeer(self) | 253 | self.peer.setPeer(self) |
381 | 248 | self.peer.transport.registerProducer(self, True) | 254 | self.peer.transport.registerProducer(self, True) |
384 | 249 | self.transport.registerProducer( | 255 | if not self.cmd_input: |
385 | 250 | UnstoppableProducerWrapper(self.peer.transport), True) | 256 | self.transport.registerProducer( |
386 | 257 | UnstoppableProducerWrapper(self.peer.transport), True) | ||
387 | 258 | else: | ||
388 | 259 | self.transport.write(self.cmd_input) | ||
389 | 260 | self.loseWriteConnection() | ||
390 | 251 | self.peer.resumeProducing() | 261 | self.peer.resumeProducing() |
391 | 252 | 262 | ||
392 | 253 | def outReceived(self, data): | 263 | def outReceived(self, data): |
393 | @@ -415,7 +425,7 @@ class PackProxyServerProtocol(PackServerProtocol): | |||
394 | 415 | def resumeProducing(self): | 425 | def resumeProducing(self): |
395 | 416 | # Send our translated request and then open the gate to the client. | 426 | # Send our translated request and then open the gate to the client. |
396 | 417 | self.sendNextCommand() | 427 | self.sendNextCommand() |
398 | 418 | PackServerProtocol.resumeProducing(self) | 428 | super(PackProxyServerProtocol, self).resumeProducing() |
399 | 419 | 429 | ||
400 | 420 | def readConnectionLost(self): | 430 | def readConnectionLost(self): |
401 | 421 | # Forward the closed stdin down the stack. | 431 | # Forward the closed stdin down the stack. |
402 | @@ -431,12 +441,14 @@ class PackBackendProtocol(PackServerProtocol): | |||
403 | 431 | 441 | ||
404 | 432 | hookrpc_key = None | 442 | hookrpc_key = None |
405 | 433 | expect_set_symbolic_ref = False | 443 | expect_set_symbolic_ref = False |
406 | 444 | extension = None | ||
407 | 434 | 445 | ||
408 | 435 | @defer.inlineCallbacks | 446 | @defer.inlineCallbacks |
409 | 436 | def requestReceived(self, command, raw_pathname, params): | 447 | def requestReceived(self, command, raw_pathname, params): |
410 | 437 | self.extractRequestMeta(command, raw_pathname, params) | 448 | self.extractRequestMeta(command, raw_pathname, params) |
411 | 438 | self.command = command | 449 | self.command = command |
412 | 439 | self.raw_pathname = raw_pathname | 450 | self.raw_pathname = raw_pathname |
413 | 451 | self.params = params | ||
414 | 440 | self.path = compose_path(self.factory.root, self.raw_pathname) | 452 | self.path = compose_path(self.factory.root, self.raw_pathname) |
415 | 441 | auth_params = self.createAuthParams(params) | 453 | auth_params = self.createAuthParams(params) |
416 | 442 | 454 | ||
417 | @@ -457,14 +469,42 @@ class PackBackendProtocol(PackServerProtocol): | |||
418 | 457 | return | 469 | return |
419 | 458 | 470 | ||
420 | 459 | write_operation = False | 471 | write_operation = False |
426 | 460 | if command == b'git-upload-pack': | 472 | cmd_input = None |
427 | 461 | subcmd = b'upload-pack' | 473 | send_path_as_option = False |
428 | 462 | elif command == b'git-receive-pack': | 474 | cmd_env = {} |
429 | 463 | subcmd = b'receive-pack' | 475 | if params.get('version') != '2': |
430 | 464 | write_operation = True | 476 | if command == b'git-upload-pack': |
431 | 477 | subcmd = b'upload-pack' | ||
432 | 478 | elif command == b'git-receive-pack': | ||
433 | 479 | subcmd = b'receive-pack' | ||
434 | 480 | write_operation = True | ||
435 | 481 | else: | ||
436 | 482 | self.die(b"Invalid command: %s" % command) | ||
437 | 465 | else: | 483 | else: |
440 | 466 | self.die(b'Unsupported command in request') | 484 | # v2 protocol |
441 | 467 | return | 485 | if command == b'git-upload-pack': |
442 | 486 | self.expectNextCommand() | ||
443 | 487 | self.transport.loseConnection() | ||
444 | 488 | return | ||
445 | 489 | else: | ||
446 | 490 | subcmd = b'upload-pack' | ||
447 | 491 | cmd_env["GIT_PROTOCOL"] = 'version=2' | ||
448 | 492 | send_path_as_option = True | ||
449 | 493 | params.pop(b'turnip-advertise-refs', None) | ||
450 | 494 | cmd_input = encode_packet(b"command=%s\n" % command) | ||
451 | 495 | for capability in params["capabilities"].split(b"\n"): | ||
452 | 496 | cmd_input += encode_packet(b"%s\n" % capability) | ||
453 | 497 | cmd_input += DELIM_PKT | ||
454 | 498 | ignore_keys = ('capabilities', 'version', 'extra_flags') | ||
455 | 499 | for k, v in params.items(): | ||
456 | 500 | k = six.ensure_binary(k) | ||
457 | 501 | if k.startswith(b"turnip-") or k in ignore_keys: | ||
458 | 502 | continue | ||
459 | 503 | for param_value in v.split(b'\n'): | ||
460 | 504 | value = (b"" if not param_value | ||
461 | 505 | else b" %s" % param_value) | ||
462 | 506 | cmd_input += encode_packet(b"%s%s\n" % (k, value)) | ||
463 | 507 | cmd_input += FLUSH_PKT | ||
464 | 468 | 508 | ||
465 | 469 | args = [] | 509 | args = [] |
466 | 470 | if params.pop(b'turnip-stateless-rpc', None): | 510 | if params.pop(b'turnip-stateless-rpc', None): |
467 | @@ -475,10 +515,14 @@ class PackBackendProtocol(PackServerProtocol): | |||
468 | 475 | self.spawnGit(subcmd, | 515 | self.spawnGit(subcmd, |
469 | 476 | args, | 516 | args, |
470 | 477 | write_operation=write_operation, | 517 | write_operation=write_operation, |
472 | 478 | auth_params=auth_params) | 518 | auth_params=auth_params, |
473 | 519 | send_path_as_option=send_path_as_option, | ||
474 | 520 | cmd_env=cmd_env, | ||
475 | 521 | cmd_input=cmd_input) | ||
476 | 479 | 522 | ||
477 | 480 | def spawnGit(self, subcmd, extra_args, write_operation=False, | 523 | def spawnGit(self, subcmd, extra_args, write_operation=False, |
479 | 481 | send_path_as_option=False, auth_params=None): | 524 | send_path_as_option=False, auth_params=None, |
480 | 525 | cmd_env=None, cmd_input=None): | ||
481 | 482 | cmd = b'git' | 526 | cmd = b'git' |
482 | 483 | args = [b'git'] | 527 | args = [b'git'] |
483 | 484 | if send_path_as_option: | 528 | if send_path_as_option: |
484 | @@ -487,6 +531,7 @@ class PackBackendProtocol(PackServerProtocol): | |||
485 | 487 | args.extend(extra_args) | 531 | args.extend(extra_args) |
486 | 488 | 532 | ||
487 | 489 | env = {} | 533 | env = {} |
488 | 534 | env.update((cmd_env or {})) | ||
489 | 490 | if write_operation and self.factory.hookrpc_handler: | 535 | if write_operation and self.factory.hookrpc_handler: |
490 | 491 | # This is a write operation, so prepare config, hooks, the hook | 536 | # This is a write operation, so prepare config, hooks, the hook |
491 | 492 | # RPC server, and the environment variables that link them up. | 537 | # RPC server, and the environment variables that link them up. |
492 | @@ -499,7 +544,7 @@ class PackBackendProtocol(PackServerProtocol): | |||
493 | 499 | env[b'TURNIP_HOOK_RPC_KEY'] = self.hookrpc_key | 544 | env[b'TURNIP_HOOK_RPC_KEY'] = self.hookrpc_key |
494 | 500 | 545 | ||
495 | 501 | self.log.info('Spawning {args}', args=args) | 546 | self.log.info('Spawning {args}', args=args) |
497 | 502 | self.peer = GitProcessProtocol(self) | 547 | self.peer = GitProcessProtocol(self, cmd_input) |
498 | 503 | self.spawnProcess(cmd, args, env=env) | 548 | self.spawnProcess(cmd, args, env=env) |
499 | 504 | 549 | ||
500 | 505 | def spawnProcess(self, cmd, args, env=None): | 550 | def spawnProcess(self, cmd, args, env=None): |
501 | @@ -628,7 +673,7 @@ class PackVirtServerProtocol(PackProxyServerProtocol): | |||
502 | 628 | @defer.inlineCallbacks | 673 | @defer.inlineCallbacks |
503 | 629 | def requestReceived(self, command, pathname, params): | 674 | def requestReceived(self, command, pathname, params): |
504 | 630 | self.extractRequestMeta(command, pathname, params) | 675 | self.extractRequestMeta(command, pathname, params) |
506 | 631 | permission = 'read' if command == b'git-upload-pack' else 'write' | 676 | permission = 'read' if command != b'git-receive-pack' else 'write' |
507 | 632 | proxy = xmlrpc.Proxy(self.factory.virtinfo_endpoint, allowNone=True) | 677 | proxy = xmlrpc.Proxy(self.factory.virtinfo_endpoint, allowNone=True) |
508 | 633 | try: | 678 | try: |
509 | 634 | auth_params = self.createAuthParams(params) | 679 | auth_params = self.createAuthParams(params) |
510 | diff --git a/turnip/pack/helpers.py b/turnip/pack/helpers.py | |||
511 | index 9cf411d..52649a9 100644 | |||
512 | --- a/turnip/pack/helpers.py | |||
513 | +++ b/turnip/pack/helpers.py | |||
514 | @@ -7,6 +7,8 @@ from __future__ import ( | |||
515 | 7 | unicode_literals, | 7 | unicode_literals, |
516 | 8 | ) | 8 | ) |
517 | 9 | 9 | ||
518 | 10 | from collections import OrderedDict | ||
519 | 11 | |||
520 | 10 | import enum | 12 | import enum |
521 | 11 | import hashlib | 13 | import hashlib |
522 | 12 | import os.path | 14 | import os.path |
523 | @@ -25,6 +27,8 @@ import yaml | |||
524 | 25 | import turnip.pack.hooks | 27 | import turnip.pack.hooks |
525 | 26 | 28 | ||
526 | 27 | 29 | ||
527 | 30 | FLUSH_PKT = b'0000' | ||
528 | 31 | DELIM_PKT = b'0001' | ||
529 | 28 | PKT_LEN_SIZE = 4 | 32 | PKT_LEN_SIZE = 4 |
530 | 29 | PKT_PAYLOAD_MAX = 65520 | 33 | PKT_PAYLOAD_MAX = 65520 |
531 | 30 | INCOMPLETE_PKT = object() | 34 | INCOMPLETE_PKT = object() |
532 | @@ -33,7 +37,7 @@ INCOMPLETE_PKT = object() | |||
533 | 33 | def encode_packet(payload): | 37 | def encode_packet(payload): |
534 | 34 | if payload is None: | 38 | if payload is None: |
535 | 35 | # flush-pkt. | 39 | # flush-pkt. |
537 | 36 | return b'0000' | 40 | return FLUSH_PKT |
538 | 37 | else: | 41 | else: |
539 | 38 | # data-pkt | 42 | # data-pkt |
540 | 39 | if len(payload) > PKT_PAYLOAD_MAX: | 43 | if len(payload) > PKT_PAYLOAD_MAX: |
541 | @@ -47,21 +51,37 @@ def decode_packet(input): | |||
542 | 47 | """Consume a packet, returning the payload and any unconsumed tail.""" | 51 | """Consume a packet, returning the payload and any unconsumed tail.""" |
543 | 48 | if len(input) < PKT_LEN_SIZE: | 52 | if len(input) < PKT_LEN_SIZE: |
544 | 49 | return (INCOMPLETE_PKT, input) | 53 | return (INCOMPLETE_PKT, input) |
546 | 50 | if input.startswith(b'0000'): | 54 | if input.startswith(FLUSH_PKT): |
547 | 51 | # flush-pkt | 55 | # flush-pkt |
548 | 52 | return (None, input[PKT_LEN_SIZE:]) | 56 | return (None, input[PKT_LEN_SIZE:]) |
561 | 53 | else: | 57 | # data-pkt |
562 | 54 | # data-pkt | 58 | try: |
563 | 55 | try: | 59 | pkt_len = int(input[:PKT_LEN_SIZE], 16) |
564 | 56 | pkt_len = int(input[:PKT_LEN_SIZE], 16) | 60 | except ValueError: |
565 | 57 | except ValueError: | 61 | pkt_len = 0 |
566 | 58 | pkt_len = 0 | 62 | if not (PKT_LEN_SIZE <= pkt_len <= (PKT_LEN_SIZE + PKT_PAYLOAD_MAX)): |
567 | 59 | if not (PKT_LEN_SIZE <= pkt_len <= (PKT_LEN_SIZE + PKT_PAYLOAD_MAX)): | 63 | raise ValueError("Invalid pkt-len") |
568 | 60 | raise ValueError("Invalid pkt-len") | 64 | if len(input) < pkt_len: |
569 | 61 | if len(input) < pkt_len: | 65 | # Some of the packet is yet to be received. |
570 | 62 | # Some of the packet is yet to be received. | 66 | return (INCOMPLETE_PKT, input) |
571 | 63 | return (INCOMPLETE_PKT, input) | 67 | |
572 | 64 | return (input[PKT_LEN_SIZE:pkt_len], input[pkt_len:]) | 68 | # v2 protocol "hides" extra parameters after the end of the packet. |
573 | 69 | if len(input) > pkt_len and b'version=2\x00' in input: | ||
574 | 70 | if FLUSH_PKT not in input: | ||
575 | 71 | return INCOMPLETE_PKT, input | ||
576 | 72 | end = input.index(FLUSH_PKT) | ||
577 | 73 | return input[PKT_LEN_SIZE:end], input[end + len(FLUSH_PKT):] | ||
578 | 74 | |||
579 | 75 | return (input[PKT_LEN_SIZE:pkt_len], input[pkt_len:]) | ||
580 | 76 | |||
581 | 77 | |||
582 | 78 | def decode_packet_list(data): | ||
583 | 79 | remaining = data | ||
584 | 80 | retval = [] | ||
585 | 81 | while remaining: | ||
586 | 82 | pkt, remaining = decode_packet(remaining) | ||
587 | 83 | retval.append(pkt) | ||
588 | 84 | return retval | ||
589 | 65 | 85 | ||
590 | 66 | 86 | ||
591 | 67 | def decode_request(data): | 87 | def decode_request(data): |
592 | @@ -77,10 +97,11 @@ def decode_request(data): | |||
593 | 77 | bits = rest.split(b'\0') | 97 | bits = rest.split(b'\0') |
594 | 78 | # Following the command is a pathname, then any number of named | 98 | # Following the command is a pathname, then any number of named |
595 | 79 | # parameters. Each of these is NUL-terminated. | 99 | # parameters. Each of these is NUL-terminated. |
597 | 80 | if len(bits) < 2 or bits[-1] != b'': | 100 | # After that, v1 should end (v2 might have extra commands). |
598 | 101 | if len(bits) < 2 or (b'version=2' not in bits and bits[-1] != b''): | ||
599 | 81 | raise ValueError('Invalid git-proto-request') | 102 | raise ValueError('Invalid git-proto-request') |
600 | 82 | pathname = bits[0] | 103 | pathname = bits[0] |
602 | 83 | params = {} | 104 | params = OrderedDict() |
603 | 84 | for index, param in enumerate(bits[1:-1]): | 105 | for index, param in enumerate(bits[1:-1]): |
604 | 85 | if param == b'': | 106 | if param == b'': |
605 | 86 | if (index < len(bits) - 1): | 107 | if (index < len(bits) - 1): |
606 | @@ -94,7 +115,42 @@ def decode_request(data): | |||
607 | 94 | if name in params: | 115 | if name in params: |
608 | 95 | raise ValueError('Parameters must not be repeated') | 116 | raise ValueError('Parameters must not be repeated') |
609 | 96 | params[name] = value | 117 | params[name] = value |
611 | 97 | return (command, pathname, params) | 118 | |
612 | 119 | if not bits[-1]: | ||
613 | 120 | return command, pathname, params | ||
614 | 121 | else: | ||
615 | 122 | # Parse v2 extra commands. | ||
616 | 123 | cmd, remaining = decode_packet(bits[-1]) | ||
617 | 124 | cmd = cmd.split(b'=', 1)[-1].strip() | ||
618 | 125 | capabilities, args = remaining.split(DELIM_PKT) | ||
619 | 126 | cmd_params = OrderedDict({ | ||
620 | 127 | b"capabilities": decode_packet_list(capabilities)}) | ||
621 | 128 | cmd_params.update(params) | ||
622 | 129 | for arg in decode_packet_list(args): | ||
623 | 130 | if arg is None: | ||
624 | 131 | continue | ||
625 | 132 | arg = arg.strip('\n') | ||
626 | 133 | if b' ' in arg: | ||
627 | 134 | k, v = arg.split(b' ', 1) | ||
628 | 135 | if k not in cmd_params: | ||
629 | 136 | cmd_params[k] = [] | ||
630 | 137 | cmd_params[k].append(v) | ||
631 | 138 | else: | ||
632 | 139 | cmd_params[arg] = b"" | ||
633 | 140 | return (cmd, pathname, cmd_params) | ||
634 | 141 | |||
635 | 142 | |||
636 | 143 | def get_encoded_value(value): | ||
637 | 144 | """Encode a value for serialization on encode_request""" | ||
638 | 145 | if value is None: | ||
639 | 146 | return b'' | ||
640 | 147 | if isinstance(value, list): | ||
641 | 148 | if any(b'\n' in i for i in value): | ||
642 | 149 | raise ValueError('Metacharacter in list argument') | ||
643 | 150 | return b"\n".join(get_encoded_value(i) for i in value) | ||
644 | 151 | if isinstance(value, bool): | ||
645 | 152 | return b'1' if value else b'0' | ||
646 | 153 | return six.ensure_binary(value) | ||
647 | 98 | 154 | ||
648 | 99 | 155 | ||
649 | 100 | def encode_request(command, pathname, params): | 156 | def encode_request(command, pathname, params): |
650 | @@ -105,9 +161,9 @@ def encode_request(command, pathname, params): | |||
651 | 105 | if b' ' in command or b'\0' in pathname: | 161 | if b' ' in command or b'\0' in pathname: |
652 | 106 | raise ValueError('Metacharacter in arguments') | 162 | raise ValueError('Metacharacter in arguments') |
653 | 107 | bits = [pathname] | 163 | bits = [pathname] |
655 | 108 | for name in sorted(params): | 164 | for name in params: |
656 | 109 | value = params[name] | 165 | value = params[name] |
658 | 110 | value = six.ensure_binary(value) if value is not None else b'' | 166 | value = get_encoded_value(value) |
659 | 111 | name = six.ensure_binary(name) | 167 | name = six.ensure_binary(name) |
660 | 112 | if b'=' in name or b'\0' in name + value: | 168 | if b'=' in name or b'\0' in name + value: |
661 | 113 | raise ValueError('Metacharacter in arguments') | 169 | raise ValueError('Metacharacter in arguments') |
662 | diff --git a/turnip/pack/http.py b/turnip/pack/http.py | |||
663 | index c0becf0..7ae8765 100644 | |||
664 | --- a/turnip/pack/http.py | |||
665 | +++ b/turnip/pack/http.py | |||
666 | @@ -12,6 +12,7 @@ import json | |||
667 | 12 | import os.path | 12 | import os.path |
668 | 13 | import tempfile | 13 | import tempfile |
669 | 14 | import textwrap | 14 | import textwrap |
670 | 15 | |||
671 | 15 | try: | 16 | try: |
672 | 16 | from urllib.parse import urlencode | 17 | from urllib.parse import urlencode |
673 | 17 | except ImportError: | 18 | except ImportError: |
674 | @@ -29,6 +30,7 @@ from paste.auth.cookie import ( | |||
675 | 29 | decode as decode_cookie, | 30 | decode as decode_cookie, |
676 | 30 | encode as encode_cookie, | 31 | encode as encode_cookie, |
677 | 31 | ) | 32 | ) |
678 | 33 | import six | ||
679 | 32 | from twisted.internet import ( | 34 | from twisted.internet import ( |
680 | 33 | defer, | 35 | defer, |
681 | 34 | error, | 36 | error, |
682 | @@ -49,6 +51,7 @@ from twisted.web import ( | |||
683 | 49 | ) | 51 | ) |
684 | 50 | 52 | ||
685 | 51 | from turnip.helpers import compose_path | 53 | from turnip.helpers import compose_path |
686 | 54 | from turnip.pack.extensions import get_capabilities_description | ||
687 | 52 | from turnip.pack.git import ( | 55 | from turnip.pack.git import ( |
688 | 53 | ERROR_PREFIX, | 56 | ERROR_PREFIX, |
689 | 54 | PackProtocol, | 57 | PackProtocol, |
690 | @@ -80,6 +83,16 @@ def fail_request(request, message, code=http.INTERNAL_SERVER_ERROR): | |||
691 | 80 | return b'' | 83 | return b'' |
692 | 81 | 84 | ||
693 | 82 | 85 | ||
694 | 86 | def get_protocol_version_from_request(request): | ||
695 | 87 | version_header = request.requestHeaders.getRawHeaders( | ||
696 | 88 | 'git-protocol', ['version=1'])[0] | ||
697 | 89 | try: | ||
698 | 90 | return version_header.split('version=', 1)[1] | ||
699 | 91 | except Exception: | ||
700 | 92 | pass | ||
701 | 93 | return 1 | ||
702 | 94 | |||
703 | 95 | |||
704 | 83 | class HTTPPackClientProtocol(PackProtocol): | 96 | class HTTPPackClientProtocol(PackProtocol): |
705 | 84 | """Abstract bridge between a Git pack connection and a smart HTTP request. | 97 | """Abstract bridge between a Git pack connection and a smart HTTP request. |
706 | 85 | 98 | ||
707 | @@ -99,7 +112,11 @@ class HTTPPackClientProtocol(PackProtocol): | |||
708 | 99 | 112 | ||
709 | 100 | def startGoodResponse(self): | 113 | def startGoodResponse(self): |
710 | 101 | """Prepare the HTTP response for forwarding from the backend.""" | 114 | """Prepare the HTTP response for forwarding from the backend.""" |
712 | 102 | raise NotImplementedError() | 115 | self.factory.http_request.write( |
713 | 116 | get_capabilities_description(self.getProtocolVersion())) | ||
714 | 117 | |||
715 | 118 | def getProtocolVersion(self): | ||
716 | 119 | return get_protocol_version_from_request(self.factory.http_request) | ||
717 | 103 | 120 | ||
718 | 104 | def backendConnected(self): | 121 | def backendConnected(self): |
719 | 105 | """Called when the backend is connected and has sent a good packet.""" | 122 | """Called when the backend is connected and has sent a good packet.""" |
720 | @@ -209,12 +226,14 @@ class HTTPPackClientRefsProtocol(HTTPPackClientProtocol): | |||
721 | 209 | self.factory.http_request.setHeader( | 226 | self.factory.http_request.setHeader( |
722 | 210 | b'Content-Type', | 227 | b'Content-Type', |
723 | 211 | b'application/x-%s-advertisement' % self.factory.command) | 228 | b'application/x-%s-advertisement' % self.factory.command) |
724 | 229 | super(HTTPPackClientRefsProtocol, self).startGoodResponse() | ||
725 | 212 | 230 | ||
726 | 213 | def backendConnected(self): | 231 | def backendConnected(self): |
727 | 214 | HTTPPackClientProtocol.backendConnected(self) | 232 | HTTPPackClientProtocol.backendConnected(self) |
731 | 215 | self.rawDataReceived( | 233 | if self.getProtocolVersion() != b'2': |
732 | 216 | encode_packet(b'# service=%s\n' % self.factory.command)) | 234 | self.rawDataReceived( |
733 | 217 | self.rawDataReceived(encode_packet(None)) | 235 | encode_packet(b'# service=%s\n' % self.factory.command)) |
734 | 236 | self.rawDataReceived(encode_packet(None)) | ||
735 | 218 | 237 | ||
736 | 219 | 238 | ||
737 | 220 | class HTTPPackClientCommandProtocol(HTTPPackClientProtocol): | 239 | class HTTPPackClientCommandProtocol(HTTPPackClientProtocol): |
738 | @@ -225,6 +244,7 @@ class HTTPPackClientCommandProtocol(HTTPPackClientProtocol): | |||
739 | 225 | self.factory.http_request.setHeader( | 244 | self.factory.http_request.setHeader( |
740 | 226 | b'Content-Type', | 245 | b'Content-Type', |
741 | 227 | b'application/x-%s-result' % self.factory.command) | 246 | b'application/x-%s-result' % self.factory.command) |
742 | 247 | # super(HTTPPackClientCommandProtocol, self).startGoodResponse() | ||
743 | 228 | 248 | ||
744 | 229 | 249 | ||
745 | 230 | class HTTPPackClientFactory(protocol.ClientFactory): | 250 | class HTTPPackClientFactory(protocol.ClientFactory): |
746 | @@ -279,9 +299,24 @@ class BaseSmartHTTPResource(resource.Resource): | |||
747 | 279 | The turnip-authenticated-* parameters are set to the values returned | 299 | The turnip-authenticated-* parameters are set to the values returned |
748 | 280 | by the virt service, if any. | 300 | by the virt service, if any. |
749 | 281 | """ | 301 | """ |
750 | 302 | # where = content.tell() | ||
751 | 303 | # content.seek(0) | ||
752 | 304 | # data = content.read() | ||
753 | 305 | # content.seek(where) | ||
754 | 306 | # print(b"req#%s will connect to backend" % (id(request), )) | ||
755 | 307 | # print("--- body ---\n", data, "\n--- endbody ---") | ||
756 | 308 | # # import ipdb; ipdb.set_trace() | ||
757 | 309 | # orig = request.write | ||
758 | 310 | # def x(data): | ||
759 | 311 | # print(b"\n\nReq#%s %s %s -> user\n---%s----" % ( | ||
760 | 312 | # id(request), request.method, request.path, data)) | ||
761 | 313 | # orig(data) | ||
762 | 314 | # request.write = x | ||
763 | 282 | params = { | 315 | params = { |
764 | 283 | b'turnip-can-authenticate': b'yes', | 316 | b'turnip-can-authenticate': b'yes', |
765 | 284 | b'turnip-request-id': str(uuid.uuid4()), | 317 | b'turnip-request-id': str(uuid.uuid4()), |
766 | 318 | b'version': six.ensure_binary( | ||
767 | 319 | get_protocol_version_from_request(request)) | ||
768 | 285 | } | 320 | } |
769 | 286 | authenticated_params = yield self.authenticateUser(request) | 321 | authenticated_params = yield self.authenticateUser(request) |
770 | 287 | for key, value in authenticated_params.items(): | 322 | for key, value in authenticated_params.items(): |
771 | diff --git a/turnip/pack/tests/test_functional.py b/turnip/pack/tests/test_functional.py | |||
772 | index d6aaa7f..2809593 100644 | |||
773 | --- a/turnip/pack/tests/test_functional.py | |||
774 | +++ b/turnip/pack/tests/test_functional.py | |||
775 | @@ -35,6 +35,7 @@ from fixtures import ( | |||
776 | 35 | TempDir, | 35 | TempDir, |
777 | 36 | ) | 36 | ) |
778 | 37 | from pygit2 import GIT_OID_HEX_ZERO | 37 | from pygit2 import GIT_OID_HEX_ZERO |
779 | 38 | from testscenarios.testcase import WithScenarios | ||
780 | 38 | from testtools import TestCase | 39 | from testtools import TestCase |
781 | 39 | from testtools.content import text_content | 40 | from testtools.content import text_content |
782 | 40 | from testtools.deferredruntest import AsynchronousDeferredRunTest | 41 | from testtools.deferredruntest import AsynchronousDeferredRunTest |
783 | @@ -77,10 +78,14 @@ from turnip.pack.tests.fake_servers import ( | |||
784 | 77 | from turnip.version_info import version_info | 78 | from turnip.version_info import version_info |
785 | 78 | 79 | ||
786 | 79 | 80 | ||
788 | 80 | class FunctionalTestMixin(object): | 81 | class FunctionalTestMixin(WithScenarios): |
789 | 81 | 82 | ||
790 | 82 | run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=30) | 83 | run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=30) |
791 | 83 | 84 | ||
792 | 85 | scenarios = [ | ||
793 | 86 | ('v1 protocol', {"protocol_version": "1"}), | ||
794 | 87 | ('v2 protocol', {"protocol_version": "2"})] | ||
795 | 88 | |||
796 | 84 | def startVirtInfo(self): | 89 | def startVirtInfo(self): |
797 | 85 | # Set up a fake virt information XML-RPC server which just | 90 | # Set up a fake virt information XML-RPC server which just |
798 | 86 | # maps paths to their SHA-256 hash. | 91 | # maps paths to their SHA-256 hash. |
799 | @@ -120,8 +125,13 @@ class FunctionalTestMixin(object): | |||
800 | 120 | 125 | ||
801 | 121 | @defer.inlineCallbacks | 126 | @defer.inlineCallbacks |
802 | 122 | def assertCommandSuccess(self, command, path='.'): | 127 | def assertCommandSuccess(self, command, path='.'): |
803 | 128 | if command[0] == b'git' and self.protocol_version == '2': | ||
804 | 129 | args = list(command[1:]) | ||
805 | 130 | command = [b'git', b'-c', b'protocol.version=2'] + args | ||
806 | 131 | env = {"GIT_TRACE_PACKET": '1'} | ||
807 | 132 | env.update(os.environ) | ||
808 | 123 | out, err, code = yield utils.getProcessOutputAndValue( | 133 | out, err, code = yield utils.getProcessOutputAndValue( |
810 | 124 | command[0], command[1:], env=os.environ, path=path) | 134 | command[0], command[1:], env=env, path=path) |
811 | 125 | if code != 0: | 135 | if code != 0: |
812 | 126 | self.addDetail('stdout', text_content(out)) | 136 | self.addDetail('stdout', text_content(out)) |
813 | 127 | self.addDetail('stderr', text_content(err)) | 137 | self.addDetail('stderr', text_content(err)) |
814 | @@ -130,6 +140,9 @@ class FunctionalTestMixin(object): | |||
815 | 130 | 140 | ||
816 | 131 | @defer.inlineCallbacks | 141 | @defer.inlineCallbacks |
817 | 132 | def assertCommandFailure(self, command, path='.'): | 142 | def assertCommandFailure(self, command, path='.'): |
818 | 143 | if self.protocol_version == '2': | ||
819 | 144 | args = list(command[1:]) | ||
820 | 145 | command = [b'git', b'-c', b'protocol.version=2'] + args | ||
821 | 133 | out, err, code = yield utils.getProcessOutputAndValue( | 146 | out, err, code = yield utils.getProcessOutputAndValue( |
822 | 134 | command[0], command[1:], env=os.environ, path=path) | 147 | command[0], command[1:], env=os.environ, path=path) |
823 | 135 | if code == 0: | 148 | if code == 0: |
824 | diff --git a/turnip/pack/tests/test_helpers.py b/turnip/pack/tests/test_helpers.py | |||
825 | index a7a3870..8d09c98 100644 | |||
826 | --- a/turnip/pack/tests/test_helpers.py | |||
827 | +++ b/turnip/pack/tests/test_helpers.py | |||
828 | @@ -23,7 +23,7 @@ from testtools import TestCase | |||
829 | 23 | 23 | ||
830 | 24 | from turnip.pack import helpers | 24 | from turnip.pack import helpers |
831 | 25 | import turnip.pack.hooks | 25 | import turnip.pack.hooks |
833 | 26 | 26 | from turnip.pack.helpers import FLUSH_PKT | |
834 | 27 | 27 | ||
835 | 28 | TEST_DATA = b'0123456789abcdef' | 28 | TEST_DATA = b'0123456789abcdef' |
836 | 29 | TEST_PKT = b'00140123456789abcdef' | 29 | TEST_PKT = b'00140123456789abcdef' |
837 | @@ -91,8 +91,8 @@ class TestDecodeRequest(TestCase): | |||
838 | 91 | # We parse extra params behind 2 NUL bytes | 91 | # We parse extra params behind 2 NUL bytes |
839 | 92 | req = b'git-upload-pack /test_repo\0host=git.launchpad.test\0\0ver=2\0' | 92 | req = b'git-upload-pack /test_repo\0host=git.launchpad.test\0\0ver=2\0' |
840 | 93 | self.assertEqual( | 93 | self.assertEqual( |
843 | 94 | (b'git-upload-pack', b'/test_repo', | 94 | [(b'git-upload-pack', b'/test_repo', |
844 | 95 | {b'host': b'git.launchpad.test', b'ver': b'2'}), | 95 | {b'host': b'git.launchpad.test', b'ver': b'2'})], |
845 | 96 | helpers.decode_request(req)) | 96 | helpers.decode_request(req)) |
846 | 97 | 97 | ||
847 | 98 | def test_parse_multiple_extra_params_after_2_null_bytes(self): | 98 | def test_parse_multiple_extra_params_after_2_null_bytes(self): |
848 | @@ -102,9 +102,9 @@ class TestDecodeRequest(TestCase): | |||
849 | 102 | b'ver=2\0\0param2=value2\0\0param3=value3\0' | 102 | b'ver=2\0\0param2=value2\0\0param3=value3\0' |
850 | 103 | ) | 103 | ) |
851 | 104 | self.assertEqual( | 104 | self.assertEqual( |
853 | 105 | (b'git-upload-pack', b'/test_repo', | 105 | [(b'git-upload-pack', b'/test_repo', |
854 | 106 | {b'host': b'git.launchpad.test', b'ver': b'2', | 106 | {b'host': b'git.launchpad.test', b'ver': b'2', |
856 | 107 | b'param2': b'value2', b'param3': b'value3'}), | 107 | b'param2': b'value2', b'param3': b'value3'})], |
857 | 108 | helpers.decode_request(req)) | 108 | helpers.decode_request(req)) |
858 | 109 | 109 | ||
859 | 110 | def test_rejects_extra_param_without_end_null_bytes(self): | 110 | def test_rejects_extra_param_without_end_null_bytes(self): |
860 | @@ -124,25 +124,25 @@ class TestDecodeRequest(TestCase): | |||
861 | 124 | def test_allow_2_end_nul_bytes(self): | 124 | def test_allow_2_end_nul_bytes(self): |
862 | 125 | req = b'git-upload-pack /test_repo\0host=git.launchpad.test\0\0' | 125 | req = b'git-upload-pack /test_repo\0host=git.launchpad.test\0\0' |
863 | 126 | self.assertEqual( | 126 | self.assertEqual( |
866 | 127 | (b'git-upload-pack', b'/test_repo', | 127 | [(b'git-upload-pack', b'/test_repo', |
867 | 128 | {b'host': b'git.launchpad.test'}), | 128 | {b'host': b'git.launchpad.test'})], |
868 | 129 | helpers.decode_request(req)) | 129 | helpers.decode_request(req)) |
869 | 130 | 130 | ||
870 | 131 | def test_without_parameters(self): | 131 | def test_without_parameters(self): |
871 | 132 | self.assertEqual( | 132 | self.assertEqual( |
873 | 133 | (b'git-do-stuff', b'/some/path', {}), | 133 | [(b'git-do-stuff', b'/some/path', {})], |
874 | 134 | helpers.decode_request(b'git-do-stuff /some/path\0')) | 134 | helpers.decode_request(b'git-do-stuff /some/path\0')) |
875 | 135 | 135 | ||
876 | 136 | def test_with_host_parameter(self): | 136 | def test_with_host_parameter(self): |
877 | 137 | self.assertEqual( | 137 | self.assertEqual( |
879 | 138 | (b'git-do-stuff', b'/some/path', {b'host': b'example.com'}), | 138 | [(b'git-do-stuff', b'/some/path', {b'host': b'example.com'})], |
880 | 139 | helpers.decode_request( | 139 | helpers.decode_request( |
881 | 140 | b'git-do-stuff /some/path\0host=example.com\0')) | 140 | b'git-do-stuff /some/path\0host=example.com\0')) |
882 | 141 | 141 | ||
883 | 142 | def test_with_host_and_user_parameters(self): | 142 | def test_with_host_and_user_parameters(self): |
884 | 143 | self.assertEqual( | 143 | self.assertEqual( |
887 | 144 | (b'git-do-stuff', b'/some/path', | 144 | [(b'git-do-stuff', b'/some/path', |
888 | 145 | {b'host': b'example.com', b'user': b'foo=bar'}), | 145 | {b'host': b'example.com', b'user': b'foo=bar'})], |
889 | 146 | helpers.decode_request( | 146 | helpers.decode_request( |
890 | 147 | b'git-do-stuff /some/path\0host=example.com\0user=foo=bar\0')) | 147 | b'git-do-stuff /some/path\0host=example.com\0user=foo=bar\0')) |
891 | 148 | 148 | ||
892 | @@ -173,6 +173,33 @@ class TestDecodeRequest(TestCase): | |||
893 | 173 | b'git-do-stuff /foo\0host=foo\0host=bar\0', | 173 | b'git-do-stuff /foo\0host=foo\0host=bar\0', |
894 | 174 | b'Parameters must not be repeated') | 174 | b'Parameters must not be repeated') |
895 | 175 | 175 | ||
896 | 176 | def test_v2_extra_commands(self): | ||
897 | 177 | data = ( | ||
898 | 178 | b"git-upload-pack b306d" + | ||
899 | 179 | b"\x00turnip-x=yes\x00turnip-request-id=123\x00" + | ||
900 | 180 | b"version=2\x000014command=ls-refs\n0014agent=git/2.25.1" + | ||
901 | 181 | b'00010009peel\n000csymrefs\n0014ref-prefix HEAD\n' + | ||
902 | 182 | b'001bref-prefix refs/heads/\n' | ||
903 | 183 | b'001aref-prefix refs/tags/\n0000' + | ||
904 | 184 | FLUSH_PKT) | ||
905 | 185 | |||
906 | 186 | decoded = helpers.decode_request(data) | ||
907 | 187 | self.assertEqual(2, len(decoded)) | ||
908 | 188 | first, second = decoded | ||
909 | 189 | self.assertEqual((b'ls-refs', b'b306d', { | ||
910 | 190 | b'turnip-x': b'yes', | ||
911 | 191 | b'turnip-request-id': b'123', | ||
912 | 192 | b'version': b'2', | ||
913 | 193 | b'capabilities': [b'agent=git/2.25.1'], | ||
914 | 194 | b'symrefs': True, | ||
915 | 195 | b'peel': True, | ||
916 | 196 | b'ref-prefix': [b'HEAD', b'refs/heads/', b'refs/tags/'] | ||
917 | 197 | }), first) | ||
918 | 198 | self.assertEqual((b'git-upload-pack', b'b306d', { | ||
919 | 199 | b'turnip-x': b'yes', | ||
920 | 200 | b'turnip-request-id': b'123', | ||
921 | 201 | b'version': b'2'}), second) | ||
922 | 202 | |||
923 | 176 | 203 | ||
924 | 177 | class TestEncodeRequest(TestCase): | 204 | class TestEncodeRequest(TestCase): |
925 | 178 | """Test git-proto-request encoding.""" | 205 | """Test git-proto-request encoding.""" |
926 | diff --git a/turnip/pack/tests/test_http.py b/turnip/pack/tests/test_http.py | |||
927 | index 26e2754..0ce06f5 100644 | |||
928 | --- a/turnip/pack/tests/test_http.py | |||
929 | +++ b/turnip/pack/tests/test_http.py | |||
930 | @@ -9,6 +9,7 @@ from __future__ import ( | |||
931 | 9 | 9 | ||
932 | 10 | from io import BytesIO | 10 | from io import BytesIO |
933 | 11 | 11 | ||
934 | 12 | from fixtures import EnvironmentVariable | ||
935 | 12 | from testtools import TestCase | 13 | from testtools import TestCase |
936 | 13 | from testtools.deferredruntest import AsynchronousDeferredRunTest | 14 | from testtools.deferredruntest import AsynchronousDeferredRunTest |
937 | 14 | from twisted.internet import ( | 15 | from twisted.internet import ( |
938 | @@ -24,7 +25,10 @@ from turnip.pack import ( | |||
939 | 24 | helpers, | 25 | helpers, |
940 | 25 | http, | 26 | http, |
941 | 26 | ) | 27 | ) |
942 | 28 | from turnip.pack.helpers import encode_packet | ||
943 | 27 | from turnip.pack.tests.fake_servers import FakeVirtInfoService | 29 | from turnip.pack.tests.fake_servers import FakeVirtInfoService |
944 | 30 | from turnip.tests.compat import mock | ||
945 | 31 | from turnip.version_info import version_info | ||
946 | 28 | 32 | ||
947 | 29 | 33 | ||
948 | 30 | class LessDummyRequest(requesthelper.DummyRequest): | 34 | class LessDummyRequest(requesthelper.DummyRequest): |
949 | @@ -74,12 +78,14 @@ class FakeRoot(object): | |||
950 | 74 | 78 | ||
951 | 75 | def __init__(self): | 79 | def __init__(self): |
952 | 76 | self.backend_transport = None | 80 | self.backend_transport = None |
953 | 81 | self.client_factory = None | ||
954 | 77 | self.backend_connected = defer.Deferred() | 82 | self.backend_connected = defer.Deferred() |
955 | 78 | 83 | ||
956 | 79 | def authenticateWithPassword(self, user, password): | 84 | def authenticateWithPassword(self, user, password): |
957 | 80 | return {} | 85 | return {} |
958 | 81 | 86 | ||
959 | 82 | def connectToBackend(self, client_factory): | 87 | def connectToBackend(self, client_factory): |
960 | 88 | self.client_factory = client_factory | ||
961 | 83 | self.backend_transport = testing.StringTransportWithDisconnection() | 89 | self.backend_transport = testing.StringTransportWithDisconnection() |
962 | 84 | p = client_factory.buildProtocol(None) | 90 | p = client_factory.buildProtocol(None) |
963 | 85 | self.backend_transport.protocol = p | 91 | self.backend_transport.protocol = p |
964 | @@ -186,6 +192,33 @@ class TestSmartHTTPRefsResource(ErrorTestMixin, TestCase): | |||
965 | 186 | 'And I am raw, since we got a good packet to start with.', | 192 | 'And I am raw, since we got a good packet to start with.', |
966 | 187 | self.request.value) | 193 | self.request.value) |
967 | 188 | 194 | ||
968 | 195 | @defer.inlineCallbacks | ||
969 | 196 | def test_good_v2_included_version_and_capabilities(self): | ||
970 | 197 | self.useFixture(EnvironmentVariable("PACK_EXTENSIONS", ".agent.Agent")) | ||
971 | 198 | self.request.requestHeaders.addRawHeader("Git-Protocol", "version=2") | ||
972 | 199 | yield self.performRequest( | ||
973 | 200 | helpers.encode_packet(b'I am git protocol data.') + | ||
974 | 201 | b'And I am raw, since we got a good packet to start with.') | ||
975 | 202 | self.assertEqual(200, self.request.responseCode) | ||
976 | 203 | self.assertEqual(self.root.client_factory.params, { | ||
977 | 204 | 'version': '2', | ||
978 | 205 | 'turnip-advertise-refs': 'yes', | ||
979 | 206 | 'turnip-can-authenticate': 'yes', | ||
980 | 207 | 'turnip-request-id': mock.ANY, | ||
981 | 208 | 'turnip-stateless-rpc': 'yes'}) | ||
982 | 209 | |||
983 | 210 | capabilities = ( | ||
984 | 211 | '000eversion 2\n' + | ||
985 | 212 | encode_packet( | ||
986 | 213 | 'agent=turnip/%s\n' % version_info["revision_id"]) + | ||
987 | 214 | '0000\n') | ||
988 | 215 | self.assertEqual( | ||
989 | 216 | capabilities + | ||
990 | 217 | '001e# service=git-upload-pack\n' | ||
991 | 218 | '0000001bI am git protocol data.' | ||
992 | 219 | 'And I am raw, since we got a good packet to start with.', | ||
993 | 220 | self.request.value) | ||
994 | 221 | |||
995 | 189 | 222 | ||
996 | 190 | class TestSmartHTTPCommandResource(ErrorTestMixin, TestCase): | 223 | class TestSmartHTTPCommandResource(ErrorTestMixin, TestCase): |
997 | 191 | 224 |
I'll keep this MP as "Work in progress" until I write some functional tests using protocol v2, to make sure it will be compatible with both protocol versions (even if no relevant capability is implemented for v2 right now).