diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-08-24 10:21:02 +0900 |
---|---|---|
committer | Gustaf Lundh <gustaf.lundh@sonymobile.com> | 2012-09-11 09:45:48 +0200 |
commit | 86d973d24ec6771504c37eddc535dff8e03c724f (patch) | |
tree | 1676f107c59efd1d92bd92cb48a9dc726bf00e59 | |
parent | 34acdd253439448b6c08c3abfc5e7b8bd03f383f (diff) | |
download | git-repo-86d973d24ec6771504c37eddc535dff8e03c724f.tar.gz |
sync: Support authentication to manifest server with .netrc
When using the --smart-sync or --smart-tag option, and the specified
manifest server is hosted on a server that requires authentication,
repo sync fails with the error: HTTP 401 Unauthorized.
Add support for getting the credentials from the .netrc file.
If a .netrc file exists in the user's home directory, and it contains
credentials for the hostname of the manifest server specified in the
manifest, use the credentials to authenticate with the manifest server
using the URL syntax extension for Basic Authentication:
http://user:password@host:port/path
Credentials from the .netrc file are only used if the manifest server
URL specified in the manifest does not already include credentials.
Change-Id: I06e6586e8849d0cd12fa9746789e8d45d5b1f848
-rw-r--r-- | subcmds/sync.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/subcmds/sync.py b/subcmds/sync.py index cbf0decc..b75bedc1 100644 --- a/subcmds/sync.py +++ b/subcmds/sync.py | |||
@@ -13,6 +13,7 @@ | |||
13 | # See the License for the specific language governing permissions and | 13 | # See the License for the specific language governing permissions and |
14 | # limitations under the License. | 14 | # limitations under the License. |
15 | 15 | ||
16 | import netrc | ||
16 | from optparse import SUPPRESS_HELP | 17 | from optparse import SUPPRESS_HELP |
17 | import os | 18 | import os |
18 | import re | 19 | import re |
@@ -21,6 +22,7 @@ import socket | |||
21 | import subprocess | 22 | import subprocess |
22 | import sys | 23 | import sys |
23 | import time | 24 | import time |
25 | import urlparse | ||
24 | import xmlrpclib | 26 | import xmlrpclib |
25 | 27 | ||
26 | try: | 28 | try: |
@@ -365,8 +367,34 @@ uncommitted changes are present' % project.relpath | |||
365 | print >>sys.stderr, \ | 367 | print >>sys.stderr, \ |
366 | 'error: cannot smart sync: no manifest server defined in manifest' | 368 | 'error: cannot smart sync: no manifest server defined in manifest' |
367 | sys.exit(1) | 369 | sys.exit(1) |
370 | |||
371 | manifest_server = self.manifest.manifest_server | ||
372 | if not '@' in manifest_server: | ||
373 | try: | ||
374 | info = netrc.netrc() | ||
375 | except IOError: | ||
376 | print >>sys.stderr, '.netrc file does not exist or could not be opened' | ||
377 | else: | ||
378 | try: | ||
379 | parse_result = urlparse.urlparse(manifest_server) | ||
380 | if parse_result.hostname: | ||
381 | username, _account, password = \ | ||
382 | info.authenticators(parse_result.hostname) | ||
383 | except TypeError: | ||
384 | # TypeError is raised when the given hostname is not present | ||
385 | # in the .netrc file. | ||
386 | print >>sys.stderr, 'No credentials found for %s in .netrc' % \ | ||
387 | parse_result.hostname | ||
388 | except netrc.NetrcParseError as e: | ||
389 | print >>sys.stderr, 'Error parsing .netrc file: %s' % e | ||
390 | else: | ||
391 | if (username and password): | ||
392 | manifest_server = manifest_server.replace('://', '://%s:%s@' % | ||
393 | (username, password), | ||
394 | 1) | ||
395 | |||
368 | try: | 396 | try: |
369 | server = xmlrpclib.Server(self.manifest.manifest_server) | 397 | server = xmlrpclib.Server(manifest_server) |
370 | if opt.smart_sync: | 398 | if opt.smart_sync: |
371 | p = self.manifest.manifestProject | 399 | p = self.manifest.manifestProject |
372 | b = p.GetBranch(p.CurrentBranch) | 400 | b = p.GetBranch(p.CurrentBranch) |