Project

General

Profile

Repositories access control with apache mod dav svn and mod perl » History » Version 43

James Ang, 2012-10-11 04:59
Printer-friendly formatting

1 16 Jean-Philippe Lang
h1. Repositories access control with apache, mod_dav_svn and mod_perl
2 1 Nicolas Chuche
3 2 Nicolas Chuche
{{>TOC}}
4
5 4 Jean-Philippe Lang
h2. Overview
6 1 Nicolas Chuche
7 23 Eric Davis
In this documentation, we will configure apache to delegate authentication to mod_perl. It's tested on apache2 (@apache2-mpm-prefork@) with mysql and postgresql but should work with allmost every databases for which there is a perl DBD module.  Apache2 with the high speed thread model might not load Perl correctly (@apache2-mpm-worker@).
8 1 Nicolas Chuche
9
You need a working apache on your SVN server and you must install some modules at least mod_dav_svn, mod_perl2, DBI and DBD::mysql (or the DBD driver for you database as it should work on allmost all databases).
10 4 Jean-Philippe Lang
11 15 Jean-Philippe Lang
On Debian/ubuntu you can do :
12 11 Shaun Mangelsdorf
13 43 James Ang
<pre>sudo aptitude install libapache2-svn libapache-dbi-perl \
14
libapache2-mod-perl2 libdbd-mysql-perl libdigest-sha1-perl \
15
libauthen-simple-ldap-perl</pre>
16 41 Zack s
17 1 Nicolas Chuche
On Turnkey Redmine Virtual Appliance you can do :
18
19 43 James Ang
<pre>apt-get install libapache2-svn libapache-dbi-perl \
20
libapache2-mod-perl2 libauthen-simple-ldap-perl</pre>
21 40 Zack s
22 37 Mischa The Evil
If the repositories are not created automatically by reposman.rb, it is important that the repository name is the same as the project identifier in Redmine, otherwise Redmine.pm will fail to authenticate users.  For example, if the path to the repository is @/path/to/repository/foo-bar@, then the project Identifier on the Settings page must be @foo-bar@.
23 1 Nicolas Chuche
24
h2. Enabling apache modules
25
26
On debian/ubuntu :
27
28
<pre>
29 15 Jean-Philippe Lang
sudo a2enmod dav
30 21 Marko Roeder
sudo a2enmod dav_svn # if you want to use svn
31
sudo a2enmod dav_fs  # if you want to use git
32 15 Jean-Philippe Lang
sudo a2enmod perl
33 4 Jean-Philippe Lang
</pre>
34 1 Nicolas Chuche
35 15 Jean-Philippe Lang
h2. Apache configuration for Subversion repositories
36 1 Nicolas Chuche
37
You first need to copy or link @Redmine.pm@ to @/usr/lib/perl5/Apache/Redmine.pm@
38 37 Mischa The Evil
* Redmine.pm can be found in $REDMINE_DIR/extra/svn/Redmine.pm
39
* In the Debian install, it is found /usr/share/redmine/extra/svn/Redmine.pm
40
41 15 Jean-Philippe Lang
Then add the following Location directives to your apache configuration (for example in @/etc/APACHE_DIR/conf.d/@):
42 1 Nicolas Chuche
43 17 Joachim Fritschi
* the old how-to which suggested two separate locations for with @/svn@  and @/svn-private@ can be avoided
44
* with the @Satisfy any@ keyword from Apache you can define different authentication policies
45
* read access from the redmine-server or any validated user
46
* write access only validated users
47 15 Jean-Philippe Lang
48 17 Joachim Fritschi
49 15 Jean-Philippe Lang
<pre>
50 1 Nicolas Chuche
   # /svn location for users
51
   PerlLoadModule Apache::Redmine
52
   <Location /svn>
53
     DAV svn
54
     SVNParentPath "/var/svn"
55
     Order deny,allow
56
     Deny from all
57
     Satisfy any
58 43 James Ang
     # If a client tries to svn update which involves updating many files,
