2

I have an Apache httpd+mod_wsgi+Mercurial setup and I would like to get authenticated user in a hook, specifically pretxnchangegroup hook. I have read (most likely incorrectly) that os.environ['REMOTE_USER'] should contain that info, but doesn't look like it does.

I currently have an extension, that prints os.environ to stderr.

import sys
import os

def pretxnchangegroup_hook(ui, repo, **kwargs):
    print >> sys.stderr, 'pretxnchangegroup activated'
    print >> sys.stderr, os.environ

def reposetup(ui, repo):
    ui.setconfig('hooks', 'incoming.accesscontrol', pretxnchangegroup_hook)

With hg -v push this code outputs:

remote: calling hook incoming.accesscontrol: <function pretxnchangegroup_hook at 0x7f8310221230>
remote: pretxnchangegroup activated
remote: {'LANG': 'C', 'TERM': 'xterm', 'SHLVL': '2', 'PWD': '/', 'PATH': '/sbin:/usr/sbin:/bin:/usr/bin', '_': '/usr/sbin/httpd'}
Paulius K.
  • 813
  • 1
  • 7
  • 13
  • I think that `os.environ['REMOTE_USER']` should do it. This may be a silly question but is your authentication working - i.e. do you need to type in a username and password to access the Mercurial server? – Steve Kaye Jul 16 '13 at 15:03
  • Under a WSGI application, it is not in os.environ. It is in the environ dictionary passed to the WSGI application as an argument upon each request. How you access it in your Mercurial code I have node idea. – Graham Dumpleton Jul 17 '13 at 01:39
  • @SteveKaye: yes, the authentication is working. – Paulius K. Jul 17 '13 at 06:26
  • @GrahamDumpleton: well, at least I know now that os.environ is not a good place to look for that information – Paulius K. Jul 17 '13 at 06:27

1 Answers1

3

Ok, I'm hoping this will help anyone besides me, because I haven't been able to find this mentioned anywhere else.

Authenticated user (REMOTE_USER) and a lot of other information can be reached at repo.ui.environ map, where repo is a parameter passed to a hook.

I imagine this map is the same as the one in os.environ under mod_cgi.

Paulius K.
  • 813
  • 1
  • 7
  • 13
  • Hey! Could you please give a bit more details on how to achieve this? I am having the very same problem but i am unable to figure out how the `repo` parameter has to be passed to the hook. I tryed printing `sys.argv` in my hook but it only has one entry, which ofc is the script name and path. Thank you very much! – mofoe Sep 15 '16 at 11:58
  • You shouldn't pass anything to the hook, that's Mercurial's responsibility. You just register it in `reposetup`, and the second parameter of your hook method will be your repo object. – Paulius K. Sep 19 '16 at 13:00
  • I got it, thanks. I did not understand (or know about) the difference of mercurials `external` and `in-process` hooks. There is a good example for the syntax to use in the `hg.rc` and the hooks here: http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html – mofoe Sep 21 '16 at 08:53