Documentation: shared repository management in tutorial.
The branch policy script I outlined was improved and polished by Carl and posted on the list twice since then. It is a shame not to pick it up, so replace the original outline in howto/update-hook-example.txt with the latest from Carl. Also talk about setting up git-shell to allow git-push/git-fetch only SSH access to a shared repository host in the tutorial. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 5, 2005 at 00:57 UTC
dc5f9239f7cc577b7d503017583dcc26ad5991f0
2 files changed
+171
-69
Documentation/howto/update-hook-example.txt
+136
-69
@@ -1,4 +1,4 @@
1
-From: Junio C Hamano <junkio@cox.net>
1
+From: Junio C Hamano <junkio@cox.net> and Carl Baldwin <cnb@fc.hp.com>
2
Subject: control access to branches.
3
Date: Thu, 17 Nov 2005 23:55:32 -0800
4
Message-ID: <7vfypumlu3.fsf@assigned-by-dhcp.cox.net>
@@ -26,63 +26,137 @@ section of the documentation:
26
So if your policy is (1) always require fast-forward push
27
(i.e. never allow "git-push repo +branch:branch"), (2) you
28
have a list of users allowed to update each branch, and (3) you
29
-do not let tags to be overwritten, then:
30
-
31
- #!/bin/sh
32
- # This is a sample hooks/update script, written by JC
33
- # in his e-mail buffer, so naturally it is not tested
34
- # but hopefully would convey the idea.
35
-
36
- umask 002
37
- case "$1" in
38
- refs/tags/*)
39
- # No overwriting an existing tag
40
- if test -f "$GIT_DIR/$1"
41
- then
42
- exit 1
43
- fi
44
- ;;
45
- refs/heads/*)
46
- # No rebasing or rewinding
47
- if expr "$2" : '0*$' >/dev/null
48
- then
49
- # creating a new branch
50
- ;
51
- else
52
- # updating -- make sure it is a fast forward
53
- mb=`git-merge-base "$2" "$3"`
54
- case "$mb,$2" in
55
- "$2,$mb")
56
- ;; # fast forward -- happy
57
- *)
58
- exit 1 ;; # unhappy
59
- esac
60
- fi
61
- ;;
62
- *)
63
- # No funny refs allowed
64
- exit 1
65
- ;;
66
- esac
67
-
68
- # Is the user allowed to update it?
69
- me=`id -u -n` ;# e.g. "junio"
70
- while read head_pattern users
71
- do
72
- if expr "$1" : "$head_pattern" >/dev/null
73
- then
74
- case " $users " in
75
- *" $me "*)
76
- exit 0 ;; # happy
77
- ' * ')
78
- exit 0 ;; # anybody
79
- esac
80
- fi
81
- done
82
- exit 1
83
-
84
-For the sake of simplicity, I assumed that you keep something
85
-like this in $GIT_DIR/info/allowed-pushers file:
29
+do not let tags to be overwritten, then you can use something
30
+like this as your hooks/update script.
31
+
32
+[jc: editorial note. This is a much improved version by Carl
33
+since I posted the original outline]
34
+
35
+-- >8 -- beginning of script -- >8 --
36
+
37
+#!/bin/bash
38
+
39
+umask 002
40
+
41
+# If you are having trouble with this access control hook script
42
+# you can try setting this to true. It will tell you exactly
43
+# why a user is being allowed/denied access.
44
+
45
+verbose=false
46
+
47
+# Default shell globbing messes things up downstream
48
+GLOBIGNORE=*
49
+
50
+function grant {
51
+ $verbose && echo >&2 "-Grant- $1"
52
+ echo grant
53
+ exit 0
54
+}
55
+
56
+function deny {
57
+ $verbose && echo >&2 "-Deny- $1"
58
+ echo deny
59
+ exit 1
60
+}
61
+
62
+function info {
63
+ $verbose && echo >&2 "-Info- $1"
64
+}
65
+
66
+# Implement generic branch and tag policies.
67
+# - Tags should not be updated once created.
68
+# - Branches should only be fast-forwarded.
69
+case "$1" in
70
+ refs/tags/*)
71
+ [ -f "$GIT_DIR/$1" ] &&
72
+ deny >/dev/null "You can't overwrite an existing tag"
73
+ ;;
74
+ refs/heads/*)
75
+ # No rebasing or rewinding
76
+ if expr "$2" : '0*$' >/dev/null; then
77
+ info "The branch '$1' is new..."
78
+ else
79
+ # updating -- make sure it is a fast forward
80
+ mb=$(git-merge-base "$2" "$3")
81
+ case "$mb,$2" in
82
+ "$2,$mb") info "Update is fast-forward" ;;
83
+ *) deny >/dev/null "This is not a fast-forward update." ;;
84
+ esac
85
+ fi
86
+ ;;
87
+ *)
88
+ deny >/dev/null \
89
+ "Branch is not under refs/heads or refs/tags. What are you trying to do?"
90
+ ;;
91
+esac
92
+
93
+# Implement per-branch controls based on username
94
+allowed_users_file=$GIT_DIR/info/allowed-users
95
+username=$(id -u -n)
96
+info "The user is: '$username'"
97
+
98
+if [ -f "$allowed_users_file" ]; then
99
+ rc=$(cat $allowed_users_file | grep -v '^#' | grep -v '^$' |
100
+ while read head_pattern user_patterns; do
101
+ matchlen=$(expr "$1" : "$head_pattern")
102
+ if [ "$matchlen" == "${#1}" ]; then
103
+ info "Found matching head pattern: '$head_pattern'"
104
+ for user_pattern in $user_patterns; do
105
+ info "Checking user: '$username' against pattern: '$user_pattern'"
106
+ matchlen=$(expr "$username" : "$user_pattern")
107
+ if [ "$matchlen" == "${#username}" ]; then
108
+ grant "Allowing user: '$username' with pattern: '$user_pattern'"
109
+ fi
110
+ done
111
+ deny "The user is not in the access list for this branch"
112
+ fi
113
+ done
114
+ )
115
+ case "$rc" in
116
+ grant) grant >/dev/null "Granting access based on $allowed_users_file" ;;
117
+ deny) deny >/dev/null "Denying access based on $allowed_users_file" ;;
118
+ *) ;;
119
+ esac
120
+fi
121
+
122
+allowed_groups_file=$GIT_DIR/info/allowed-groups
123
+groups=$(id -G -n)
124
+info "The user belongs to the following groups:"
125
+info "'$groups'"
126
+
127
+if [ -f "$allowed_groups_file" ]; then
128
+ rc=$(cat $allowed_groups_file | grep -v '^#' | grep -v '^$' |
129
+ while read head_pattern group_patterns; do
130
+ matchlen=$(expr "$1" : "$head_pattern")
131
+ if [ "$matchlen" == "${#1}" ]; then
132
+ info "Found matching head pattern: '$head_pattern'"
133
+ for group_pattern in $group_patterns; do
134
+ for groupname in $groups; do
135
+ info "Checking group: '$groupname' against pattern: '$group_pattern'"
136
+ matchlen=$(expr "$groupname" : "$group_pattern")
137
+ if [ "$matchlen" == "${#groupname}" ]; then
138
+ grant "Allowing group: '$groupname' with pattern: '$group_pattern'"
139
+ fi
140
+ done
141
+ done
142
+ deny "None of the user's groups are in the access list for this branch"
143
+ fi
144
+ done
145
+ )
146
+ case "$rc" in
147
+ grant) grant >/dev/null "Granting access based on $allowed_groups_file" ;;
148
+ deny) deny >/dev/null "Denying access based on $allowed_groups_file" ;;
149
+ *) ;;
150
+ esac
151
+fi
152
+
153
+deny >/dev/null "There are no more rules to check. Denying access"
154
+
155
+-- >8 -- end of script -- >8 --
156
+
157
+This uses two files, $GIT_DIR/info/allowed-users and
158
+allowed-groups, to describe which heads can be pushed into by
159
+whom. The format of each file would look like this:
160
161
refs/heads/master junio
162
refs/heads/cogito$ pasky
@@ -91,15 +165,8 @@ like this in $GIT_DIR/info/allowed-pushers file:
165
refs/tags/v[0-9]* junio
166
167
With this, Linus can push or create "bw/penguin" or "bw/zebra"
94
-or "bw/panda" branches, Pasky can do only "cogito", and I can do
95
-master branch and make versioned tags. And anybody can do
96
-tmp/blah branches. This assumes all the users are in a single
97
-group that can write into $GIT_DIR/ and underneath.
98
-
99
-
100
-
101
-
102
-
103
-
104
-
168
+or "bw/panda" branches, Pasky can do only "cogito", and JC can
169
+do master branch and make versioned tags. And anybody can do
170
+tmp/blah branches.
171
172
+------------
Documentation/tutorial.txt
+35
@@ -1636,6 +1636,41 @@ fast forward. You need to pull and merge those other changes
1636
back before you push your work when it happens.
1637
1638
1639
+Advanced Shared Repository Management
1640
+-------------------------------------
1641
+
1642
+Being able to push into a shared repository means being able to
1643
+write into it. If your developers are coming over the network,
1644
+this means you, as the repository administrator, need to give
1645
+each of them an SSH access to the shared repository machine.
1646
+
1647
+In some cases, though, you may not want to give a normal shell
1648
+account to them, but want to restrict them to be able to only
1649
+do `git push` into the repository and nothing else.
1650
+
1651
+You can achieve this by setting the login shell of your
1652
+developers on the shared repository host to `git-shell` program.
1653
+
1654
+[NOTE]
1655
+Most likely you would also need to list `git-shell` program in
1656
+`/etc/shells` file.
1657
+
1658
+This restricts the set of commands that can be run from incoming
1659
+SSH connection for these users to only `receive-pack` and
1660
+`upload-pack`, so the only thing they can do are `git fetch` and
1661
+`git push`.
1662
+
1663
+You still need to create UNIX user accounts for each developer,
1664
+and put them in the same group. Make sure that the repository
1665
+shared among these developers is writable by that group.
1666
+
1667
+You can implement finer grained branch policies using update
1668
+hooks. There is a document ("control access to branches") in
1669
+Documentation/howto by Carl Baldwin and JC outlining how to (1)
1670
+limit access to branch per user, (2) forbid overwriting existing
1671
+tags.
1672
+
1673
+
1674
Bundling your work together
1675
---------------------------
1676