59
     # the update request might result in an error Server sent unexpected
60
     # return value (413 Request  Entity Too Large) in response to REPORT
61
     # request,because the size of the update request exceeds the limit
62
     # allowed by the server. You can avoid this error by disabling the
63
     # request size limit by adding the line LimitXMLRequestBody 0
64
     # between the <Location...> and </Location> lines. 
65 42 Terence Mill
     LimitXMLRequestBody 0
66
     
67 43 James Ang
     # Only check Authentication for root path, nor again for recursive
68
     # folder.
69
     # Redmine core does only permit access on repository level, so this
70
     # doesn't hurt security. On the other hand it does boost performance
71
     # a lot!
72 42 Terence Mill
     SVNPathAuthz off
73 1 Nicolas Chuche
74
     PerlAccessHandler Apache::Authn::Redmine::access_handler
75
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
76 17 Joachim Fritschi
     AuthType Basic
77 18 Joachim Fritschi
     AuthName "Redmine SVN Repository"
78 17 Joachim Fritschi
79
     #read-only access	
80
     <Limit GET PROPFIND OPTIONS REPORT>
81 19 Joachim Fritschi
        Require valid-user
82 17 Joachim Fritschi
        Allow from redmine.server.ip
83
        # Allow from another-ip
84
     	Satisfy any
85
     </Limit>
86
     # write access
87
     <LimitExcept GET PROPFIND OPTIONS REPORT>
88
   	Require valid-user
89
     </LimitExcept>
90
91
92 1 Nicolas Chuche
     ## for mysql
93
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
94 4 Jean-Philippe Lang
     ## for postgres
95 1 Nicolas Chuche
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
96 4 Jean-Philippe Lang
     ## for SQLite3
97 1 Nicolas Chuche
     # RedmineDSN "DBI:SQLite:dbname=database.db"
98
99
     RedmineDbUser "redmine"
100
     RedmineDbPass "password"
101
  </Location>
102
103
</pre>
104
105 17 Joachim Fritschi
h3. Testing the configuration:
106 1 Nicolas Chuche
107 17 Joachim Fritschi
After reloading apache conf, you can try to browse some repository with:
108
109 1 Nicolas Chuche
<pre>
110
svn ls http://my.svn.server/svn/myproject
111 4 Jean-Philippe Lang
</pre>
112 1 Nicolas Chuche
113 17 Joachim Fritschi
Any non-public repository should ask for a username and password.
114 4 Jean-Philippe Lang
115 17 Joachim Fritschi
To test the authentication that allows you redmine server to read all repositories:
116 1 Nicolas Chuche
117 17 Joachim Fritschi
Reading a private repository:
118 3 Jean-Philippe Lang
<pre>
119 17 Joachim Fritschi
svn ls http://my.svn.server/svn/myproject
120
</pre>
121
Try writing to the repository:
122
<pre>
123
svn mkdir http://my.svn.server/svn/myproject/testdir
124
</pre>
125
This should fail and ask for a password.
126
127
128
h3. optional LDAP Authentication
129
130
If you want to connect your LDAP authentication to Apache, you can install the Authen::Simple::LDAP perl module. I found that connecting to my LDAP server to authenticate with every request can be quite slow. I added the following to my configuration and had a significant performance increase. If you have configured an encrypted connection to the LDAP server you will need the IO::Socket::SSL module.
131
132 20 Stefan Stefansson
> *NOTE: the above wording is a little confusing. I attempt to clear up the issues I had with this in the following paragraph.*
133
> 
134 1 Nicolas Chuche
> First of all, make sure that you have the Net::LDAP module installed as well. I installed Authen::Simple::LDAP through CPAN and found that nothing worked. Eventually I figured out that this was because the Authen::Simple::LDAP did not require the Net::LDAP module as a dependency but it is needed for our purpose here. I did this on CentOS and it seems that the Net::LDAP module can be installed via yum (@yum install perl-LDAP@) but the Authen::Simple::LDAP had to be installed via CPAN since there's no RPM for it in the CentOS repositories.
135 37 Mischa The Evil
> 
136
137
To install the Authen::Simple::LDAP using CPAN use commands:
138
<pre>
139 20 Stefan Stefansson
cpan
140 1 Nicolas Chuche
cpan> install Authen::Simple::LDAP
141
</pre>
142
if the installation failed due to some dependencies, resolve the dependencies first.
143
144
> My second point is related to the below Apache config. The @PerlLoadModule Authen::Simple::LDAP@ is actually not required for having users authenticated via LDAP. It will happen automatically if both of the above modules are installed. So there really is no difference between the config snippet below and the one above except for the @RedmineCacheCredsMax 50@ line which is probably a good idea although it can result in users that have been deleted or removed in redmine still getting access to the repositories, at least for a little while.
145
146
<pre>
147 20 Stefan Stefansson
   PerlLoadModule Apache::Redmine
