From e495c9a66e583f009a53af865eacca3a5e7b953e Mon Sep 17 00:00:00 2001 From: Tommi Virtanen Date: Mon, 31 Dec 2007 21:11:22 +0200 Subject: [PATCH] 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