Explain limitations of branch/file rule combination
hg-admin-toolsA set of tools for managing authorization and access control forssh-based Mercurial repositoriesPaul Crowley, paul@lshift.net, 2008This software may be used and distributed according to the termsof the GNU General Public License, incorporated herein by reference.WHAT IT GIVES YOUThese tools make it easier to provide a centralized repository hostwith read/write access to many repositories for many developers.Access control is managed with a special repository on the servercalled "hgadmin"; pushes to this repository immediately change therules that are in effect.Inside "hgadmin" is a "keys" directory containing the SSH keys of alldevelopers who have access, and a file "hg-ssh-access.conf" whichgives a set of rules defining who can do what to what.HOW IT WORKSAll of the repositories controlled by these tools are owned by asingle user (the "hg" user in what follows), but many remote users canact on them. We don't use file permissions to achieve that - instead,developers log in as the "hg" user when they connect to the repositoryhost using ssh, using ssh URLs of the form"ssh://hg@repository-host/repository-name". A restricted shellprevents them from using this access for unauthorized purposes.Developers are authenticated only using SSH keys; no other form ofauthentication is supported. When a developer attempts to connect toa repository via ssh, the SSH daemon searches for a match for thatuser's key in ~hg/.ssh/authorized_keys. If the developer isauthorised to connect to the repository they will have an entry inthis file. The entry includes a "command" prefix which specifies thatthe restricted shell should be used; this shell is passed an argumentidentifying the developer. The shell parses the command the developeris trying to execute, and consults a rules file to see if thatdeveloper is allowed to perform that action on that repository. Thebulk of the work of the restricted shell is done by the Python program"hg-ssh", but the shell script "hg-ssh-wrapper" sets up someconfiguration so that you can change it to suit your localinstallation.The file ~hg/.ssh/authorized_keys is generated by "refresh-auth",which recurses through a directory of files containing SSH keys andgenerates an entry in authorized_keys for each one, using the name ofthe key file as the identifier for the developer. These keys willlive in the "keys" subdirectory of a repository called "hgadmin". Ahook in this repository re-runs "refresh-auth" on the most recentversion after every push.Finally, a hook in an extension is run for each changeset that isremotely committed, which uses the rules file to determine whether toallow the changeset.GETTING STARTEDThis is only one setup - it can be tweaked in many ways, and is asspecific as it is only in the interests of brevity.You, and all users of this repository host, will need SSH public keyauthentication set up, preferably working with ssh-agent so you don'thave to type in your passphrase all the time. I assume you've donethat in what follows, so if you've done something different you'llneed to change it appropriately.Issue these commands to get the repository host started. These arewritten out here rather than encapsulated in a script because many ofthem may need to be different for your local setup. You will needroot access on the repository host, because you need to create a newuser. ssh -A repository-host ssh-add -L >> /tmp/my-ssh-public-key sudo adduser --system --shell /bin/sh --group --disabled-password \ --gecos "Mercurial repositories" hg sudo -u hg -H -s cd mkdir -p admin repos/hgadmin/keys/admin .ssh cd admin hg clone http://hg.opensource.lshift.net/hg-admin-tools cp hg-admin-tools/hg-ssh-wrapper hg-admin-tools/remote-hgrc ~ cd ../repos/hgadmin hg init . echo "init user=admin/*" > hg-ssh-access.conf cp /tmp/my-ssh-public-key keys/admin/myname hg add hg commit -m "initial commit" cp ~/admin/hg-admin-tools/hgadmin-hgrc .hg/hgrc ../../admin/hg-admin-tools/refresh-auth ./hg-ssh-wrapper exit exitYou are now the sole user able to change and create repositories onthis repository host. To administer these controls (and test youraccess), check out hgadmin: mkdir ~/hg cd ~/hg hg clone ssh://hg@repository-host/hgadmin cd hgadminYou can now add other users by putting their keys in an appropriatesubdirectory of the "keys" directory, and control their access byediting hg-ssh-access.conf. Changes will take effect as soon as youpush them to "ssh://hg@repository-host/hgadmin".Users authorized to do so can now also create new repositories on thishost with "clone": hg clone . ssh://hg@repository-host/my-project-nameHG-SSH-ACCESS.CONFEach line of hg-ssh-access.conf has the following syntax:<rule> <condition> <condition> ...Rule is one ofinit - allow any operation, including the creation of new repositorieswrite - allow reads and writes to this file in this repositoryread - allow the repo to be read but reject matching writesdeny - deny all requestsA condition is a globpattern matched against a relative path, one of:user=<globpattern> - user's keyrepo=<globpattern> - repo (as the user supplies it)file=<globpattern> - file in the repobranch=<globpattern> - name of the branchThe first rule in the file which has all its conditions satisfied isused to determine whether an action is allowed.Paths cannot contain any special characters except "/"; glob patternscannot contain any special characters except "/" and "*". "*" matcheszero or more characters not including "/" while "**" matches zero ormore characters including "/".Blank lines and lines that start with "#" are ignored.FILE CONDITIONSThe rules file is used to make four decisions:- Whether to allow a repository to be created- Whether to allow access to a repository- Whether to allow a changeset on a particular branch at all- Whether to allow a changeset to change a particular fileWhen the first two of these decisions are being made, nothing is knownabout what files might be changed, and so all file conditionsautomatically succeed for the purpose of such decisions. This meansthat doing tricky things with file conditions can havecounterintuitive consequences:- You cannot limit read access to a subset of a repository with a"read" rule and a file condition: any user who has access to arepository can read all of it and its full history. Such a rule canonly have the effect of masking a later "write" rule, as in thisexample: read repo=specialrepo file=dontwritethis write repo=specialrepoallows all users to read specialrepo, and to write to all files*except* that any changeset which writes to "dontwritethis" will berejected.- For similar reasons, don't give "init" rules file conditions.- Don't try to deny write access to a particular file on a particularbranch - a developer can write to the file on another branch and thenmerge it in. Either deny all writes to the branch from that user, orallow them to write to all the files they can write to on any branch.In other words, something like this will have the intended effect write user=docs/* branch=docs file=docs/*But something like this will not have the intended effect; it willeffectively allow these users to write to any file on any branch, bywriting it to "docs" first: write user=docs/* branch=docs write user=docs/* file=docs/* read user=docs/*LOCKING YOURSELF OUTIf you find yourself "locked out" - that is, that you no longer havethe permissions needed in hgadmin - you can break back in again ifyou're able to become the "hg" user on the repository host. Once youare that user, delete ~hg/.ssh/authorized_keys (to stop any user whomight have access but shouldn't from using the repository while youfix things). Then go into ~hg/repos/hgadmin, do an "hg update", editthings to your satisfaction, and commit the change. Finally, run~/admin/hg-admin-tools/refresh-auth to regenerate~hg/.ssh/authorized_keys. THANKSThanks for reading this far. If you use hg-admin-tools, please tellme about it.Paul Crowley, 2008