148 17 Joachim Fritschi
   PerlLoadModule  Authen::Simple::LDAP
149 1 Nicolas Chuche
   # PerlLoadModule  IO::Socket::SSL
150
   <Location /svn>
151 8 Nicolas Chuche
     DAV svn
152 43 James Ang
     # If a client tries to svn update which involves updating many files, the
153
     # update request might result in an error Server sent unexpected return
154
     # value (413 Request  Entity Too Large) in response to REPORT request,
155
     # because the size of the update request exceeds the limit allowed by the
156
     # server. You can avoid this error by disabling the request size limit by
157
     # adding the line LimitXMLRequestBody 0 between the <Location...> and
158
     # </Location> lines. 
159 42 Terence Mill
     LimitXMLRequestBody 0
160
     
161
     # Only check Authentification for root path, nor again for recursive folder
162 43 James Ang
     # Redmine core does only permit acces on repository level, so this doesn't
163
     # hurt security. On the other hand it does boost performance a lot!
164 42 Terence Mill
     SVNPathAuthz off
165
166 12 Todd Nine
     SVNParentPath "/var/svn"
167
168
     AuthType Basic
169
     AuthName redmine
170
     Require valid-user
171
172
     PerlAccessHandler Apache::Authn::Redmine::access_handler
173
     PerlAuthenHandler Apache::Authn::Redmine::authen_handler
174
  
175
     ## for mysql
176
     RedmineDSN "DBI:mysql:database=databasename;host=my.db.server"
177
     ## for postgres
178
     # RedmineDSN "DBI:Pg:dbname=databasename;host=my.db.server"
179
180 1 Nicolas Chuche
     RedmineDbUser "redmine"
181 12 Todd Nine
     RedmineDbPass "password"
182
     #Cache the last 50 auth entries
183
     RedmineCacheCredsMax 50
184 15 Jean-Philippe Lang
  </Location>
185 12 Todd Nine
</pre>
186 36 neil johnson
187 1 Nicolas Chuche
h2. Apache configuration for Git repositories
188 38 Lluís Vilanova
189
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced git integration]]
190 15 Jean-Philippe Lang
191 1 Nicolas Chuche
Now that reposman.rb can create git repositories, you can use Redmine.pm to access them the same way than subversion. 
192
193
You first need to copy or link Redmine.pm to /usr/lib/perl5/Apache/Redmine.pm, then you add this configuration to apache : 
194
195
<pre>
196
197 37 Mischa The Evil
PerlLoadModule Apache::Authn::Redmine
198 1 Nicolas Chuche
199 37 Mischa The Evil
SetEnv GIT_PROJECT_ROOT /path/to/git/repos
200
SetEnv GIT_HTTP_EXPORT_ALL
201
202
ScriptAlias /git/ /usr/bin/git-http-backend/
203
204
<Location /git>
205 8 Nicolas Chuche
  AuthType Basic
206
  Require valid-user
207 1 Nicolas Chuche
  AuthName "Git"
