From be91386b458cb29f7548b93e31884c2a9622fe80 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 31 Dec 2007 19:18:52 +0200 Subject: Don't always init repository when doing fast-import. --- gitosis/repository.py | 1 - gitosis/test/test_repository.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/gitosis/repository.py b/gitosis/repository.py index 07e657d..18a789c 100644 --- a/gitosis/repository.py +++ b/gitosis/repository.py @@ -67,7 +67,6 @@ def fast_import( """ Create an initial commit. """ - init(path=git_dir) child = subprocess.Popen( args=[ 'git', diff --git a/gitosis/test/test_repository.py b/gitosis/test/test_repository.py index 4890c47..b7338e5 100644 --- a/gitosis/test/test_repository.py +++ b/gitosis/test/test_repository.py @@ -106,6 +106,7 @@ exec git "$@" def test_fast_import_environment(): tmp = maketemp() path = os.path.join(tmp, 'repo.git') + repository.init(path=path) mockbindir = os.path.join(tmp, 'mockbin') os.mkdir(mockbindir) mockgit = os.path.join(mockbindir, 'git') -- cgit v1.2.3 From 895fd8b7ad0766f5c320befa221fa7e4cdacfc71 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 31 Dec 2007 19:30:27 +0200 Subject: Allow using fast_import for more than initial commit. --- gitosis/repository.py | 16 ++++++++++++---- gitosis/test/test_repository.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/gitosis/repository.py b/gitosis/repository.py index 18a789c..092e41d 100644 --- a/gitosis/repository.py +++ b/gitosis/repository.py @@ -63,6 +63,7 @@ def fast_import( commit_msg, committer, files, + parent=None, ): """ Create an initial commit. @@ -97,10 +98,17 @@ committer %(committer)s now data %(commit_msg_len)d %(commit_msg)s """ % dict( - committer=committer, - commit_msg_len=len(commit_msg), - commit_msg=commit_msg, - )) + committer=committer, + commit_msg_len=len(commit_msg), + commit_msg=commit_msg, + )) + if parent is not None: + assert not parent.startswith(':') + child.stdin.write("""\ +from %(parent)s +""" % dict( + parent=parent, + )) for index, (path, content) in enumerate(files): child.stdin.write('M 100644 :%d %s\n' % (index+1, path)) child.stdin.close() diff --git a/gitosis/test/test_repository.py b/gitosis/test/test_repository.py index b7338e5..6ce4129 100644 --- a/gitosis/test/test_repository.py +++ b/gitosis/test/test_repository.py @@ -313,3 +313,31 @@ exec git "$@" got = readFile(os.path.join(tmp, 'cookie')) eq(got, magic_cookie) +def test_fast_import_parent(): + tmp = maketemp() + path = os.path.join(tmp, 'repo.git') + repository.init(path=path) + repository.fast_import( + git_dir=path, + commit_msg='foo initial bar', + committer='Mr. Unit Test ', + files=[ + ('foo', 'bar\n'), + ], + ) + repository.fast_import( + git_dir=path, + commit_msg='another', + committer='Sam One Else ', + parent='refs/heads/master^0', + files=[ + ('quux', 'thud\n'), + ], + ) + export = os.path.join(tmp, 'export') + repository.export( + git_dir=path, + path=export, + ) + eq(sorted(os.listdir(export)), + sorted(['foo', 'quux'])) -- cgit v1.2.3 From e495c9a66e583f009a53af865eacca3a5e7b953e Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 31 Dec 2007 21:11:22 +0200 Subject: Make post-update hook reload config after writing it out. Without this, any changes to repository settings would only be applied after one extra (non-empty) push. Add unit test coverage for the post-update hook. Make SSH authorized_keys path configurable, mostly for unit tests. --- gitosis/run_hook.py | 5 ++- gitosis/test/test_run_hook.py | 95 +++++++++++++++++++++++++++++++++++++++++++ gitosis/util.py | 7 ++++ 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 gitosis/test/test_run_hook.py diff --git a/gitosis/run_hook.py b/gitosis/run_hook.py index f9a009d..e535e6a 100644 --- a/gitosis/run_hook.py +++ b/gitosis/run_hook.py @@ -29,6 +29,8 @@ def post_update(cfg, git_dir): os.path.join(export, 'gitosis.conf'), os.path.join(export, '..', 'gitosis.conf'), ) + # re-read config to get up-to-date settings + cfg.read(os.path.join(export, '..', 'gitosis.conf')) gitweb.set_descriptions( config=cfg, ) @@ -40,8 +42,9 @@ def post_update(cfg, git_dir): gitdaemon.set_export_ok( config=cfg, ) + authorized_keys = util.getSSHAuthorizedKeysPath(config=cfg) ssh.writeAuthorizedKeys( - path=os.path.expanduser('~/.ssh/authorized_keys'), + path=authorized_keys, keydir=os.path.join(export, 'keydir'), ) diff --git a/gitosis/test/test_run_hook.py b/gitosis/test/test_run_hook.py new file mode 100644 index 0000000..db01e0c --- /dev/null +++ b/gitosis/test/test_run_hook.py @@ -0,0 +1,95 @@ +from nose.tools import eq_ as eq + +import os +from ConfigParser import RawConfigParser +from cStringIO import StringIO + +from gitosis import init, repository, run_hook +from gitosis.test.util import maketemp, readFile + +def test_post_update_simple(): + tmp = maketemp() + repos = os.path.join(tmp, 'repositories') + os.mkdir(repos) + admin_repository = os.path.join(repos, 'gitosis-admin.git') + pubkey = ( + 'ssh-somealgo ' + +'0123456789ABCDEFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + +'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= fakeuser@fakehost') + user = 'theadmin' + init.init_admin_repository( + git_dir=admin_repository, + pubkey=pubkey, + user=user, + ) + repository.init(path=os.path.join(repos, 'forweb.git')) + repository.init(path=os.path.join(repos, 'fordaemon.git')) + repository.fast_import( + git_dir=admin_repository, + committer='John Doe ', + commit_msg="""\ +stuff +""", + parent='refs/heads/master^0', + files=[ + ('gitosis.conf', """\ +[gitosis] + +[group gitosis-admin] +members = theadmin +writable = gitosis-admin + +[repo fordaemon] +daemon = yes + +[repo forweb] +gitweb = yes +owner = John Doe +description = blah blah +"""), + ('keydir/jdoe.pub', + 'ssh-somealgo ' + +'0123456789ABCDEFBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' + +'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' + +'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB' + +'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB= jdoe@host.example.com'), + ], + ) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', repos) + generated = os.path.join(tmp, 'generated') + os.mkdir(generated) + cfg.set('gitosis', 'generate-files-in', generated) + ssh = os.path.join(tmp, 'ssh') + os.mkdir(ssh) + cfg.set( + 'gitosis', + 'ssh-authorized-keys-path', + os.path.join(ssh, 'authorized_keys'), + ) + run_hook.post_update( + cfg=cfg, + git_dir=admin_repository, + ) + got = readFile(os.path.join(repos, 'forweb.git', 'description')) + eq(got, 'blah blah\n') + got = os.listdir(generated) + eq(got, ['projects.list']) + got = readFile(os.path.join(generated, 'projects.list')) + eq( + got, + """\ +forweb.git John+Doe +""", + ) + got = os.listdir(os.path.join(repos, 'fordaemon.git')) + assert 'git-daemon-export-ok' in got, \ + "git-daemon-export-ok not created: %r" % got + got = os.listdir(ssh) + eq(got, ['authorized_keys']) + got = readFile(os.path.join(ssh, 'authorized_keys')).splitlines(True) + assert 'command="gitosis-serve jdoe",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-somealgo 0123456789ABCDEFBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB= jdoe@host.example.com\n' in got, \ + "SSH authorized_keys line for jdoe not found: %r" % got diff --git a/gitosis/util.py b/gitosis/util.py index 1c88ed1..a1b8c76 100644 --- a/gitosis/util.py +++ b/gitosis/util.py @@ -27,3 +27,10 @@ def getGeneratedFilesDir(config): except (NoSectionError, NoOptionError): generated = os.path.expanduser('~/gitosis') return generated + +def getSSHAuthorizedKeysPath(config): + try: + path = config.get('gitosis', 'ssh-authorized-keys-path') + except (NoSectionError, NoOptionError): + path = os.path.expanduser('~/.ssh/authorized_keys'), + return path -- cgit v1.2.3 From 5b3f2f7a4d5916f80c2e797d0a12c4c250933769 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 31 Dec 2007 21:30:36 +0200 Subject: Fix typo that made push fail in writeAuthorizedKeys. --- gitosis/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitosis/util.py b/gitosis/util.py index a1b8c76..479b2e9 100644 --- a/gitosis/util.py +++ b/gitosis/util.py @@ -32,5 +32,5 @@ def getSSHAuthorizedKeysPath(config): try: path = config.get('gitosis', 'ssh-authorized-keys-path') except (NoSectionError, NoOptionError): - path = os.path.expanduser('~/.ssh/authorized_keys'), + path = os.path.expanduser('~/.ssh/authorized_keys') return path -- cgit v1.2.3 From b04fbb1df75c7f1f3f6bb4b4b37565f4a2ede54d Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Thu, 17 Jan 2008 02:15:52 +0200 Subject: Fix bug where members=@all didn't actually give access. --- gitosis/group.py | 6 +++++- gitosis/test/test_access.py | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gitosis/group.py b/gitosis/group.py index 975bbc5..a18a731 100644 --- a/gitosis/group.py +++ b/gitosis/group.py @@ -19,7 +19,11 @@ def _getMembership(config, user, seen): else: members = members.split() - if user in members: + # @all is the only group where membership needs to be + # bootstrapped like this, anything else gets started from the + # username itself + if (user in members + or '@all' in members): log.debug('found %(user)r in %(group)r' % dict( user=user, group=group, diff --git a/gitosis/test/test_access.py b/gitosis/test/test_access.py index 9f9d81a..751b0b4 100644 --- a/gitosis/test/test_access.py +++ b/gitosis/test/test_access.py @@ -78,6 +78,14 @@ def test_read_yes_map_wouldHaveWritable(): eq(access.haveAccess(config=cfg, user='jdoe', mode='readonly', path='foo/bar'), None) +def test_read_yes_all(): + cfg = RawConfigParser() + cfg.add_section('group fooers') + cfg.set('group fooers', 'members', '@all') + cfg.set('group fooers', 'readonly', 'foo/bar') + eq(access.haveAccess(config=cfg, user='jdoe', mode='readonly', path='foo/bar'), + ('repositories', 'foo/bar')) + def test_base_global_absolute(): cfg = RawConfigParser() cfg.add_section('gitosis') -- cgit v1.2.3 From f724a2e724daaed9afb875ed2f410042fec15850 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Tue, 5 Feb 2008 13:18:46 +0200 Subject: Add mod_redirect to lighttpd example config snippet. --- lighttpd-gitweb.conf | 1 + 1 file changed, 1 insertion(+) diff --git a/lighttpd-gitweb.conf b/lighttpd-gitweb.conf index cd0bedd..1046add 100644 --- a/lighttpd-gitweb.conf +++ b/lighttpd-gitweb.conf @@ -1,6 +1,7 @@ server.modules += ( "mod_cgi", "mod_setenv", + "mod_redirect", ) url.redirect += ( -- cgit v1.2.3 From 9708fb2cc956fb30348aee9bee763cdd81f35840 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sat, 16 Feb 2008 12:17:18 +0200 Subject: State that sharing user accounts for two uses is advanced usage. --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ee14705..9204776 100644 --- a/README.rst +++ b/README.rst @@ -41,7 +41,8 @@ First, we will create the user that will own the repositories. This is usually called ``git``, but any name will work, and you can have more than one per system if you really want to. The user does not need a password, but does need a valid shell (otherwise, SSH will refuse to -work). +work). Don't use an existing account unless you know what you're +doing. I usually store ``git`` repositories in the subtree ``/srv/example.com/git`` (replace ``example.com`` with your own -- cgit v1.2.3 From e1d150daf55456cd9b830b37d509046924fde1c9 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Tue, 19 Feb 2008 08:57:59 +0200 Subject: Show alternate gitweb.conf access control config. --- gitweb.conf | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gitweb.conf b/gitweb.conf index 8fb62d1..32d81b1 100644 --- a/gitweb.conf +++ b/gitweb.conf @@ -15,6 +15,15 @@ $projectroot = "/srv/example.com/git/repositories"; # is already sharing anonymously. $export_ok = "git-daemon-export-ok"; +# Alternatively, you could set these, to allow exactly the things in +# projects.list, which in this case is the repos with gitweb=yes +# in gitosis.conf. This means you don't need daemon=yes, but you +# can't have repositories hidden but browsable if you know the name. +# And note gitweb already allows downloading the full repository, +# so you might as well serve git-daemon too. +# $export_ok = ""; +# $strict_export = "true"; + # A list of base urls where all the repositories can be cloned from. # Easier than having per-repository cloneurl files. @git_base_url_list = ('git://example.com'); -- cgit v1.2.3 From f7bcd554fae642585af5f99c3c858eb2d343e1da Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Wed, 19 Mar 2008 21:28:46 +0200 Subject: Test that incoming paths cannot contain /../ --- gitosis/test/test_serve.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index d6030d2..cf54cc6 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -57,6 +57,18 @@ def test_bad_unsafeArguments(): eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) +def test_bad_unsafeArguments_dotdot(): + cfg = RawConfigParser() + e = assert_raises( + serve.UnsafeArgumentsError, + serve.serve, + cfg=cfg, + user='jdoe', + command='git-upload-pack something/../evil', + ) + eq(str(e), 'Arguments to command look dangerous') + assert isinstance(e, serve.ServingError) + def test_bad_forbiddenCommand_read(): cfg = RawConfigParser() e = assert_raises( -- cgit v1.2.3 From f839f889b607c9920659516959795859aab0a86e Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Wed, 19 Mar 2008 21:52:03 +0200 Subject: Make serve acceptable path unit tests more careful. Tests used to trigger the wanted security exception merely by being unquoted, that's not good enough. --- gitosis/test/test_serve.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index cf54cc6..23b6a6f 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -45,14 +45,38 @@ def test_bad_command(): eq(str(e), 'Unknown command denied') assert isinstance(e, serve.ServingError) -def test_bad_unsafeArguments(): +def test_bad_unsafeArguments_notQuoted(): cfg = RawConfigParser() e = assert_raises( serve.UnsafeArgumentsError, serve.serve, cfg=cfg, user='jdoe', - command='git-upload-pack /evil/attack', + command="git-upload-pack foo", + ) + eq(str(e), 'Arguments to command look dangerous') + assert isinstance(e, serve.ServingError) + +def test_bad_unsafeArguments_absolute(): + cfg = RawConfigParser() + e = assert_raises( + serve.UnsafeArgumentsError, + serve.serve, + cfg=cfg, + user='jdoe', + command="git-upload-pack '/evil/attack'", + ) + eq(str(e), 'Arguments to command look dangerous') + assert isinstance(e, serve.ServingError) + +def test_bad_unsafeArguments_badCharacters(): + cfg = RawConfigParser() + e = assert_raises( + serve.UnsafeArgumentsError, + serve.serve, + cfg=cfg, + user='jdoe', + command="git-upload-pack 'ev!l'", ) eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) @@ -64,7 +88,7 @@ def test_bad_unsafeArguments_dotdot(): serve.serve, cfg=cfg, user='jdoe', - command='git-upload-pack something/../evil', + command="git-upload-pack 'something/../evil'", ) eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) -- cgit v1.2.3 From 4d8ba7788d10e62928404b0272de241580e00e92 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Wed, 19 Mar 2008 21:49:47 +0200 Subject: Allow absolute paths in repo paths, treat them as relative. As the only convenient way to use non-standard SSH ports with git is via the ssh://user@host:port/path syntax, and that syntax forces absolute urls, just force convert absolute paths to relative paths; you'll never really want absolute paths via gitosis, anyway. --- gitosis/serve.py | 2 +- gitosis/test/test_serve.py | 32 ++++++++++++++++++++------------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/gitosis/serve.py b/gitosis/serve.py index 0f9cb5c..37ad97f 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -15,7 +15,7 @@ from gitosis import gitdaemon from gitosis import app from gitosis import util -ALLOW_RE = re.compile("^'(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") +ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") COMMANDS_READONLY = [ 'git-upload-pack', diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index 23b6a6f..a223c43 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -57,18 +57,6 @@ def test_bad_unsafeArguments_notQuoted(): eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) -def test_bad_unsafeArguments_absolute(): - cfg = RawConfigParser() - e = assert_raises( - serve.UnsafeArgumentsError, - serve.serve, - cfg=cfg, - user='jdoe', - command="git-upload-pack '/evil/attack'", - ) - eq(str(e), 'Arguments to command look dangerous') - assert isinstance(e, serve.ServingError) - def test_bad_unsafeArguments_badCharacters(): cfg = RawConfigParser() e = assert_raises( @@ -402,3 +390,23 @@ def test_push_inits_sets_export_ok(): path = os.path.join(repositories, 'foo.git', 'git-daemon-export-ok') assert os.path.exists(path) +def test_absolute(): + # as the only convenient way to use non-standard SSH ports with + # git is via the ssh://user@host:port/path syntax, and that syntax + # forces absolute urls, just force convert absolute paths to + # relative paths; you'll never really want absolute paths via + # gitosis, anyway. + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'readonly', 'foo') + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git-upload-pack '/foo'", + ) + eq(got, "git-upload-pack '%s/foo.git'" % tmp) -- cgit v1.2.3 From a938dccf82aab1e8655c50a5d2974d7ea46a61a0 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Tue, 15 Apr 2008 15:52:17 +0300 Subject: Show how group sections in config can be used in example.conf. --- example.conf | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/example.conf b/example.conf index 09ee5f4..87bd822 100644 --- a/example.conf +++ b/example.conf @@ -23,6 +23,11 @@ members = jdoe wsmith @anothergroup writable = foo bar baz/thud readonly = xyzzy +## You can use groups just to avoid listing users multiple times. Note +## no writable= or readonly= lines. +[group anothergroup] +members = alice bill + ## You can play fancy tricks by making some repositories appear with ## different names in different contexts. Not really supported ## everywhere (e.g. gitweb) and can be confusing -- experts only. -- cgit v1.2.3 From 38561aa6a51a2ef6cc04aa119481df62d213ffa4 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Sat, 19 Apr 2008 19:10:36 +0300 Subject: Understand the popular gitosis.conf typo "writeable". Log a warning still, don't want that to get too common. --- gitosis/serve.py | 29 +++++++++++++++++++++++------ gitosis/test/test_access.py | 1 + gitosis/test/test_serve.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/gitosis/serve.py b/gitosis/serve.py index 37ad97f..c0b7135 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -15,6 +15,8 @@ from gitosis import gitdaemon from gitosis import app from gitosis import util +log = logging.getLogger('gitosis.serve') + ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-zA-Z0-9@._-]*)*)'$") COMMANDS_READONLY = [ @@ -80,6 +82,21 @@ def serve( mode='writable', path=path) + if newpath is None: + # didn't have write access; try once more with the popular + # misspelling + newpath = access.haveAccess( + config=cfg, + user=user, + mode='writeable', + path=path) + if newpath is not None: + log.warning( + 'Repository %r config has typo "writeable", ' + +'should be "writable"', + path, + ) + if newpath is None: # didn't have write access @@ -147,15 +164,15 @@ class Main(app.App): except ValueError: parser.error('Missing argument USER.') - log = logging.getLogger('gitosis.serve.main') + main_log = logging.getLogger('gitosis.serve.main') os.umask(0022) cmd = os.environ.get('SSH_ORIGINAL_COMMAND', None) if cmd is None: - log.error('Need SSH_ORIGINAL_COMMAND in environment.') + main_log.error('Need SSH_ORIGINAL_COMMAND in environment.') sys.exit(1) - log.debug('Got command %(cmd)r' % dict( + main_log.debug('Got command %(cmd)r' % dict( cmd=cmd, )) @@ -168,10 +185,10 @@ class Main(app.App): command=cmd, ) except ServingError, e: - log.error('%s', e) + main_log.error('%s', e) sys.exit(1) - log.debug('Serving %s', newcmd) + main_log.debug('Serving %s', newcmd) os.execvp('git-shell', ['git-shell', '-c', newcmd]) - log.error('Cannot execute git-shell.') + main_log.error('Cannot execute git-shell.') sys.exit(1) diff --git a/gitosis/test/test_access.py b/gitosis/test/test_access.py index 751b0b4..f39444c 100644 --- a/gitosis/test/test_access.py +++ b/gitosis/test/test_access.py @@ -1,5 +1,6 @@ from nose.tools import eq_ as eq +import logging from ConfigParser import RawConfigParser from gitosis import access diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index a223c43..56d50b0 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -1,7 +1,9 @@ from nose.tools import eq_ as eq from gitosis.test.util import assert_raises +import logging import os +from cStringIO import StringIO from ConfigParser import RawConfigParser from gitosis import serve @@ -410,3 +412,32 @@ def test_absolute(): command="git-upload-pack '/foo'", ) eq(got, "git-upload-pack '%s/foo.git'" % tmp) + +def test_typo_writeable(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'writeable', 'foo') + log = logging.getLogger('gitosis.serve') + buf = StringIO() + handler = logging.StreamHandler(buf) + log.addHandler(handler) + try: + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git-receive-pack 'foo'", + ) + finally: + log.removeHandler(handler) + eq(got, "git-receive-pack '%s/foo.git'" % tmp) + handler.flush() + eq( + buf.getvalue(), + "Repository 'foo' config has typo \"writeable\", shou" + +"ld be \"writable\"\n", + ) -- cgit v1.2.3 From 72c754b2f03a139122dc4a3877b05704fa88f751 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Thu, 26 Jun 2008 11:33:48 +0300 Subject: Accept "git upload-pack" etc, for future compatibility. --- gitosis/serve.py | 14 ++++++- gitosis/test/test_serve.py | 100 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/gitosis/serve.py b/gitosis/serve.py index c0b7135..9a91fcb 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -21,10 +21,12 @@ ALLOW_RE = re.compile("^'/*(?P[a-zA-Z0-9][a-zA-Z0-9@._-]*(/[a-zA-Z0-9][a-z COMMANDS_READONLY = [ 'git-upload-pack', + 'git upload-pack', ] COMMANDS_WRITE = [ 'git-receive-pack', + 'git receive-pack', ] class ServingError(Exception): @@ -62,9 +64,19 @@ def serve( try: verb, args = command.split(None, 1) except ValueError: - # all known commands take one argument; improve if/when needed + # all known "git-foo" commands take one argument; improve + # if/when needed raise UnknownCommandError() + if verb == 'git': + try: + subverb, args = args.split(None, 1) + except ValueError: + # all known "git foo" commands take one argument; improve + # if/when needed + raise UnknownCommandError() + verb = '%s %s' % (verb, subverb) + if (verb not in COMMANDS_WRITE and verb not in COMMANDS_READONLY): raise UnknownCommandError() diff --git a/gitosis/test/test_serve.py b/gitosis/test/test_serve.py index 56d50b0..f1c1930 100644 --- a/gitosis/test/test_serve.py +++ b/gitosis/test/test_serve.py @@ -23,7 +23,7 @@ def test_bad_newLine(): eq(str(e), 'Command may not contain newline') assert isinstance(e, serve.ServingError) -def test_bad_nospace(): +def test_bad_dash_noargs(): cfg = RawConfigParser() e = assert_raises( serve.UnknownCommandError, @@ -35,6 +35,18 @@ def test_bad_nospace(): eq(str(e), 'Unknown command denied') assert isinstance(e, serve.ServingError) +def test_bad_space_noargs(): + cfg = RawConfigParser() + e = assert_raises( + serve.UnknownCommandError, + serve.serve, + cfg=cfg, + user='jdoe', + command='git upload-pack', + ) + eq(str(e), 'Unknown command denied') + assert isinstance(e, serve.ServingError) + def test_bad_command(): cfg = RawConfigParser() e = assert_raises( @@ -83,7 +95,7 @@ def test_bad_unsafeArguments_dotdot(): eq(str(e), 'Arguments to command look dangerous') assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_read(): +def test_bad_forbiddenCommand_read_dash(): cfg = RawConfigParser() e = assert_raises( serve.ReadAccessDenied, @@ -96,7 +108,20 @@ def test_bad_forbiddenCommand_read(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_write_noAccess(): +def test_bad_forbiddenCommand_read_space(): + cfg = RawConfigParser() + e = assert_raises( + serve.ReadAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git upload-pack 'foo'", + ) + eq(str(e), 'Repository read access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_bad_forbiddenCommand_write_noAccess_dash(): cfg = RawConfigParser() e = assert_raises( serve.ReadAccessDenied, @@ -111,7 +136,22 @@ def test_bad_forbiddenCommand_write_noAccess(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_bad_forbiddenCommand_write_readAccess(): +def test_bad_forbiddenCommand_write_noAccess_space(): + cfg = RawConfigParser() + e = assert_raises( + serve.ReadAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + # error message talks about read in an effort to make it more + # obvious that jdoe doesn't have *even* read access + eq(str(e), 'Repository read access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_bad_forbiddenCommand_write_readAccess_dash(): cfg = RawConfigParser() cfg.add_section('group foo') cfg.set('group foo', 'members', 'jdoe') @@ -127,7 +167,23 @@ def test_bad_forbiddenCommand_write_readAccess(): assert isinstance(e, serve.AccessDenied) assert isinstance(e, serve.ServingError) -def test_simple_read(): +def test_bad_forbiddenCommand_write_readAccess_space(): + cfg = RawConfigParser() + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'readonly', 'foo') + e = assert_raises( + serve.WriteAccessDenied, + serve.serve, + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + eq(str(e), 'Repository write access denied') + assert isinstance(e, serve.AccessDenied) + assert isinstance(e, serve.ServingError) + +def test_simple_read_dash(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() @@ -143,7 +199,23 @@ def test_simple_read(): ) eq(got, "git-upload-pack '%s/foo.git'" % tmp) -def test_simple_write(): +def test_simple_read_space(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'readonly', 'foo') + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git upload-pack 'foo'", + ) + eq(got, "git upload-pack '%s/foo.git'" % tmp) + +def test_simple_write_dash(): tmp = util.maketemp() repository.init(os.path.join(tmp, 'foo.git')) cfg = RawConfigParser() @@ -159,6 +231,22 @@ def test_simple_write(): ) eq(got, "git-receive-pack '%s/foo.git'" % tmp) +def test_simple_write_space(): + tmp = util.maketemp() + repository.init(os.path.join(tmp, 'foo.git')) + cfg = RawConfigParser() + cfg.add_section('gitosis') + cfg.set('gitosis', 'repositories', tmp) + cfg.add_section('group foo') + cfg.set('group foo', 'members', 'jdoe') + cfg.set('group foo', 'writable', 'foo') + got = serve.serve( + cfg=cfg, + user='jdoe', + command="git receive-pack 'foo'", + ) + eq(got, "git receive-pack '%s/foo.git'" % tmp) + def test_push_inits_if_needed(): # a push to a non-existent repository (but where config authorizes # you to do that) will create the repository on the fly -- cgit v1.2.3 From 7e407d1013e2610401792302325c04c40c57a376 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Wed, 30 Jul 2008 14:20:43 +0300 Subject: Add a MANIFEST.in to please setuptools sdist. --- MANIFEST.in | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..7e64813 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,7 @@ +include COPYING +include README.rst +include example.conf +include etc-event.d-local-git-daemon +include gitweb.conf +include lighttpd-gitweb.conf +recursive-include gitosis/templates * -- cgit v1.2.3 From 73a032520493f6b4186185d4826d12edb5614135 Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 25 Aug 2008 19:55:45 +0300 Subject: Use "git shell" instead of "git-shell", for compatibility with git 1.6. --- gitosis/serve.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitosis/serve.py b/gitosis/serve.py index 9a91fcb..867249e 100644 --- a/gitosis/serve.py +++ b/gitosis/serve.py @@ -201,6 +201,6 @@ class Main(app.App): sys.exit(1) main_log.debug('Serving %s', newcmd) - os.execvp('git-shell', ['git-shell', '-c', newcmd]) + os.execvp('git', ['git', 'shell', '-c', newcmd]) main_log.error('Cannot execute git-shell.') sys.exit(1) -- cgit v1.2.3