208 8 Nicolas Chuche
209
  PerlAccessHandler Apache::Authn::Redmine::access_handler
210
  PerlAuthenHandler Apache::Authn::Redmine::authen_handler
211 1 Nicolas Chuche
212 8 Nicolas Chuche
  RedmineDSN "DBI:mysql:database=redmine;host=localhost"
213
  RedmineDbUser "redmine"
214
  RedmineDbPass "password"
215 37 Mischa The Evil
  RedmineGitSmartHttp yes
216 8 Nicolas Chuche
</Location>
217
218
Alias /git-private /var/git
219
220
<Location /git-private>
221
   Order deny,allow
222
   Deny from all
223
   <Limit GET PROPFIND OPTIONS REPORT>
224
      Options Indexes FollowSymLinks MultiViews
225
   Allow from 127.0.0.1
226
   </Limit>
227
</Location>
228
</pre>
229
230
To verify that you can access repository through Redmine.pm, you can use curl :
231
<pre>
232
% curl --netrc --location http://localhost/git/ecookbook/HEAD   
233 13 Thomas Pihl
ref: refs/heads/master
234
</pre>
235
236 22 Diego Oliveira
h2. Apache configuration for Mercurial repositories
237
238 39 Lluís Vilanova
*TODO*: This should probably be moved into [[HowTo configure Redmine for advanced Mercurial integration]]
239
240 22 Diego Oliveira
Create a file caled "hgweb.config" in the same folder as "hgwebdir.cgi". This foder will be the root repository folder. Then edit the "hgweb.config" with something like this:
241
242
<pre>
243
[paths]
244
/=/path/to/root/repository/**
245
246
[web]
247
allow_push = *
248
allowbz2 = yes
249
allowgz = yes
250
allowzip = yes
251
252
</pre>
253
254
Follows the instructions to install Redmine.pm as described and configure your apache like this.
255
256
<pre>
257
    RewriteEngine on
258
    PerlLoadModule Apache2::Redmine
259
    PerlLoadModule Authen::Simple::LDAP
260
    ScriptAliasMatch ^/hg(.*)  /path/to/the/hgwebdir.cgi/$1
261
    <Location /hg>
262
        AuthType Basic
263
        AuthName "Mercurial"
264
        Require valid-user
265 1 Nicolas Chuche
266
        #Redmine auth
267
        PerlAccessHandler Apache::Authn::Redmine::access_handler
268
        PerlAuthenHandler Apache::Authn::Redmine::authen_handler
269
        RedmineDSN "DBI:mysql:database=redmine;host=localhost"
270
        RedmineDbUser "DB_USER"
271
        RedmineDbPass "DB_PASSWD"
272
    </Location>
273 37 Mischa The Evil
274
    # Note: Do not use hg-private as the rewrite rule above will cause strange things to occur.
275
    Alias /private-hg /path/to/hg/repos
276
    
277
    <Location /private-hg>
278
        Order deny, allow
279
        Deny from all
280
        <Limit GET PROPFIND OPTIONS REPORT>
281
            Options Indexes FollowSymLinks MultiViews
282
            Allow from 127.0.0.1
283
        </Limit>
284
    </Location>
285
286 22 Diego Oliveira
287 1 Nicolas Chuche
</pre>
288 22 Diego Oliveira
289
h2. Gotchas
290 13 Thomas Pihl
291
If you run this in Phusion Passenger, make sure you don't turn PassengerHighPerformance on. If you do, the rewrites to catch subversion dav will be bypassed with some interesting dump in the log as a result.
292
Example: 
293 1 Nicolas Chuche
> ActionController::RoutingError (No route matches "/svn/rm-code" with {:method=>:get}):
294
(if your repo are named rm-code)
295 27 Bill Dieter
296 37 Mischa The Evil
When using @Authen::Simple::LDAP@ for authentication, it is not sufficient to have the Administrator role to access a repository.  The user must have a role with that specifically allows the user to browse, read, or write the repository.